Skip to content

Commit

Permalink
Code: apply phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jan 2, 2024
1 parent 246d131 commit dd7366f
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 104 deletions.
77 changes: 75 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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

39 changes: 5 additions & 34 deletions src/Bridge/Nette/DI/ImagistExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -75,7 +74,9 @@
/**
* @final
*/
/*final*/ class ImagistExtension extends CompilerExtension
/*final*/

class ImagistExtension extends CompilerExtension
{

/** @var callable[] */
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/Bridge/Nette/Form/ImageUploadControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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(
Expand Down
52 changes: 0 additions & 52 deletions src/Bridge/Nette/Macro/ImageMacro.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Bridge/Symfony/Extension/ImageStorageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class ImageStorageExtension extends Extension
{

/**
* @param string[] $configs
* @param mixed[] $configs
*/
public function load(array $configs, ContainerBuilder $container): void
{
Expand Down
18 changes: 15 additions & 3 deletions src/Bridge/Symfony/Serializer/ImagistNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,9 +28,9 @@ public function __construct(LinkGeneratorInterface $linkGenerator)

/**
* @param mixed[] $context
* @return PersistentImageInterface|array<string|null>|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);

Expand Down Expand Up @@ -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<string, FilterInterface> $filters
* @return array<string|null>
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/EmptyImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__));
}
Expand Down
10 changes: 5 additions & 5 deletions src/Entity/PromisedImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,28 @@ public function hasFilter(): bool
/**
* @inheritDoc
*/
public function withScope(Scope $scope): PersistentImageInterface
public function withScope(Scope $scope): static
{
return $this->getResult()->withScope($scope);
}

/**
* @inheritDoc
*/
public function withName(string $name): PersistentImageInterface
public function withName(string $name): static
{
return $this->getResult()->withName($name);
}

/**
* @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();
}
Expand Down Expand Up @@ -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');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Scope/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Storage/ImageStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Transaction/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit dd7366f

Please sign in to comment.