diff --git a/Makefile b/Makefile index fe8b029..5e2ba09 100644 --- a/Makefile +++ b/Makefile @@ -19,11 +19,23 @@ ci: quality test install: clean-vendor composer-install .PHONY: quality -quality: cs-ci phpstan +quality: cs-ci phpstan rector .PHONY: test test: phpunit +.PHONY: rector +## Run rector in dry run mode +rector: + $(call printSection,TEST rector) + php ${BIN_DIR}/rector process src --dry-run + +.PHONY: rector-fix +## Run rector +rector-fix: + $(call printSection,TEST rector-fix) + php ${BIN_DIR}/rector process src + ### DEPENDENCIES ### .PHONY: clean-vendor diff --git a/composer.json b/composer.json index 0161692..571a9c2 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,8 @@ "m6web/php-cs-fixer-config": "^2.1", "phpstan/phpstan": "^1.5", "phpstan/phpstan-phpunit": "^1.1", - "symfony/var-dumper": "^6.0" + "symfony/var-dumper": "^6.0", + "rector/rector": "^0.12.22" }, "suggest": { "webonyx/graphql-php": "Needed to support @GraphQLRateLimit annotation" diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..e8130b9 --- /dev/null +++ b/rector.php @@ -0,0 +1,18 @@ +sets([ + LevelSetList::UP_TO_PHP_80, + SymfonySetList::SYMFONY_60, + SymfonySetList::SYMFONY_CODE_QUALITY, + SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION, + SymfonySetList::SYMFONY_STRICT, + SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES, + ]); +}; diff --git a/src/EventListener/AddRateLimitHeadersListener.php b/src/EventListener/AddRateLimitHeadersListener.php index ea8b8db..9148971 100644 --- a/src/EventListener/AddRateLimitHeadersListener.php +++ b/src/EventListener/AddRateLimitHeadersListener.php @@ -10,11 +10,8 @@ class AddRateLimitHeadersListener implements EventSubscriberInterface { - private bool $displayHeaders; - - public function __construct(bool $displayHeaders) + public function __construct(private bool $displayHeaders) { - $this->displayHeaders = $displayHeaders; } public function onKernelResponse(ResponseEvent $event): void diff --git a/src/EventListener/LimitRateListener.php b/src/EventListener/LimitRateListener.php index 0544b08..0a0bd3d 100644 --- a/src/EventListener/LimitRateListener.php +++ b/src/EventListener/LimitRateListener.php @@ -14,13 +14,8 @@ class LimitRateListener implements EventSubscriberInterface { - private RateLimitStorageInterface $storage; - private bool $displayHeaders; - - public function __construct(RateLimitStorageInterface $storage, bool $displayHeaders) + public function __construct(private RateLimitStorageInterface $storage, private bool $displayHeaders) { - $this->storage = $storage; - $this->displayHeaders = $displayHeaders; } public function onKernelController(ControllerArgumentsEvent $event): void @@ -38,7 +33,7 @@ public function onKernelController(ControllerArgumentsEvent $event): void $rateLimit = $request->attributes->get('_rate_limit'); if (!$rateLimit instanceof RateLimit) { - throw new \InvalidArgumentException(sprintf('Request attribute "_rate_limit" should be of type "%s". "%s" given.', RateLimit::class, \is_object($rateLimit) ? \get_class($rateLimit) : \gettype($rateLimit))); + throw new \InvalidArgumentException(sprintf('Request attribute "_rate_limit" should be of type "%s". "%s" given.', RateLimit::class, get_debug_type($rateLimit))); } $storedRateLimit = $this->storage->getStoredRateLimit($rateLimit); @@ -55,12 +50,10 @@ public function onKernelController(ControllerArgumentsEvent $event): void if (null !== $storedRateLimit && $storedRateLimit->getHits() >= $rateLimit->getLimit()) { $displayHeaders = $this->displayHeaders; $event->setController( - static function () use ($displayHeaders, $storedRateLimit) { - return new JsonResponse( - $displayHeaders ? $storedRateLimit->getLimitReachedOutput() : Response::$statusTexts[Response::HTTP_TOO_MANY_REQUESTS], - Response::HTTP_TOO_MANY_REQUESTS - ); - } + static fn () => new JsonResponse( + $displayHeaders ? $storedRateLimit->getLimitReachedOutput() : Response::$statusTexts[Response::HTTP_TOO_MANY_REQUESTS], + Response::HTTP_TOO_MANY_REQUESTS + ) ); } diff --git a/src/EventListener/ReadGraphQLRateLmitAnnotationListener.php b/src/EventListener/ReadGraphQLRateLimitAnnotationListener.php similarity index 87% rename from src/EventListener/ReadGraphQLRateLmitAnnotationListener.php rename to src/EventListener/ReadGraphQLRateLimitAnnotationListener.php index c4a6ae7..7deb9d9 100644 --- a/src/EventListener/ReadGraphQLRateLmitAnnotationListener.php +++ b/src/EventListener/ReadGraphQLRateLimitAnnotationListener.php @@ -13,31 +13,22 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\ControllerEvent; -class ReadGraphQLRateLmitAnnotationListener implements EventSubscriberInterface +class ReadGraphQLRateLimitAnnotationListener implements EventSubscriberInterface { - private Reader $annotationReader; /** @var iterable */ - private $rateLimitModifiers; - private int $limit; - private int $period; - private ContainerInterface $container; + private iterable $rateLimitModifiers; /** * @param RateLimitModifierInterface[] $rateLimitModifiers */ - public function __construct(ContainerInterface $container, Reader $annotationReader, iterable $rateLimitModifiers, int $limit, int $period) + public function __construct(private ContainerInterface $container, private Reader $annotationReader, iterable $rateLimitModifiers, private int $limit, private int $period) { foreach ($rateLimitModifiers as $rateLimitModifier) { if (!($rateLimitModifier instanceof RateLimitModifierInterface)) { throw new \InvalidArgumentException('$rateLimitModifiers must be instance of '.RateLimitModifierInterface::class); } } - - $this->annotationReader = $annotationReader; $this->rateLimitModifiers = $rateLimitModifiers; - $this->limit = $limit; - $this->period = $period; - $this->container = $container; } public function onKernelController(ControllerEvent $event): void @@ -66,7 +57,7 @@ public function onKernelController(ControllerEvent $event): void return; } - if (!class_exists('GraphQL\Language\Parser')) { + if (!class_exists(\GraphQL\Language\Parser::class)) { throw new \Exception('Run "composer require webonyx/graphql-php" to use @GraphQLRateLimit annotation.'); } diff --git a/src/EventListener/ReadRateLimitAnnotationListener.php b/src/EventListener/ReadRateLimitAnnotationListener.php index ace7653..99c3e51 100644 --- a/src/EventListener/ReadRateLimitAnnotationListener.php +++ b/src/EventListener/ReadRateLimitAnnotationListener.php @@ -14,31 +14,20 @@ class ReadRateLimitAnnotationListener implements EventSubscriberInterface { - private Reader $annotationReader; /** @var iterable */ - private $rateLimitModifiers; - private int $limit; - private int $period; - private bool $limitByRoute; - private ContainerInterface $container; + private iterable $rateLimitModifiers; /** * @param RateLimitModifierInterface[] $rateLimitModifiers */ - public function __construct(ContainerInterface $container, Reader $annotationReader, iterable $rateLimitModifiers, int $limit, int $period, bool $limitByRoute) + public function __construct(private ContainerInterface $container, private Reader $annotationReader, iterable $rateLimitModifiers, private int $limit, private int $period, private bool $limitByRoute) { foreach ($rateLimitModifiers as $rateLimitModifier) { if (!($rateLimitModifier instanceof RateLimitModifierInterface)) { throw new \InvalidArgumentException('$rateLimitModifiers must be instance of '.RateLimitModifierInterface::class); } } - - $this->annotationReader = $annotationReader; $this->rateLimitModifiers = $rateLimitModifiers; - $this->limit = $limit; - $this->period = $period; - $this->limitByRoute = $limitByRoute; - $this->container = $container; } public function onKernelController(ControllerEvent $event): void diff --git a/src/EventListener/ReadRateLimitConfigurationListener.php b/src/EventListener/ReadRateLimitConfigurationListener.php index 57c8103..87e9e90 100644 --- a/src/EventListener/ReadRateLimitConfigurationListener.php +++ b/src/EventListener/ReadRateLimitConfigurationListener.php @@ -12,17 +12,13 @@ class ReadRateLimitConfigurationListener implements EventSubscriberInterface { /** @var iterable */ - private $rateLimitModifiers; - private int $limit; - private int $period; - /** @var array> */ - private array $routes; + private iterable $rateLimitModifiers; /** * @param RateLimitModifierInterface[] $rateLimitModifiers * @param array> $routes */ - public function __construct(iterable $rateLimitModifiers, int $limit, int $period, array $routes) + public function __construct(iterable $rateLimitModifiers, private int $limit, private int $period, private array $routes) { foreach ($rateLimitModifiers as $rateLimitModifier) { if (!($rateLimitModifier instanceof RateLimitModifierInterface)) { @@ -31,9 +27,6 @@ public function __construct(iterable $rateLimitModifiers, int $limit, int $perio } $this->rateLimitModifiers = $rateLimitModifiers; - $this->limit = $limit; - $this->period = $period; - $this->routes = $routes; } public function onKernelController(ControllerEvent $event): void diff --git a/src/Model/GraphQLEndpointConfiguration.php b/src/Model/GraphQLEndpointConfiguration.php index fea6107..db24e2a 100644 --- a/src/Model/GraphQLEndpointConfiguration.php +++ b/src/Model/GraphQLEndpointConfiguration.php @@ -4,17 +4,8 @@ class GraphQLEndpointConfiguration { - private ?int $limit; - - private ?int $period; - - private string $endpoint; - - public function __construct(?int $limit, ?int $period, string $endpoint) + public function __construct(private ?int $limit, private ?int $period, private string $endpoint) { - $this->limit = $limit; - $this->period = $period; - $this->endpoint = $endpoint; } public function getEndpoint(): string diff --git a/src/Model/RateLimit.php b/src/Model/RateLimit.php index db94a2f..0b39bcf 100644 --- a/src/Model/RateLimit.php +++ b/src/Model/RateLimit.php @@ -46,7 +46,7 @@ public function getDiscriminator(): string throw new \InvalidArgumentException('Cannot compute rate limit discriminator with an empty vary.'); } - return (string) json_encode($this->vary); + return (string) json_encode($this->vary, JSON_THROW_ON_ERROR); } /** diff --git a/src/Model/StoredRateLimit.php b/src/Model/StoredRateLimit.php index bd6ba44..b901db9 100644 --- a/src/Model/StoredRateLimit.php +++ b/src/Model/StoredRateLimit.php @@ -6,15 +6,8 @@ class StoredRateLimit { - private RateLimit $rateLimit; - private int $hits; - private \DateTimeImmutable $validUntil; - - public function __construct(RateLimit $rateLimit, int $hits, \DateTimeImmutable $validUntil) + public function __construct(private RateLimit $rateLimit, private int $hits, private \DateTimeImmutable $validUntil) { - $this->rateLimit = $rateLimit; - $this->hits = $hits; - $this->validUntil = $validUntil; } public function getHash(): string diff --git a/src/RateLimitModifier/RequestAttributeRateLimitModifier.php b/src/RateLimitModifier/RequestAttributeRateLimitModifier.php index a89b0fd..2fe92e2 100644 --- a/src/RateLimitModifier/RequestAttributeRateLimitModifier.php +++ b/src/RateLimitModifier/RequestAttributeRateLimitModifier.php @@ -7,11 +7,8 @@ class RequestAttributeRateLimitModifier implements RateLimitModifierInterface { - private string $attributeName; - - public function __construct(string $attributeName) + public function __construct(private string $attributeName) { - $this->attributeName = $attributeName; } public function support(Request $request): bool diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 1f60579..b0cf55d 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -20,7 +20,7 @@ services: $rateLimitModifiers: !tagged rate_limit.modifiers $routes: '%bedrock_rate_limit.routes%' - Bedrock\Bundle\RateLimitBundle\EventListener\ReadGraphQLRateLmitAnnotationListener: + Bedrock\Bundle\RateLimitBundle\EventListener\ReadGraphQLRateLimitAnnotationListener: arguments: $limit: '%bedrock_rate_limit.limit%' $period: '%bedrock_rate_limit.period%' diff --git a/src/Storage/RateLimitInMemoryStorage.php b/src/Storage/RateLimitInMemoryStorage.php index 615c9a2..9b012ca 100644 --- a/src/Storage/RateLimitInMemoryStorage.php +++ b/src/Storage/RateLimitInMemoryStorage.php @@ -10,7 +10,7 @@ class RateLimitInMemoryStorage implements ManuallyResetableRateLimitStorageInterface { /** @var array */ - private static $storedRateLimits = []; + private static array $storedRateLimits = []; public function getStoredRateLimit(RateLimit $rateLimit): ?StoredRateLimit { diff --git a/tests/EventListener/ReadGraphQLRateLmitAnnotationListenerTest.php b/tests/EventListener/ReadGraphQLRateLimitAnnotationListenerTest.php similarity index 97% rename from tests/EventListener/ReadGraphQLRateLmitAnnotationListenerTest.php rename to tests/EventListener/ReadGraphQLRateLimitAnnotationListenerTest.php index a97c114..18c8a54 100644 --- a/tests/EventListener/ReadGraphQLRateLmitAnnotationListenerTest.php +++ b/tests/EventListener/ReadGraphQLRateLimitAnnotationListenerTest.php @@ -4,7 +4,7 @@ use Bedrock\Bundle\RateLimitBundle\Annotation\GraphQLRateLimit; use Bedrock\Bundle\RateLimitBundle\Annotation\GraphQLRateLimit as GraphQLRateLimitAnnotation; -use Bedrock\Bundle\RateLimitBundle\EventListener\ReadGraphQLRateLmitAnnotationListener; +use Bedrock\Bundle\RateLimitBundle\EventListener\ReadGraphQLRateLimitAnnotationListener; use Bedrock\Bundle\RateLimitBundle\Model\RateLimit; use Bedrock\Bundle\RateLimitBundle\RateLimitModifier\RateLimitModifierInterface; use Doctrine\Common\Annotations\AnnotationReader; @@ -17,9 +17,9 @@ use Symfony\Component\HttpKernel\Event\ControllerEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; -class ReadGraphQLRateLmitAnnotationListenerTest extends TestCase +class ReadGraphQLRateLimitAnnotationListenerTest extends TestCase { - private ReadGraphQLRateLmitAnnotationListener $readRateLimitAnnotationListener; + private ReadGraphQLRateLimitAnnotationListener $readRateLimitAnnotationListener; /** @var AnnotationReader|MockObject */ private $annotationReader; /** @var array */ @@ -31,7 +31,7 @@ class ReadGraphQLRateLmitAnnotationListenerTest extends TestCase public function createGraphQLReadRateLimitAnnotationListener(): void { - $this->readRateLimitAnnotationListener = new ReadGraphQLRateLmitAnnotationListener( + $this->readRateLimitAnnotationListener = new ReadGraphQLRateLimitAnnotationListener( $this->container = $this->createMock(ContainerInterface::class), $this->annotationReader = $this->createMock(AnnotationReader::class), $this->rateLimitModifiers = [