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 |
||
14 | abstract class NamedColumn extends TableColumn implements NamedColumnInterface |
||
15 | { |
||
16 | /** |
||
17 | * Column field name. |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $name; |
||
21 | |||
22 | /** |
||
23 | * @var bool |
||
24 | */ |
||
25 | protected $orderable = true; |
||
26 | |||
27 | /** |
||
28 | * @param Closure|null|string $name |
||
29 | * @param null|string $label |
||
30 | */ |
||
31 | 21 | public function __construct($name, $label = null, $small = null) |
|
43 | |||
44 | /** |
||
45 | * @return string |
||
46 | 17 | */ |
|
47 | public function getName() |
||
51 | |||
52 | /** |
||
53 | * @param string $name |
||
54 | * |
||
55 | * @return $this |
||
56 | 21 | */ |
|
57 | public function setName($name) |
||
63 | |||
64 | /** |
||
65 | * @return string |
||
66 | 7 | */ |
|
67 | public function getSmall() |
||
71 | |||
72 | /** |
||
73 | * @param string $small |
||
74 | * |
||
75 | * @return $this |
||
76 | 15 | */ |
|
77 | public function setSmall($small) |
||
83 | |||
84 | 15 | /** |
|
85 | * @return mixed |
||
86 | */ |
||
87 | public function getModelValue() |
||
91 | |||
92 | 1 | /** |
|
93 | * @return mixed |
||
94 | 1 | */ |
|
95 | 1 | public function getModelSmallValue() |
|
99 | |||
100 | /** |
||
101 | * @param OrderByClauseInterface|bool $orderable |
||
102 | * @deprecated |
||
103 | * @return TableColumn |
||
104 | */ |
||
105 | public function setOrderable($orderable = true) |
||
115 | |||
116 | 7 | /** |
|
117 | * Get the instance as an array. |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | public function toArray() |
||
127 | |||
128 | /** |
||
129 | * Get column value from instance. |
||
130 | * |
||
131 | * @param Collection|Model|Closure $instance |
||
132 | * @param string $name |
||
133 | 7 | * |
|
134 | 7 | * @return mixed |
|
135 | */ |
||
136 | 7 | protected function getValueFromObject($instance, $name) |
|
186 | } |
||
187 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.