Skip to content

Commit

Permalink
Fix cs errors. Use PHP 7.2 as phpstan reference language version to a…
Browse files Browse the repository at this point in the history
…void false positive errors
  • Loading branch information
scaytrase committed Mar 23, 2023
1 parent d4f2ba2 commit 367e7c0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
parameters:
phpVersion: 70200
level: 6
paths:
- src
Expand Down
16 changes: 11 additions & 5 deletions src/PSR7/OperationAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
use League\OpenAPIValidation\PSR7\Exception\Validation\InvalidPath;
use League\OpenAPIValidation\Schema\Exception\InvalidSchema;

use function explode;
use function implode;
use function preg_match;
use function preg_match_all;
use function preg_quote;
use function preg_replace;
use function preg_split;
use function sprintf;
use function strtok;
use function trim;

use const PREG_SPLIT_DELIM_CAPTURE;

Expand Down Expand Up @@ -73,13 +76,16 @@ public function countPlaceholders(): int
public function countExactMatchParts(string $comparisonPath): int
{
$comparisonPathParts = explode('/', trim($comparisonPath, '/'));
$pathParts = explode('/', trim($this->path(), '/'));
$exactMatchCount = 0;
foreach($comparisonPathParts as $key => $comparisonPathPart) {
if ($comparisonPathPart === $pathParts[$key]) {
$exactMatchCount++;
$pathParts = explode('/', trim($this->path(), '/'));
$exactMatchCount = 0;
foreach ($comparisonPathParts as $key => $comparisonPathPart) {
if ($comparisonPathPart !== $pathParts[$key]) {
continue;
}

$exactMatchCount++;
}

return $exactMatchCount;
}

Expand Down
12 changes: 9 additions & 3 deletions src/PSR7/PathFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
use cebe\openapi\spec\PathItem;
use cebe\openapi\spec\Server;

use function array_keys;
use function array_unique;
use function count;
use function ltrim;
use function max;
use function parse_url;
use function preg_match;
use function preg_match_all;
use function preg_replace;
use function rtrim;
use function sprintf;
use function strtolower;
use function trim;
use function usort;

use const PHP_URL_PATH;
Expand Down Expand Up @@ -222,12 +227,13 @@ private function attemptNarrowDown(array $paths): array
return $paths;
}

$partCounts = [];
$partCounts = [];
$placeholderCounts = [];
foreach ($paths as $path) {
$partCounts[] = $this->countParts($path->path());
$partCounts[] = $this->countParts($path->path());
$placeholderCounts[] = $path->countPlaceholders();
}

$partCounts[] = $this->countParts($this->path);
if (count(array_unique($partCounts)) === 1 && count(array_unique($placeholderCounts)) > 1) {
// All paths have the same number of parts but there are differing placeholder counts. We can narrow down!
Expand All @@ -248,7 +254,7 @@ private function filterToHighestExactMatchingParts(array $paths): array
{
$scoredCandidates = [];
foreach ($paths as $candidate) {
$score = $candidate->countExactMatchParts($this->path);
$score = $candidate->countExactMatchParts($this->path);
$scoredCandidates[$score][] = $candidate;
}

Expand Down
1 change: 0 additions & 1 deletion tests/PSR7/PathFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,4 @@ public function testItPrioritises2EquallyDynamicPaths(): void
$this->assertEquals('/products/{product}/images/thumbnails/{size}', $opAddrs[0]->path());
$this->assertEquals('/products/{product}/images/{image}/primary', $opAddrs[1]->path());
}

}

0 comments on commit 367e7c0

Please sign in to comment.