|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Wtyd\GitHooks\Jobs\CacheResolver; |
| 6 | + |
| 7 | +/** |
| 8 | + * Static-best-effort cache path extractor for PHP config files (rector.php, |
| 9 | + * .php-cs-fixer.php). |
| 10 | + * |
| 11 | + * These tools store cache config inside PHP code, which is not statically |
| 12 | + * parseable in the general case. We cover the three patterns that show up |
| 13 | + * in real projects: |
| 14 | + * |
| 15 | + * $config->method('absolute/path') → 'absolute/path' |
| 16 | + * $config->method(__DIR__ . '/literal') → dir(configFile)/literal |
| 17 | + * $config->method(\sys_get_temp_dir() . '/literal') → sys_get_temp_dir()/literal |
| 18 | + * |
| 19 | + * Anything dynamic (variables, helpers, env vars) is not extractable. The |
| 20 | + * caller distinguishes "match" (returns a string) from "no match" |
| 21 | + * (returns null) — when null, falling back to a default and emitting a |
| 22 | + * "could not parse" warning is up to the caller. |
| 23 | + * |
| 24 | + * The resolver intentionally does not load or execute the config file. |
| 25 | + */ |
| 26 | +final class PhpConfigCacheResolver |
| 27 | +{ |
| 28 | + private const STRING_PATTERN = '(?:\'((?:\\\\.|[^\'\\\\])*)\'|"((?:\\\\.|[^"\\\\])*)")'; |
| 29 | + |
| 30 | + /** |
| 31 | + * @return string|null Resolved cache path, or null when no recognized |
| 32 | + * pattern matches (or the file is unreadable). |
| 33 | + */ |
| 34 | + public static function resolve(string $configPath, string $methodName): ?string |
| 35 | + { |
| 36 | + $content = self::readSanitizedContent($configPath); |
| 37 | + if ($content === null) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + $methodEscaped = preg_quote($methodName, '/'); |
| 41 | + |
| 42 | + $allCallOffsets = self::allInvocationOffsets($content, $methodEscaped); |
| 43 | + if ($allCallOffsets === []) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + $lastCallOffset = max($allCallOffsets); |
| 47 | + |
| 48 | + $candidates = array_merge( |
| 49 | + self::collectDirConcat($content, $methodEscaped, $configPath), |
| 50 | + self::collectSysTempConcat($content, $methodEscaped), |
| 51 | + self::collectLiteral($content, $methodEscaped) |
| 52 | + ); |
| 53 | + |
| 54 | + foreach ($candidates as $hit) { |
| 55 | + if ($hit['pos'] === $lastCallOffset) { |
| 56 | + return $hit['path']; |
| 57 | + } |
| 58 | + } |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + /** @return int[] */ |
| 63 | + private static function allInvocationOffsets(string $content, string $methodEscaped): array |
| 64 | + { |
| 65 | + if (preg_match_all('/->\s*' . $methodEscaped . '\s*\(/', $content, $matches, PREG_OFFSET_CAPTURE) === false) { |
| 66 | + return []; |
| 67 | + } |
| 68 | + $offsets = []; |
| 69 | + foreach ($matches[0] as $match) { |
| 70 | + $offsets[] = (int) $match[1]; |
| 71 | + } |
| 72 | + return $offsets; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Reads the file and strips PHP comments so commented-out method calls |
| 77 | + * don't pollute the regex match. |
| 78 | + */ |
| 79 | + private static function readSanitizedContent(string $configPath): ?string |
| 80 | + { |
| 81 | + if (!is_file($configPath) || !is_readable($configPath)) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + $content = file_get_contents($configPath); |
| 85 | + if ($content === false) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + return self::stripComments($content); |
| 89 | + } |
| 90 | + |
| 91 | + private static function stripComments(string $content): string |
| 92 | + { |
| 93 | + $tokens = token_get_all($content); |
| 94 | + if ($tokens === []) { |
| 95 | + return $content; |
| 96 | + } |
| 97 | + $out = ''; |
| 98 | + foreach ($tokens as $token) { |
| 99 | + if (is_array($token)) { |
| 100 | + if (in_array($token[0], [T_COMMENT, T_DOC_COMMENT], true)) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + $out .= $token[1]; |
| 104 | + continue; |
| 105 | + } |
| 106 | + $out .= $token; |
| 107 | + } |
| 108 | + return $out; |
| 109 | + } |
| 110 | + |
| 111 | + /** @return array<int, array{pos: int, path: string}> */ |
| 112 | + private static function collectDirConcat(string $content, string $methodEscaped, string $configPath): array |
| 113 | + { |
| 114 | + $regex = '/->\s*' . $methodEscaped . '\s*\(\s*__DIR__\s*\.\s*' . self::STRING_PATTERN . '\s*\)/'; |
| 115 | + $base = dirname(realpath($configPath) ?: $configPath); |
| 116 | + return self::collectMatches($regex, $content, static function (array $m) use ($base): string { |
| 117 | + return $base . self::pickLiteral($m); |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + /** @return array<int, array{pos: int, path: string}> */ |
| 122 | + private static function collectSysTempConcat(string $content, string $methodEscaped): array |
| 123 | + { |
| 124 | + $regex = '/->\s*' . $methodEscaped . '\s*\(\s*\\\\?sys_get_temp_dir\s*\(\s*\)\s*\.\s*' . self::STRING_PATTERN . '\s*\)/'; |
| 125 | + $tmp = sys_get_temp_dir(); |
| 126 | + return self::collectMatches($regex, $content, static function (array $m) use ($tmp): string { |
| 127 | + return $tmp . self::pickLiteral($m); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + /** @return array<int, array{pos: int, path: string}> */ |
| 132 | + private static function collectLiteral(string $content, string $methodEscaped): array |
| 133 | + { |
| 134 | + $regex = '/->\s*' . $methodEscaped . '\s*\(\s*' . self::STRING_PATTERN . '\s*\)/'; |
| 135 | + return self::collectMatches($regex, $content, static function (array $m): string { |
| 136 | + return self::pickLiteral($m); |
| 137 | + }); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @param callable(array<int, string>): string $resolver |
| 142 | + * @return array<int, array{pos: int, path: string}> |
| 143 | + */ |
| 144 | + private static function collectMatches(string $regex, string $content, callable $resolver): array |
| 145 | + { |
| 146 | + if (preg_match_all($regex, $content, $matches, PREG_OFFSET_CAPTURE) === false) { |
| 147 | + return []; |
| 148 | + } |
| 149 | + $hits = []; |
| 150 | + $count = isset($matches[0]) ? count($matches[0]) : 0; |
| 151 | + for ($i = 0; $i < $count; $i++) { |
| 152 | + $offset = (int) $matches[0][$i][1]; |
| 153 | + $captured = [ |
| 154 | + $matches[0][$i][0], |
| 155 | + isset($matches[1][$i]) ? (string) $matches[1][$i][0] : '', |
| 156 | + isset($matches[2][$i]) ? (string) $matches[2][$i][0] : '', |
| 157 | + ]; |
| 158 | + $hits[] = ['pos' => $offset, 'path' => $resolver($captured)]; |
| 159 | + } |
| 160 | + return $hits; |
| 161 | + } |
| 162 | + |
| 163 | + /** @param array<int, string> $matches */ |
| 164 | + private static function pickLiteral(array $matches): string |
| 165 | + { |
| 166 | + return $matches[1] !== '' ? $matches[1] : ($matches[2] ?? ''); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Whether the config file declares the given method but with an expression |
| 171 | + * that we can't resolve statically (variables, function calls other than |
| 172 | + * __DIR__/sys_get_temp_dir, env vars, ...). Used by callers to decide |
| 173 | + * whether to surface a "could not parse" warning. |
| 174 | + */ |
| 175 | + public static function declaresUnresolvable(string $configPath, string $methodName): bool |
| 176 | + { |
| 177 | + $content = self::readSanitizedContent($configPath); |
| 178 | + if ($content === null) { |
| 179 | + return false; |
| 180 | + } |
| 181 | + $methodEscaped = preg_quote($methodName, '/'); |
| 182 | + return preg_match('/->\s*' . $methodEscaped . '\s*\(/', $content) === 1 |
| 183 | + && self::resolve($configPath, $methodName) === null; |
| 184 | + } |
| 185 | +} |
0 commit comments