Skip to content

Commit

Permalink
Merge branch 'main' into add-missing-strict-psr-violations
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek authored May 3, 2024
2 parents e28e78c + a5758da commit 920a6a9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/ClassMapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ public function getClassMap(): ClassMap
* @param non-empty-string|null $excluded Regex that matches file paths to be excluded from the classmap
* @param 'classmap'|'psr-0'|'psr-4' $autoloadType Optional autoload standard to use mapping rules with the namespace instead of purely doing a classmap
* @param string|null $namespace Optional namespace prefix to filter by, only for psr-0/psr-4 autoloading
* @param array<string> $excludedDirs Optional dirs to exclude from search relative to $path
*
* @throws \RuntimeException When the path is neither an existing file nor directory
*/
public function scanPaths($path, ?string $excluded = null, string $autoloadType = 'classmap', ?string $namespace = null): void
public function scanPaths($path, ?string $excluded = null, string $autoloadType = 'classmap', ?string $namespace = null, array $excludedDirs = []): void
{
if (!in_array($autoloadType, ['psr-0', 'psr-4', 'classmap'], true)) {
throw new \InvalidArgumentException('$autoloadType must be one of: "psr-0", "psr-4" or "classmap"');
Expand All @@ -124,7 +125,8 @@ public function scanPaths($path, ?string $excluded = null, string $autoloadType
->files()
->followLinks()
->name('/\.(?:'.implode('|', array_map('preg_quote', $this->extensions)).')$/')
->in($path);
->in($path)
->exclude($excludedDirs);
} else {
throw new \RuntimeException(
'Could not scan for classes inside "'.$path.'" which does not appear to be a file nor a folder'
Expand Down
5 changes: 4 additions & 1 deletion src/PhpFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ private static function getExtraTypes(): string
$extraTypes .= '|enum';
}

PhpFileCleaner::setTypeConfig(array_merge(['class', 'interface', 'trait'], array_filter(explode('|', $extraTypes))));
$extraTypesArray = array_filter(explode('|', $extraTypes), function (string $type) {
return $type !== '';
});
PhpFileCleaner::setTypeConfig(array_merge(['class', 'interface', 'trait'], $extraTypesArray));
}

return $extraTypes;
Expand Down
14 changes: 13 additions & 1 deletion tests/ClassMapGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public function testGetPSR4Violations(): void
{
$this->generator->scanPaths(__DIR__ . '/Fixtures/psrViolations', null, 'psr-4', 'ExpectedNamespace\\');
$classMap = $this->generator->getClassMap();
static::assertSame(
self::assertSame(
[
'Class ClassWithoutNameSpace located in ./tests/Fixtures/psrViolations/ClassWithoutNameSpace.php does not comply with psr-4 autoloading standard. Skipping.',
'Class ExpectedNamespace\UnexpectedSubNamespace\ClassWithIncorrectSubNamespace located in ./tests/Fixtures/psrViolations/ClassWithIncorrectSubNamespace.php does not comply with psr-4 autoloading standard. Skipping.',
Expand All @@ -254,6 +254,18 @@ public function testGetPSR4Violations(): void
);
}

public function testCreateMapWithDirectoryExcluded(): void
{
$expected = array(
'PrefixCollision_A_B_Bar' => realpath(__DIR__) . '/Fixtures/beta/PrefixCollision/A/B/Bar.php',
'PrefixCollision_A_B_Foo' => realpath(__DIR__) . '/Fixtures/beta/PrefixCollision/A/B/Foo.php',
);

$this->generator->scanPaths(realpath(__DIR__) . '/Fixtures/beta', null, 'classmap', null, ['NamespaceCollision']);
$result = $this->generator->getClassMap();
self::assertEqualsNormalized($expected, $result->getMap());
}

/**
* @param array<string, string> $expected
* @param array<class-string, string> $actual
Expand Down

0 comments on commit 920a6a9

Please sign in to comment.