|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\DependencyInjection\AttributeServices; |
| 4 | + |
| 5 | +use PHPStan\File\CouldNotReadFileException; |
| 6 | +use PHPStan\File\FileHelper; |
| 7 | +use RecursiveDirectoryIterator; |
| 8 | +use RecursiveIteratorIterator; |
| 9 | +use SplFileInfo; |
| 10 | +use function count; |
| 11 | +use function hash_file; |
| 12 | +use function intdiv; |
| 13 | +use function is_array; |
| 14 | +use function is_dir; |
| 15 | +use function is_string; |
| 16 | +use function ksort; |
| 17 | +use function rtrim; |
| 18 | +use function sort; |
| 19 | +use function sprintf; |
| 20 | +use function str_contains; |
| 21 | +use function str_ends_with; |
| 22 | +use function str_starts_with; |
| 23 | +use function strlen; |
| 24 | +use function usort; |
| 25 | +use const PHP_VERSION_ID; |
| 26 | + |
| 27 | +/** |
| 28 | + * Validates the merged `attributeServicesDirectories` section against the analysed project's |
| 29 | + * Composer metadata and computes each directory's contribution to the container cache key. |
| 30 | + * |
| 31 | + * Runs on every PHPStan run, before the container compiles - the result decides whether |
| 32 | + * a cached container can be reused, so it cannot sit behind the container cache itself. |
| 33 | + * Directories owned by a regularly installed vendor package contribute their package's |
| 34 | + * version identity (no filesystem walk); directories of the project itself, and packages |
| 35 | + * installed from path repositories, contribute per-file content hashes. |
| 36 | + */ |
| 37 | +final class AttributeServicesDirectoriesResolver |
| 38 | +{ |
| 39 | + |
| 40 | + private ComposerProjectFactory $composerProjectFactory; |
| 41 | + |
| 42 | + /** @var list<ComposerProject>|null */ |
| 43 | + private ?array $composerProjects = null; |
| 44 | + |
| 45 | + /** |
| 46 | + * @param string[] $composerAutoloaderProjectPaths |
| 47 | + */ |
| 48 | + public function __construct( |
| 49 | + private FileHelper $fileHelper, |
| 50 | + private array $composerAutoloaderProjectPaths, |
| 51 | + private int $runtimePhpVersionId = PHP_VERSION_ID, |
| 52 | + ) |
| 53 | + { |
| 54 | + $this->composerProjectFactory = new ComposerProjectFactory($fileHelper); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param mixed $rawSectionValue |
| 59 | + * @throws InvalidAttributeServicesDirectoriesException |
| 60 | + */ |
| 61 | + public function resolve($rawSectionValue): ResolvedAttributeServicesDirectories |
| 62 | + { |
| 63 | + if ($rawSectionValue === null || $rawSectionValue === []) { |
| 64 | + return ResolvedAttributeServicesDirectories::createEmpty(); |
| 65 | + } |
| 66 | + |
| 67 | + if (!is_array($rawSectionValue)) { |
| 68 | + throw new InvalidAttributeServicesDirectoriesException([ |
| 69 | + 'The attributeServicesDirectories section must contain a list of directory paths.', |
| 70 | + ]); |
| 71 | + } |
| 72 | + |
| 73 | + $errors = []; |
| 74 | + $directories = []; |
| 75 | + foreach ($rawSectionValue as $entry) { |
| 76 | + if (!is_string($entry)) { |
| 77 | + $errors[] = 'The attributeServicesDirectories section must contain a list of directory paths.'; |
| 78 | + continue; |
| 79 | + } |
| 80 | + if (str_contains($entry, '%')) { |
| 81 | + $errors[] = sprintf('Entry %s in the attributeServicesDirectories section must be a plain path - %% parameters are not supported.', $entry); |
| 82 | + continue; |
| 83 | + } |
| 84 | + if (str_starts_with($entry, '*')) { |
| 85 | + $errors[] = sprintf('Entry %s in the attributeServicesDirectories section must be a plain path - wildcards are not supported.', $entry); |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + $directories[] = rtrim($this->fileHelper->normalizePath($this->fileHelper->absolutizePath($entry), '/'), '/'); |
| 90 | + } |
| 91 | + |
| 92 | + $directories = $this->deduplicateNested($directories); |
| 93 | + |
| 94 | + if (count($directories) > 0 && $this->runtimePhpVersionId < 80000) { |
| 95 | + $errors[] = sprintf( |
| 96 | + 'The attributeServicesDirectories section requires PHP 8.0 or later, PHPStan is running on PHP %d.%d.%d.', |
| 97 | + intdiv($this->runtimePhpVersionId, 10000), |
| 98 | + intdiv($this->runtimePhpVersionId, 100) % 100, |
| 99 | + $this->runtimePhpVersionId % 100, |
| 100 | + ); |
| 101 | + throw new InvalidAttributeServicesDirectoriesException($errors); |
| 102 | + } |
| 103 | + |
| 104 | + $resolved = []; |
| 105 | + foreach ($directories as $directory) { |
| 106 | + if (!is_dir($directory)) { |
| 107 | + $errors[] = sprintf('Directory %s from the attributeServicesDirectories section does not exist.', $directory); |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + $resolvedDirectory = $this->resolveDirectory($directory, $errors); |
| 112 | + if ($resolvedDirectory === null) { |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + $resolved[] = $resolvedDirectory; |
| 117 | + } |
| 118 | + |
| 119 | + if (count($errors) > 0) { |
| 120 | + throw new InvalidAttributeServicesDirectoriesException($errors); |
| 121 | + } |
| 122 | + |
| 123 | + return new ResolvedAttributeServicesDirectories($resolved); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @param list<string> $errors |
| 128 | + */ |
| 129 | + private function resolveDirectory(string $directory, array &$errors): ?ResolvedAttributeServicesDirectory |
| 130 | + { |
| 131 | + $ownership = $this->findOwnership($directory); |
| 132 | + if ($ownership === null) { |
| 133 | + $errors[] = sprintf('Directory %s from the attributeServicesDirectories section is not inside any project with Composer metadata known to PHPStan.', $directory); |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + [$project, $package] = $ownership; |
| 138 | + |
| 139 | + if ($package !== null) { |
| 140 | + // autoload-dev of a dependency is never installed by Composer |
| 141 | + $rules = $package->autoload; |
| 142 | + } elseif ($project->devInstalled) { |
| 143 | + $rules = $project->rootAutoload->union($project->rootAutoloadDev); |
| 144 | + } else { |
| 145 | + $rules = $project->rootAutoload; |
| 146 | + } |
| 147 | + |
| 148 | + $psr4 = []; |
| 149 | + foreach ($rules->psr4 as $namespacePrefix => $baseDirectories) { |
| 150 | + foreach ($baseDirectories as $baseDirectory) { |
| 151 | + if (!$this->pathsIntersect($directory, $baseDirectory)) { |
| 152 | + continue; |
| 153 | + } |
| 154 | + |
| 155 | + $psr4[$namespacePrefix][] = $baseDirectory; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + $classmapPaths = []; |
| 160 | + foreach ($rules->classmapPaths as $classmapPath) { |
| 161 | + if (!$this->pathsIntersect($directory, $classmapPath)) { |
| 162 | + continue; |
| 163 | + } |
| 164 | + |
| 165 | + $classmapPaths[] = $classmapPath; |
| 166 | + } |
| 167 | + |
| 168 | + if (count($psr4) === 0 && count($classmapPaths) === 0) { |
| 169 | + $errors[] = $this->describeUncoveredDirectory($directory, $project, $package); |
| 170 | + return null; |
| 171 | + } |
| 172 | + |
| 173 | + if ($package !== null && $package->cacheToken !== null) { |
| 174 | + $cacheKeyComponent = [$directory => sprintf('package:%s:%s', $package->name, $package->cacheToken)]; |
| 175 | + } else { |
| 176 | + $cacheKeyComponent = $this->hashDirectory($directory, $errors); |
| 177 | + } |
| 178 | + |
| 179 | + return new ResolvedAttributeServicesDirectory( |
| 180 | + $directory, |
| 181 | + $package === null ? null : $package->name, |
| 182 | + $psr4, |
| 183 | + $classmapPaths, |
| 184 | + $project->getAutoloadClassmapPath(), |
| 185 | + $cacheKeyComponent, |
| 186 | + ); |
| 187 | + } |
| 188 | + |
| 189 | + private function describeUncoveredDirectory(string $directory, ComposerProject $project, ?ComposerPackage $package): string |
| 190 | + { |
| 191 | + $subject = $package === null |
| 192 | + ? sprintf('the autoload section of %s/composer.json', $project->rootPath) |
| 193 | + : sprintf('the autoload section of the Composer package %s', $package->name); |
| 194 | + |
| 195 | + if ($package === null) { |
| 196 | + foreach ($this->collectPsrAndFilePaths($project->rootAutoloadDev) as $path) { |
| 197 | + if (!$this->pathsIntersect($directory, $path)) { |
| 198 | + continue; |
| 199 | + } |
| 200 | + |
| 201 | + if (!$project->devInstalled) { |
| 202 | + return sprintf( |
| 203 | + 'Directory %s from the attributeServicesDirectories section is only covered by the autoload-dev section of %s/composer.json but Composer dependencies were installed with --no-dev.', |
| 204 | + $directory, |
| 205 | + $project->rootPath, |
| 206 | + ); |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + $unsupportedRules = $package === null |
| 212 | + ? ($project->devInstalled ? $project->rootAutoload->union($project->rootAutoloadDev) : $project->rootAutoload) |
| 213 | + : $package->autoload; |
| 214 | + foreach ($unsupportedRules->psr0 as $baseDirectories) { |
| 215 | + foreach ($baseDirectories as $baseDirectory) { |
| 216 | + if (!$this->pathsIntersect($directory, $baseDirectory)) { |
| 217 | + continue; |
| 218 | + } |
| 219 | + |
| 220 | + return sprintf( |
| 221 | + 'Directory %s from the attributeServicesDirectories section is only covered by a psr-0 autoload rule of %s. Only psr-4 and classmap rules are supported.', |
| 222 | + $directory, |
| 223 | + $subject, |
| 224 | + ); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + return sprintf( |
| 229 | + 'Directory %s from the attributeServicesDirectories section is not covered by %s. Add it to autoload.psr-4 or autoload.classmap in composer.json and run composer dump-autoload.', |
| 230 | + $directory, |
| 231 | + $subject, |
| 232 | + ); |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * @return array{ComposerProject, ComposerPackage|null}|null |
| 237 | + */ |
| 238 | + private function findOwnership(string $directory): ?array |
| 239 | + { |
| 240 | + foreach ($this->getComposerProjects() as $project) { |
| 241 | + $package = $project->findPackageOfDirectory($directory); |
| 242 | + if ($package !== null) { |
| 243 | + return [$project, $package]; |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + $containing = null; |
| 248 | + foreach ($this->getComposerProjects() as $project) { |
| 249 | + if (!$project->containsDirectory($directory)) { |
| 250 | + continue; |
| 251 | + } |
| 252 | + |
| 253 | + if ($containing !== null && strlen($containing->rootPath) >= strlen($project->rootPath)) { |
| 254 | + continue; |
| 255 | + } |
| 256 | + |
| 257 | + $containing = $project; |
| 258 | + } |
| 259 | + |
| 260 | + if ($containing === null) { |
| 261 | + return null; |
| 262 | + } |
| 263 | + |
| 264 | + return [$containing, null]; |
| 265 | + } |
| 266 | + |
| 267 | + /** |
| 268 | + * @return list<ComposerProject> |
| 269 | + */ |
| 270 | + private function getComposerProjects(): array |
| 271 | + { |
| 272 | + if ($this->composerProjects !== null) { |
| 273 | + return $this->composerProjects; |
| 274 | + } |
| 275 | + |
| 276 | + $projects = []; |
| 277 | + foreach ($this->composerAutoloaderProjectPaths as $projectPath) { |
| 278 | + $project = $this->composerProjectFactory->create($projectPath); |
| 279 | + if ($project === null) { |
| 280 | + continue; |
| 281 | + } |
| 282 | + |
| 283 | + $projects[] = $project; |
| 284 | + } |
| 285 | + |
| 286 | + return $this->composerProjects = $projects; |
| 287 | + } |
| 288 | + |
| 289 | + /** |
| 290 | + * @return list<string> |
| 291 | + */ |
| 292 | + private function collectPsrAndFilePaths(AutoloadRules $rules): array |
| 293 | + { |
| 294 | + $paths = []; |
| 295 | + foreach ($rules->psr4 as $baseDirectories) { |
| 296 | + foreach ($baseDirectories as $baseDirectory) { |
| 297 | + $paths[] = $baseDirectory; |
| 298 | + } |
| 299 | + } |
| 300 | + foreach ($rules->classmapPaths as $path) { |
| 301 | + $paths[] = $path; |
| 302 | + } |
| 303 | + |
| 304 | + return $paths; |
| 305 | + } |
| 306 | + |
| 307 | + private function pathsIntersect(string $a, string $b): bool |
| 308 | + { |
| 309 | + return $a === $b || str_starts_with($a, $b . '/') || str_starts_with($b, $a . '/'); |
| 310 | + } |
| 311 | + |
| 312 | + /** |
| 313 | + * Content hashes of all PHP files under the directory - the cache-key fallback when |
| 314 | + * a package version cannot stand in for the directory contents. Mirrors how config |
| 315 | + * files invalidate the container in Configurator::getAllConfigFilesHashes(). |
| 316 | + * |
| 317 | + * @param list<string> $errors |
| 318 | + * @return array<string, string> |
| 319 | + */ |
| 320 | + private function hashDirectory(string $directory, array &$errors): array |
| 321 | + { |
| 322 | + $files = []; |
| 323 | + $iterator = new RecursiveIteratorIterator( |
| 324 | + new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS), |
| 325 | + ); |
| 326 | + foreach ($iterator as $fileInfo) { |
| 327 | + if (!$fileInfo instanceof SplFileInfo || !$fileInfo->isFile()) { |
| 328 | + continue; |
| 329 | + } |
| 330 | + if (!str_ends_with($fileInfo->getFilename(), '.php')) { |
| 331 | + continue; |
| 332 | + } |
| 333 | + |
| 334 | + $files[] = $this->fileHelper->normalizePath($fileInfo->getPathname(), '/'); |
| 335 | + } |
| 336 | + |
| 337 | + sort($files); |
| 338 | + |
| 339 | + $hashes = []; |
| 340 | + foreach ($files as $file) { |
| 341 | + $hash = hash_file('sha256', $file); |
| 342 | + if ($hash === false) { |
| 343 | + $errors[] = (new CouldNotReadFileException($file))->getMessage(); |
| 344 | + continue; |
| 345 | + } |
| 346 | + |
| 347 | + $hashes[$file] = $hash; |
| 348 | + } |
| 349 | + |
| 350 | + ksort($hashes); |
| 351 | + |
| 352 | + return $hashes; |
| 353 | + } |
| 354 | + |
| 355 | + /** |
| 356 | + * Unique directories with entries nested inside another configured directory dropped - |
| 357 | + * the outer directory already covers them for both discovery and hashing. |
| 358 | + * |
| 359 | + * @param list<string> $directories |
| 360 | + * @return list<string> |
| 361 | + */ |
| 362 | + private function deduplicateNested(array $directories): array |
| 363 | + { |
| 364 | + usort($directories, static fn (string $a, string $b): int => strlen($a) <=> strlen($b)); |
| 365 | + |
| 366 | + $result = []; |
| 367 | + foreach ($directories as $directory) { |
| 368 | + foreach ($result as $kept) { |
| 369 | + if ($directory === $kept || str_starts_with($directory, $kept . '/')) { |
| 370 | + continue 2; |
| 371 | + } |
| 372 | + } |
| 373 | + |
| 374 | + $result[] = $directory; |
| 375 | + } |
| 376 | + |
| 377 | + sort($result); |
| 378 | + |
| 379 | + return $result; |
| 380 | + } |
| 381 | + |
| 382 | +} |
0 commit comments