Skip to content

Commit 5292524

Browse files
committed
refactor: add types, misc minor refactoring/cleanup
1 parent 057ad74 commit 5292524

21 files changed

+193
-275
lines changed

Attribute/RateLimit.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22

33
namespace Noxlogic\RateLimitBundle\Attribute;
44

5+
use Symfony\Component\HttpFoundation\Request;
6+
57
#[\Attribute(\Attribute::IS_REPEATABLE |\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
68
final class RateLimit
79
{
8-
/**
9-
* @var array HTTP Methods protected by this attribute. Defaults to all method
10-
*/
11-
public array $methods = [];
12-
1310
public function __construct(
14-
$methods = [],
11+
/**
12+
* @var array<Request::METHOD_*> $methods HTTP Methods protected by this attribute. Defaults to all methods
13+
* Passing strings is allowed for backward-compatibility but deprecated. Pass an array instead
14+
*/
15+
public array|string $methods = [],
1516

1617
/**
17-
* @var int Number of calls per period
18+
* @var int<-1, max> Number of calls per period
1819
*/
1920
public int $limit = -1,
2021

2122
/**
22-
* @var int Number of seconds of the time period in which the calls can be made
23+
* @var positive-int Number of seconds of the time period in which the calls can be made
2324
*/
2425
public int $period = 3600,
2526

@@ -31,36 +32,52 @@ public function __construct(
3132
// @RateLimit annotation used to support single method passed as string, keep that for retrocompatibility
3233
if (!is_array($methods)) {
3334
$this->methods = [$methods];
34-
} else {
35-
$this->methods = $methods;
3635
}
3736
}
3837

38+
/**
39+
* @return int<-1, max>
40+
*/
3941
public function getLimit(): int
4042
{
4143
return $this->limit;
4244
}
4345

46+
/**
47+
* @param int<-1, max> $limit
48+
*/
4449
public function setLimit(int $limit): void
4550
{
4651
$this->limit = $limit;
4752
}
4853

54+
/**
55+
* @return array<Request::METHOD_*>
56+
*/
4957
public function getMethods(): array
5058
{
5159
return $this->methods;
5260
}
5361

54-
public function setMethods($methods): void
62+
/**
63+
* @param array<Request::METHOD_*> $methods Passing strings is allowed for backward-compatibility but deprecated. Pass an array instead
64+
*/
65+
public function setMethods(array|string $methods): void
5566
{
5667
$this->methods = (array) $methods;
5768
}
5869

70+
/**
71+
* @return positive-int
72+
*/
5973
public function getPeriod(): int
6074
{
6175
return $this->period;
6276
}
6377

78+
/**
79+
* @param positive-int $period
80+
*/
6481
public function setPeriod(int $period): void
6582
{
6683
$this->period = $period;

DependencyInjection/Configuration.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace Noxlogic\RateLimitBundle\DependencyInjection;
44

5+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
56
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
67
use Symfony\Component\Config\Definition\ConfigurationInterface;
78
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
89

910
/**
1011
* This is the class that validates and merges configuration from your app/config files
11-
*
1212
*/
1313
class Configuration implements ConfigurationInterface
1414
{
@@ -22,7 +22,11 @@ public function getConfigTreeBuilder(): TreeBuilder
2222
$treeBuilder = new TreeBuilder('noxlogic_rate_limit');
2323
$rootNode = $treeBuilder->getRootNode();
2424

25-
$rootNode // @phpstan-ignore method.notFound
25+
if (!$rootNode instanceof ArrayNodeDefinition) {
26+
throw new \InvalidArgumentException('The "noxlogic_rate_limit" config root node must be an array');
27+
}
28+
29+
$rootNode
2630
->canBeDisabled()
2731
->children()
2832
->enumNode('storage_engine')

EventListener/BaseListener.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,13 @@ abstract class BaseListener
1010
*/
1111
protected $parameters;
1212

13-
/**
14-
* @param $name
15-
* @param $value
16-
*/
17-
public function setParameter($name, $value)
13+
public function setParameter(string $name, mixed $value): void
1814
{
1915
$this->parameters[$name] = $value;
2016
}
2117

22-
/**
23-
* @param $name
24-
* @param mixed $default
25-
* @return mixed
26-
*/
27-
public function getParameter($name, $default = null)
18+
public function getParameter(string $name, mixed $default = null): mixed
2819
{
29-
return isset($this->parameters[$name]) ? $this->parameters[$name] : $default;
20+
return $this->parameters[$name] ?? $default;
3021
}
3122
}

EventListener/HeaderModificationListener.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,12 @@
77

88
class HeaderModificationListener extends BaseListener
99
{
10-
11-
/**
12-
* @param array $defaultParameters
13-
*/
14-
public function __construct($defaultParameters = array())
10+
public function __construct(array $defaultParameters = [])
1511
{
1612
$this->parameters = $defaultParameters;
1713
}
1814

19-
/**
20-
* @param ResponseEvent $event
21-
*/
22-
public function onKernelResponse($event)
15+
public function onKernelResponse(ResponseEvent $event): void
2316
{
2417
$request = $event->getRequest();
2518

EventListener/RateLimitAnnotationListener.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,17 @@
1717

1818
class RateLimitAnnotationListener extends BaseListener
1919
{
20-
protected EventDispatcherInterface $eventDispatcher;
21-
22-
protected RateLimitService $rateLimitService;
23-
24-
protected PathLimitProcessor $pathLimitProcessor;
25-
2620
public function __construct(
27-
EventDispatcherInterface $eventDispatcher,
28-
RateLimitService $rateLimitService,
29-
PathLimitProcessor $pathLimitProcessor
21+
protected EventDispatcherInterface $eventDispatcher,
22+
protected RateLimitService $rateLimitService,
23+
protected PathLimitProcessor $pathLimitProcessor
3024
) {
31-
$this->eventDispatcher = $eventDispatcher;
32-
$this->rateLimitService = $rateLimitService;
33-
$this->pathLimitProcessor = $pathLimitProcessor;
3425
}
3526

3627
public function onKernelController(ControllerEvent $event): void
3728
{
3829
// Skip if the bundle isn't enabled (for instance in test environment)
39-
if( ! $this->getParameter('enabled', true)) {
30+
if(! $this->getParameter('enabled', true)) {
4031
return;
4132
}
4233

@@ -120,10 +111,8 @@ public function onKernelController(ControllerEvent $event): void
120111
});
121112
$event->stopPropagation();
122113
}
123-
124114
}
125115

126-
127116
/**
128117
* @param RateLimit[] $rateLimits
129118
*/
@@ -134,19 +123,19 @@ protected function findBestMethodMatch(Request $request, array $rateLimits): ?Ra
134123
return $this->pathLimitProcessor->getRateLimit($request);
135124
}
136125

137-
$best_match = null;
126+
$bestMatch = null;
138127
foreach ($rateLimits as $rateLimit) {
139128
if (in_array($request->getMethod(), $rateLimit->getMethods(), true)) {
140-
$best_match = $rateLimit;
129+
$bestMatch = $rateLimit;
141130
}
142131

143132
// Only match "default" annotation when we don't have a best match
144-
if ($best_match === null && count($rateLimit->methods) === 0) {
145-
$best_match = $rateLimit;
133+
if ($bestMatch === null && count($rateLimit->methods) === 0) {
134+
$bestMatch = $rateLimit;
146135
}
147136
}
148137

149-
return $best_match;
138+
return $bestMatch;
150139
}
151140

152141
/** @param RateLimit[] $rateLimits */

Service/Storage/DoctrineCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function resetRate($key)
6262
return true;
6363
}
6464

65-
private function createRateInfo(array $info)
65+
private function createRateInfo(array $info): RateLimitInfo
6666
{
6767
$rateLimitInfo = new RateLimitInfo();
6868
$rateLimitInfo->setLimit($info['limit']);

Service/Storage/Memcache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function resetRate($key)
6464
return true;
6565
}
6666

67-
private function createRateInfo(array $info)
67+
private function createRateInfo(array $info): RateLimitInfo
6868
{
6969
$rateLimitInfo = new RateLimitInfo();
7070
$rateLimitInfo->setLimit($info['limit']);

Service/Storage/PhpRedisCluster.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace Noxlogic\RateLimitBundle\Service\Storage;
54

6-
75
class PhpRedisCluster extends PhpRedis
86
{
97

Service/Storage/PsrCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function resetRate($key)
6969
return true;
7070
}
7171

72-
private function createRateInfo(array $info)
72+
private function createRateInfo(array $info): RateLimitInfo
7373
{
7474
$rateLimitInfo = new RateLimitInfo();
7575
$rateLimitInfo->setLimit($info['limit']);

Service/Storage/SimpleCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function resetRate($key)
6161
return true;
6262
}
6363

64-
private function createRateInfo(array $info)
64+
private function createRateInfo(array $info): RateLimitInfo
6565
{
6666
$rateLimitInfo = new RateLimitInfo();
6767
$rateLimitInfo->setLimit($info['limit']);

0 commit comments

Comments
 (0)