From f9f2204c149576891541a873cf4f0750f19f1d73 Mon Sep 17 00:00:00 2001 From: Alessandro Chitolina Date: Wed, 19 Jul 2023 16:25:02 +0200 Subject: [PATCH] chore: fixed code style and tests --- lib/ClassMetadata.php | 6 ++---- lib/Event/ClassMetadataLoadedEvent.php | 4 +--- lib/Exception/InvalidArgumentException.php | 8 ++++---- lib/Factory/AbstractMetadataFactory.php | 12 ++---------- lib/Loader/AbstractProcessorLoader.php | 5 +---- lib/Loader/ChainLoader.php | 11 ++--------- lib/Loader/FileLoader.php | 5 +---- lib/Loader/FileLoaderTrait.php | 2 +- lib/Loader/FilesLoader.php | 7 +------ lib/Loader/Locator/IteratorFileLocator.php | 4 ++-- lib/Loader/Processor/CompositeProcessor.php | 6 +----- lib/Loader/Processor/ProcessorFactory.php | 6 ++---- lib/Loader/Processor/ProcessorFactoryInterface.php | 2 +- lib/MethodMetadata.php | 4 +--- lib/NullMetadata.php | 5 +---- lib/PropertyMetadata.php | 4 +--- tests/Factory/MetadataFactoryTest.php | 8 ++++++-- 17 files changed, 30 insertions(+), 69 deletions(-) diff --git a/lib/ClassMetadata.php b/lib/ClassMetadata.php index e6f804f..8c1e970 100644 --- a/lib/ClassMetadata.php +++ b/lib/ClassMetadata.php @@ -66,7 +66,7 @@ public function merge(MetadataInterface $metadata): void throw InvalidArgumentException::create( InvalidArgumentException::NOT_MERGEABLE_METADATA, $this->getReflectionClass()->name, - $metadata->getReflectionClass()->name + $metadata->getReflectionClass()->name, ); } @@ -110,9 +110,7 @@ public function addAttributeMetadata(MetadataInterface $metadata): void $this->attributesNames[strtolower($name)] = $name; } - /** - * @phpstan-return class-string - */ + /** @phpstan-return class-string */ public function getName(): string { return $this->getReflectionClass()->name; diff --git a/lib/Event/ClassMetadataLoadedEvent.php b/lib/Event/ClassMetadataLoadedEvent.php index fd5d1a8..40ae004 100644 --- a/lib/Event/ClassMetadataLoadedEvent.php +++ b/lib/Event/ClassMetadataLoadedEvent.php @@ -9,12 +9,10 @@ class ClassMetadataLoadedEvent implements StoppableEventInterface { - private ClassMetadataInterface $metadata; private bool $propagationStopped = false; - public function __construct(ClassMetadataInterface $metadata) + public function __construct(private ClassMetadataInterface $metadata) { - $this->metadata = $metadata; } public function getMetadata(): ClassMetadataInterface diff --git a/lib/Exception/InvalidArgumentException.php b/lib/Exception/InvalidArgumentException.php index 027b9cc..77c295c 100644 --- a/lib/Exception/InvalidArgumentException.php +++ b/lib/Exception/InvalidArgumentException.php @@ -23,7 +23,7 @@ class InvalidArgumentException extends \InvalidArgumentException /** * {@inheritdoc} */ - final public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null) + final public function __construct(string $message = '', int $code = 0, Throwable|null $previous = null) { parent::__construct($message, $code, $previous); } @@ -50,7 +50,7 @@ public static function create(mixed $reason): self $message = sprintf( 'Cannot merge metadata of class "%s" with "%s"', is_object($arguments[2]) ? get_class($arguments[2]) : $arguments[2], - is_object($arguments[1]) ? get_class($arguments[1]) : $arguments[1] + is_object($arguments[1]) ? get_class($arguments[1]) : $arguments[1], ); return new static($message); @@ -58,7 +58,7 @@ public static function create(mixed $reason): self case self::INVALID_METADATA_CLASS: $message = sprintf( '"%s" is not a valid metadata object class', - is_object($arguments[1]) ? get_class($arguments[1]) : $arguments[1] + is_object($arguments[1]) ? get_class($arguments[1]) : $arguments[1], ); return new static($message); @@ -66,7 +66,7 @@ public static function create(mixed $reason): self case self::INVALID_PROCESSOR_INTERFACE_CLASS: $message = sprintf( '"%s" is not a valid ProcessorInterface class', - $arguments[1] + $arguments[1], ); return new static($message); diff --git a/lib/Factory/AbstractMetadataFactory.php b/lib/Factory/AbstractMetadataFactory.php index 5642f0c..5b2ffa5 100644 --- a/lib/Factory/AbstractMetadataFactory.php +++ b/lib/Factory/AbstractMetadataFactory.php @@ -25,19 +25,11 @@ abstract class AbstractMetadataFactory implements MetadataFactoryInterface { - private LoaderInterface $loader; - private ?EventDispatcherInterface $eventDispatcher; - private ?CacheItemPoolInterface $cache; - /** @var array */ private array $loadedClasses; - public function __construct(LoaderInterface $loader, ?EventDispatcherInterface $eventDispatcher = null, ?CacheItemPoolInterface $cache = null) + public function __construct(private LoaderInterface $loader, private EventDispatcherInterface|null $eventDispatcher = null, private CacheItemPoolInterface|null $cache = null) { - $this->loader = $loader; - $this->eventDispatcher = $eventDispatcher; - $this->cache = $cache; - $this->loadedClasses = []; } @@ -162,7 +154,7 @@ protected function dispatchClassMetadataLoadedEvent(ClassMetadataInterface $clas * * @phpstan-return class-string|bool */ - private function getClass(mixed $value): string | bool + private function getClass(mixed $value): string|bool { if (! is_object($value) && ! is_string($value)) { return false; diff --git a/lib/Loader/AbstractProcessorLoader.php b/lib/Loader/AbstractProcessorLoader.php index 8f3f688..fb4e6c0 100644 --- a/lib/Loader/AbstractProcessorLoader.php +++ b/lib/Loader/AbstractProcessorLoader.php @@ -15,11 +15,8 @@ abstract class AbstractProcessorLoader implements LoaderInterface { - protected ProcessorFactoryInterface $processorFactory; - - public function __construct(ProcessorFactoryInterface $processorFactory) + public function __construct(protected ProcessorFactoryInterface $processorFactory) { - $this->processorFactory = $processorFactory; } public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool diff --git a/lib/Loader/ChainLoader.php b/lib/Loader/ChainLoader.php index 725eeed..a2661bd 100644 --- a/lib/Loader/ChainLoader.php +++ b/lib/Loader/ChainLoader.php @@ -11,21 +11,14 @@ class ChainLoader implements LoaderInterface { - /** @var LoaderInterface[] */ - private array $loaders; - - /** - * @param LoaderInterface[] $loaders - */ - public function __construct(array $loaders) + /** @param LoaderInterface[] $loaders */ + public function __construct(private array $loaders) { foreach ($loaders as $loader) { if (! $loader instanceof LoaderInterface) { throw new InvalidArgumentException(sprintf('Class %s is expected to implement LoaderInterface', $loader::class)); } } - - $this->loaders = $loaders; } public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool diff --git a/lib/Loader/FileLoader.php b/lib/Loader/FileLoader.php index 4166a40..d836a97 100644 --- a/lib/Loader/FileLoader.php +++ b/lib/Loader/FileLoader.php @@ -10,11 +10,8 @@ abstract class FileLoader implements LoaderInterface { use FileLoaderTrait; - private string $filePath; - - public function __construct(string $filePath) + public function __construct(private string $filePath) { - $this->filePath = $filePath; } public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool diff --git a/lib/Loader/FileLoaderTrait.php b/lib/Loader/FileLoaderTrait.php index ee728bf..fbd1f57 100644 --- a/lib/Loader/FileLoaderTrait.php +++ b/lib/Loader/FileLoaderTrait.php @@ -22,7 +22,7 @@ private function loadFile(string $filePath): string throw new IOException(sprintf( "Cannot load file '%s': %s", $filePath, - $error['message'] ?? 'Unknown error' + $error['message'] ?? 'Unknown error', )); } diff --git a/lib/Loader/FilesLoader.php b/lib/Loader/FilesLoader.php index c87744f..b117734 100644 --- a/lib/Loader/FilesLoader.php +++ b/lib/Loader/FilesLoader.php @@ -8,18 +8,13 @@ class FilesLoader extends ChainLoader { - /** @phpstan-var class-string|null */ - private ?string $loaderClass; - /** * {@inheritdoc} * * @phpstan-param class-string|null $loaderClass */ - public function __construct(array $paths, ?string $loaderClass = null) + public function __construct(array $paths, private string|null $loaderClass = null) { - $this->loaderClass = $loaderClass; - $loaders = []; foreach ($paths as $path) { $loaders[] = $this->getLoader($path); diff --git a/lib/Loader/Locator/IteratorFileLocator.php b/lib/Loader/Locator/IteratorFileLocator.php index 6baab92..b86c556 100644 --- a/lib/Loader/Locator/IteratorFileLocator.php +++ b/lib/Loader/Locator/IteratorFileLocator.php @@ -33,11 +33,11 @@ public function locate(string $basePath, string $extension): array $iterator = new CallbackFilterIterator( new RecursiveIteratorIterator( new RecursiveDirectoryIterator($basePath, RecursiveDirectoryIterator::SKIP_DOTS), - RecursiveIteratorIterator::LEAVES_ONLY + RecursiveIteratorIterator::LEAVES_ONLY, ), static function (SplFileInfo $fileInfo) use ($regex) { return preg_match($regex, $fileInfo->getPathname()); - } + }, ); return array_map(static function (SplFileInfo $fileInfo) { diff --git a/lib/Loader/Processor/CompositeProcessor.php b/lib/Loader/Processor/CompositeProcessor.php index 5797df1..9abe6ba 100644 --- a/lib/Loader/Processor/CompositeProcessor.php +++ b/lib/Loader/Processor/CompositeProcessor.php @@ -13,17 +13,13 @@ */ class CompositeProcessor implements ProcessorInterface { - /** @var ProcessorInterface[] */ - private array $processors; - /** * Create a new instance containing specified processors. * * @param ProcessorInterface[] $processors */ - public function __construct(array $processors) + public function __construct(private array $processors) { - $this->processors = $processors; } public function process(MetadataInterface $metadata, mixed $subject): void diff --git a/lib/Loader/Processor/ProcessorFactory.php b/lib/Loader/Processor/ProcessorFactory.php index 8345646..b3a98bb 100644 --- a/lib/Loader/Processor/ProcessorFactory.php +++ b/lib/Loader/Processor/ProcessorFactory.php @@ -66,7 +66,7 @@ public function registerProcessors(string $dir): void $this->registerProcessorsByAttributes($dir); } - public function getProcessor(object | string $class): ?ProcessorInterface + public function getProcessor(object|string $class): ProcessorInterface|null { if (is_object($class)) { $class = $class::class; @@ -99,9 +99,7 @@ private function createComposite(array $processors): CompositeProcessor return new CompositeProcessor(array_map([self::class, 'instantiateProcessor'], $processors)); } - /** - * @phpstan-param class-string $processorClass - */ + /** @phpstan-param class-string $processorClass */ private static function instantiateProcessor(string $processorClass): ProcessorInterface // phpcs:ignore SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod { return new $processorClass(); diff --git a/lib/Loader/Processor/ProcessorFactoryInterface.php b/lib/Loader/Processor/ProcessorFactoryInterface.php index 7ba160f..36e42ab 100644 --- a/lib/Loader/Processor/ProcessorFactoryInterface.php +++ b/lib/Loader/Processor/ProcessorFactoryInterface.php @@ -12,5 +12,5 @@ interface ProcessorFactoryInterface /** * Get a processor able to handle $subject. */ - public function getProcessor(object | string $subject): ?ProcessorInterface; + public function getProcessor(object|string $subject): ProcessorInterface|null; } diff --git a/lib/MethodMetadata.php b/lib/MethodMetadata.php index 17cd224..ff13331 100644 --- a/lib/MethodMetadata.php +++ b/lib/MethodMetadata.php @@ -13,9 +13,7 @@ class MethodMetadata implements MetadataInterface private ReflectionMethod $reflectionMethod; - /** - * @phpstan-param class-string $class - */ + /** @phpstan-param class-string $class */ public function __construct(string $class, string $name) { $this->class = $class; diff --git a/lib/NullMetadata.php b/lib/NullMetadata.php index dc6ce6f..38a0b6d 100644 --- a/lib/NullMetadata.php +++ b/lib/NullMetadata.php @@ -9,11 +9,8 @@ */ final class NullMetadata implements MetadataInterface { - public string $name; - - public function __construct(string $name) + public function __construct(public string $name) { - $this->name = $name; } public function merge(MetadataInterface $metadata): void diff --git a/lib/PropertyMetadata.php b/lib/PropertyMetadata.php index c4d1c8f..f294773 100644 --- a/lib/PropertyMetadata.php +++ b/lib/PropertyMetadata.php @@ -13,9 +13,7 @@ class PropertyMetadata implements MetadataInterface private ReflectionProperty $reflectionProperty; - /** - * @phpstan-param class-string $class - */ + /** @phpstan-param class-string $class */ public function __construct(string $class, string $name) { $this->class = $class; diff --git a/tests/Factory/MetadataFactoryTest.php b/tests/Factory/MetadataFactoryTest.php index 343a4c4..2519a23 100644 --- a/tests/Factory/MetadataFactoryTest.php +++ b/tests/Factory/MetadataFactoryTest.php @@ -164,7 +164,9 @@ public function set_metadata_for_should_store_data_to_cache(): void $this->cache->getItem(\str_replace('\\', '_', $className)) ->willReturn($item = $this->prophesize(ItemInterface::class)); - $item->set($metadata)->shouldBeCalled(); + $item->set($metadata) + ->willReturn($item) + ->shouldBeCalled(); $this->cache->save($item)->shouldBeCalled(); $factory = new MetadataFactory($this->loader->reveal(), null, $this->cache->reveal()); @@ -198,7 +200,9 @@ public function get_metadata_for_should_store_data_into_cache(): void $this->cache->getItem(Argument::type('string')) ->willReturn($item = $this->prophesize(ItemInterface::class)); $item->isHit()->willReturn(false); - $item->set(Argument::type(ClassMetadataInterface::class))->shouldBeCalled(); + $item->set(Argument::type(ClassMetadataInterface::class)) + ->willReturn($item) + ->shouldBeCalled(); $this->cache->save($item)->shouldBeCalled(); $this->loader->loadClassMetadata(Argument::type(ClassMetadataInterface::class))->willReturn(true);