Complex classes like Compare 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 Compare, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Compare implements \Caridea\Validate\Rule |
||
30 | { |
||
31 | /** |
||
32 | * @var string The operator type |
||
33 | */ |
||
34 | private $operator; |
||
35 | /** |
||
36 | * @var mixed The comparison value |
||
37 | */ |
||
38 | private $operand; |
||
39 | |||
40 | /** |
||
41 | * Creates a new CompareRule. |
||
42 | * |
||
43 | * @param string $operator The operator type |
||
44 | * @param mixed $operand Optional comparison value |
||
45 | */ |
||
46 | 10 | protected function __construct(string $operator, $operand = null) |
|
51 | |||
52 | /** |
||
53 | * {@inheritDoc} |
||
54 | */ |
||
55 | 10 | public function apply($value, $data = []): ?array |
|
99 | |||
100 | /** |
||
101 | * Gets a field from the values. |
||
102 | * |
||
103 | * This can be overridden to access by other means (e.g. object properties, |
||
104 | * getter methods). |
||
105 | * |
||
106 | * @param mixed $values The values |
||
107 | * @param string $field The field to access |
||
108 | * @return mixed The accessed value |
||
109 | */ |
||
110 | protected function access($values, string $field) |
||
114 | |||
115 | /** |
||
116 | * Gets a rule that matches a value against another value. |
||
117 | * |
||
118 | * @param string $value The accepted value |
||
119 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
120 | */ |
||
121 | 1 | public static function eq(string $value): Compare |
|
125 | |||
126 | /** |
||
127 | * Gets a rule that matches a value against a list of accepted values. |
||
128 | * |
||
129 | * @param array $values The accepted values |
||
130 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
131 | */ |
||
132 | 1 | public static function oneOf(array $values): Compare |
|
136 | |||
137 | /** |
||
138 | * Gets a rule that requires numbers to be no greater than a limit. |
||
139 | * |
||
140 | * @param int|float $value The maximum value |
||
141 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
142 | */ |
||
143 | 1 | public static function max($value): Compare |
|
147 | |||
148 | /** |
||
149 | * Gets a rule that requires numbers to be no less than a limit. |
||
150 | * |
||
151 | * @param int|float $value The minimum value |
||
152 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
153 | */ |
||
154 | 1 | public static function min($value): Compare |
|
158 | |||
159 | /** |
||
160 | * Gets a rule that requires numbers to be in a given range. |
||
161 | * |
||
162 | * @param int|float $min The minimum value, inclusive |
||
163 | * @param int|float $max The maximum value, inclusive |
||
164 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
165 | */ |
||
166 | 1 | public static function between($min, $max): Compare |
|
171 | |||
172 | /** |
||
173 | * Gets a rule that matches integers and strings with integer values. |
||
174 | * |
||
175 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
176 | */ |
||
177 | 1 | public static function integer(): Compare |
|
181 | |||
182 | /** |
||
183 | * Gets a rule that matches positive integers |
||
184 | * |
||
185 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
186 | */ |
||
187 | 1 | public static function positiveInteger(): Compare |
|
191 | |||
192 | /** |
||
193 | * Gets a rule that matches floats and strings with float values. |
||
194 | * |
||
195 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
196 | */ |
||
197 | 1 | public static function decimal(): Compare |
|
201 | |||
202 | /** |
||
203 | * Gets a rule that matches positive floats |
||
204 | * |
||
205 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
206 | */ |
||
207 | 1 | public static function positiveDecimal(): Compare |
|
211 | |||
212 | /** |
||
213 | * Gets a rule that compares two fields for equality |
||
214 | * |
||
215 | * @param string $field The other field whose value will be compared |
||
216 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
217 | */ |
||
218 | 1 | public static function equalToField(string $field): Compare |
|
222 | } |
||
223 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.