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 | 28 | public function init() |
|
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | 22 | 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 | 9 | private function checkTargetRelationExistence($model, $attribute) |
|
118 | { |
||
119 | 9 | $exists = false; |
|
120 | /** @var ActiveQuery $relationQuery */ |
||
121 | 9 | $relationQuery = $model->{'get' . ucfirst($this->targetRelation)}(); |
|
122 | |||
123 | 9 | if ($this->filter instanceof \Closure) { |
|
124 | 3 | call_user_func($this->filter, $relationQuery); |
|
125 | 6 | } elseif ($this->filter !== null) { |
|
126 | $relationQuery->andWhere($this->filter); |
||
127 | } |
||
128 | |||
129 | 9 | if ($this->forceMasterDb && method_exists($model::getDb(), 'useMaster')) { |
|
130 | 9 | $model::getDb()->useMaster(function() use ($relationQuery, &$exists) { |
|
131 | 9 | $exists = $relationQuery->exists(); |
|
132 | 9 | }); |
|
133 | } else { |
||
134 | 3 | $exists = $relationQuery->exists(); |
|
135 | } |
||
136 | |||
137 | |||
138 | 9 | if (!$exists) { |
|
139 | 6 | $this->addError($model, $attribute, $this->message); |
|
140 | } |
||
141 | 9 | } |
|
142 | |||
143 | /** |
||
144 | * Validates existence of the current attribute based on targetAttribute |
||
145 | * @param \yii\base\Model $model the data model to be validated |
||
146 | * @param string $attribute the name of the attribute to be validated. |
||
147 | */ |
||
148 | 13 | private function checkTargetAttributeExistence($model, $attribute) |
|
174 | |||
175 | /** |
||
176 | * Processes attributes' relations described in $targetAttribute parameter into conditions, compatible with |
||
177 | * [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
178 | * |
||
179 | * @param $targetAttribute array|string $attribute the name of the ActiveRecord attribute that should be used to |
||
180 | * validate the existence of the current attribute value. If not set, it will use the name |
||
181 | * of the attribute currently being validated. You may use an array to validate the existence |
||
182 | * of multiple columns at the same time. The array key is the name of the attribute with the value to validate, |
||
183 | * the array value is the name of the database field to search. |
||
184 | * If the key and the value are the same, you can just specify the value. |
||
185 | * @param \yii\base\Model $model the data model to be validated |
||
186 | * @param string $attribute the name of the attribute to be validated in the $model |
||
187 | * @return array conditions, compatible with [[\yii\db\Query::where()|Query::where()]] key-value format. |
||
188 | * @throws InvalidConfigException |
||
189 | */ |
||
190 | 13 | private function prepareConditions($targetAttribute, $model, $attribute) |
|
212 | |||
213 | /** |
||
214 | * @param Model $model the data model to be validated |
||
215 | * @return string Target class name |
||
216 | */ |
||
217 | 13 | private function getTargetClass($model) |
|
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | 6 | protected function validateValue($value) |
|
242 | |||
243 | /** |
||
244 | * Check whether value exists in target table |
||
245 | * |
||
246 | * @param string $targetClass |
||
247 | * @param QueryInterface $query |
||
248 | * @param mixed $value the value want to be checked |
||
249 | * @return bool |
||
250 | */ |
||
251 | 16 | private function valueExists($targetClass, $query, $value) |
|
252 | { |
||
253 | 16 | $db = $targetClass::getDb(); |
|
254 | 16 | $exists = false; |
|
255 | |||
256 | 16 | if ($this->forceMasterDb && method_exists($db, 'useMaster')) { |
|
257 | 16 | $db->useMaster(function ($db) use ($query, $value, &$exists) { |
|
|
|||
258 | 16 | $exists = $this->queryValueExists($query, $value); |
|
259 | 16 | }); |
|
260 | } else { |
||
261 | $exists = $this->queryValueExists($query, $value); |
||
262 | } |
||
263 | |||
264 | 16 | return $exists; |
|
265 | } |
||
266 | |||
267 | |||
268 | /** |
||
269 | * Run query to check if value exists |
||
270 | * |
||
271 | * @param QueryInterface $query |
||
272 | * @param mixed $value the value to be checked |
||
273 | * @return bool |
||
274 | */ |
||
275 | 16 | private function queryValueExists($query, $value) |
|
282 | |||
283 | /** |
||
284 | * Creates a query instance with the given condition. |
||
285 | * @param string $targetClass the target AR class |
||
286 | * @param mixed $condition query condition |
||
287 | * @return \yii\db\ActiveQueryInterface the query instance |
||
288 | */ |
||
289 | 16 | protected function createQuery($targetClass, $condition) |
|
301 | |||
302 | /** |
||
303 | * Returns conditions with alias. |
||
304 | * @param ActiveQuery $query |
||
305 | * @param array $conditions array of condition, keys to be modified |
||
306 | * @param null|string $alias set empty string for no apply alias. Set null for apply primary table alias |
||
307 | * @return array |
||
308 | */ |
||
309 | 13 | private function applyTableAlias($query, $conditions, $alias = null) |
|
331 | } |
||
332 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.