|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Utils\Duplicates; |
| 6 | + |
| 7 | +use Rector\Utils\Duplicates\ValueObject\CodeClone; |
| 8 | +use Rector\Utils\Duplicates\ValueObject\CodeCloneFile; |
| 9 | + |
| 10 | +/** |
| 11 | + * Token-based copy-paste detector using a Rabin-Karp rolling hash over the |
| 12 | + * normalized token stream, mirroring the classic phpcpd behaviour. |
| 13 | + * @see \Rector\Utils\Duplicates\Tests\CloneDetectorTest |
| 14 | + */ |
| 15 | +final class CloneDetector |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Tokens that carry no structural meaning for clone detection. |
| 19 | + * |
| 20 | + * @var array<int, true> |
| 21 | + */ |
| 22 | + private const array IGNORED_TOKENS = [ |
| 23 | + T_INLINE_HTML => true, |
| 24 | + T_COMMENT => true, |
| 25 | + T_DOC_COMMENT => true, |
| 26 | + T_OPEN_TAG => true, |
| 27 | + T_OPEN_TAG_WITH_ECHO => true, |
| 28 | + T_CLOSE_TAG => true, |
| 29 | + T_WHITESPACE => true, |
| 30 | + ]; |
| 31 | + |
| 32 | + /** |
| 33 | + * Bytes contributed by each kept token to the signature: 1 type byte + 4 CRC32 bytes. |
| 34 | + */ |
| 35 | + private const int BYTES_PER_TOKEN = 5; |
| 36 | + |
| 37 | + /** |
| 38 | + * First seen location per window hash: hash => [filePath, tokenIndex]. |
| 39 | + * |
| 40 | + * @var array<string, array{string, int}> |
| 41 | + */ |
| 42 | + private array $hashes = []; |
| 43 | + |
| 44 | + /** |
| 45 | + * Per-file token line numbers, kept for span line lookup. |
| 46 | + * |
| 47 | + * @var array<string, int[]> |
| 48 | + */ |
| 49 | + private array $fileLines = []; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var CodeClone[] |
| 53 | + */ |
| 54 | + private array $clones = []; |
| 55 | + |
| 56 | + public function __construct( |
| 57 | + private readonly int $minLines, |
| 58 | + private readonly int $minTokens, |
| 59 | + private readonly bool $fuzzy |
| 60 | + ) { |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param string[] $filePaths |
| 65 | + * @return CodeClone[] |
| 66 | + */ |
| 67 | + public function detect(array $filePaths): array |
| 68 | + { |
| 69 | + foreach ($filePaths as $filePath) { |
| 70 | + $this->processFile($filePath); |
| 71 | + } |
| 72 | + |
| 73 | + return $this->clones; |
| 74 | + } |
| 75 | + |
| 76 | + private function processFile(string $filePath): void |
| 77 | + { |
| 78 | + $source = file_get_contents($filePath); |
| 79 | + if ($source === false) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + [$signature, $lines] = $this->tokenize($source); |
| 84 | + $this->fileLines[$filePath] = $lines; |
| 85 | + |
| 86 | + $tokenCount = count($lines); |
| 87 | + if ($tokenCount < $this->minTokens) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + $lastWindow = $tokenCount - $this->minTokens; |
| 92 | + $windowBytes = $this->minTokens * self::BYTES_PER_TOKEN; |
| 93 | + |
| 94 | + $found = false; |
| 95 | + $firstToken = 0; |
| 96 | + $originFile = ''; |
| 97 | + $originToken = 0; |
| 98 | + |
| 99 | + for ($i = 0; $i <= $lastWindow; ++$i) { |
| 100 | + $hash = substr(md5(substr($signature, $i * self::BYTES_PER_TOKEN, $windowBytes), true), 0, 8); |
| 101 | + |
| 102 | + if (isset($this->hashes[$hash])) { |
| 103 | + if (! $found) { |
| 104 | + $found = true; |
| 105 | + $firstToken = $i; |
| 106 | + [$originFile, $originToken] = $this->hashes[$hash]; |
| 107 | + } |
| 108 | + |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + if ($found) { |
| 113 | + $this->recordClone($originFile, $originToken, $filePath, $firstToken, $i); |
| 114 | + $found = false; |
| 115 | + } |
| 116 | + |
| 117 | + $this->hashes[$hash] = [$filePath, $i]; |
| 118 | + } |
| 119 | + |
| 120 | + if ($found) { |
| 121 | + $this->recordClone($originFile, $originToken, $filePath, $firstToken, $lastWindow + 1); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private function recordClone( |
| 126 | + string $originFile, |
| 127 | + int $originToken, |
| 128 | + string $currentFile, |
| 129 | + int $currentToken, |
| 130 | + int $mismatchWindow |
| 131 | + ): void { |
| 132 | + $windowCount = $mismatchWindow - $currentToken; |
| 133 | + $tokenSpan = $windowCount + $this->minTokens - 1; |
| 134 | + |
| 135 | + $originLines = $this->fileLines[$originFile]; |
| 136 | + $currentLines = $this->fileLines[$currentFile]; |
| 137 | + |
| 138 | + $originStartLine = $originLines[$originToken]; |
| 139 | + $originEndLine = $originLines[min($originToken + $tokenSpan - 1, count($originLines) - 1)]; |
| 140 | + |
| 141 | + $currentStartLine = $currentLines[$currentToken]; |
| 142 | + $currentEndLine = $currentLines[min($currentToken + $tokenSpan - 1, count($currentLines) - 1)]; |
| 143 | + |
| 144 | + $numberOfLines = $originEndLine - $originStartLine + 1; |
| 145 | + if ($numberOfLines < $this->minLines) { |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + $this->clones[] = new CodeClone( |
| 150 | + new CodeCloneFile($originFile, $originStartLine, $originEndLine), |
| 151 | + new CodeCloneFile($currentFile, $currentStartLine, $currentEndLine), |
| 152 | + $numberOfLines, |
| 153 | + $tokenSpan |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Builds the token signature and the parallel line map for a source string. |
| 159 | + * |
| 160 | + * @return array{string, int[]} |
| 161 | + */ |
| 162 | + private function tokenize(string $source): array |
| 163 | + { |
| 164 | + $signature = ''; |
| 165 | + $lines = []; |
| 166 | + $currentLine = 1; |
| 167 | + |
| 168 | + foreach (token_get_all($source) as $token) { |
| 169 | + if (is_array($token)) { |
| 170 | + $tokenId = $token[0]; |
| 171 | + $tokenText = $token[1]; |
| 172 | + $currentLine = $token[2]; |
| 173 | + |
| 174 | + if (isset(self::IGNORED_TOKENS[$tokenId])) { |
| 175 | + $currentLine += substr_count($tokenText, "\n"); |
| 176 | + continue; |
| 177 | + } |
| 178 | + |
| 179 | + if ($this->fuzzy && $tokenId === T_VARIABLE) { |
| 180 | + $tokenText = '$'; |
| 181 | + } |
| 182 | + |
| 183 | + $signature .= chr($tokenId & 255) . pack('N*', crc32($tokenText)); |
| 184 | + $lines[] = $currentLine; |
| 185 | + $currentLine += substr_count($tokenText, "\n"); |
| 186 | + |
| 187 | + continue; |
| 188 | + } |
| 189 | + |
| 190 | + $signature .= chr(0) . pack('N*', crc32($token)); |
| 191 | + $lines[] = $currentLine; |
| 192 | + } |
| 193 | + |
| 194 | + return [$signature, $lines]; |
| 195 | + } |
| 196 | +} |
0 commit comments