Skip to content

Commit

Permalink
chore: fixed code style and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alekitto committed Jul 19, 2023
1 parent ceba3a6 commit f9f2204
Show file tree
Hide file tree
Showing 17 changed files with 30 additions and 69 deletions.
6 changes: 2 additions & 4 deletions lib/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions lib/Event/ClassMetadataLoadedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -50,23 +50,23 @@ 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);

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);

case self::INVALID_PROCESSOR_INTERFACE_CLASS:
$message = sprintf(
'"%s" is not a valid ProcessorInterface class',
$arguments[1]
$arguments[1],
);

return new static($message);
Expand Down
12 changes: 2 additions & 10 deletions lib/Factory/AbstractMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,11 @@

abstract class AbstractMetadataFactory implements MetadataFactoryInterface
{
private LoaderInterface $loader;
private ?EventDispatcherInterface $eventDispatcher;
private ?CacheItemPoolInterface $cache;

/** @var array<string, ClassMetadataInterface> */
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 = [];
}

Expand Down Expand Up @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions lib/Loader/AbstractProcessorLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 2 additions & 9 deletions lib/Loader/ChainLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions lib/Loader/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/Loader/FileLoaderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
));
}

Expand Down
7 changes: 1 addition & 6 deletions lib/Loader/FilesLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@

class FilesLoader extends ChainLoader
{
/** @phpstan-var class-string<LoaderInterface>|null */
private ?string $loaderClass;

/**
* {@inheritdoc}
*
* @phpstan-param class-string<LoaderInterface>|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);
Expand Down
4 changes: 2 additions & 2 deletions lib/Loader/Locator/IteratorFileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 1 addition & 5 deletions lib/Loader/Processor/CompositeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions lib/Loader/Processor/ProcessorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -99,9 +99,7 @@ private function createComposite(array $processors): CompositeProcessor
return new CompositeProcessor(array_map([self::class, 'instantiateProcessor'], $processors));
}

/**
* @phpstan-param class-string<ProcessorInterface> $processorClass
*/
/** @phpstan-param class-string<ProcessorInterface> $processorClass */
private static function instantiateProcessor(string $processorClass): ProcessorInterface // phpcs:ignore SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod
{
return new $processorClass();
Expand Down
2 changes: 1 addition & 1 deletion lib/Loader/Processor/ProcessorFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 1 addition & 3 deletions lib/MethodMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions lib/NullMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions lib/PropertyMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions tests/Factory/MetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f9f2204

Please sign in to comment.