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) |
|
49 | |||
50 | /** |
||
51 | * MakeRules |
||
52 | * |
||
53 | * @return array |
||
54 | */ |
||
55 | 2 | protected function makeRules() |
|
59 | |||
60 | /** |
||
61 | * Make Messages |
||
62 | * |
||
63 | * @return array |
||
64 | */ |
||
65 | 2 | protected function makeMessages() |
|
69 | |||
70 | /** |
||
71 | * Make Attributes |
||
72 | * |
||
73 | * @return array |
||
74 | */ |
||
75 | 2 | protected function makeAttributes() |
|
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() |
||
109 | |||
110 | /** |
||
111 | * Set Key Name |
||
112 | * |
||
113 | * @param string $keyName |
||
114 | * |
||
115 | * @return BaseValidator |
||
116 | */ |
||
117 | public function setKeyName($keyName) |
||
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 | protected function parserValidationRules($rules, $id = null) |
||
241 | |||
242 | /** |
||
243 | * Get the extra conditions for a unique / exists rule. |
||
244 | * |
||
245 | * @param array $segments |
||
246 | * @return array |
||
247 | */ |
||
248 | protected function getExtraConditions(array $segments) |
||
260 | |||
261 | /** |
||
262 | * Replace Values Rules |
||
263 | * |
||
264 | * @param string $rule |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | 2 | protected function replaceValuesRules($rule) |
|
287 | |||
288 | /** |
||
289 | * Get the value of a given attribute. |
||
290 | * |
||
291 | * @param string $attribute |
||
292 | * |
||
293 | * @return mixed |
||
294 | */ |
||
295 | protected function getValue($attribute) |
||
301 | } |
||
302 |
It seems like you are assigning to a variable which was imported through a
use
statement 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