From 67479076dd34b401631178bf3df95307d0c5c2ea Mon Sep 17 00:00:00 2001 From: Marcel Strahl Date: Sun, 29 Jan 2023 14:53:24 +0100 Subject: [PATCH] Fixed coverage and code issues --- psalm.xml | 2 +- src/ClassContainer.php | 6 ------ src/Dto/ObjectStoreInterface.php | 6 ------ src/Dto/ObjectStoreItem.php | 7 ++++--- src/Exception/CannotRetrieveException.php | 3 ++- .../FileLoader/AdapterBuilderException.php | 3 ++- src/Exception/NonExistingFileException.php | 3 ++- src/Exception/NotFoundInContainerException.php | 3 ++- src/Exception/NotInitializedException.php | 4 +++- .../CanNotCreateClassWithNoneClassDependencies.php | 7 +++---- src/Exception/UnknownFileExtensionException.php | 3 ++- src/FileLoader/PHPArrayAdapter.php | 7 ++++++- src/ObjectBuilder/ReflectionBuilder.php | 14 ++++++++++---- src/ObjectContainer.php | 9 ++------- tests/Acceptance/WorkflowTest.php | 2 +- tests/AppContainerTest.php | 1 - tests/ClassContainerTest.php | 1 - tests/Dto/ClassItemTest.php | 2 +- tests/Dto/ClassStoreTest.php | 2 +- tests/Dto/ObjectStoreItemTest.php | 2 +- tests/Dto/ObjectStoreTest.php | 2 +- .../Exception/NotFoundInContainerExceptionTest.php | 2 +- tests/Exception/NotInitializedExceptionTest.php | 2 +- ...NotCreateClassWithNoneClassDependenciesTest.php | 2 +- tests/FileLoader/AdapterBuilderTest.php | 1 - tests/FileLoader/PhpArrayAdapterTest.php | 2 -- tests/ObjectBuilder/ReflectionBuilderTest.php | 3 +-- tests/ObjectContainerTest.php | 1 - 28 files changed, 48 insertions(+), 54 deletions(-) diff --git a/psalm.xml b/psalm.xml index e42c91b..4ffcb14 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,6 +1,6 @@ compiled = true; } - /** - * @psalm-param class-string $id - */ public function get(string $id): ClassItemInterface { $metadata = $this->classes->searchById($id); @@ -50,9 +47,6 @@ public function get(string $id): ClassItemInterface return $metadata; } - /** - * @psalm-param class-string $id - */ public function has(string $id): bool { return $this->classes->hasEntry($id); diff --git a/src/Dto/ObjectStoreInterface.php b/src/Dto/ObjectStoreInterface.php index 7856a5d..a69233b 100644 --- a/src/Dto/ObjectStoreInterface.php +++ b/src/Dto/ObjectStoreInterface.php @@ -8,13 +8,7 @@ interface ObjectStoreInterface { public static function create(): self; - /** - * @param class-string $class - */ public function append(string $class, object $object): void; - /** - * @param class-string $class - */ public function searchById(string $class): null|object; } diff --git a/src/Dto/ObjectStoreItem.php b/src/Dto/ObjectStoreItem.php index efeb92e..ba207e6 100644 --- a/src/Dto/ObjectStoreItem.php +++ b/src/Dto/ObjectStoreItem.php @@ -4,6 +4,8 @@ namespace MarcelStrahl\Container\Dto; +use function in_array; + final class ObjectStoreItem { /** @@ -21,7 +23,6 @@ private function __construct( } /** - * @psalm-param string|class-string $id * @psalm-param class-string $class * @psalm-param list $interfaces * @psalm-param list $abstracts @@ -75,11 +76,11 @@ private function searchByClass(string $class): bool private function searchByInterface(string $interface): bool { - return \in_array($interface, $this->interfaces, true); + return in_array($interface, $this->interfaces, true); } private function searchByAbstract(string $abstract): bool { - return \in_array($abstract, $this->abstracts, true); + return in_array($abstract, $this->abstracts, true); } } diff --git a/src/Exception/CannotRetrieveException.php b/src/Exception/CannotRetrieveException.php index 3561e15..537c4eb 100644 --- a/src/Exception/CannotRetrieveException.php +++ b/src/Exception/CannotRetrieveException.php @@ -5,9 +5,10 @@ namespace MarcelStrahl\Container\Exception; use Psr\Container\ContainerExceptionInterface; +use RuntimeException; use Webmozart\Assert\Assert; -final class CannotRetrieveException extends \RuntimeException implements ContainerExceptionInterface +final class CannotRetrieveException extends RuntimeException implements ContainerExceptionInterface { /** * @psalm-param class-string $class diff --git a/src/Exception/FileLoader/AdapterBuilderException.php b/src/Exception/FileLoader/AdapterBuilderException.php index 6f8ef9a..96a8425 100644 --- a/src/Exception/FileLoader/AdapterBuilderException.php +++ b/src/Exception/FileLoader/AdapterBuilderException.php @@ -4,12 +4,13 @@ namespace MarcelStrahl\Container\Exception\FileLoader; +use RuntimeException; use Webmozart\Assert\Assert; /** * @author Marcel Strahl */ -final class AdapterBuilderException extends \RuntimeException +final class AdapterBuilderException extends RuntimeException { public static function createByUnknownAdapterType(string $adapter): self { diff --git a/src/Exception/NonExistingFileException.php b/src/Exception/NonExistingFileException.php index e845da2..e8467fa 100644 --- a/src/Exception/NonExistingFileException.php +++ b/src/Exception/NonExistingFileException.php @@ -4,12 +4,13 @@ namespace MarcelStrahl\Container\Exception; +use RuntimeException; use Webmozart\Assert\Assert; /** * @author Marcel Strahl */ -final class NonExistingFileException extends \RuntimeException +final class NonExistingFileException extends RuntimeException { /** * @psalm-param non-empty-string $file diff --git a/src/Exception/NotFoundInContainerException.php b/src/Exception/NotFoundInContainerException.php index 901070a..da52a50 100644 --- a/src/Exception/NotFoundInContainerException.php +++ b/src/Exception/NotFoundInContainerException.php @@ -6,11 +6,12 @@ use Psr\Container\NotFoundExceptionInterface; use RuntimeException; +use Throwable; use Webmozart\Assert\Assert; final class NotFoundInContainerException extends RuntimeException implements NotFoundExceptionInterface { - public static function create(string $id, ?\LogicException $previousException = null): self + public static function create(string $id, ?Throwable $previousException = null): self { $message = sprintf('Can not found a entry in container with given id "%s"', $id); Assert::stringNotEmpty($message); diff --git a/src/Exception/NotInitializedException.php b/src/Exception/NotInitializedException.php index 5a48f0b..8e24c02 100644 --- a/src/Exception/NotInitializedException.php +++ b/src/Exception/NotInitializedException.php @@ -4,7 +4,9 @@ namespace MarcelStrahl\Container\Exception; -final class NotInitializedException extends \LogicException +use LogicException; + +final class NotInitializedException extends LogicException { /** * @param class-string $class diff --git a/src/Exception/ObjectBuilder/CanNotCreateClassWithNoneClassDependencies.php b/src/Exception/ObjectBuilder/CanNotCreateClassWithNoneClassDependencies.php index 031aef3..f853293 100644 --- a/src/Exception/ObjectBuilder/CanNotCreateClassWithNoneClassDependencies.php +++ b/src/Exception/ObjectBuilder/CanNotCreateClassWithNoneClassDependencies.php @@ -4,11 +4,10 @@ namespace MarcelStrahl\Container\Exception\ObjectBuilder; -final class CanNotCreateClassWithNoneClassDependencies extends \LogicException +use LogicException; + +final class CanNotCreateClassWithNoneClassDependencies extends LogicException { - /** - * @psalm-param non-empty-string $type - */ public static function create(string $type): self { return new self(sprintf( diff --git a/src/Exception/UnknownFileExtensionException.php b/src/Exception/UnknownFileExtensionException.php index 177b723..19c1c05 100644 --- a/src/Exception/UnknownFileExtensionException.php +++ b/src/Exception/UnknownFileExtensionException.php @@ -4,12 +4,13 @@ namespace MarcelStrahl\Container\Exception; +use RuntimeException; use Webmozart\Assert\Assert; /** * @author Marcel Strahl */ -final class UnknownFileExtensionException extends \RuntimeException +final class UnknownFileExtensionException extends RuntimeException { /** * @psalm-param non-empty-string $path diff --git a/src/FileLoader/PHPArrayAdapter.php b/src/FileLoader/PHPArrayAdapter.php index 8b5bf6f..fe971d3 100644 --- a/src/FileLoader/PHPArrayAdapter.php +++ b/src/FileLoader/PHPArrayAdapter.php @@ -4,10 +4,15 @@ namespace MarcelStrahl\Container\FileLoader; +use function array_walk; +use function call_user_func_array; +use function file_exists; use MarcelStrahl\Container\Dto\ClassStore; + use MarcelStrahl\Container\Dto\ClassStoreInterface; use MarcelStrahl\Container\Exception\NonExistingFileException; use MarcelStrahl\Container\Exception\UnknownFileExtensionException; +use function str_contains; final class PHPArrayAdapter implements FileLoader { @@ -39,7 +44,7 @@ public function loadFileFromPaths(array $paths, ClassStoreInterface $classStore) array_walk( $paths, static function (string $path) use ($instance, $classStore): void { - \call_user_func_array([$instance, 'loadFileFromPath'], [$path, $classStore]); + call_user_func_array([$instance, 'loadFileFromPath'], [$path, $classStore]); } ); diff --git a/src/ObjectBuilder/ReflectionBuilder.php b/src/ObjectBuilder/ReflectionBuilder.php index 6e7d301..7ecb49a 100644 --- a/src/ObjectBuilder/ReflectionBuilder.php +++ b/src/ObjectBuilder/ReflectionBuilder.php @@ -4,19 +4,25 @@ namespace MarcelStrahl\Container\ObjectBuilder; +use LogicException; use MarcelStrahl\Container\Exception\ObjectBuilder\CanNotCreateClassWithNoneClassDependencies; +use ReflectionClass; +use ReflectionException; use ReflectionParameter; +use Webmozart\Assert\Assert; final class ReflectionBuilder implements ObjectBuilder { public function initialize(string $class): object { try { - $reflectionClass = new \ReflectionClass($class); - } catch (\ReflectionException $exception) { - throw new \LogicException('Cannot find your class, try `composer dumpautoload` command.'); + $reflectionClass = new ReflectionClass($class); + } catch (ReflectionException $exception) { + throw new LogicException('Cannot find your class, try `composer dumpautoload` command.'); } + Assert::classExists($class); + $parameters = $reflectionClass->getConstructor()?->getParameters(); // Current class has no constructor and can initialize directly, or it has a constructor but no dependencies. @@ -35,7 +41,7 @@ public function initialize(string $class): object } /** - * @param array $parameters + * @param array $parameters * * @psalm-param list $parameters * diff --git a/src/ObjectContainer.php b/src/ObjectContainer.php index 5f65a88..02d17e1 100644 --- a/src/ObjectContainer.php +++ b/src/ObjectContainer.php @@ -4,6 +4,7 @@ namespace MarcelStrahl\Container; +use LogicException; use MarcelStrahl\Container\Dto\ObjectStoreInterface; use MarcelStrahl\Container\Exception\NotFoundInContainerException; use MarcelStrahl\Container\ObjectBuilder\ObjectBuilder; @@ -17,9 +18,6 @@ public function __construct( ) { } - /** - * @param class-string $id - */ public function get(string $id): object { $object = $this->objectStore->searchById($id); @@ -29,7 +27,7 @@ public function get(string $id): object try { $object = $this->builder->initialize($id); - } catch (\LogicException $exception) { + } catch (LogicException $exception) { throw NotFoundInContainerException::create($id, $exception); } @@ -38,9 +36,6 @@ public function get(string $id): object return $object; } - /** - * @param class-string $id - */ public function has(string $id): bool { return null !== $this->objectStore->searchById($id); diff --git a/tests/Acceptance/WorkflowTest.php b/tests/Acceptance/WorkflowTest.php index cb53e7d..21b241a 100644 --- a/tests/Acceptance/WorkflowTest.php +++ b/tests/Acceptance/WorkflowTest.php @@ -22,7 +22,7 @@ * * @internal * - * @coversNothing + */ final class WorkflowTest extends TestCase { diff --git a/tests/AppContainerTest.php b/tests/AppContainerTest.php index dd8a20f..9ef813b 100644 --- a/tests/AppContainerTest.php +++ b/tests/AppContainerTest.php @@ -18,7 +18,6 @@ * * @internal * - * @coversNothing */ final class AppContainerTest extends TestCase { diff --git a/tests/ClassContainerTest.php b/tests/ClassContainerTest.php index aed4148..fe0706b 100644 --- a/tests/ClassContainerTest.php +++ b/tests/ClassContainerTest.php @@ -15,7 +15,6 @@ /** * @internal * - * @coversNothing */ final class ClassContainerTest extends TestCase { diff --git a/tests/Dto/ClassItemTest.php b/tests/Dto/ClassItemTest.php index e284375..564c2c2 100644 --- a/tests/Dto/ClassItemTest.php +++ b/tests/Dto/ClassItemTest.php @@ -13,7 +13,7 @@ /** * @internal * - * @coversNothing + */ final class ClassItemTest extends TestCase { diff --git a/tests/Dto/ClassStoreTest.php b/tests/Dto/ClassStoreTest.php index 5f9118a..39948b4 100644 --- a/tests/Dto/ClassStoreTest.php +++ b/tests/Dto/ClassStoreTest.php @@ -13,7 +13,7 @@ /** * @internal * - * @coversNothing + */ final class ClassStoreTest extends TestCase { diff --git a/tests/Dto/ObjectStoreItemTest.php b/tests/Dto/ObjectStoreItemTest.php index 7d7d832..902f41a 100644 --- a/tests/Dto/ObjectStoreItemTest.php +++ b/tests/Dto/ObjectStoreItemTest.php @@ -14,7 +14,7 @@ /** * @internal * - * @coversNothing + */ final class ObjectStoreItemTest extends TestCase { diff --git a/tests/Dto/ObjectStoreTest.php b/tests/Dto/ObjectStoreTest.php index a01ec8f..13caf64 100644 --- a/tests/Dto/ObjectStoreTest.php +++ b/tests/Dto/ObjectStoreTest.php @@ -11,7 +11,7 @@ /** * @internal * - * @coversNothing + */ final class ObjectStoreTest extends TestCase { diff --git a/tests/Exception/NotFoundInContainerExceptionTest.php b/tests/Exception/NotFoundInContainerExceptionTest.php index 601667a..1db28f3 100644 --- a/tests/Exception/NotFoundInContainerExceptionTest.php +++ b/tests/Exception/NotFoundInContainerExceptionTest.php @@ -11,7 +11,7 @@ /** * @internal * - * @coversNothing + */ final class NotFoundInContainerExceptionTest extends TestCase { diff --git a/tests/Exception/NotInitializedExceptionTest.php b/tests/Exception/NotInitializedExceptionTest.php index 6c9e65e..b9b3a15 100644 --- a/tests/Exception/NotInitializedExceptionTest.php +++ b/tests/Exception/NotInitializedExceptionTest.php @@ -10,7 +10,7 @@ /** * @internal * - * @coversNothing + */ final class NotInitializedExceptionTest extends TestCase { diff --git a/tests/Exception/ObjectBuilder/CanNotCreateClassWithNoneClassDependenciesTest.php b/tests/Exception/ObjectBuilder/CanNotCreateClassWithNoneClassDependenciesTest.php index 2ee52d6..dd31f31 100644 --- a/tests/Exception/ObjectBuilder/CanNotCreateClassWithNoneClassDependenciesTest.php +++ b/tests/Exception/ObjectBuilder/CanNotCreateClassWithNoneClassDependenciesTest.php @@ -10,7 +10,7 @@ /** * @internal * - * @coversNothing + */ final class CanNotCreateClassWithNoneClassDependenciesTest extends TestCase { diff --git a/tests/FileLoader/AdapterBuilderTest.php b/tests/FileLoader/AdapterBuilderTest.php index 1fe6403..0a5d0b9 100644 --- a/tests/FileLoader/AdapterBuilderTest.php +++ b/tests/FileLoader/AdapterBuilderTest.php @@ -15,7 +15,6 @@ * * @internal * - * @coversNothing */ final class AdapterBuilderTest extends TestCase { diff --git a/tests/FileLoader/PhpArrayAdapterTest.php b/tests/FileLoader/PhpArrayAdapterTest.php index f5034f5..1028089 100644 --- a/tests/FileLoader/PhpArrayAdapterTest.php +++ b/tests/FileLoader/PhpArrayAdapterTest.php @@ -17,8 +17,6 @@ * @author Marcel Strahl * * @internal - * - * @coversNothing */ final class PhpArrayAdapterTest extends TestCase { diff --git a/tests/ObjectBuilder/ReflectionBuilderTest.php b/tests/ObjectBuilder/ReflectionBuilderTest.php index 101c507..2f0873f 100644 --- a/tests/ObjectBuilder/ReflectionBuilderTest.php +++ b/tests/ObjectBuilder/ReflectionBuilderTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace MarcelStrahl\Tests\Container\ObjectBuilder; +namespace MarcelStrahl\Tests\ObjectBuilder; use MarcelStrahl\Container\Exception\ObjectBuilder\CanNotCreateClassWithNoneClassDependencies; use MarcelStrahl\Container\ObjectBuilder\ObjectBuilder; @@ -16,7 +16,6 @@ /** * @internal * - * @coversNothing */ final class ReflectionBuilderTest extends TestCase { diff --git a/tests/ObjectContainerTest.php b/tests/ObjectContainerTest.php index 3320a5b..07f8817 100644 --- a/tests/ObjectContainerTest.php +++ b/tests/ObjectContainerTest.php @@ -15,7 +15,6 @@ /** * @internal * - * @coversNothing */ final class ObjectContainerTest extends TestCase {