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 |
||
9 | final class ArrayExpectations |
||
10 | { |
||
11 | use ConditionTrait; |
||
12 | |||
13 | /** |
||
14 | * ArrayExpectations constructor. |
||
15 | * |
||
16 | * @param $value |
||
17 | */ |
||
18 | public function __construct($value) |
||
24 | |||
25 | /** |
||
26 | * @param $value |
||
27 | * |
||
28 | * @return bool |
||
29 | */ |
||
30 | private function isArray($value): bool |
||
34 | |||
35 | /** |
||
36 | * @param int $length |
||
37 | * |
||
38 | * @return ArrayExpectations |
||
39 | */ |
||
40 | public function hasLength(int $length): self |
||
44 | |||
45 | /** |
||
46 | * @return ArrayExpectations |
||
47 | */ |
||
48 | public function isEmpty(): self |
||
52 | |||
53 | /** |
||
54 | * @return ArrayExpectations |
||
55 | */ |
||
56 | public function isNotEmpty(): self |
||
60 | |||
61 | /** |
||
62 | * @param callable $callback |
||
63 | * |
||
64 | * @return ArrayExpectations |
||
65 | */ |
||
66 | View Code Duplication | public function isAny(callable $callback): self |
|
80 | |||
81 | /** |
||
82 | * @param callable $callback |
||
83 | * |
||
84 | * @return ArrayExpectations |
||
85 | */ |
||
86 | View Code Duplication | public function isAll(callable $callback): self |
|
100 | } |
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.