Skip to content

Commit

Permalink
feat(rector): setting up rector (#10)
Browse files Browse the repository at this point in the history
* feat(rector): setting up rector

* fix(naming): corrected typo in class name

* fix(review): change suggested from review

* fix(constructor): bring back constructor without php8.1 things

* feat(php): restore 8.0 compatibility only

Co-authored-by: Louis Pinsembert <[email protected]>
Co-authored-by: Millet Morgan <[email protected]>
  • Loading branch information
3 people authored Aug 18, 2022
1 parent 10dd195 commit 3edb6a6
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 83 deletions.
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 18 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Symfony\Set\SymfonySetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80,
SymfonySetList::SYMFONY_60,
SymfonySetList::SYMFONY_CODE_QUALITY,
SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION,
SymfonySetList::SYMFONY_STRICT,
SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
]);
};
5 changes: 1 addition & 4 deletions src/EventListener/AddRateLimitHeadersListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 6 additions & 13 deletions src/EventListener/LimitRateListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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
)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RateLimitModifierInterface> */
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
Expand Down Expand Up @@ -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.');
}

Expand Down
15 changes: 2 additions & 13 deletions src/EventListener/ReadRateLimitAnnotationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,20 @@

class ReadRateLimitAnnotationListener implements EventSubscriberInterface
{
private Reader $annotationReader;
/** @var iterable<RateLimitModifierInterface> */
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
Expand Down
11 changes: 2 additions & 9 deletions src/EventListener/ReadRateLimitConfigurationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@
class ReadRateLimitConfigurationListener implements EventSubscriberInterface
{
/** @var iterable<RateLimitModifierInterface> */
private $rateLimitModifiers;
private int $limit;
private int $period;
/** @var array<string, array<string, int>> */
private array $routes;
private iterable $rateLimitModifiers;

/**
* @param RateLimitModifierInterface[] $rateLimitModifiers
* @param array<string, array<string, int>> $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)) {
Expand All @@ -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
Expand Down
11 changes: 1 addition & 10 deletions src/Model/GraphQLEndpointConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Model/RateLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
9 changes: 1 addition & 8 deletions src/Model/StoredRateLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions src/RateLimitModifier/RequestAttributeRateLimitModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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%'
Expand Down
2 changes: 1 addition & 1 deletion src/Storage/RateLimitInMemoryStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class RateLimitInMemoryStorage implements ManuallyResetableRateLimitStorageInterface
{
/** @var array<string, StoredRateLimit> */
private static $storedRateLimits = [];
private static array $storedRateLimits = [];

public function getStoredRateLimit(RateLimit $rateLimit): ?StoredRateLimit
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<RateLimitModifierInterface|MockObject> */
Expand All @@ -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 = [
Expand Down

0 comments on commit 3edb6a6

Please sign in to comment.