Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Processor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Processor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | final class Processor implements ProcessorInterface |
||
31 | { |
||
32 | /**@+ |
||
33 | * The codes representing different JSON elements |
||
34 | * |
||
35 | * These come from the Seld\JsonLint\JsonParser class. The values are returned by the lexer when |
||
36 | * the lex() method is called. |
||
37 | * |
||
38 | * @var int |
||
39 | */ |
||
40 | const JSON_OBJECT_START = 17; |
||
41 | const JSON_OBJECT_END = 18; |
||
42 | const JSON_ARRAY_START = 23; |
||
43 | const JSON_ARRAY_END = 24; |
||
44 | const JSON_EOF = 1; |
||
45 | const JSON_STRING = 4; |
||
46 | const JSON_COLON = 21; |
||
47 | /**@-*/ |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private $resourceDir; |
||
53 | |||
54 | /** |
||
55 | * The pattern ids encountered during the test run. These are compared against the JSON file structure to determine |
||
56 | * if the statement/function/branch is covered. |
||
57 | * |
||
58 | * @var string[] |
||
59 | */ |
||
60 | private $coveredIds = []; |
||
61 | |||
62 | /** |
||
63 | * This is the full coverage array that gets output in the write method. For each file an entry in the array |
||
64 | * is added. Each entry contains the elements required for Istanbul compatible coverage reporters. |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | private $coverage = []; |
||
69 | |||
70 | /** |
||
71 | * An incrementing integer for every "function" (child match) encountered in all processed files. This is used |
||
72 | * to name the anonymous functions in the coverage report. |
||
73 | * |
||
74 | * @var int |
||
75 | */ |
||
76 | private $funcCount = 0; |
||
77 | |||
78 | /** |
||
79 | * A storage variable for the lines of a file while processing that file, used for determining column |
||
80 | * position of a statement/function/branch |
||
81 | * |
||
82 | * @var string[] |
||
83 | */ |
||
84 | private $fileLines = []; |
||
85 | |||
86 | /** |
||
87 | * A storage variable of the pattern ids covered by tests for a specific file (set when processing of that |
||
88 | * file begins) |
||
89 | * |
||
90 | * @var string[] |
||
91 | */ |
||
92 | private $fileCoveredIds = []; |
||
93 | |||
94 | /** |
||
95 | * A temporary storage for coverage information for a specific file that is later merged into the main $coverage |
||
96 | * property after the file is done processing. |
||
97 | * |
||
98 | * @var array |
||
99 | */ |
||
100 | private $fileCoverage = []; |
||
101 | |||
102 | /** |
||
103 | * Create a new Coverage Processor for the specified directory |
||
104 | * |
||
105 | * @param string $resourceDir |
||
106 | */ |
||
107 | 10 | public function __construct(string $resourceDir) |
|
111 | |||
112 | /** |
||
113 | * Process the directory of JSON files using the collected pattern ids |
||
114 | * |
||
115 | * @param string[] $coveredIds |
||
116 | * |
||
117 | * @return void |
||
118 | */ |
||
119 | public function process(array $coveredIds) |
||
144 | |||
145 | /** |
||
146 | * Write the coverage data in JSON format to specified filename |
||
147 | * |
||
148 | * @param string $fileName |
||
149 | * |
||
150 | * @return void |
||
151 | */ |
||
152 | public function write(string $fileName) |
||
163 | |||
164 | /** |
||
165 | * Stores passed in pattern ids, grouping them by file first |
||
166 | * |
||
167 | * @param string[] $coveredIds |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | 1 | public function setCoveredPatternIds(array $coveredIds) |
|
175 | |||
176 | /** |
||
177 | * Returns the grouped pattern ids previously set |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | 1 | public function getCoveredPatternIds() : array |
|
185 | |||
186 | /** |
||
187 | * Process an individual file for coverage data using covered ids |
||
188 | * |
||
189 | * @param string $file |
||
190 | * @param string $contents |
||
191 | * @param string[] $coveredIds |
||
192 | * |
||
193 | * @return array |
||
194 | */ |
||
195 | 9 | public function processFile(string $file, string $contents, array $coveredIds) : array |
|
241 | |||
242 | /** |
||
243 | * Builds the location object for the current position in the JSON file |
||
244 | * |
||
245 | * @param Lexer $lexer |
||
246 | * @param bool $end |
||
247 | * @param string $content |
||
248 | * |
||
249 | * @return array |
||
250 | */ |
||
251 | 9 | private function getLocationCoordinates(Lexer $lexer, bool $end = false, string $content = '') : array |
|
268 | |||
269 | /** |
||
270 | * JSON file processing entry point |
||
271 | * |
||
272 | * Hands execution off to applicable method when certain tokens are encountered |
||
273 | * (in this case, Division is the only one), returns to caller when EOF is reached |
||
274 | * |
||
275 | * @param Lexer $lexer |
||
276 | * |
||
277 | * @return void |
||
278 | */ |
||
279 | 9 | private function handleJsonRoot(Lexer $lexer) |
|
289 | |||
290 | /** |
||
291 | * Processes the Division block (which is the root JSON object essentially) |
||
292 | * |
||
293 | * Lexes the main division object and hands execution off to relevant methods for further |
||
294 | * processing. Returns the next token code returned from the lexer. |
||
295 | * |
||
296 | * @param Lexer $lexer |
||
297 | * |
||
298 | * @return int |
||
299 | */ |
||
300 | 9 | View Code Duplication | private function handleJsonDivision(Lexer $lexer) : int |
319 | |||
320 | /** |
||
321 | * Processes the userAgents array |
||
322 | * |
||
323 | * @param Lexer $lexer |
||
324 | * |
||
325 | * @return int |
||
326 | */ |
||
327 | 9 | View Code Duplication | private function handleUseragentGroup(Lexer $lexer) : int |
342 | |||
343 | /** |
||
344 | * Processes each userAgent object in the userAgents array |
||
345 | * |
||
346 | * @param Lexer $lexer |
||
347 | * @param int $useragentPosition |
||
348 | * |
||
349 | * @return int |
||
350 | */ |
||
351 | 9 | View Code Duplication | private function handleUseragentBlock(Lexer $lexer, int $useragentPosition) : int |
370 | |||
371 | /** |
||
372 | * Processes the children array of a userAgent object |
||
373 | * |
||
374 | * @param Lexer $lexer |
||
375 | * @param int $useragentPosition |
||
376 | * |
||
377 | * @return int |
||
378 | */ |
||
379 | 9 | View Code Duplication | private function handleChildrenGroup(Lexer $lexer, int $useragentPosition) : int |
394 | |||
395 | /** |
||
396 | * Processes each child object in the children array |
||
397 | * |
||
398 | * @param Lexer $lexer |
||
399 | * @param int $useragentPosition |
||
400 | * @param int $childPosition |
||
401 | * |
||
402 | * @return int |
||
403 | */ |
||
404 | 9 | private function handleChildBlock(Lexer $lexer, int $useragentPosition, int $childPosition) : int |
|
461 | |||
462 | /** |
||
463 | * Process the "devices" has in the child object |
||
464 | * |
||
465 | * @param Lexer $lexer |
||
466 | * @param int $useragentPosition |
||
467 | * @param int $childPosition |
||
468 | * |
||
469 | * @return int |
||
470 | */ |
||
471 | 4 | private function handleDeviceBlock(Lexer $lexer, int $useragentPosition, int $childPosition) : int |
|
506 | |||
507 | /** |
||
508 | * Processes the "platforms" hash in the child object |
||
509 | * |
||
510 | * @param Lexer $lexer |
||
511 | * @param int $useragentPosition |
||
512 | * @param int $childPosition |
||
513 | * |
||
514 | * @return int |
||
515 | */ |
||
516 | 9 | private function handlePlatformBlock(Lexer $lexer, int $useragentPosition, int $childPosition) : int |
|
543 | |||
544 | /** |
||
545 | * Processes JSON object block that isn't needed for coverage data |
||
546 | * |
||
547 | * @param Lexer $lexer |
||
548 | * |
||
549 | * @return int |
||
550 | */ |
||
551 | 9 | private function ignoreObjectBlock(Lexer $lexer) : int |
|
564 | |||
565 | /** |
||
566 | * Collects and stores a function's location information as well as any passed in coverage counts |
||
567 | * |
||
568 | * @param array $start |
||
569 | * @param array $end |
||
570 | * @param array $declaration |
||
571 | * @param int $coverage |
||
572 | * |
||
573 | * @return void |
||
574 | */ |
||
575 | 9 | private function collectFunction(array $start, array $end, array $declaration, int $coverage = 0) |
|
591 | |||
592 | /** |
||
593 | * Collects and stores a branch's location information as well as any coverage counts |
||
594 | * |
||
595 | * @param array $start |
||
596 | * @param array $end |
||
597 | * @param array $locations |
||
598 | * @param int[] $coverage |
||
599 | * |
||
600 | * @return void |
||
601 | */ |
||
602 | 9 | private function collectBranch(array $start, array $end, array $locations, array $coverage = []) |
|
619 | |||
620 | /** |
||
621 | * Collects and stores a statement's location information as well as any coverage counts |
||
622 | * |
||
623 | * @param array $start |
||
624 | * @param array $end |
||
625 | * @param int $coverage |
||
626 | * |
||
627 | * @return void |
||
628 | */ |
||
629 | 9 | private function collectStatement(array $start, array $end, int $coverage = 0) |
|
638 | |||
639 | /** |
||
640 | * Groups pattern ids by their filename prefix |
||
641 | * |
||
642 | * @param string[] $ids |
||
643 | * |
||
644 | * @return array |
||
645 | */ |
||
646 | 1 | private function groupIdsByFile(array $ids) : array |
|
662 | |||
663 | /** |
||
664 | * Counts number of times given generated pattern id is covered by patterns ids collected during tests |
||
665 | * |
||
666 | * @param string $id |
||
667 | * @param string[] $covered |
||
668 | * |
||
669 | * @return int |
||
670 | */ |
||
671 | 9 | private function getCoverageCount(string $id, array $covered) : int |
|
700 | } |
||
701 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: