Conditions | 15 |
Paths | 160 |
Total Lines | 76 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
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 |
||
9 | function diffBaseline(string $baselineFilePath, string $targetFilePath): int |
||
10 | { |
||
11 | $realBaselineFilePath = getRealFilePath($baselineFilePath); |
||
12 | $realTargetFilePath = getRealFilePath($targetFilePath); |
||
13 | |||
14 | // Collect violationIds from baseline file, put them into a map for faster lookup |
||
15 | // If violationId is not found, the use single filename + line + error hash. This works for simple errors e.g. PSR-12 |
||
16 | $errorFileLine = ''; |
||
17 | $violationIdMap = []; |
||
18 | $lineHashMap = []; |
||
19 | $baselineFile = fopen($realBaselineFilePath, 'r'); |
||
20 | while (!feof($baselineFile)) { |
||
21 | $line = fgets($baselineFile); |
||
22 | if (false !== strpos($line, '<file')) { |
||
23 | $errorFileLine = $line; |
||
24 | continue; |
||
25 | } |
||
26 | |||
27 | if (false === strpos($line, '<error')) { |
||
28 | continue; |
||
29 | } |
||
30 | |||
31 | if ($violationId = findViolationId($line)) { |
||
32 | $violationIdMap[$violationId] = null; |
||
33 | } else { |
||
34 | $lineHash = getLineHash($errorFileLine, $line); |
||
35 | $lineHashMap[$lineHash] = null; |
||
36 | } |
||
37 | } |
||
38 | fclose($baselineFile); |
||
39 | |||
40 | // Iterate over report file, keep errors only if they are not in the ignored error maps. |
||
41 | // If a file segment contains new errors, then keep file segment with those new errors. |
||
42 | // In the end, write collected file (with errors) segments to the same file. |
||
43 | $remainingWarningCount = 0; |
||
44 | $removedWarningCount = 0; |
||
45 | $newFileLines = []; |
||
46 | $errorFileLine = ''; |
||
47 | $errorLines = []; |
||
48 | $realTargetFile = fopen($realTargetFilePath, 'r'); |
||
49 | while (!feof($realTargetFile)) { |
||
50 | $line = fgets($realTargetFile); |
||
51 | |||
52 | if (false !== strpos($line, '<file')) { |
||
53 | $errorFileLine = $line; |
||
54 | } elseif (false !== strpos($line, '</file')) { |
||
55 | if (!empty($errorLines)) { |
||
56 | array_push($newFileLines, $errorFileLine, ...$errorLines); |
||
57 | array_push($newFileLines, $line); |
||
58 | } |
||
59 | $errorFileLine = ''; |
||
60 | $errorLines = []; |
||
61 | } elseif (false !== strpos($line, '<error')) { |
||
62 | $violationId = findViolationId($line); |
||
63 | if (null !== $violationId && key_exists($violationId, $violationIdMap)) { |
||
64 | $removedWarningCount++; |
||
65 | } elseif (key_exists(getLineHash($errorFileLine, $line), $lineHashMap)) { |
||
66 | $removedWarningCount++; |
||
67 | } else { |
||
68 | $remainingWarningCount++; |
||
69 | $errorLines[] = $line; |
||
70 | } |
||
71 | } else { |
||
72 | $newFileLines[] = $line; |
||
73 | } |
||
74 | } |
||
75 | fclose($realTargetFile); |
||
76 | |||
77 | echo sprintf('Found %s warning(s) in %s.%s', count($violationIdMap) + count($lineHashMap), $baselineFilePath, PHP_EOL); |
||
78 | echo sprintf('Removed %s warning(s) from %s, %s warning(s) remain.%s', $removedWarningCount, $targetFilePath, $remainingWarningCount, PHP_EOL); |
||
79 | |||
80 | if ($removedWarningCount > 0) { |
||
81 | file_put_contents($realTargetFilePath, implode('', $newFileLines)); |
||
82 | } |
||
83 | |||
84 | return $remainingWarningCount > 0 ? 1 : 0; |
||
85 | } |
||
114 |