Skip to content

Commit

Permalink
Fix psr violations being kept for paths that are later scanned correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed May 31, 2024
1 parent d90e2bc commit 6307dcf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
25 changes: 21 additions & 4 deletions src/ClassMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ClassMap implements \Countable
private $ambiguousClasses = [];

/**
* @var string[]
* @var array<string, array<array{warning: string, className: string}>>
*/
private $psrViolations = [];

Expand All @@ -55,7 +55,9 @@ public function getMap(): array
*/
public function getPsrViolations(): array
{
return $this->psrViolations;
return array_map(static function (array $violation): string {
return $violation['warning'];
}, array_merge(...array_values($this->psrViolations)));
}

/**
Expand Down Expand Up @@ -86,6 +88,8 @@ public function sort(): void
*/
public function addClass(string $className, string $path): void
{
unset($this->psrViolations[strtr('\\', '/', $path)]);

$this->map[$className] = $path;
}

Expand All @@ -110,9 +114,22 @@ public function hasClass(string $className): bool
return isset($this->map[$className]);
}

public function addPsrViolation(string $warning): void
public function addPsrViolation(string $warning, string $className, string $path): void
{
$this->psrViolations[] = $warning;
$path = rtrim(strtr('\\', '/', $path), '/');

$this->psrViolations[$path][] = ['warning' => $warning, 'className' => $className];
}

public function clearViolationsByPath(string $pathPrefix): void
{
$pathPrefix = rtrim(strtr('\\', '/', $pathPrefix), '/');

foreach ($this->psrViolations as $path => $violations) {
if ($path === $pathPrefix || 0 === \strpos($path, $pathPrefix.'/')) {
unset($this->psrViolations[$path]);
}
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ClassMapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private function filterByNamespace(array $classes, string $filePath, string $bas
foreach ($rejectedClasses as $class) {
$shortPath = Preg::replace('{^'.preg_quote(self::getCwd()).'}', '.', $filePath, 1);
$shortBasePath = Preg::replace('{^'.preg_quote(self::getCwd()).'}', '.', $basePath, 1);
$this->classMap->addPsrViolation("Class $class located in $shortPath does not comply with $namespaceType autoloading standard (rule: $baseNamespace => $shortBasePath). Skipping.");
$this->classMap->addPsrViolation("Class $class located in $shortPath does not comply with $namespaceType autoloading standard (rule: $baseNamespace => $shortBasePath). Skipping.", $class, $filePath);
}

return [];
Expand Down

0 comments on commit 6307dcf

Please sign in to comment.