Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to use container as a factory #354

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix psalm
  • Loading branch information
xepozz committed Feb 23, 2024
commit 684e8c01b21514427ce3253f79afb132f53d2ef1
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ function (Container $container, string $id) {
/**
* @var $this Container
*/
/** @psalm-scope-this Container */
unset($this->instances[$id]); // remove the instance from the container
};
```
Expand Down
3 changes: 2 additions & 1 deletion src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
if (!array_key_exists($id, $this->instances)) {
try {
try {
/** @psalm-suppress MixedAssignment */
$result = $this->instances[$id] = $this->build($id);
} catch (NotFoundExceptionInterface $e) {
if (!$this->delegates->has($id)) {
Expand Down Expand Up @@ -193,7 +194,7 @@
}

/** @psalm-suppress MixedReturnStatement */
return $result ?? $this->instances[$id];

Check warning on line 197 in src/Container.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Coalesce": --- Original +++ New @@ @@ } } /** @psalm-suppress MixedReturnStatement */ - return $result ?? $this->instances[$id]; + return $this->instances[$id] ?? $result; } /** * Sets a definition to the container. Definition may be defined multiple ways.

Check warning on line 197 in src/Container.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Coalesce": --- Original +++ New @@ @@ } } /** @psalm-suppress MixedReturnStatement */ - return $result ?? $this->instances[$id]; + return $this->instances[$id] ?? $result; } /** * Sets a definition to the container. Definition may be defined multiple ways.
}

/**
Expand All @@ -215,7 +216,7 @@
$this->validateMeta($meta);
}
/**
* @psalm-var array{reset?:Closure,tags?:string[]} $meta
* @psalm-var array{reset?:Closure,tags?:string[],afterBuilt?:Closure} $meta
*/

if (isset($meta[self::META_TAGS])) {
Expand Down Expand Up @@ -288,7 +289,7 @@

$this->delegates->attach($delegate);
}
$this->definitions->setDelegateContainer($this->delegates);

Check warning on line 292 in src/Container.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ } $this->delegates->attach($delegate); } - $this->definitions->setDelegateContainer($this->delegates); + } /** * @param mixed $definition Definition to validate.

Check warning on line 292 in src/Container.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ } $this->delegates->attach($delegate); } - $this->definitions->setDelegateContainer($this->delegates); + } /** * @param mixed $definition Definition to validate.
}

/**
Expand All @@ -315,7 +316,7 @@

$definition = array_merge(
$class === null ? [] : [ArrayDefinition::CLASS_NAME => $class],
[ArrayDefinition::CONSTRUCTOR => $constructorArguments],

Check warning on line 319 in src/Container.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ $methodsAndProperties = $definition['methodsAndProperties']; $definition = array_merge( $class === null ? [] : [ArrayDefinition::CLASS_NAME => $class], - [ArrayDefinition::CONSTRUCTOR => $constructorArguments], + [], // extract only value from parsed definition method array_map(fn(array $data): mixed => $data[2], $methodsAndProperties) );

Check warning on line 319 in src/Container.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ $methodsAndProperties = $definition['methodsAndProperties']; $definition = array_merge( $class === null ? [] : [ArrayDefinition::CLASS_NAME => $class], - [ArrayDefinition::CONSTRUCTOR => $constructorArguments], + [], // extract only value from parsed definition method array_map(fn(array $data): mixed => $data[2], $methodsAndProperties) );
// extract only value from parsed definition method
array_map(fn (array $data): mixed => $data[2], $methodsAndProperties),
);
Expand Down Expand Up @@ -501,7 +502,7 @@
);
}

$this->building[$id] = 1;

Check warning on line 505 in src/Container.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ } throw new CircularReferenceException(sprintf('Circular reference to "%s" detected while building: %s.', $id, implode(', ', array_keys($this->building)))); } - $this->building[$id] = 1; + $this->building[$id] = 2; try { /** @var mixed $object */ $object = $this->buildInternal($id);

Check warning on line 505 in src/Container.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ } throw new CircularReferenceException(sprintf('Circular reference to "%s" detected while building: %s.', $id, implode(', ', array_keys($this->building)))); } - $this->building[$id] = 1; + $this->building[$id] = 2; try { /** @var mixed $object */ $object = $this->buildInternal($id);
try {
/** @var mixed $object */
$object = $this->buildInternal($id);
Expand Down
1 change: 1 addition & 0 deletions src/Hook/AfterBuiltHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
/**
* @var $this Container
*/
/** @psalm-scope-this Container */
unset($this->instances[$id]);

Check failure on line 19 in src/Hook/AfterBuiltHook.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

UndefinedDocblockClass

src/Hook/AfterBuiltHook.php:19:13: UndefinedDocblockClass: Scope class Container does not exist (see https://psalm.dev/200)

Check failure on line 19 in src/Hook/AfterBuiltHook.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

InvalidScope

src/Hook/AfterBuiltHook.php:19:19: InvalidScope: Invalid reference to $this in a non-class context (see https://psalm.dev/013)

Check failure on line 19 in src/Hook/AfterBuiltHook.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

UndefinedDocblockClass

src/Hook/AfterBuiltHook.php:19:13: UndefinedDocblockClass: Scope class Container does not exist (see https://psalm.dev/200)

Check failure on line 19 in src/Hook/AfterBuiltHook.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

InvalidScope

src/Hook/AfterBuiltHook.php:19:19: InvalidScope: Invalid reference to $this in a non-class context (see https://psalm.dev/013)
};
}
}
Loading