Skip to content

Commit 3edb6a6

Browse files
lpinsembertLouis PinsembertMilletMorgan
authored
feat(rector): setting up rector (#10)
* 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 <lpinsembert.external@bedrockstreaming.com> Co-authored-by: Millet Morgan <millet.morgan5@gmail.com>
1 parent 10dd195 commit 3edb6a6

15 files changed

Lines changed: 58 additions & 83 deletions

Makefile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,23 @@ ci: quality test
1919
install: clean-vendor composer-install
2020

2121
.PHONY: quality
22-
quality: cs-ci phpstan
22+
quality: cs-ci phpstan rector
2323

2424
.PHONY: test
2525
test: phpunit
2626

27+
.PHONY: rector
28+
## Run rector in dry run mode
29+
rector:
30+
$(call printSection,TEST rector)
31+
php ${BIN_DIR}/rector process src --dry-run
32+
33+
.PHONY: rector-fix
34+
## Run rector
35+
rector-fix:
36+
$(call printSection,TEST rector-fix)
37+
php ${BIN_DIR}/rector process src
38+
2739
### DEPENDENCIES ###
2840

2941
.PHONY: clean-vendor

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"m6web/php-cs-fixer-config": "^2.1",
3232
"phpstan/phpstan": "^1.5",
3333
"phpstan/phpstan-phpunit": "^1.1",
34-
"symfony/var-dumper": "^6.0"
34+
"symfony/var-dumper": "^6.0",
35+
"rector/rector": "^0.12.22"
3536
},
3637
"suggest": {
3738
"webonyx/graphql-php": "Needed to support @GraphQLRateLimit annotation"

rector.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Set\ValueObject\LevelSetList;
7+
use Rector\Symfony\Set\SymfonySetList;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->sets([
11+
LevelSetList::UP_TO_PHP_80,
12+
SymfonySetList::SYMFONY_60,
13+
SymfonySetList::SYMFONY_CODE_QUALITY,
14+
SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION,
15+
SymfonySetList::SYMFONY_STRICT,
16+
SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
17+
]);
18+
};

src/EventListener/AddRateLimitHeadersListener.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010

1111
class AddRateLimitHeadersListener implements EventSubscriberInterface
1212
{
13-
private bool $displayHeaders;
14-
15-
public function __construct(bool $displayHeaders)
13+
public function __construct(private bool $displayHeaders)
1614
{
17-
$this->displayHeaders = $displayHeaders;
1815
}
1916

2017
public function onKernelResponse(ResponseEvent $event): void

src/EventListener/LimitRateListener.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,8 @@
1414

1515
class LimitRateListener implements EventSubscriberInterface
1616
{
17-
private RateLimitStorageInterface $storage;
18-
private bool $displayHeaders;
19-
20-
public function __construct(RateLimitStorageInterface $storage, bool $displayHeaders)
17+
public function __construct(private RateLimitStorageInterface $storage, private bool $displayHeaders)
2118
{
22-
$this->storage = $storage;
23-
$this->displayHeaders = $displayHeaders;
2419
}
2520

2621
public function onKernelController(ControllerArgumentsEvent $event): void
@@ -38,7 +33,7 @@ public function onKernelController(ControllerArgumentsEvent $event): void
3833
$rateLimit = $request->attributes->get('_rate_limit');
3934

4035
if (!$rateLimit instanceof RateLimit) {
41-
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)));
36+
throw new \InvalidArgumentException(sprintf('Request attribute "_rate_limit" should be of type "%s". "%s" given.', RateLimit::class, get_debug_type($rateLimit)));
4237
}
4338

4439
$storedRateLimit = $this->storage->getStoredRateLimit($rateLimit);
@@ -55,12 +50,10 @@ public function onKernelController(ControllerArgumentsEvent $event): void
5550
if (null !== $storedRateLimit && $storedRateLimit->getHits() >= $rateLimit->getLimit()) {
5651
$displayHeaders = $this->displayHeaders;
5752
$event->setController(
58-
static function () use ($displayHeaders, $storedRateLimit) {
59-
return new JsonResponse(
60-
$displayHeaders ? $storedRateLimit->getLimitReachedOutput() : Response::$statusTexts[Response::HTTP_TOO_MANY_REQUESTS],
61-
Response::HTTP_TOO_MANY_REQUESTS
62-
);
63-
}
53+
static fn () => new JsonResponse(
54+
$displayHeaders ? $storedRateLimit->getLimitReachedOutput() : Response::$statusTexts[Response::HTTP_TOO_MANY_REQUESTS],
55+
Response::HTTP_TOO_MANY_REQUESTS
56+
)
6457
);
6558
}
6659

src/EventListener/ReadGraphQLRateLmitAnnotationListener.php renamed to src/EventListener/ReadGraphQLRateLimitAnnotationListener.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,22 @@
1313
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1414
use Symfony\Component\HttpKernel\Event\ControllerEvent;
1515

