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:
1 | <?php |
||
5 | trait StopIfTrait |
||
6 | { |
||
7 | /** |
||
8 | * Stops the iteration if the callback returns a true-ish value. |
||
9 | * The current element will not be included. |
||
10 | * |
||
11 | * The passed callback will be invoked with the following argument: |
||
12 | * |
||
13 | * - $value (iterator's current()) |
||
14 | * |
||
15 | * If the second parameter is true the following arguments will be added |
||
16 | * to the call: |
||
17 | * |
||
18 | * - $key (iterator's key()) |
||
19 | * - $iterator (the iterator itself) |
||
20 | * |
||
21 | * @param $______callback |
||
22 | * @param bool $______allArgs |
||
23 | * |
||
24 | * @return \Pipes\Pipe |
||
25 | */ |
||
26 | View Code Duplication | public function stopIf($______callback, $______allArgs = false) |
|
43 | |||
44 | /** |
||
45 | * Stops the iteration if the callback returns a true-ish value. |
||
46 | * The current element will not be included. |
||
47 | * |
||
48 | * The passed callback will be invoked with the following argument: |
||
49 | * |
||
50 | * - $value (iterator's current()) |
||
51 | * |
||
52 | * If the second parameter is true the following arguments will be added |
||
53 | * to the call: |
||
54 | * |
||
55 | * - $key (iterator's key()) |
||
56 | * - $iterator (the iterator itself) |
||
57 | * |
||
58 | * @param $______callback |
||
59 | * @param bool $______allArgs |
||
60 | * |
||
61 | * @return \Pipes\Pipe |
||
62 | */ |
||
63 | View Code Duplication | public function continueIf($______callback, $______allArgs = false) |
|
80 | } |
||
81 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.