Complex classes like BaseValidator 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 BaseValidator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | abstract class BaseValidator extends AbstractValidator |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Validator |
||
| 17 | * |
||
| 18 | * @var \Illuminate\Validation\Factory |
||
| 19 | */ |
||
| 20 | protected $validator; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $keyName; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $messages = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $attributes = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Construct |
||
| 39 | * |
||
| 40 | * @param \Illuminate\Validation\Factory $validator |
||
| 41 | */ |
||
| 42 | 2 | public function __construct(Factory $validator) |
|
| 43 | { |
||
| 44 | 2 | $this->validator = $validator; |
|
| 45 | 2 | $this->rules = array_merge_recursive((array) $this->rules, $this->makeRules()); |
|
| 46 | 2 | $this->messages = array_merge_recursive((array) $this->messages, $this->makeMessages()); |
|
| 47 | 2 | $this->attributes = array_merge_recursive((array) $this->attributes, $this->makeAttributes()); |
|
| 48 | 2 | } |
|
| 49 | |||
| 50 | /** |
||
| 51 | * MakeRules |
||
| 52 | * |
||
| 53 | * @return array |
||
| 54 | */ |
||
| 55 | 2 | protected function makeRules() |
|
| 56 | { |
||
| 57 | 2 | return []; |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Make Messages |
||
| 62 | * |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | 2 | protected function makeMessages() |
|
| 66 | { |
||
| 67 | 2 | return []; |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Make Attributes |
||
| 72 | * |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | 2 | protected function makeAttributes() |
|
| 76 | { |
||
| 77 | 2 | return []; |
|
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get Messages |
||
| 82 | * |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | 2 | public function getMessages() |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Get Attributes |
||
| 92 | * |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | 2 | public function getAttributes() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Get Validator |
||
| 102 | * |
||
| 103 | * @return \Illuminate\Validation\Factory |
||
| 104 | */ |
||
| 105 | public function getValidator() |
||
| 106 | { |
||
| 107 | return $this->validator; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Set Key Name |
||
| 112 | * |
||
| 113 | * @param string $keyName |
||
| 114 | * |
||
| 115 | * @return BaseValidator |
||
| 116 | */ |
||
| 117 | public function setKeyName($keyName) |
||
| 118 | { |
||
| 119 | $this->keyName = $keyName; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get rule for validation by action ValidatorInterface::RULE_CREATE or ValidatorInterface::RULE_UPDATE |
||
| 124 | * |
||
| 125 | * Default rule: ValidatorInterface::RULE_CREATE |
||
| 126 | * |
||
| 127 | * @param string|null $action |
||
| 128 | * |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | 2 | public function getRules($action = null) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Pass the data and the rules to the validator |
||
| 144 | * |
||
| 145 | * @param string $action |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | 2 | public function passes($action = null) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Parser Validation Rules |
||
| 165 | * |
||
| 166 | * @param array $rules |
||
| 167 | * @param int|null $id |
||
| 168 | * |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | 2 | protected function parserValidationRules($rules, $id = null) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Get the extra conditions for a unique / exists rule. |
||
| 247 | * |
||
| 248 | * @param array $segments |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | protected function getExtraConditions(array $segments) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Replace Values Rules |
||
| 266 | * |
||
| 267 | * @param string $rule |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | 2 | protected function replaceValuesRules($rule) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Get the value of a given attribute. |
||
| 293 | * |
||
| 294 | * @param string $attribute |
||
| 295 | * |
||
| 296 | * @return mixed |
||
| 297 | */ |
||
| 298 | protected function getValue($attribute) |
||
| 304 | } |
||
| 305 |
It seems like you are assigning to a variable which was imported through a
usestatement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope