Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed May 3, 2024
1 parent 6cb3070 commit 0d856c0
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/Composer/Autoload/AutoloadGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
*/
class AutoloadGenerator
{
private const EXCLUDE_FROM_CLASSMAP_SUFFIX_PATTERN = '($|/)';

/**
* @var EventDispatcher
*/
Expand Down Expand Up @@ -325,7 +327,8 @@ public static function autoload(\$class)
}

foreach ($autoloads['classmap'] as $dir) {
$classMapGenerator->scanPaths($dir, $this->buildExclusionRegex($dir, $excluded));
$excludeRules = $this->buildExclusionRegex($dir, $excluded);
$classMapGenerator->scanPaths($dir, $excludeRules['pattern'], 'classmap', null, $excludeRules['paths']);

Check failure on line 331 in src/Composer/Autoload/AutoloadGenerator.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, false)

Method Composer\ClassMapGenerator\ClassMapGenerator::scanPaths() invoked with 5 parameters, 1-4 required.

Check failure on line 331 in src/Composer/Autoload/AutoloadGenerator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, true)

Method Composer\ClassMapGenerator\ClassMapGenerator::scanPaths() invoked with 5 parameters, 1-4 required.
}

if ($scanPsrPackages) {
Expand All @@ -348,7 +351,8 @@ public static function autoload(\$class)
continue;
}

$classMapGenerator->scanPaths($dir, $this->buildExclusionRegex($dir, $excluded), $group['type'], $namespace);
$excludeRules = $this->buildExclusionRegex($dir, $excluded);
$classMapGenerator->scanPaths($dir, $excludeRules['pattern'], $group['type'], $namespace, $excludeRules['paths']);

Check failure on line 355 in src/Composer/Autoload/AutoloadGenerator.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, false)

Method Composer\ClassMapGenerator\ClassMapGenerator::scanPaths() invoked with 5 parameters, 1-4 required.

Check failure on line 355 in src/Composer/Autoload/AutoloadGenerator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, true)

Method Composer\ClassMapGenerator\ClassMapGenerator::scanPaths() invoked with 5 parameters, 1-4 required.
}
}
}
Expand Down Expand Up @@ -463,12 +467,14 @@ public static function autoload(\$class)
* @param array<string>|null $excluded
* @return non-empty-string|null
*/
private function buildExclusionRegex(string $dir, ?array $excluded): ?string
private function buildExclusionRegex(string $dir, ?array $excluded): array

Check failure on line 470 in src/Composer/Autoload/AutoloadGenerator.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, false)

Method Composer\Autoload\AutoloadGenerator::buildExclusionRegex() return type has no value type specified in iterable type array.

Check failure on line 470 in src/Composer/Autoload/AutoloadGenerator.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, false)

PHPDoc tag @return with type string|null is incompatible with native type array.

Check failure on line 470 in src/Composer/Autoload/AutoloadGenerator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, true)

Method Composer\Autoload\AutoloadGenerator::buildExclusionRegex() return type has no value type specified in iterable type array.

Check failure on line 470 in src/Composer/Autoload/AutoloadGenerator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, true)

PHPDoc tag @return with type string|null is incompatible with native type array.
{
if (null === $excluded) {
return null;
return ['pattern' => null, 'paths' => []];
}

$excludedPaths = [];

// filter excluded patterns here to only use those matching $dir
// exclude-from-classmap patterns are all realpath'd so we can only filter them if $dir exists so that realpath($dir) will work
// if $dir does not exist, it should anyway not find anything there so no trouble
Expand All @@ -478,14 +484,22 @@ private function buildExclusionRegex(string $dir, ?array $excluded): ?string
foreach ($excluded as $index => $pattern) {
// extract the constant string prefix of the pattern here, until we reach a non-escaped regex special character
$pattern = Preg::replace('{^(([^.+*?\[^\]$(){}=!<>|:\\\\#-]+|\\\\[.+*?\[^\]$(){}=!<>|:#-])*).*}', '$1', $pattern);

// if the pattern is not a subset or superset of $dir, it is unrelated and we skip it
if (0 !== strpos($pattern, $dirMatch) && 0 !== strpos($dirMatch, $pattern)) {
if (!Preg::isMatch('{^'.preg_quote($pattern).'($|/)}', $dirMatch) && !Preg::isMatch('{^'.preg_quote($dirMatch).'($|/)}', $pattern)) {
unset($excluded[$index]);
continue;
}

if ($pattern.self::EXCLUDE_FROM_CLASSMAP_SUFFIX_PATTERN === $excluded[$index]) {
$excludedPaths[] = substr($pattern, strlen($dirMatch) + 1);

// TODO maybe this shuold have a /prefix to make sure it does not exclude "Tests" for example from any sub-path
}
}
}

return \count($excluded) > 0 ? '{(' . implode('|', $excluded) . ')}' : null;
return ['pattern' => \count($excluded) > 0 ? '{^(' . implode('|', $excluded) . ')}' : null, 'paths' => $excludedPaths];
}

/**
Expand Down Expand Up @@ -612,7 +626,8 @@ public function createLoader(array $autoloads, ?string $vendorDir = null)

foreach ($autoloads['classmap'] as $dir) {
try {
$classMapGenerator->scanPaths($dir, $this->buildExclusionRegex($dir, $excluded));
$excludeRules = $this->buildExclusionRegex($dir, $excluded);
$classMapGenerator->scanPaths($dir, $excludeRules['pattern'], 'classmap', null, $excludeRules['paths']);

Check failure on line 630 in src/Composer/Autoload/AutoloadGenerator.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, false)

Method Composer\ClassMapGenerator\ClassMapGenerator::scanPaths() invoked with 5 parameters, 1-4 required.

Check failure on line 630 in src/Composer/Autoload/AutoloadGenerator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, true)

Method Composer\ClassMapGenerator\ClassMapGenerator::scanPaths() invoked with 5 parameters, 1-4 required.
} catch (\RuntimeException $e) {
$this->io->writeError('<warning>'.$e->getMessage().'</warning>');
}
Expand Down Expand Up @@ -1272,7 +1287,7 @@ static function ($matches) use (&$updir): string {
if (false === $resolvedPath) {
continue;
}
$autoloads[] = preg_quote(strtr($resolvedPath, '\\', '/')) . '/' . $path . '($|/)';
$autoloads[] = preg_quote(strtr($resolvedPath, '\\', '/')) . '/' . $path . self::EXCLUDE_FROM_CLASSMAP_SUFFIX_PATTERN;
continue;
}

Expand Down

0 comments on commit 0d856c0

Please sign in to comment.