Complex classes like DomainsQuery 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 DomainsQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class DomainsQuery extends Query |
||
| 26 | { |
||
| 27 | use ArrayableTrait, PopulateModel, traits\Attributes, AuditAttributes; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var bool Whether results should be returned in the order specified by [[domain]]. |
||
| 31 | */ |
||
| 32 | public $fixedOrder = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @inheritdoc |
||
| 36 | */ |
||
| 37 | public $orderBy = 'sortOrder'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Domain[]|null The cached query result |
||
| 41 | * @see setCachedResult() |
||
| 42 | */ |
||
| 43 | private $result; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var Domain[]|null The criteria params that were set when the cached query result was set |
||
| 47 | * @see setCachedResult() |
||
| 48 | */ |
||
| 49 | private $resultCriteria; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var Domains |
||
| 53 | */ |
||
| 54 | private $field; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @inheritdoc |
||
| 58 | */ |
||
| 59 | public function __construct(Domains $domains, $config = []) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @inheritdoc |
||
| 67 | */ |
||
| 68 | public function init() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return Domains |
||
| 88 | */ |
||
| 89 | public function getField(): Domains |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @inheritdoc |
||
| 96 | */ |
||
| 97 | protected function getIndexBy() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @inheritdoc |
||
| 104 | * return static |
||
| 105 | */ |
||
| 106 | public function fixedOrder(bool $value = true) |
||
| 112 | |||
| 113 | // Query preparation/execution |
||
| 114 | // ------------------------------------------------------------------------- |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @inheritdoc |
||
| 118 | * |
||
| 119 | * @throws QueryAbortedException if it can be determined that there won’t be any results |
||
| 120 | */ |
||
| 121 | public function prepare($builder) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Applies the 'fixedOrder' and 'orderBy' params to the query being prepared. |
||
| 139 | * |
||
| 140 | * @param Connection|null $db The database connection used to generate the SQL statement. |
||
| 141 | * If this parameter is not given, the `db` application component will be used. |
||
| 142 | * |
||
| 143 | * @throws Exception if the DB connection doesn't support fixed ordering |
||
| 144 | * @throws QueryAbortedException |
||
| 145 | */ |
||
| 146 | private function applyOrderByParams(Connection $db) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param Connection $db |
||
| 162 | * @throws Exception |
||
| 163 | * @throws QueryAbortedException |
||
| 164 | */ |
||
| 165 | private function applyEmptyOrderByParams(Connection $db) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @inheritdoc |
||
| 190 | */ |
||
| 191 | public function count($q = '*', $db = null) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @inheritdoc |
||
| 203 | */ |
||
| 204 | public function all($db = null) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @inheritdoc |
||
| 216 | */ |
||
| 217 | public function one($db = null) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Executes the query and returns a single row of result at a given offset. |
||
| 236 | * |
||
| 237 | * @param int $n The offset of the row to return. If [[offset]] is set, $offset will be added to it. |
||
| 238 | * @param Connection|null $db The database connection used to generate the SQL statement. |
||
| 239 | * If this parameter is not given, the `db` application component will be used. |
||
| 240 | * |
||
| 241 | * @return Domain|array|bool The element or row of the query result. False is returned if the query |
||
| 242 | * results in nothing. |
||
| 243 | */ |
||
| 244 | public function nth(int $n, Connection $db = null) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Returns the resulting domains set by [[setCachedResult()]], if the criteria params haven’t changed since then. |
||
| 256 | * |
||
| 257 | * @return Domain[]|null The resulting domains, or null if setCachedResult() was never called or the criteria has |
||
| 258 | * changed |
||
| 259 | * @see setCachedResult() |
||
| 260 | */ |
||
| 261 | public function getCachedResult() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Sets the resulting domains. |
||
| 279 | * |
||
| 280 | * If this is called, [[all()]] will return these domains rather than initiating a new SQL query, |
||
| 281 | * as long as none of the parameters have changed since setCachedResult() was called. |
||
| 282 | * |
||
| 283 | * @param Domain[] $elements The resulting elements. |
||
| 284 | * |
||
| 285 | * @see getCachedResult() |
||
| 286 | */ |
||
| 287 | public function setCachedResult(array $elements) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Returns an array of the current criteria attribute values. |
||
| 295 | * |
||
| 296 | * @return array |
||
| 297 | */ |
||
| 298 | public function getCriteria(): array |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Returns the query's criteria attributes. |
||
| 305 | * |
||
| 306 | * @return string[] |
||
| 307 | */ |
||
| 308 | public function criteriaAttributes(): array |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param $row |
||
| 330 | * |
||
| 331 | * @return Domain |
||
| 332 | */ |
||
| 333 | function createModel($row): Domain |
||
| 337 | } |
||
| 338 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..