| Conditions | 9 |
| Paths | 30 |
| Total Lines | 64 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 50 | public function find($data) |
||
| 51 | { |
||
| 52 | if (!isset($data['element'])) { |
||
| 53 | throw new Exception('Required parameter `element` was not supplied to `craft.similar.find`.'); |
||
| 54 | } |
||
| 55 | |||
| 56 | if (!isset($data['context'])) { |
||
| 57 | throw new Exception('Required parameter `context` was not supplied to `craft.similar.find`.'); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** @var Element $element */ |
||
| 61 | $element = $data['element']; |
||
| 62 | $context = $data['context']; |
||
| 63 | $criteria = $data['criteria'] ?? []; |
||
| 64 | if (\is_object($criteria)) { |
||
| 65 | /** @var ElementQueryInterface $criteria */ |
||
| 66 | $criteria = $criteria->toArray(); |
||
| 67 | } |
||
| 68 | |||
| 69 | // Get an ElementQuery for this Element |
||
| 70 | $elementClass = \is_object($element) ? \get_class($element) : $element; |
||
| 71 | /** @var EntryQuery $query */ |
||
| 72 | $query = $this->getElementQuery($elementClass, $criteria); |
||
| 73 | |||
| 74 | // If the $query is null, just return an empty Entry |
||
| 75 | if (!$query) { // no results |
||
|
|
|||
| 76 | return new Entry(); |
||
| 77 | } |
||
| 78 | |||
| 79 | // Stash any orderBy directives from the $query for our anonymous function |
||
| 80 | $this->preOrder = $query->orderBy; |
||
| 81 | |||
| 82 | // Extract the $tagIds from the $context |
||
| 83 | if (\is_array($context)) { |
||
| 84 | $tagIds = $context; |
||
| 85 | } else { |
||
| 86 | /** @var ElementQueryInterface $context */ |
||
| 87 | $tagIds = $context->ids(); |
||
| 88 | } |
||
| 89 | |||
| 90 | // We need to modify the actual craft\db\Query after the ElementQuery has been prepared |
||
| 91 | $query->on(ElementQuery::EVENT_AFTER_PREPARE, [$this, 'eventAfterPrepareHandler']); |
||
| 92 | // Return the data as an array, and only fetch the `id` and `siteId` |
||
| 93 | $query->asArray(true); |
||
| 94 | $query->select(['elements.id', 'elements_sites.siteId']); |
||
| 95 | $query->andWhere('elements.id != :id', ['id' => $element->id]); |
||
| 96 | $query->andWhere(['in', '{{%relations}}.targetId', $tagIds]); |
||
| 97 | $query->leftJoin('{{%relations}}', 'elements.id={{%relations}}.sourceId'); |
||
| 98 | $results = $query->all(); |
||
| 99 | |||
| 100 | // Fetch the elements based on the returned `id` and `siteId` |
||
| 101 | $elements = Craft::$app->getElements(); |
||
| 102 | $models = []; |
||
| 103 | foreach ($results as $config) { |
||
| 104 | $model = $elements->getElementById($config['id'], $elementClass, $config['siteId']); |
||
| 105 | if ($model) { |
||
| 106 | // The `count` property is added dynamically by our CountBehavior behavior |
||
| 107 | /** @noinspection PhpUndefinedFieldInspection */ |
||
| 108 | $model->count = $config['count']; |
||
| 109 | $models[] = $model; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | return $models; |
||
| 114 | } |
||
| 151 |