Complex classes like ExistValidator 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 ExistValidator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class ExistValidator extends Validator |
||
45 | { |
||
46 | /** |
||
47 | * @var string the name of the ActiveRecord class that should be used to validate the existence |
||
48 | * of the current attribute value. If not set, it will use the ActiveRecord class of the attribute being validated. |
||
49 | * @see targetAttribute |
||
50 | */ |
||
51 | public $targetClass; |
||
52 | /** |
||
53 | * @var string|array the name of the ActiveRecord attribute that should be used to |
||
54 | * validate the existence of the current attribute value. If not set, it will use the name |
||
55 | * of the attribute currently being validated. You may use an array to validate the existence |
||
56 | * of multiple columns at the same time. The array key is the name of the attribute with the value to validate, |
||
57 | * the array value is the name of the database field to search. |
||
58 | */ |
||
59 | public $targetAttribute; |
||
60 | /** |
||
61 | * @var string the name of the relation that should be used to validate the existence of the current attribute value |
||
62 | * This param overwrites $targetClass and $targetAttribute |
||
63 | * @since 2.0.14 |
||
64 | */ |
||
65 | public $targetRelation; |
||
66 | /** |
||
67 | * @var string|array|\Closure additional filter to be applied to the DB query used to check the existence of the attribute value. |
||
68 | * This can be a string or an array representing the additional query condition (refer to [[\yii\db\Query::where()]] |
||
69 | * on the format of query condition), or an anonymous function with the signature `function ($query)`, where `$query` |
||
70 | * is the [[\yii\db\Query|Query]] object that you can modify in the function. |
||
71 | */ |
||
72 | public $filter; |
||
73 | /** |
||
74 | * @var bool whether to allow array type attribute. |
||
75 | */ |
||
76 | public $allowArray = false; |
||
77 | /** |
||
78 | * @var string and|or define how target attributes are related |
||
79 | * @since 2.0.11 |
||
80 | */ |
||
81 | public $targetAttributeJunction = 'and'; |
||
82 | |||
83 | /** |
||
84 | * @var bool whether this validator is forced to always use master DB |
||
85 | * @since 2.0.14 |
||
86 | */ |
||
87 | public $forceMasterDb = true; |
||
88 | |||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 25 | public function init() |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 19 | public function validateAttribute($model, $attribute) |
|
112 | |||
113 | /** |
||
114 | * Validates existence of the current attribute based on relation name. |
||
115 | * @param \yii\db\ActiveRecord $model the data model to be validated |
||
116 | * @param string $attribute the name of the attribute to be validated. |
||
117 | */ |
||
118 | 6 | private function checkTargetRelationExistence($model, $attribute) |
|
143 | |||
144 | /** |
||
145 | * Validates existence of the current attribute based on targetAttribute. |
||
146 | * @param \yii\base\Model $model the data model to be validated |
||
147 | * @param string $attribute the name of the attribute to be validated. |
||
148 | */ |
||
149 | 13 | private function checkTargetAttributeExistence($model, $attribute) |
|
175 | |||
176 | /** |
||
177 | * Processes attributes' relations described in $targetAttribute parameter into conditions, compatible with |
||
178 | * [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
179 | * |
||
180 | * @param $targetAttribute array|string $attribute the name of the ActiveRecord attribute that should be used to |
||
181 | * validate the existence of the current attribute value. If not set, it will use the name |
||
182 | * of the attribute currently being validated. You may use an array to validate the existence |
||
183 | * of multiple columns at the same time. The array key is the name of the attribute with the value to validate, |
||
184 | * the array value is the name of the database field to search. |
||
185 | * If the key and the value are the same, you can just specify the value. |
||
186 | * @param \yii\base\Model $model the data model to be validated |
||
187 | * @param string $attribute the name of the attribute to be validated in the $model |
||
188 | * @return array conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
189 | * @throws InvalidConfigException |
||
190 | */ |
||
191 | 13 | private function prepareConditions($targetAttribute, $model, $attribute) |
|
213 | |||
214 | /** |
||
215 | * @param Model $model the data model to be validated |
||
216 | * @return string Target class name |
||
217 | */ |
||
218 | 13 | private function getTargetClass($model) |
|
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | */ |
||
226 | 6 | protected function validateValue($value) |
|
243 | |||
244 | /** |
||
245 | * Check whether value exists in target table. |
||
246 | * |
||
247 | * @param string $targetClass |
||
248 | * @param QueryInterface $query |
||
249 | * @param mixed $value the value want to be checked |
||
250 | * @return bool |
||
251 | */ |
||
252 | 16 | private function valueExists($targetClass, $query, $value) |
|
267 | |||
268 | |||
269 | /** |
||
270 | * Run query to check if value exists. |
||
271 | * |
||
272 | * @param QueryInterface $query |
||
273 | * @param mixed $value the value to be checked |
||
274 | * @return bool |
||
275 | */ |
||
276 | 16 | private function queryValueExists($query, $value) |
|
283 | |||
284 | /** |
||
285 | * Creates a query instance with the given condition. |
||
286 | * @param string $targetClass the target AR class |
||
287 | * @param mixed $condition query condition |
||
288 | * @return \yii\db\ActiveQueryInterface the query instance |
||
289 | */ |
||
290 | 16 | protected function createQuery($targetClass, $condition) |
|
302 | |||
303 | /** |
||
304 | * Returns conditions with alias. |
||
305 | * @param ActiveQuery $query |
||
306 | * @param array $conditions array of condition, keys to be modified |
||
307 | * @param null|string $alias set empty string for no apply alias. Set null for apply primary table alias |
||
308 | * @return array |
||
309 | */ |
||
310 | 13 | private function applyTableAlias($query, $conditions, $alias = null) |
|
333 | } |
||
334 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.