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 | class FailureCollection extends \ArrayObject |
||
6 | { |
||
7 | /** |
||
8 | * Factory method to return a new Failure object. |
||
9 | * |
||
10 | * @param string $field The field that failed |
||
11 | * @param string $message The failure message |
||
12 | * @param array $args The arguments passed to the rule specification |
||
13 | * |
||
14 | * @return ValidationFailure |
||
15 | */ |
||
16 | 42 | protected function newFailure(string $field, string $message, array $args = []): ValidationFailure |
|
20 | |||
21 | /** |
||
22 | * Returns bool indicating if the array is empty. |
||
23 | * |
||
24 | * @return bool |
||
25 | */ |
||
26 | 54 | public function isEmpty(): bool |
|
30 | |||
31 | /** |
||
32 | * Set a failure on a field, removing all previous failures. |
||
33 | * |
||
34 | * @param string $field The field that failed |
||
35 | * @param string $message The failure message |
||
36 | * @param array $args The arguments passed to the rule specification |
||
37 | * |
||
38 | * @return ValidationFailure |
||
39 | */ |
||
40 | 9 | View Code Duplication | public function set(string $field, string $message, array $args = []): ValidationFailure |
47 | |||
48 | /** |
||
49 | * Adds an additional failure on a field. |
||
50 | * |
||
51 | * @param string $field The field that failed |
||
52 | * @param string $message The failure message |
||
53 | * @param array $args The arguments passed to the rule specification |
||
54 | * |
||
55 | * @return ValidationFailure |
||
56 | */ |
||
57 | 33 | View Code Duplication | public function add(string $field, string $message, array $args = []): ValidationFailure |
64 | |||
65 | /** |
||
66 | * Returns all failures for a field. |
||
67 | * |
||
68 | * @param string $field The field name |
||
69 | * |
||
70 | * @return array |
||
71 | */ |
||
72 | 9 | public function forField(string $field): array |
|
80 | |||
81 | /** |
||
82 | * Returns all failure messages for all fields. |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | 24 | public function getMessages(): array |
|
95 | |||
96 | /** |
||
97 | * Returns all failure messages for one field. |
||
98 | * |
||
99 | * @param string $field The field name |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | 27 | public function getMessagesForField(string $field): array |
|
117 | |||
118 | /** |
||
119 | * Returns a single string of all failure messages for all fields. |
||
120 | * |
||
121 | * @param string $prefix Prefix each line with this string |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | 3 | public function getMessagesAsString($prefix = ''): string |
|
137 | |||
138 | /** |
||
139 | * Returns a single string of all failure messages for one field. |
||
140 | * |
||
141 | * @param string $field The field name |
||
142 | * @param string $prefix Prefix each line with this string |
||
143 | * |
||
144 | * @return string |
||
145 | */ |
||
146 | 3 | public function getMessagesForFieldAsstring($field, $prefix = ''): string |
|
155 | } |
||
156 |
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.