Skip to content

Commit

Permalink
Refactor instance creation logic for clarity
Browse files Browse the repository at this point in the history
Consolidate the logic for adding prototype or singleton instances into a single conditional expression, improving code readability and maintainability. This change eliminates redundant checks and ensures that the corresponding method is invoked directly based on the $isSingleton condition.
  • Loading branch information
koriym committed Nov 30, 2024
1 parent ababe09 commit 06b44a8
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions src/InstanceScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,11 @@ private function addDependencyArg(bool $isSingleton, string $index, ReflectionPa
{
/** @psalm-suppress PossiblyNullReference / The $parameter here can never be null */
$ip = sprintf("['%s', '%s', '%s']", $parameter->getDeclaringClass()->getName(), $parameter->getDeclaringFunction()->getName(), $parameter->name); //@phpstan-ignore-line
$func = $isSingleton ? '$singleton' : '$prototype';
$filePath = sprintf('/%s.php', str_replace('\\', '_', $index));
if ($isSingleton === false) {
$arg = sprintf("\\Ray\\Compiler\\prototype(\$scriptDir, '%s', %s)", $filePath, $ip);
$this->args[] = $arg;

return;
}

// function singleton(string $scriptDir, array &$singletons, string $dependencyIndex, string $filePath, array $ip = null) {

$arg = sprintf("\\Ray\\Compiler\\singleton(\$scriptDir, \$singletons, '%s', '%s', %s)", $index, $filePath, $ip);
$this->args[] = $arg;
// Add prototype or singleton
$this->args[] = $isSingleton ?
sprintf("\\Ray\\Compiler\\singleton(\$scriptDir, \$singletons, '%s', '%s', %s)", $index, $filePath, $ip) :
sprintf("\\Ray\\Compiler\\prototype(\$scriptDir, '%s', %s)", $filePath, $ip);
}

/** @param mixed $default */
Expand Down

0 comments on commit 06b44a8

Please sign in to comment.