Conditions | 8 |
Paths | 7 |
Total Lines | 51 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php // phpcs:ignore SR1.Files.SideEffects.FoundWithSymbols |
||
7 | function diffBaseline(string $baselineFilePath, string $targetFilePath): void |
||
8 | { |
||
9 | $realBaselineFilePath = getRealFilePath($baselineFilePath); |
||
10 | $realTargetFilePath = getRealFilePath($targetFilePath); |
||
11 | |||
12 | $baselineXml = file_get_contents($realBaselineFilePath); |
||
13 | |||
14 | $matches = []; |
||
15 | preg_match_all('#\[(\w{16})\]#', $baselineXml, $matches); |
||
16 | |||
17 | $ignoredHashMap = array_flip($matches[1]); |
||
18 | unset($matches); |
||
19 | |||
20 | $remainingWarningCount = 0; |
||
21 | $removedWarningCount = 0; |
||
22 | $newFileLines = []; |
||
23 | $errorFileLine = null; |
||
24 | $errorLines = []; |
||
25 | $realTargetFile = fopen($realTargetFilePath, 'r'); |
||
26 | while (!feof($realTargetFile)) { |
||
27 | $line = fgets($realTargetFile); |
||
28 | |||
29 | if (false !== strpos($line, '<file')) { |
||
30 | $errorFileLine = $line; |
||
31 | } elseif (false !== strpos($line, '</file')) { |
||
32 | if (!empty($errorLines)) { |
||
33 | array_push($newFileLines, $errorFileLine, ...$errorLines); |
||
34 | array_push($newFileLines, $line); |
||
35 | } |
||
36 | $errorFileLine = null; |
||
37 | $errorLines = []; |
||
38 | } elseif (false !== strpos($line, '<error')) { |
||
39 | $matches = []; |
||
40 | preg_match('#\[(\w{16})\]#', $line, $matches); |
||
41 | $errorHash = $matches[1] ?? null; |
||
42 | if (null !== $errorHash && key_exists($errorHash, $ignoredHashMap)) { |
||
43 | $removedWarningCount++; |
||
44 | } else { |
||
45 | $remainingWarningCount++; |
||
46 | $errorLines[] = $line; |
||
47 | } |
||
48 | } else { |
||
49 | $newFileLines[] = $line; |
||
50 | } |
||
51 | } |
||
52 | fclose($realTargetFile); |
||
53 | |||
54 | echo sprintf('Found %s warning(s) in %s.%s', count($ignoredHashMap), $baselineFilePath, PHP_EOL); |
||
55 | echo sprintf('Removed %s warning(s) from %s, %s warning(s) remain.%s', $removedWarningCount, $targetFilePath, $remainingWarningCount, PHP_EOL); |
||
56 | |||
57 | file_put_contents($realTargetFilePath, implode('', $newFileLines)); |
||
58 | } |
||
74 |