From f4cc327011ecb4132e54a9b61d45d8af539355f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Wed, 3 Dec 2025 11:19:10 +0100 Subject: [PATCH] Update PhpStan and fix all issues --- composer.json | 2 +- phpstan-baseline.neon | 18 ++++++++-- phpstan.neon | 3 -- src/Archive/DeleteArchivist.php | 5 +-- src/DependencyInjection/Configuration.php | 34 +++++++++++++++++-- .../YokaiSecurityTokenExtension.php | 11 ++++-- src/Entity/Token.php | 5 +++ src/Event/ConsumeTokenEvent.php | 10 +++--- src/Event/CreateTokenEvent.php | 14 ++++---- src/EventDispatcher.php | 6 +++- src/Factory/TokenFactoryInterface.php | 6 ++-- src/Manager/TokenManagerInterface.php | 6 ++-- src/Repository/DoctrineORMTokenRepository.php | 6 ++-- 13 files changed, 93 insertions(+), 33 deletions(-) diff --git a/composer.json b/composer.json index 3990831..b3df4af 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "require-dev": { "phpunit/phpunit": "^9.5", "symfony/yaml": "^6.4|^7.4|^8.0", - "phpstan/phpstan": "^1.7", + "phpstan/phpstan": "^2.1", "symplify/easy-coding-standard": "^13.0" }, "autoload": { diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 0ef85a8..e7a567f 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,12 +1,26 @@ parameters: ignoreErrors: - - message: "#^Property Yokai\\\\SecurityTokenBundle\\\\Entity\\\\Token\\:\\:\\$id is never written, only read\\.$#" + message: '#^Property Yokai\\SecurityTokenBundle\\Entity\\Token\:\:\$id \(int\|null\) is never assigned int so it can be removed from the property type\.$#' + identifier: property.unusedType count: 1 path: src/Entity/Token.php - - message: "#^Property Yokai\\\\SecurityTokenBundle\\\\Entity\\\\TokenUsage\\:\\:\\$id is never written, only read\\.$#" + message: '#^Property Yokai\\SecurityTokenBundle\\Entity\\Token\:\:\$id is never written, only read\.$#' + identifier: property.onlyRead + count: 1 + path: src/Entity/Token.php + + - + message: '#^Property Yokai\\SecurityTokenBundle\\Entity\\TokenUsage\:\:\$id \(int\|null\) is never assigned int so it can be removed from the property type\.$#' + identifier: property.unusedType + count: 1 + path: src/Entity/TokenUsage.php + + - + message: '#^Property Yokai\\SecurityTokenBundle\\Entity\\TokenUsage\:\:\$id is never written, only read\.$#' + identifier: property.onlyRead count: 1 path: src/Entity/TokenUsage.php diff --git a/phpstan.neon b/phpstan.neon index 7f1a1f4..5cfa48d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,8 +2,5 @@ includes: - phpstan-baseline.neon parameters: level: max - ignoreErrors: - - identifier: missingType.generics - - identifier: missingType.iterableValue paths: - src/ diff --git a/src/Archive/DeleteArchivist.php b/src/Archive/DeleteArchivist.php index fcd2f52..5948ab2 100644 --- a/src/Archive/DeleteArchivist.php +++ b/src/Archive/DeleteArchivist.php @@ -6,6 +6,7 @@ use DateTime; use Doctrine\ORM\EntityRepository; +use Yokai\SecurityTokenBundle\Entity\Token; /** * This archivist is removing all outdated tokens based on the `keepUntil` property. @@ -15,12 +16,12 @@ final class DeleteArchivist implements ArchivistInterface { /** - * @var EntityRepository + * @var EntityRepository */ private $tokenRepository; /** - * @param EntityRepository $tokenRepository The token entity repository + * @param EntityRepository $tokenRepository The token entity repository */ public function __construct(EntityRepository $tokenRepository) { diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index aa37a13..0ea2337 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -4,15 +4,37 @@ namespace Yokai\SecurityTokenBundle\DependencyInjection; -use Symfony\Component\Config\Definition\Builder\NodeDefinition; +use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; /** * @author Yann Eugoné + * + * @phpstan-type Config array{ + * tokens: array< + * array{ + * generator: string, + * duration: string, + * usages: int, + * keep: string, + * unique: bool, + * }, + * >, + * services: array{ + * information_guesser: string, + * token_factory: string, + * token_repository: string, + * token_manager: string, + * archivist: string, + * }, + * } */ final class Configuration implements ConfigurationInterface { + /** + * @return TreeBuilder<'array'> + */ public function getConfigTreeBuilder(): TreeBuilder { $builder = new TreeBuilder('yokai_security_token'); @@ -29,7 +51,10 @@ public function getConfigTreeBuilder(): TreeBuilder return $builder; } - private function getTokensNode(): NodeDefinition + /** + * @return ArrayNodeDefinition> + */ + private function getTokensNode(): ArrayNodeDefinition { $builder = new TreeBuilder('tokens'); $node = $builder->getRootNode(); @@ -60,7 +85,10 @@ private function getTokensNode(): NodeDefinition return $node; } - private function getServicesNode(): NodeDefinition + /** + * @return ArrayNodeDefinition> + */ + private function getServicesNode(): ArrayNodeDefinition { $builder = new TreeBuilder('services'); $node = $builder->getRootNode(); diff --git a/src/DependencyInjection/YokaiSecurityTokenExtension.php b/src/DependencyInjection/YokaiSecurityTokenExtension.php index e6de3d4..e338140 100644 --- a/src/DependencyInjection/YokaiSecurityTokenExtension.php +++ b/src/DependencyInjection/YokaiSecurityTokenExtension.php @@ -18,11 +18,14 @@ /** * @author Yann Eugoné + * + * @phpstan-import-type Config from Configuration */ final class YokaiSecurityTokenExtension extends Extension { public function load(array $configs, ContainerBuilder $container): void { + /** @var Config $config */ $config = $this->processConfiguration(new Configuration(), $configs); $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../../config')); @@ -33,6 +36,9 @@ public function load(array $configs, ContainerBuilder $container): void $this->registerAutoconfigureAliases($container); } + /** + * @param Config $config + */ private function registerTokens(array $config, ContainerBuilder $container): void { foreach ($config['tokens'] as $name => $token) { @@ -48,10 +54,11 @@ private function registerTokens(array $config, ContainerBuilder $container): voi } } + /** + * @param Config $config + */ private function registerAliases(array $config, ContainerBuilder $container): void { - $isTest = $container->getParameter('kernel.environment') === 'test'; - foreach ($config['services'] as $name => $service) { $alias = $container->setAlias(\sprintf('yokai_security_token.%s', $name), $service); $alias->setPublic(true); diff --git a/src/Entity/Token.php b/src/Entity/Token.php index 7dac656..71cf2fa 100644 --- a/src/Entity/Token.php +++ b/src/Entity/Token.php @@ -144,6 +144,9 @@ public function getCreatedAt(): DateTime return $this->createdAt; } + /** + * @return array + */ public function getCreatedInformation(): array { return $this->createdInformation; @@ -198,6 +201,8 @@ public function getLastUsage(): TokenUsage|null } /** + * @param array $information + * * @throws LogicException */ public function consume(array $information, DateTime|null $date = null): void diff --git a/src/Event/ConsumeTokenEvent.php b/src/Event/ConsumeTokenEvent.php index 433fb3a..eb86142 100644 --- a/src/Event/ConsumeTokenEvent.php +++ b/src/Event/ConsumeTokenEvent.php @@ -26,14 +26,14 @@ final class ConsumeTokenEvent extends Event private $at; /** - * @var array + * @var array */ private $information; /** - * @param Token $token The consumed token - * @param DateTime|null $at Date/time at which the token has been consumed - * @param array $information Some context information + * @param Token $token The consumed token + * @param DateTime|null $at Date/time at which the token has been consumed + * @param array $information Some context information */ public function __construct(Token $token, DateTime|null $at, array $information) { @@ -60,6 +60,8 @@ public function getAt(): DateTime|null /** * Some context information + * + * @return array */ public function getInformation(): array { diff --git a/src/Event/CreateTokenEvent.php b/src/Event/CreateTokenEvent.php index 95137f9..7934a30 100644 --- a/src/Event/CreateTokenEvent.php +++ b/src/Event/CreateTokenEvent.php @@ -24,14 +24,14 @@ final class CreateTokenEvent extends Event private $user; /** - * @var array + * @var array */ private $payload; /** - * @param string $purpose The token purpose - * @param mixed $user The associated user - * @param array $payload The token payload + * @param string $purpose The token purpose + * @param mixed $user The associated user + * @param array $payload The token payload */ public function __construct(string $purpose, $user, array $payload) { @@ -62,6 +62,8 @@ public function getUser() /** * The token payload + * + * @return array */ public function getPayload(): array { @@ -71,7 +73,7 @@ public function getPayload(): array /** * Replace token payload * - * @param array $payload The new payload value + * @param array $payload The new payload value */ public function setPayload(array $payload): void { @@ -81,7 +83,7 @@ public function setPayload(array $payload): void /** * Add payload information to token * - * @param array $payload Some payload to add + * @param array $payload Some payload to add */ public function addPayload(array $payload): void { diff --git a/src/EventDispatcher.php b/src/EventDispatcher.php index 1d42d28..40d33d8 100644 --- a/src/EventDispatcher.php +++ b/src/EventDispatcher.php @@ -33,7 +33,8 @@ public function __construct(EventDispatcherInterface $eventDispatcher) } /** - * @param mixed $user + * @param mixed $user + * @param array $payload */ public function createToken(string $purpose, $user, array $payload): CreateTokenEvent { @@ -53,6 +54,9 @@ public function tokenCreated(Token $token): TokenCreatedEvent return $event; } + /** + * @param array $information + */ public function consumeToken(Token $token, DateTime|null $at = null, array $information = []): ConsumeTokenEvent { $this->eventDispatcher->dispatch( diff --git a/src/Factory/TokenFactoryInterface.php b/src/Factory/TokenFactoryInterface.php index 14a2aea..30ee843 100644 --- a/src/Factory/TokenFactoryInterface.php +++ b/src/Factory/TokenFactoryInterface.php @@ -16,9 +16,9 @@ interface TokenFactoryInterface /** * Create a new token. * - * @param mixed $user The to associated to the token - * @param string $purpose The token purpose - * @param array $payload The token payload + * @param mixed $user The to associated to the token + * @param string $purpose The token purpose + * @param array $payload The token payload * * @return Token The created token */ diff --git a/src/Manager/TokenManagerInterface.php b/src/Manager/TokenManagerInterface.php index 61931a0..e7003b9 100644 --- a/src/Manager/TokenManagerInterface.php +++ b/src/Manager/TokenManagerInterface.php @@ -34,9 +34,9 @@ public function get(string $purpose, string $value): Token; /** * Create a token. * - * @param string $purpose The token purpose - * @param mixed $user The user to associate to the token - * @param array $payload Some additional payload for the token + * @param string $purpose The token purpose + * @param mixed $user The user to associate to the token + * @param array $payload Some additional payload for the token * * @return Token The created token */ diff --git a/src/Repository/DoctrineORMTokenRepository.php b/src/Repository/DoctrineORMTokenRepository.php index 7050317..d68d508 100644 --- a/src/Repository/DoctrineORMTokenRepository.php +++ b/src/Repository/DoctrineORMTokenRepository.php @@ -24,13 +24,13 @@ final class DoctrineORMTokenRepository implements TokenRepositoryInterface private $manager; /** - * @var EntityRepository + * @var EntityRepository */ private $repository; /** - * @param EntityManager $manager The token entity manager - * @param EntityRepository $repository The token entity repository + * @param EntityManager $manager The token entity manager + * @param EntityRepository $repository The token entity repository */ public function __construct(EntityManager $manager, EntityRepository $repository) {