Skip to content

Commit 057ad74

Browse files
authored
Merge pull request #139 from orlandothoeny/fix/str-replace-deprecation
fix(deprecation): str_replace(): Passing null to parameter #3 ($subject)
2 parents cfbcee2 + 72545ba commit 057ad74

File tree

4 files changed

+48
-50
lines changed

4 files changed

+48
-50
lines changed

DependencyInjection/Configuration.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function getConfigTreeBuilder(): TreeBuilder
2626
->canBeDisabled()
2727
->children()
2828
->enumNode('storage_engine')
29-
->values(array('redis','memcache','doctrine', 'php_redis', 'php_redis_cluster', 'simple_cache', 'cache'))
29+
->values(['redis','memcache','doctrine', 'php_redis', 'php_redis_cluster', 'simple_cache', 'cache'])
3030
->defaultValue('redis')
3131
->info('The storage engine where all the rates will be stored')
3232
->end()
@@ -109,19 +109,20 @@ public function getConfigTreeBuilder(): TreeBuilder
109109
->end()
110110
->end()
111111
->arrayNode('path_limits')
112-
->defaultValue(array())
112+
->defaultValue([])
113113
->info('Rate limits for paths')
114114
->prototype('array')
115115
->children()
116116
->scalarNode('path')
117117
->isRequired()
118+
->cannotBeEmpty()
118119
->end()
119120
->arrayNode('methods')
120121
->prototype('enum')
121-
->values(array('*', 'GET', 'POST', 'PUT', 'DELETE', 'PATCH'))
122+
->values(['*', 'GET', 'POST', 'PUT', 'DELETE', 'PATCH'])
122123
->end()
123124
->requiresAtLeastOneElement()
124-
->defaultValue(array('*'))
125+
->defaultValue(['*'])
125126
->end()
126127
->integerNode('limit')
127128
->isRequired()

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,23 @@
77
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
88
use Symfony\Component\Config\Definition\Processor;
99

10-
/**
11-
* ConfigurationTest
12-
*/
1310
class ConfigurationTest extends WebTestCase
1411
{
15-
/**
16-
* @var Processor
17-
*/
18-
private $processor;
12+
private Processor $processor;
1913

2014
public function setUp():void
2115
{
2216
$this->processor = new Processor();
2317
}
2418

25-
private function getConfigs(array $configArray)
19+
private function getConfigs(array $configArray): array
2620
{
2721
$configuration = new Configuration();
2822

2923
return $this->processor->processConfiguration($configuration, array($configArray));
3024
}
3125

32-
public function testUnconfiguredConfiguration()
26+
public function testUnconfiguredConfiguration(): void
3327
{
3428
$configuration = $this->getConfigs(array());
3529

@@ -59,15 +53,15 @@ public function testUnconfiguredConfiguration()
5953
), $configuration);
6054
}
6155

62-
public function testDisabledConfiguration()
56+
public function testDisabledConfiguration(): void
6357
{
6458
$configuration = $this->getConfigs(array('enabled' => false));
6559

6660
$this->assertArrayHasKey('enabled', $configuration);
6761
$this->assertFalse($configuration['enabled']);
6862
}
6963

