|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Caching; |
| 6 | + |
| 7 | +use Rector\Contract\DependencyInjection\ResettableInterface; |
| 8 | +use Rector\Util\FileHasher; |
| 9 | + |
| 10 | +/** |
| 11 | + * Collects the files each processed file depends on, as surfaced by PHPStan's |
| 12 | + * DependencyResolver during scope resolution, and provides memoized content hashes |
| 13 | + * for dependency validation by the unchanged-files cache. |
| 14 | + */ |
| 15 | +final class FileDependencyCollector implements ResettableInterface |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var array<string, array<string, true>> |
| 19 | + */ |
| 20 | + private array $dependenciesByFile = []; |
| 21 | + |
| 22 | + /** |
| 23 | + * files whose capture threw, their possibly partial set must never be cached |
| 24 | + * |
| 25 | + * @var array<string, true> |
| 26 | + */ |
| 27 | + private array $failedFiles = []; |
| 28 | + |
| 29 | + /** |
| 30 | + * keyed by the given path, so memo hits skip realpath() as well |
| 31 | + * |
| 32 | + * @var array<string, string|null> |
| 33 | + */ |
| 34 | + private array $contentHashMemo = []; |
| 35 | + |
| 36 | + /** |
| 37 | + * a function's signature dependencies are identical at every call site |
| 38 | + * |
| 39 | + * @var array<string, string[]> |
| 40 | + */ |
| 41 | + private array $functionDependencyFilesMemo = []; |
| 42 | + |
| 43 | + public function __construct( |
| 44 | + private readonly FileHasher $fileHasher, |
| 45 | + ) { |
| 46 | + } |
| 47 | + |
| 48 | + public function record(string $filePath, string $dependencyFilePath): void |
| 49 | + { |
| 50 | + if ($filePath === $dependencyFilePath) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + $this->dependenciesByFile[$filePath][$dependencyFilePath] = true; |
| 55 | + } |
| 56 | + |
| 57 | + public function markFailed(string $filePath): void |
| 58 | + { |
| 59 | + $this->failedFiles[$filePath] = true; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @return string[]|null |
| 64 | + */ |
| 65 | + public function getMemoizedFunctionDependencyFiles(string $functionKey): ?array |
| 66 | + { |
| 67 | + return $this->functionDependencyFilesMemo[$functionKey] ?? null; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param string[] $dependencyFiles |
| 72 | + */ |
| 73 | + public function memoizeFunctionDependencyFiles(string $functionKey, array $dependencyFiles): void |
| 74 | + { |
| 75 | + $this->functionDependencyFilesMemo[$functionKey] = $dependencyFiles; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @return array<string, string>|null null when capture failed and the set cannot be trusted |
| 80 | + */ |
| 81 | + public function getDependencyFileHashes(string $filePath): ?array |
| 82 | + { |
| 83 | + if (isset($this->failedFiles[$filePath])) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + $dependencyHashes = []; |
| 88 | + foreach (array_keys($this->dependenciesByFile[$filePath] ?? []) as $dependencyFile) { |
| 89 | + $dependencyHash = $this->contentHash($dependencyFile); |
| 90 | + if ($dependencyHash !== null) { |
| 91 | + $dependencyHashes[$dependencyFile] = $dependencyHash; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return $dependencyHashes; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param array<string, string> $recordedDependencyHashes |
| 100 | + */ |
| 101 | + public function hasAnyChangedDependency(array $recordedDependencyHashes): bool |
| 102 | + { |
| 103 | + foreach ($recordedDependencyHashes as $dependencyFile => $recordedHash) { |
| 104 | + if ($this->contentHash($dependencyFile) !== $recordedHash) { |
| 105 | + return true; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * null when the file does not exist, e.g. a deleted dependency, which callers treat as changed |
| 114 | + */ |
| 115 | + public function contentHash(string $filePath): ?string |
| 116 | + { |
| 117 | + if (array_key_exists($filePath, $this->contentHashMemo)) { |
| 118 | + return $this->contentHashMemo[$filePath]; |
| 119 | + } |
| 120 | + |
| 121 | + $resolvedPath = $this->resolvePath($filePath); |
| 122 | + if (! is_file($resolvedPath)) { |
| 123 | + return $this->contentHashMemo[$filePath] = null; |
| 124 | + } |
| 125 | + |
| 126 | + return $this->contentHashMemo[$filePath] = $this->fileHasher->hashFiles([$resolvedPath]); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * drop a memoized hash, e.g. after the file has been written mid-run |
| 131 | + */ |
| 132 | + public function forgetContentHash(string $filePath): void |
| 133 | + { |
| 134 | + unset($this->contentHashMemo[$filePath]); |
| 135 | + } |
| 136 | + |
| 137 | + public function reset(): void |
| 138 | + { |
| 139 | + $this->dependenciesByFile = []; |
| 140 | + $this->failedFiles = []; |
| 141 | + $this->contentHashMemo = []; |
| 142 | + $this->functionDependencyFilesMemo = []; |
| 143 | + } |
| 144 | + |
| 145 | + private function resolvePath(string $filePath): string |
| 146 | + { |
| 147 | + $realPath = realpath($filePath); |
| 148 | + if ($realPath === false) { |
| 149 | + return $filePath; |
| 150 | + } |
| 151 | + |
| 152 | + return $realPath; |
| 153 | + } |
| 154 | +} |
0 commit comments