Skip to content

Commit 4dfad8d

Browse files
committed
phpstan level 9
1 parent 8190171 commit 4dfad8d

File tree

4 files changed

+43
-38
lines changed

4 files changed

+43
-38
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"test:infection": "vendor/bin/infection --threads=$(nproc) --min-msi=100 --verbose --coverage=build/phpunit",
6868
"test:integration": "vendor/bin/phpunit --testsuite=Integration --cache-directory=build/phpunit",
6969
"test:lint": "mkdir -p build && find src tests -name '*.php' -print0 | xargs -0 -n1 -P$(nproc) php -l | tee build/phplint.log",
70-
"test:static-analysis": "mkdir -p build && bash -c 'vendor/bin/phpstan analyse src --no-progress --level=8 --error-format=junit | tee build/phpstan.junit.xml; if [ ${PIPESTATUS[0]} -ne \"0\" ]; then exit 1; fi'",
70+
"test:static-analysis": "mkdir -p build && bash -c 'vendor/bin/phpstan analyse src --no-progress --level=9 --error-format=junit | tee build/phpstan.junit.xml; if [ ${PIPESTATUS[0]} -ne \"0\" ]; then exit 1; fi'",
7171
"test:unit": "vendor/bin/phpunit --testsuite=Unit --coverage-text --coverage-clover=build/phpunit/clover.xml --coverage-html=build/phpunit/coverage-html --coverage-xml=build/phpunit/coverage-xml --log-junit=build/phpunit/junit.xml --cache-directory=build/phpunit"
7272
}
7373
}

phpstan.neon

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
11
parameters:
2-
ignoreErrors:
3-
-
4-
message: '/no value type specified in iterable type array/'
5-
path: %currentWorkingDirectory%/src/*.php
6-
-
7-
message: '#Variable \$routeIndex might not be defined.#'
8-
path: %currentWorkingDirectory%/src/UrlGenerator.php

src/RouteMatcher.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,29 @@ public function match(ServerRequestInterface $request): RouteInterface
4646
}
4747

4848
if (Dispatcher::METHOD_NOT_ALLOWED === $routeInfo[0]) {
49+
/** @var array<string> */
50+
$allowedMethods = $routeInfo[1];
51+
4952
throw HttpException::createMethodNotAllowed([
5053
'detail' => \sprintf(
5154
'Method "%s" at path "%s" is not allowed. Must be one of: "%s"',
5255
$method,
5356
$path,
54-
implode('", "', $routeInfo[1]),
57+
implode('", "', $allowedMethods),
5558
),
5659
]);
5760
}
5861

62+
/** @var string $routeName */
63+
$routeName = $routeInfo[1];
64+
5965
/** @var RouteInterface $route */
60-
$route = $this->routesByName[$routeInfo[1]];
66+
$route = $this->routesByName[$routeName];
67+
68+
/** @var array<string, string> */
69+
$attributes = $routeInfo[2];
6170

62-
return $route->withAttributes($routeInfo[2]);
71+
return $route->withAttributes($attributes);
6372
}
6473

6574
private function getDispatcher(?string $cacheFile = null): Dispatcher

src/UrlGenerator.php

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ public function generatePath(string $name, array $attributes = [], array $queryP
5353
$routePath = $route->getPath();
5454

5555
$routePartSets = array_reverse($this->routeParser->parse($routePath));
56+
$routeParts = $this->findMatchingRouteParts($routePartSets, $attributes);
5657

57-
$routeIndex = $this->getRouteIndex($routePartSets, $attributes);
58-
59-
$path = $this->generatePathFromAttributes($name, $routePath, $routePartSets, $attributes, $routeIndex);
58+
$path = $this->buildPath($name, $routePath, $routeParts, $attributes);
6059

6160
if ([] === $queryParams) {
6261
return $this->basePath.$path;
@@ -75,42 +74,46 @@ private function getRoute(string $name): RouteInterface
7574
}
7675

7776
/**
78-
* @param array<int, array<int, array|string>> $routePartSets
79-
* @param array<string> $attributes
77+
* @param array<int, array<int, array<int, string>|string>> $routePartSets
78+
* @param array<string> $attributes
79+
*
80+
* @return array<int, array<int, string>|string>
8081
*/
81-
private function getRouteIndex(array $routePartSets, array $attributes): int
82+
private function findMatchingRouteParts(array $routePartSets, array $attributes): array
8283
{
83-
foreach ($routePartSets as $routeIndex => $routeParts) {
84-
foreach ($routeParts as $routePart) {
85-
if (\is_array($routePart)) {
86-
$parameter = $routePart[0];
87-
88-
if (!isset($attributes[$parameter])) {
89-
continue 2;
90-
}
91-
}
84+
foreach ($routePartSets as $routeParts) {
85+
if ($this->hasAllRequiredAttributes($routeParts, $attributes)) {
86+
return $routeParts;
9287
}
88+
}
89+
90+
return end($routePartSets) ?: [];
91+
}
9392

94-
return $routeIndex;
93+
/**
94+
* @param array<int, array<int, string>|string> $routeParts
95+
* @param array<string> $attributes
96+
*/
97+
private function hasAllRequiredAttributes(array $routeParts, array $attributes): bool
98+
{
99+
foreach ($routeParts as $routePart) {
100+
if (\is_array($routePart) && !isset($attributes[$routePart[0]])) {
101+
return false;
102+
}
95103
}
96104

97-
return $routeIndex;
105+
return true;
98106
}
99107

100108
/**
101-
* @param array<int, array<int, array|string>> $routePartSets
102-
* @param array<string> $attributes
109+
* @param array<int, array<int, string>|string> $routeParts
110+
* @param array<string> $attributes
103111
*/
104-
private function generatePathFromAttributes(
105-
string $name,
106-
string $path,
107-
array $routePartSets,
108-
array $attributes,
109-
int $routeIndex
110-
): string {
112+
private function buildPath(string $name, string $path, array $routeParts, array $attributes): string
113+
{
111114
$pathParts = [];
112115

113-
foreach ($routePartSets[$routeIndex] as $routePart) {
116+
foreach ($routeParts as $routePart) {
114117
$pathParts[] = \is_array($routePart)
115118
? $this->getAttributeValue($name, $path, $routePart, $attributes)
116119
: $routePart;

0 commit comments

Comments
 (0)