From dd7366fa8c72a2a74d2482e85f99a11f04e70865 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Felix=20=C5=A0ulc?= Date: Tue, 2 Jan 2024 16:18:13 +0100 Subject: [PATCH] Code: apply phpstan --- phpstan.neon | 77 ++++++++++++++++++- src/Bridge/Nette/DI/ImagistExtension.php | 39 ++-------- src/Bridge/Nette/Form/ImageUploadControl.php | 3 +- src/Bridge/Nette/Macro/ImageMacro.php | 52 ------------- .../Extension/ImageStorageExtension.php | 2 +- .../Symfony/Serializer/ImagistNormalizer.php | 18 ++++- src/Entity/EmptyImage.php | 2 +- src/Entity/PromisedImage.php | 10 +-- src/Scope/Scope.php | 2 +- src/Storage/ImageStorage.php | 4 +- src/Transaction/Transaction.php | 2 +- 11 files changed, 107 insertions(+), 104 deletions(-) delete mode 100644 src/Bridge/Nette/Macro/ImageMacro.php diff --git a/phpstan.neon b/phpstan.neon index 562ab05..12c539c 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,8 +1,12 @@ includes: - - vendor/contributte/phpstan/phpstan.neon + #- vendor/contributte/phpstan/phpstan.neon + - vendor/phpstan/phpstan-deprecation-rules/rules.neon + - vendor/phpstan/phpstan-nette/extension.neon + - vendor/phpstan/phpstan-nette/rules.neon + #- vendor/phpstan/phpstan-strict-rules/rules.neon parameters: - level: 9 + level: 8 phpVersion: 80100 scanDirectories: @@ -16,3 +20,72 @@ parameters: - .docs ignoreErrors: + - + message: """ + #^Call to deprecated method getName\\(\\) of class Contributte\\\\Imagist\\\\Bridge\\\\Doctrine\\\\ImageType\\: + this method will be removed in Doctrine DBAL 4\\.0, + use \\{@see TypeRegistry\\:\\:lookupName\\(\\)\\} instead\\.$# + """ + count: 2 + path: src/Bridge/Doctrine/ImageType.php + + - + message: "#^Class Contributte\\\\Imagist\\\\Bridge\\\\Nette\\\\DI\\\\GumletImageStorageExtension extends @final class Contributte\\\\Imagist\\\\Bridge\\\\Nette\\\\DI\\\\GumletImagistExtension\\.$#" + count: 1 + path: src/Bridge/Nette/DI/GumletImageStorageExtension.php + + - + message: "#^Class Contributte\\\\Imagist\\\\Bridge\\\\Nette\\\\DI\\\\ImageStorageConfigFiltersExtension extends @final class Contributte\\\\Imagist\\\\Bridge\\\\Nette\\\\DI\\\\ImagistNeonFiltersExtension\\.$#" + count: 1 + path: src/Bridge/Nette/DI/ImageStorageConfigFiltersExtension.php + + - + message: "#^Class Contributte\\\\Imagist\\\\Bridge\\\\Nette\\\\DI\\\\ImageStorageExtension extends @final class Contributte\\\\Imagist\\\\Bridge\\\\Nette\\\\DI\\\\ImagistExtension\\.$#" + count: 1 + path: src/Bridge/Nette/DI/ImageStorageExtension.php + + - + message: "#^Method Contributte\\\\Imagist\\\\Bridge\\\\Symfony\\\\Serializer\\\\ImagistNormalizer\\:\\:normalize\\(\\) return type has no value type specified in iterable type array\\.$#" + count: 1 + path: src/Bridge/Symfony/Serializer/ImagistNormalizer.php + + - + message: "#^Method Contributte\\\\Imagist\\\\Bridge\\\\Symfony\\\\Serializer\\\\ImagistNormalizer\\:\\:normalize\\(\\) return type with generic class ArrayObject does not specify its types\\: TKey, TValue$#" + count: 1 + path: src/Bridge/Symfony/Serializer/ImagistNormalizer.php + + - + message: "#^Method Contributte\\\\Imagist\\\\Bridge\\\\Symfony\\\\Serializer\\\\ImagistNormalizer\\:\\:normalize\\(\\) should return array\\|ArrayObject\\|bool\\|float\\|int\\|string\\|null but returns Contributte\\\\Imagist\\\\Entity\\\\PersistentImageInterface\\.$#" + count: 1 + path: src/Bridge/Symfony/Serializer/ImagistNormalizer.php + + - + message: "#^Method Contributte\\\\Imagist\\\\Bridge\\\\Symfony\\\\Serializer\\\\ImagistNormalizer\\:\\:supportsNormalization\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#" + count: 1 + path: src/Bridge/Symfony/Serializer/ImagistNormalizer.php + + - + message: "#^PHPDoc tag @return with type mixed is not subtype of native type array\\|ArrayObject\\|bool\\|float\\|int\\|string\\|null\\.$#" + count: 1 + path: src/Bridge/Symfony/Serializer/ImagistNormalizer.php + + - + message: "#^Method Contributte\\\\Imagist\\\\Entity\\\\PromisedImage\\:\\:getOriginal\\(\\) should return Contributte\\\\Imagist\\\\Entity\\\\PromisedImage but returns Contributte\\\\Imagist\\\\Entity\\\\PersistentImageInterface\\.$#" + count: 1 + path: src/Entity/PromisedImage.php + + - + message: "#^Method Contributte\\\\Imagist\\\\Entity\\\\PromisedImage\\:\\:withFilter\\(\\) should return Contributte\\\\Imagist\\\\Entity\\\\PromisedImage but returns Contributte\\\\Imagist\\\\Entity\\\\PersistentImageInterface\\.$#" + count: 1 + path: src/Entity/PromisedImage.php + + - + message: "#^Method Contributte\\\\Imagist\\\\Entity\\\\PromisedImage\\:\\:withName\\(\\) should return Contributte\\\\Imagist\\\\Entity\\\\PromisedImage but returns Contributte\\\\Imagist\\\\Entity\\\\PersistentImageInterface\\.$#" + count: 1 + path: src/Entity/PromisedImage.php + + - + message: "#^Method Contributte\\\\Imagist\\\\Entity\\\\PromisedImage\\:\\:withScope\\(\\) should return Contributte\\\\Imagist\\\\Entity\\\\PromisedImage but returns Contributte\\\\Imagist\\\\Entity\\\\PersistentImageInterface\\.$#" + count: 1 + path: src/Entity/PromisedImage.php + diff --git a/src/Bridge/Nette/DI/ImagistExtension.php b/src/Bridge/Nette/DI/ImagistExtension.php index 20fe358..35abf8d 100644 --- a/src/Bridge/Nette/DI/ImagistExtension.php +++ b/src/Bridge/Nette/DI/ImagistExtension.php @@ -9,9 +9,9 @@ use Contributte\Imagist\Bridge\Imagine\ImagineResourceFactory; use Contributte\Imagist\Bridge\Nette\Filter\NetteOperationProcessor; use Contributte\Imagist\Bridge\Nette\Filter\NetteResourceFactory; +use Contributte\Imagist\Bridge\Nette\Latte\Extension\ImagistExtension as ImagistExtensionLatte; use Contributte\Imagist\Bridge\Nette\Latte\LatteImageProvider; use Contributte\Imagist\Bridge\Nette\LinkGenerator; -use Contributte\Imagist\Bridge\Nette\Macro\ImageMacro; use Contributte\Imagist\Bridge\Nette\Tracy\ImageBarPanel; use Contributte\Imagist\Bridge\Nette\Tracy\ImagistBlueScreen; use Contributte\Imagist\Database\DatabaseConverter; @@ -56,7 +56,6 @@ use Doctrine\Common\Annotations\Reader; use Doctrine\DBAL\Connection; use Doctrine\ORM\EntityManagerInterface; -use Latte\Engine; use Nette\Bridges\ApplicationLatte\LatteFactory; use Nette\DI\CompilerExtension; use Nette\DI\ContainerBuilder; @@ -75,7 +74,9 @@ /** * @final */ -/*final*/ class ImagistExtension extends CompilerExtension +/*final*/ + +class ImagistExtension extends CompilerExtension { /** @var callable[] */ @@ -341,36 +342,6 @@ private function loadDoctrine(ContainerBuilder $builder): void } private function loadLatte(ContainerBuilder $builder): void - { - if (version_compare(Engine::VERSION, '3', '<')) { // @phpstan-ignore-line - $this->loadLatte2($builder); - } else { - $this->loadLatte3($builder); - } - } - - private function loadLatte2(ContainerBuilder $builder): void - { - $serviceName = $builder->getByType(LatteFactory::class); - if (!$serviceName) { - return; - } - - $builder->addDefinition($this->prefix('latte.provider')) - ->setFactory(LatteImageProvider::class); - - $factory = $builder->getDefinition($serviceName); - assert($factory instanceof FactoryDefinition); - - $factory->getResultDefinition() - ->addSetup('?->onCompile[] = function ($engine) { ?::install($engine->getCompiler()); }', [ - '@self', - ImageMacro::class, - ]) - ->addSetup('addProvider', ['images', $this->prefix('@latte.provider')]); - } - - private function loadLatte3(ContainerBuilder $builder): void { $serviceName = $builder->getByType(LatteFactory::class); if (!$serviceName) { @@ -381,7 +352,7 @@ private function loadLatte3(ContainerBuilder $builder): void ->setFactory(LatteImageProvider::class); $extension = $builder->addDefinition($this->prefix('latte.extension')) - ->setFactory(\Contributte\Imagist\Bridge\Nette\Latte\Extension\ImagistExtension::class); + ->setFactory(ImagistExtensionLatte::class); $factory = $builder->getDefinition($serviceName); assert($factory instanceof FactoryDefinition); diff --git a/src/Bridge/Nette/Form/ImageUploadControl.php b/src/Bridge/Nette/Form/ImageUploadControl.php index 7a82c47..093ab12 100644 --- a/src/Bridge/Nette/Form/ImageUploadControl.php +++ b/src/Bridge/Nette/Form/ImageUploadControl.php @@ -68,7 +68,7 @@ public function setScope(?Scope $scope): static /** * @return static */ - public function setValue(FileUpload|PersistentImageInterface|string|null $value): static + public function setValue(mixed $value): static { if ($value === null) { $this->entity = $this->entity->withValue(); @@ -94,7 +94,6 @@ public function setValue(FileUpload|PersistentImageInterface|string|null $value) ->withDefault(new PersistentImage($value)) ->withValue(null); } else { - // @phpstan-ignore-next-line $value is mixed $type = is_object($value) ? $value::class : gettype($value); throw new InvalidArgumentException( diff --git a/src/Bridge/Nette/Macro/ImageMacro.php b/src/Bridge/Nette/Macro/ImageMacro.php deleted file mode 100644 index 9e3cade..0000000 --- a/src/Bridge/Nette/Macro/ImageMacro.php +++ /dev/null @@ -1,52 +0,0 @@ -addMacro('img', [$me, 'beginMacro'], null, [$me, 'attrMacro']); - } - - public function beginMacro(MacroNode $node, PhpWriter $writer): string - { - $name = $node->tokenizer->fetchWord(); - if (!$name) { - throw new CompileException('Missing image name in {img}'); - } - - return $writer->write( - '%node.line ' . - 'echo $this->global->images->link(' . ($name[0] === '$' ? '%raw' : '%word') . ', %node.array?);', - $name - ); - } - - public function attrMacro(MacroNode $node, PhpWriter $writer): string - { - $htmlNode = $node->htmlNode; - if (!$htmlNode) { - throw new LogicException('Macro n:img must be in a or img attribute'); - } - - $attr = $htmlNode->name === 'img' ? '" src="' : '" href="'; - - return $writer->write( - 'echo %raw . "\""; %raw echo "\""', - $attr, - $this->beginMacro($node, $writer) - ); - } - -} diff --git a/src/Bridge/Symfony/Extension/ImageStorageExtension.php b/src/Bridge/Symfony/Extension/ImageStorageExtension.php index 5959a3a..4a0575b 100644 --- a/src/Bridge/Symfony/Extension/ImageStorageExtension.php +++ b/src/Bridge/Symfony/Extension/ImageStorageExtension.php @@ -26,7 +26,7 @@ final class ImageStorageExtension extends Extension { /** - * @param string[] $configs + * @param mixed[] $configs */ public function load(array $configs, ContainerBuilder $container): void { diff --git a/src/Bridge/Symfony/Serializer/ImagistNormalizer.php b/src/Bridge/Symfony/Serializer/ImagistNormalizer.php index ff232ee..1ab2e92 100644 --- a/src/Bridge/Symfony/Serializer/ImagistNormalizer.php +++ b/src/Bridge/Symfony/Serializer/ImagistNormalizer.php @@ -2,6 +2,7 @@ namespace Contributte\Imagist\Bridge\Symfony\Serializer; +use ArrayObject; use Contributte\Imagist\Entity\EmptyImageInterface; use Contributte\Imagist\Entity\PersistentImageInterface; use Contributte\Imagist\Filter\FilterInterface; @@ -27,9 +28,9 @@ public function __construct(LinkGeneratorInterface $linkGenerator) /** * @param mixed[] $context - * @return PersistentImageInterface|array|string|null + * @return mixed */ - public function normalize(mixed $object, ?string $format = null, array $context = []): PersistentImageInterface|array|string|null + public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|ArrayObject|null { assert($object instanceof PersistentImageInterface); @@ -72,11 +73,22 @@ public function normalize(mixed $object, ?string $format = null, array $context return $this->linkGenerator->link($object); } - public function supportsNormalization(mixed $data, ?string $format = null): bool + /** + * {@inheritDoc} + */ + public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool { return $data instanceof PersistentImageInterface; } + /** + * {@inheritDoc} + */ + public function getSupportedTypes(?string $format): array + { + return ['*' => true]; + } + /** * @param array $filters * @return array diff --git a/src/Entity/EmptyImage.php b/src/Entity/EmptyImage.php index 4f5777f..e3e931f 100644 --- a/src/Entity/EmptyImage.php +++ b/src/Entity/EmptyImage.php @@ -31,7 +31,7 @@ public function getSuffix(): ?string /** * @inheritDoc */ - public function getOriginal() + public function getOriginal(): static { throw new EmptyImageException(sprintf('Cannot call %s on empty image', __METHOD__)); } diff --git a/src/Entity/PromisedImage.php b/src/Entity/PromisedImage.php index 35d0870..0630e89 100644 --- a/src/Entity/PromisedImage.php +++ b/src/Entity/PromisedImage.php @@ -66,7 +66,7 @@ public function hasFilter(): bool /** * @inheritDoc */ - public function withScope(Scope $scope): PersistentImageInterface + public function withScope(Scope $scope): static { return $this->getResult()->withScope($scope); } @@ -74,7 +74,7 @@ public function withScope(Scope $scope): PersistentImageInterface /** * @inheritDoc */ - public function withName(string $name): PersistentImageInterface + public function withName(string $name): static { return $this->getResult()->withName($name); } @@ -82,12 +82,12 @@ public function withName(string $name): PersistentImageInterface /** * @inheritDoc */ - public function withFilter(?FilterInterface $filter): PersistentImageInterface + public function withFilter(?FilterInterface $filter): static { return $this->getResult()->withFilter($filter); } - public function getOriginal(): PersistentImageInterface + public function getOriginal(): static { return $this->getResult()->getOriginal(); } @@ -144,7 +144,7 @@ public function then(callable $callable): void public function getResult(): PersistentImageInterface { - if (!$this->result) { + if ($this->result === null) { throw new PromiseException('Promise is still pending'); } diff --git a/src/Scope/Scope.php b/src/Scope/Scope.php index a6068c8..577c1ec 100644 --- a/src/Scope/Scope.php +++ b/src/Scope/Scope.php @@ -27,7 +27,7 @@ public static function fromString(string $scope): static public function isEmpty(): bool { - return !$this->scopes; + return $this->scopes === []; } public function startsWith(string $scope): bool diff --git a/src/Storage/ImageStorage.php b/src/Storage/ImageStorage.php index 9c01c56..e775981 100644 --- a/src/Storage/ImageStorage.php +++ b/src/Storage/ImageStorage.php @@ -70,13 +70,13 @@ public function persist(ImageInterface $image, array $context = []): PersistentI $result = $this->persister->persist($image, $context); $persistent = new PersistentImage($result->getId()); - if ($clone->getFilter()) { + if ($clone->getFilter() !== null) { $persistent = $persistent->withFilter($clone->getFilter()); } $event = new PersistedImageEvent($this, $clone, $persistent); - if ($this->dispatcher) { + if ($this->dispatcher !== null) { $this->dispatcher->dispatch($event); } diff --git a/src/Transaction/Transaction.php b/src/Transaction/Transaction.php index 1d50933..86103f2 100644 --- a/src/Transaction/Transaction.php +++ b/src/Transaction/Transaction.php @@ -103,7 +103,7 @@ public function rollback(): void $this->persisted = []; $this->removed = []; - if ($exception) { + if ($exception !== null) { throw new RollbackFailedException( sprintf('Rollback failed because of: %s', $exception->getMessage()), 0,