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 | 9 | protected function __construct(string $operator, $operand = null) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Validates the provided value. |
||
| 54 | * |
||
| 55 | * @param mixed $value A value to validate against the rule |
||
| 56 | * @param array|object $data The dataset which contains this field |
||
| 57 | * @return array An array of error codes or null if validation succeeded |
||
| 58 | */ |
||
| 59 | 9 | public function apply($value, $data = []) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Gets a field from the values. |
||
| 103 | * |
||
| 104 | * This can be overridden to access by other means (e.g. object properties, |
||
| 105 | * getter methods). |
||
| 106 | * |
||
| 107 | * @param mixed $values The values |
||
| 108 | * @param string $field The field to access |
||
| 109 | * @return mixed The accessed value |
||
| 110 | */ |
||
| 111 | protected function access($values, string $field) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Gets a rule that matches a value against a list of accepted values. |
||
| 118 | * |
||
| 119 | * @param array $values The accepted values |
||
| 120 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
| 121 | */ |
||
| 122 | 1 | public static function oneOf(array $values): Compare |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Gets a rule that requires numbers to be no greater than a limit. |
||
| 129 | * |
||
| 130 | * @param int|float $value The maximum value |
||
| 131 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
| 132 | */ |
||
| 133 | 1 | public static function max($value): Compare |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Gets a rule that requires numbers to be no less than a limit. |
||
| 140 | * |
||
| 141 | * @param int|float $value The minimum value |
||
| 142 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
| 143 | */ |
||
| 144 | 1 | public static function min($value): Compare |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Gets a rule that requires numbers to be in a given range. |
||
| 151 | * |
||
| 152 | * @param int|float $min The minimum value, inclusive |
||
| 153 | * @param int|float $max The maximum value, inclusive |
||
| 154 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
| 155 | */ |
||
| 156 | 1 | public static function between($min, $max): Compare |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Gets a rule that matches integers and strings with integer values. |
||
| 164 | * |
||
| 165 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
| 166 | */ |
||
| 167 | 1 | public static function integer(): Compare |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Gets a rule that matches positive integers |
||
| 174 | * |
||
| 175 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
| 176 | */ |
||
| 177 | 1 | public static function positiveInteger(): Compare |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Gets a rule that matches floats and strings with float values. |
||
| 184 | * |
||
| 185 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
| 186 | */ |
||
| 187 | 1 | public static function decimal(): Compare |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Gets a rule that matches positive floats |
||
| 194 | * |
||
| 195 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
| 196 | */ |
||
| 197 | 1 | public static function positiveDecimal(): Compare |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Gets a rule that compares two fields for equality |
||
| 204 | * |
||
| 205 | * @param string $field The other field whose value will be compared |
||
| 206 | * @return \Caridea\Validate\Rule\Compare the created rule |
||
| 207 | */ |
||
| 208 | public static function equalToField(string $field): Compare |
||
| 212 | } |
||
| 213 |