16-
class ReadGraphQLRateLmitAnnotationListener implements EventSubscriberInterface
16+
class ReadGraphQLRateLimitAnnotationListener implements EventSubscriberInterface
1717
{
18-
private Reader $annotationReader;
1918
/** @var iterable<RateLimitModifierInterface> */
20-
private $rateLimitModifiers;
21-
private int $limit;
22-
private int $period;
23-
private ContainerInterface $container;
19+
private iterable $rateLimitModifiers;
2420

2521
/**
2622
* @param RateLimitModifierInterface[] $rateLimitModifiers
2723
*/
28-
public function __construct(ContainerInterface $container, Reader $annotationReader, iterable $rateLimitModifiers, int $limit, int $period)
24+
public function __construct(private ContainerInterface $container, private Reader $annotationReader, iterable $rateLimitModifiers, private int $limit, private int $period)
2925
{
3026
foreach ($rateLimitModifiers as $rateLimitModifier) {
3127
if (!($rateLimitModifier instanceof RateLimitModifierInterface)) {
3228
throw new \InvalidArgumentException('$rateLimitModifiers must be instance of '.RateLimitModifierInterface::class);
3329
}
3430
}
35-
36-
$this->annotationReader = $annotationReader;
3731
$this->rateLimitModifiers = $rateLimitModifiers;
38-
$this->limit = $limit;
39-
$this->period = $period;
40-
$this->container = $container;
4132
}
4233

4334
public function onKernelController(ControllerEvent $event): void
@@ -66,7 +57,7 @@ public function onKernelController(ControllerEvent $event): void
6657
return;
6758
}
6859

69-
if (!class_exists('GraphQL\Language\Parser')) {
60+
if (!class_exists(\GraphQL\Language\Parser::class)) {
7061
throw new \Exception('Run "composer require webonyx/graphql-php" to use @GraphQLRateLimit annotation.');
7162
}
7263

src/EventListener/ReadRateLimitAnnotationListener.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,20 @@
1414

1515
class ReadRateLimitAnnotationListener implements EventSubscriberInterface
1616
{
17-
private Reader $annotationReader;
1817
/** @var iterable<RateLimitModifierInterface> */
19-
private $rateLimitModifiers;
20-
private int $limit;
21-
private int $period;
22-
private bool $limitByRoute;
23-
private ContainerInterface $container;
18+
private iterable $rateLimitModifiers;
2419

2520
/**
2621
* @param RateLimitModifierInterface[] $rateLimitModifiers
2722
*/
28-
public function __construct(ContainerInterface $container, Reader $annotationReader, iterable $rateLimitModifiers, int $limit, int $period, bool $limitByRoute)
23+
public function __construct(private ContainerInterface $container, private Reader $annotationReader, iterable $rateLimitModifiers, private int $limit, private int $period, private bool $limitByRoute)
2924
{
3025
foreach ($rateLimitModifiers as $rateLimitModifier) {
3126
if (!($rateLimitModifier instanceof RateLimitModifierInterface)) {
3227
throw new \InvalidArgumentException('$rateLimitModifiers must be instance of '.RateLimitModifierInterface::class);
3328
}
3429
}
35-
36-
$this->annotationReader = $annotationReader;
3730
$this->rateLimitModifiers = $rateLimitModifiers;
38-
$this->limit = $limit;
39-
$this->period = $period;
40-
$this->limitByRoute = $limitByRoute;
41-
$this->container = $container;
4231
}
4332

4433
public function onKernelController(ControllerEvent $event): void

src/EventListener/ReadRateLimitConfigurationListener.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@
1212
class ReadRateLimitConfigurationListener implements EventSubscriberInterface
1313
{
1414
/** @var iterable<RateLimitModifierInterface> */
15-
private $rateLimitModifiers;
16-
private int $limit;
17-
private int $period;
18-
/** @var array<string, array<string, int>> */
19-
private array $routes;
15+
private iterable $rateLimitModifiers;
2016

2117
/**
2218
* @param RateLimitModifierInterface[] $rateLimitModifiers
2319
* @param array<string, array<string, int>> $routes
2420
*/
25-
public function __construct(iterable $rateLimitModifiers, int $limit, int $period, array $routes)
21+
public function __construct(iterable $rateLimitModifiers, private int $limit, private int $period, private array $routes)
2622
{
2723
foreach ($rateLimitModifiers as $rateLimitModifier) {
2824
if (!($rateLimitModifier instanceof RateLimitModifierInterface)) {
@@ -31,9 +27,6 @@ public function __construct(iterable $rateLimitModifiers, int $limit, int $perio
3127
}
3228

3329
$this->rateLimitModifiers = $rateLimitModifiers;
34-
$this->limit = $limit;
35-
$this->period = $period;
36-
$this->routes = $routes;
3730
}
3831

3932
public function onKernelController(ControllerEvent $event): void

src/Model/GraphQLEndpointConfiguration.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,8 @@
44

55
class GraphQLEndpointConfiguration
66
{
7-
private ?int $limit;
8-
9-
private ?int $period;
10-
11-
private string $endpoint;
12-
13-
public function __construct(?int $limit, ?int $period, string $endpoint)
7+
public function __construct(private ?int $limit, private ?int $period, private string $endpoint)
148
{
15-
$this->limit = $limit;
16-
$this->period = $period;
17-
$this->endpoint = $endpoint;
189
}
1910

2011
public function getEndpoint(): string

src/Model/RateLimit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function getDiscriminator(): string
4646
throw new \InvalidArgumentException('Cannot compute rate limit discriminator with an empty vary.');
4747
}
4848

49-
return (string) json_encode($this->vary);
49+
return (string) json_encode($this->vary, JSON_THROW_ON_ERROR);
5050
}
5151

5252
/**

0 commit comments

Comments
 (0)