70-
public function testPathLimitConfiguration()
64+
public function testPathLimitConfiguration(): void
7165
{
7266
$pathLimits = array(
7367
'api' => array(
@@ -86,7 +80,7 @@ public function testPathLimitConfiguration()
8680
$this->assertEquals($pathLimits, $configuration['path_limits']);
8781
}
8882

89-
public function testMultiplePathLimitConfiguration()
83+
public function testMultiplePathLimitConfiguration(): void
9084
{
9185
$pathLimits = array(
9286
'api' => array(
@@ -111,7 +105,7 @@ public function testMultiplePathLimitConfiguration()
111105
$this->assertEquals($pathLimits, $configuration['path_limits']);
112106
}
113107

114-
public function testDefaultPathLimitMethods()
108+
public function testDefaultPathLimitMethods(): void
115109
{
116110
$pathLimits = array(
117111
'api' => array(
@@ -137,28 +131,50 @@ public function testDefaultPathLimitMethods()
137131
$this->assertEquals($pathLimits, $configuration['path_limits']);
138132
}
139133

140-
public function testMustBeBasedOnExceptionClass()
134+
public function testMustBeBasedOnExceptionClass(): void
141135
{
142136
$this->expectException(InvalidConfigurationException::class);
143-
$configuration = $this->getConfigs(array('rate_response_exception' => '\StdClass'));
137+
$this->getConfigs(array('rate_response_exception' => '\StdClass'));
138+
}
139+
140+
/**
141+
* @testWith [""]
142+
* [null]
143+
*/
144+
public function testEmptyPathIsNotAllowed(mixed $path): void
145+
{
146+
$pathLimits = [
147+
'api' => [
148+
'path' => $path,
149+
'methods' => ['GET'],
150+
'limit' => 200,
151+
'period' => 10
152+
],
153+
];
154+
155+
$this->expectException(InvalidConfigurationException::class);
156+
157+
$this->getConfigs([
158+
'path_limits' => $pathLimits
159+
]);
144160
}
145161

146162
/**
147163
*
148164
*/
149-
public function testMustBeBasedOnExceptionClass2()
165+
public function testMustBeBasedOnExceptionClass2(): void
150166
{
151-
$configuration = $this->getConfigs(array('rate_response_exception' => '\InvalidArgumentException'));
167+
$this->getConfigs(array('rate_response_exception' => '\InvalidArgumentException'));
152168

153169
# no exception triggered is ok.
154-
$this->assertTrue(true);
170+
$this->expectNotToPerformAssertions();
155171
}
156172

157-
public function testMustBeBasedOnExceptionOrNull()
173+
public function testMustBeBasedOnExceptionOrNull(): void
158174
{
159-
$configuration = $this->getConfigs(array('rate_response_exception' => null));
175+
$this->getConfigs(array('rate_response_exception' => null));
160176

161177
# no exception triggered is ok.
162-
$this->assertTrue(true);
178+
$this->expectNotToPerformAssertions();
163179
}
164180
}

Util/PathLimitProcessor.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@
88

99
class PathLimitProcessor
1010
{
11-
private array $pathLimits;
12-
13-
public function __construct(array $pathLimits)
11+
/**
12+
* @param array<array{path: string, methods: array<string>, limit: int<-1, max>, period: positive-int}> $pathLimits
13+
*/
14+
public function __construct(private array $pathLimits)
1415
{
15-
$this->pathLimits = $pathLimits;
16-
1716
// Clean up any extra slashes from the config
1817
foreach ($this->pathLimits as &$pathLimit) {
1918
$pathLimit['path'] = trim($pathLimit['path'], '/');
2019
}
2120

2221
// Order the configs so that the most specific paths
2322
// are matched first
24-
usort($this->pathLimits, static function($a, $b) {
23+
usort($this->pathLimits, static function($a, $b): int {
2524
return substr_count($b['path'], '/') - substr_count($a['path'], '/');
2625
});
2726
}
@@ -44,7 +43,7 @@ public function getRateLimit(Request $request): ?RateLimit
4443
return null;
4544
}
4645

47-
public function getMatchedPath(Request $request)
46+
public function getMatchedPath(Request $request): string
4847
{
4948
$path = trim($request->getPathInfo(), '/');
5049
$method = $request->getMethod();

phpstan-baseline.neon

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -384,18 +384,6 @@ parameters:
384384
count: 1
385385
path: Service/Storage/StorageInterface.php
386386

387-
-
388-
message: '#^Method Noxlogic\\RateLimitBundle\\Util\\PathLimitProcessor\:\:__construct\(\) has parameter \$pathLimits with no value type specified in iterable type array\.$#'
389-
identifier: missingType.iterableValue
390-
count: 1
391-
path: Util/PathLimitProcessor.php
392-
393-
-
394-
message: '#^Method Noxlogic\\RateLimitBundle\\Util\\PathLimitProcessor\:\:getMatchedPath\(\) has no return type specified\.$#'
395-
identifier: missingType.return
396-
count: 1
397-
path: Util/PathLimitProcessor.php
398-
399387
-
400388
message: '#^Method Noxlogic\\RateLimitBundle\\Util\\PathLimitProcessor\:\:methodMatched\(\) has parameter \$expectedMethods with no value type specified in iterable type array\.$#'
401389
identifier: missingType.iterableValue
@@ -437,9 +425,3 @@ parameters:
437425
identifier: missingType.parameter
438426
count: 1
439427
path: Util/PathLimitProcessor.php
440-
441-
-
442-
message: '#^Property Noxlogic\\RateLimitBundle\\Util\\PathLimitProcessor\:\:\$pathLimits type has no value type specified in iterable type array\.$#'
443-
identifier: missingType.iterableValue
444-
count: 1
445-
path: Util/PathLimitProcessor.php

0 commit comments

Comments
 (0)