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 |
||
13 | class NumberProperty extends AbstractProperty |
||
14 | { |
||
15 | /** |
||
16 | * Minimal value. |
||
17 | * |
||
18 | * If null (default), then skip minimum validation (no constraint). |
||
19 | * |
||
20 | * @var mixed|null |
||
21 | */ |
||
22 | private $min; |
||
23 | |||
24 | /** |
||
25 | * Maximal value. |
||
26 | * |
||
27 | * If null (default), then skip maximum validation (no constrant). |
||
28 | * |
||
29 | * @var mixed|null |
||
30 | */ |
||
31 | private $max; |
||
32 | |||
33 | /** |
||
34 | * @return string |
||
35 | */ |
||
36 | public function type() |
||
40 | |||
41 | /** |
||
42 | * Set the minimal value. |
||
43 | * |
||
44 | * @param mixed|null $min The minimal value. |
||
45 | * @return self |
||
46 | */ |
||
47 | public function setMin($min) |
||
52 | |||
53 | /** |
||
54 | * Retrieves the minimal value. |
||
55 | * |
||
56 | * @return mixed|null |
||
57 | */ |
||
58 | public function getMin() |
||
59 | { |
||
60 | return $this->min; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Set the maximal value. |
||
65 | * |
||
66 | * @param mixed|null $max The maximal value. |
||
67 | * @return self |
||
68 | */ |
||
69 | public function setMax($max) |
||
74 | |||
75 | /** |
||
76 | * Retrieves the maximal value. |
||
77 | * |
||
78 | * @return mixed|null |
||
79 | */ |
||
80 | public function getMax() |
||
81 | { |
||
82 | return $this->max; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * The property's default validation methods. |
||
87 | * |
||
88 | * @return string[] |
||
89 | */ |
||
90 | public function validationMethods() |
||
99 | |||
100 | /** |
||
101 | * @return boolean |
||
102 | */ |
||
103 | public function validateRequired() |
||
113 | |||
114 | /** |
||
115 | * @return boolean |
||
116 | */ |
||
117 | View Code Duplication | public function validateMin() |
|
129 | |||
130 | /** |
||
131 | * @return boolean |
||
132 | */ |
||
133 | View Code Duplication | public function validateMax() |
|
145 | |||
146 | /** |
||
147 | * Get the SQL type (Storage format) |
||
148 | * |
||
149 | * Stored as `VARCHAR` for max_length under 255 and `TEXT` for other, longer strings |
||
150 | * |
||
151 | * @see StorablePropertyTrait::sqlType() |
||
152 | * @return string The SQL type |
||
153 | */ |
||
154 | public function sqlType() |
||
163 | |||
164 | /** |
||
165 | * @see StorablePropertyTrait::sqlPdoType() |
||
166 | * @return integer |
||
167 | */ |
||
168 | public function sqlPdoType() |
||
172 | } |
||
173 |
This method has been deprecated.