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:
Complex classes like Field often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Field, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Field { |
||
21 | |||
22 | protected $name; |
||
23 | |||
24 | protected $type; |
||
25 | |||
26 | protected $required; |
||
27 | |||
28 | protected $nullable; |
||
29 | |||
30 | protected $default; |
||
31 | |||
32 | protected $label; |
||
33 | |||
34 | protected $rules; |
||
35 | |||
36 | public function __construct( $name, $type = Type::TEXT, array $rules = [] ) { |
||
37 | |||
38 | $this->name = $name; |
||
39 | $this->type = $type; |
||
40 | |||
41 | // default rules |
||
42 | $defaults = [ |
||
43 | 'required' => false, |
||
44 | 'nullable' => false, |
||
45 | 'default' => '', |
||
46 | 'label' => $name, |
||
47 | ]; |
||
48 | $rules += $defaults; |
||
49 | |||
50 | $this->processRules($rules); |
||
51 | |||
52 | } |
||
53 | |||
54 | public function __get( $key ) { |
||
64 | |||
65 | public function __isset( $key ) { |
||
68 | |||
69 | public function toArray() { |
||
70 | return [ |
||
71 | 'name' => $this->name, |
||
72 | 'type' => $this->type, |
||
73 | 'required' => $this->required, |
||
74 | 'nullable' => $this->nullable, |
||
75 | 'default' => $this->default, |
||
76 | 'label' => $this->label, |
||
77 | 'rules' => $this->rules, |
||
78 | ]; |
||
79 | } |
||
80 | |||
81 | public function isNumeric() { |
||
82 | return in_array($this->type, [ |
||
83 | Type::INTEGER, |
||
84 | Type::FLOAT |
||
85 | ]); |
||
86 | } |
||
87 | |||
88 | View Code Duplication | public function isTemporal() { |
|
89 | return in_array($this->type, [ |
||
90 | Type::DATETIME, |
||
91 | Type::DATE, |
||
92 | Type::TIME, |
||
93 | Type::YEAR, |
||
94 | ]); |
||
95 | } |
||
96 | |||
97 | View Code Duplication | public function isText() { |
|
98 | return in_array($this->type, [ |
||
99 | Type::TEXT, |
||
100 | Type::IP, |
||
101 | Type::EMAIL, |
||
102 | Type::URL, |
||
103 | ]); |
||
104 | } |
||
105 | |||
106 | public function isJSON() { |
||
109 | |||
110 | public function isObject() { |
||
111 | return in_array($this->type, [ |
||
112 | Type::OBJECT, |
||
113 | Type::ENTITY, |
||
114 | ]); |
||
115 | } |
||
116 | |||
117 | public function isEntity() { |
||
120 | |||
121 | public function isCollection() { |
||
124 | |||
125 | public function isUnique() { |
||
128 | |||
129 | public function cast( $v ) { |
||
130 | |||
131 | switch( $this->type ) { |
||
132 | case Type::INTEGER: |
||
133 | case Type::TIMESTAMP: |
||
134 | case Type::YEAR: |
||
159 | |||
160 | public function validate( $v ) { |
||
176 | |||
177 | /** |
||
178 | * Return the fields default value. |
||
179 | * @return mixed |
||
180 | */ |
||
181 | protected function getDefault() { |
||
188 | |||
189 | protected function validateType( $v, $type ) { |
||
228 | |||
229 | protected function validateRules( $v ) { |
||
246 | |||
247 | protected function validateRange( $v ) { |
||
258 | |||
259 | protected function validateLength( $v ) { |
||
270 | |||
271 | View Code Duplication | protected function validateRegex( $v ) { |
|
279 | |||
280 | View Code Duplication | protected function validateValues( $v ) { |
|
288 | |||
289 | protected function processRules( array $rules, $container = 'array' ) { |
||
339 | |||
340 | } |
||
341 |