Conditions | 20 |
Paths | 450 |
Total Lines | 119 |
Code Lines | 58 |
Lines | 0 |
Ratio | 0 % |
Changes | 14 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
62 | public function find(array $data): array|ElementInterface |
||
63 | { |
||
64 | if (!isset($data['element'])) { |
||
65 | throw new Exception('Required parameter `element` was not supplied to `craft.similar.find`.'); |
||
66 | } |
||
67 | |||
68 | if (!isset($data['context'])) { |
||
69 | throw new Exception('Required parameter `context` was not supplied to `craft.similar.find`.'); |
||
70 | } |
||
71 | |||
72 | /** @var class-string|Element $element */ |
||
73 | $element = $data['element']; |
||
74 | $context = $data['context']; |
||
75 | $criteria = $data['criteria'] ?? []; |
||
76 | |||
77 | if (is_object($criteria)) { |
||
78 | /** @var ElementQueryInterface $criteria */ |
||
79 | $criteria = $criteria->toArray([], [], false); |
||
80 | } |
||
81 | |||
82 | // Get an ElementQuery for this Element |
||
83 | $elementClass = is_object($element) ? $element::class : $element; |
||
84 | /** @var EntryQuery $query */ |
||
85 | $query = $this->getElementQuery($elementClass, $criteria); |
||
86 | |||
87 | // Stash any orderBy directives from the $query for our anonymous function |
||
88 | $this->preOrder = $query->orderBy ?? []; |
||
89 | $this->limit = $query->limit; |
||
90 | // Extract the $tagIds from the $context |
||
91 | if (is_array($context)) { |
||
92 | $tagIds = $context; |
||
93 | } else { |
||
94 | /** @var ElementQueryInterface $context */ |
||
95 | $tagIds = $context->ids(); |
||
96 | } |
||
97 | |||
98 | $this->targetElements = $tagIds; |
||
99 | |||
100 | // We need to modify the actual craft\db\Query after the ElementQuery has been prepared |
||
101 | $query->on(ElementQuery::EVENT_AFTER_PREPARE, fn(CancelableEvent $event) => $this->eventAfterPrepareHandler($event)); |
||
102 | // Return the data as an array, and only fetch the `id` and `siteId` |
||
103 | $query->asArray(true); |
||
104 | $query->select(['elements.id', 'elements_sites.siteId']); |
||
105 | $query->andWhere(['not', ['elements.id' => $element->getId()]]); |
||
106 | |||
107 | // Unless site criteria is provided, force the element's site. |
||
108 | if (empty($criteria['siteId']) && empty($criteria['site'])) { |
||
109 | $query->andWhere(['elements_sites.siteId' => $element->siteId]); |
||
110 | } |
||
111 | |||
112 | $query->andWhere(['in', 'relations.targetId', $tagIds]); |
||
113 | $query->leftJoin(['relations' => Table::RELATIONS], '[[elements.id]] = [[relations.sourceId]]'); |
||
114 | |||
115 | $results = $query->all(); |
||
116 | |||
117 | // Fetch the elements based on the returned `id` and `siteId` |
||
118 | $queryConditions = []; |
||
119 | $similarCounts = []; |
||
120 | |||
121 | // Build the query conditions for a new element query. |
||
122 | // The reason we have to do it in two steps is because the `count` property is added by a behavior after element creation |
||
123 | // So if we just try to tack that on in the original query, it will throw an error on element creation |
||
124 | foreach ($results as $config) { |
||
125 | $siteId = $config['siteId']; |
||
126 | $elementId = $config['id']; |
||
127 | |||
128 | if ($elementId && $siteId) { |
||
129 | if (empty($queryConditions[$siteId])) { |
||
130 | $queryConditions[$siteId] = []; |
||
131 | } |
||
132 | |||
133 | // Write down elements per site and similar counts |
||
134 | $queryConditions[$siteId][] = $elementId; |
||
135 | $key = $siteId . '-' . $elementId; |
||
136 | $similarCounts[$key] = $config['count']; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | if (empty($results)) { |
||
141 | return []; |
||
142 | } |
||
143 | |||
144 | // Fetch all the elements in one fell swoop, including any preset eager-loaded conditions |
||
145 | $query = $this->getElementQuery($elementClass, $criteria); |
||
146 | |||
147 | // Make sure we fetch the elements that are similar only |
||
148 | $query->on(ElementQuery::EVENT_AFTER_PREPARE, function(CancelableEvent $event) use ($queryConditions): void { |
||
149 | /** @var ElementQuery $query */ |
||
150 | $query = $event->sender; |
||
151 | $first = true; |
||
152 | |||
153 | foreach ($queryConditions as $siteId => $elementIds) { |
||
154 | $method = $first ? 'where' : 'orWhere'; |
||
155 | $first = false; |
||
156 | $query->subQuery->$method(['and', [ |
||
157 | 'elements_sites.siteId' => $siteId, |
||
158 | 'elements.id' => $elementIds,], |
||
159 | ]); |
||
160 | } |
||
161 | }); |
||
162 | |||
163 | $elements = $query->all(); |
||
164 | |||
165 | /** @var Element $element */ |
||
166 | foreach ($elements as $element) { |
||
167 | // The `count` property is added dynamically by our CountBehavior behavior |
||
168 | $key = $element->siteId . '-' . $element->id; |
||
169 | if (!empty($similarCounts[$key])) { |
||
170 | /** @phpstan-ignore-next-line */ |
||
171 | $element->count = $similarCounts[$key]; |
||
172 | } |
||
173 | } |
||
174 | |||
175 | if (empty($criteria['orderBy'])) { |
||
176 | /** @phpstan-ignore-next-line */ |
||
177 | usort($elements, static fn($a, $b) => $a->count < $b->count ? 1 : ($a->count == $b->count ? 0 : -1)); |
||
178 | } |
||
179 | |||
180 | return $elements; |
||
181 | } |
||
234 |