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 | * @var bool whether this validator is forced to always use master DB |
||
84 | * @since 2.0.14 |
||
85 | */ |
||
86 | public $forceMasterDb = true; |
||
87 | |||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | 25 | public function init() |
|
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | 19 | public function validateAttribute($model, $attribute) |
|
111 | |||
112 | /** |
||
113 | * Validates existence of the current attribute based on relation name |
||
114 | * @param \yii\db\ActiveRecord $model the data model to be validated |
||
115 | * @param string $attribute the name of the attribute to be validated. |
||
116 | */ |
||
117 | 6 | private function checkTargetRelationExistence($model, $attribute) |
|
118 | { |
||
119 | 6 | $exists = false; |
|
120 | /** @var ActiveQuery $relationQuery */ |
||
121 | 6 | $relationQuery = $model->{'get' . ucfirst($this->targetRelation)}(); |
|
122 | |||
123 | 6 | if ($this->forceMasterDb) { |
|
124 | 6 | $model::getDb()->useMaster(function() use ($relationQuery, &$exists) { |
|
125 | 6 | $exists = $relationQuery->exists(); |
|
126 | 6 | }); |
|
127 | } else { |
||
128 | 3 | $relationQuery->exists(); |
|
129 | } |
||
130 | |||
131 | |||
132 | 6 | if (!$exists) { |
|
133 | 3 | $this->addError($model, $attribute, $this->message); |
|
134 | } |
||
135 | 6 | } |
|
136 | |||
137 | /** |
||
138 | * Validates existence of the current attribute based on targetAttribute |
||
139 | * @param \yii\base\Model $model the data model to be validated |
||
140 | * @param string $attribute the name of the attribute to be validated. |
||
141 | */ |
||
142 | 13 | private function checkTargetAttributeExistence($model, $attribute) |
|
168 | |||
169 | /** |
||
170 | * Processes attributes' relations described in $targetAttribute parameter into conditions, compatible with |
||
171 | * [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
172 | * |
||
173 | * @param $targetAttribute array|string $attribute the name of the ActiveRecord attribute that should be used to |
||
174 | * validate the existence of the current attribute value. If not set, it will use the name |
||
175 | * of the attribute currently being validated. You may use an array to validate the existence |
||
176 | * of multiple columns at the same time. The array key is the name of the attribute with the value to validate, |
||
177 | * the array value is the name of the database field to search. |
||
178 | * If the key and the value are the same, you can just specify the value. |
||
179 | * @param \yii\base\Model $model the data model to be validated |
||
180 | * @param string $attribute the name of the attribute to be validated in the $model |
||
181 | * @return array conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
182 | * @throws InvalidConfigException |
||
183 | */ |
||
184 | 13 | private function prepareConditions($targetAttribute, $model, $attribute) |
|
206 | |||
207 | /** |
||
208 | * @param Model $model the data model to be validated |
||
209 | * @return string Target class name |
||
210 | */ |
||
211 | 13 | private function getTargetClass($model) |
|
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | 6 | protected function validateValue($value) |
|
236 | |||
237 | /** |
||
238 | * Check whether value exists in target table |
||
239 | * |
||
240 | * @param string $targetClass |
||
241 | * @param QueryInterface $query |
||
242 | * @param mixed $value the value want to be checked |
||
243 | * @return bool |
||
244 | */ |
||
245 | 16 | private function valueExists($targetClass, $query, $value) |
|
260 | |||
261 | |||
262 | /** |
||
263 | * Run query to check if value exists |
||
264 | * |
||
265 | * @param QueryInterface $query |
||
266 | * @param mixed $value the value to be checked |
||
267 | * @return bool |
||
268 | */ |
||
269 | 16 | private function queryValueExists($query, $value) |
|
276 | |||
277 | /** |
||
278 | * Creates a query instance with the given condition. |
||
279 | * @param string $targetClass the target AR class |
||
280 | * @param mixed $condition query condition |
||
281 | * @return \yii\db\ActiveQueryInterface the query instance |
||
282 | */ |
||
283 | 16 | protected function createQuery($targetClass, $condition) |
|
295 | |||
296 | /** |
||
297 | * Returns conditions with alias. |
||
298 | * @param ActiveQuery $query |
||
299 | * @param array $conditions array of condition, keys to be modified |
||
300 | * @param null|string $alias set empty string for no apply alias. Set null for apply primary table alias |
||
301 | * @return array |
||
302 | */ |
||
303 | 13 | private function applyTableAlias($query, $conditions, $alias = null) |
|
325 | } |
||
326 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.