Skip to content

Commit

Permalink
fix: Fixed an issue where an exception would be thrown if orderBy w…
Browse files Browse the repository at this point in the history
…as `null` ([#49](#49))
  • Loading branch information
khalwat committed Apr 28, 2024
1 parent a924dbd commit 92ed5d4
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/services/Similar.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
use craft\elements\db\EntryQuery;
use craft\elements\Entry;
use craft\events\CancelableEvent;

use yii\base\Exception;
use function get_class;
use function is_array;
use function is_object;

/**
* @author nystudio107.com
Expand Down Expand Up @@ -73,13 +75,13 @@ public function find($data)
$context = $data['context'];
$criteria = $data['criteria'] ?? [];

if (\is_object($criteria)) {
if (is_object($criteria)) {
/** @var ElementQueryInterface $criteria */
$criteria = $criteria->toArray();
}

// Get an ElementQuery for this Element
$elementClass = \is_object($element) ? \get_class($element) : $element;
$elementClass = is_object($element) ? get_class($element) : $element;

Check failure on line 84 in src/services/Similar.php

View workflow job for this annotation

GitHub Actions / PHPStan

Else branch is unreachable because ternary operator condition is always true.
/** @var EntryQuery $query */
$query = $this->getElementQuery($elementClass, $criteria);

Expand All @@ -89,10 +91,10 @@ public function find($data)
}

// Stash any orderBy directives from the $query for our anonymous function
$this->preOrder = $query->orderBy;
$this->preOrder = $query->orderBy ?? [];

Check failure on line 94 in src/services/Similar.php

View workflow job for this annotation

GitHub Actions / PHPStan

Property nystudio107\similar\services\Similar::$preOrder (string) does not accept array.
$this->limit = $query->limit;
// Extract the $tagIds from the $context
if (\is_array($context)) {
if (is_array($context)) {
$tagIds = $context;
} else {
/** @var ElementQueryInterface $context */
Expand Down Expand Up @@ -150,7 +152,7 @@ public function find($data)
$query = $this->getElementQuery($elementClass, $criteria);

// Make sure we fetch the elements that are similar only
$query->on(ElementQuery::EVENT_AFTER_PREPARE, function (CancelableEvent $event) use ($queryConditions) {
$query->on(ElementQuery::EVENT_AFTER_PREPARE, function(CancelableEvent $event) use ($queryConditions) {
/** @var ElementQuery $query */
$query = $event->sender;
$first = true;
Expand All @@ -176,7 +178,7 @@ public function find($data)
}

if (empty($data['criteria']['orderBy'])) {
usort($elements, function ($a, $b) {
usort($elements, function($a, $b) {
return $a->count < $b->count ? 1 : ($a->count == $b->count ? 0 : -1);

Check failure on line 182 in src/services/Similar.php

View workflow job for this annotation

GitHub Actions / PHPStan

Access to an undefined property craft\base\ElementInterface::$count.

Check failure on line 182 in src/services/Similar.php

View workflow job for this annotation

GitHub Actions / PHPStan

Access to an undefined property craft\base\ElementInterface::$count.
});
}
Expand All @@ -201,7 +203,7 @@ protected function eventAfterPrepareHandler(CancelableEvent $event)
'count' => 'DESC',
], $this->preOrder));
} elseif (is_string($this->preOrder)) {
$query->query->orderBy('count DESC, '.str_replace('`', '', $this->preOrder));
$query->query->orderBy('count DESC, ' . str_replace('`', '', $this->preOrder));
}
$query->query->groupBy(['relations.sourceId', 'elements.id', 'elements_sites.siteId']);

Expand All @@ -224,10 +226,10 @@ protected function eventAfterPrepareHandler(CancelableEvent $event)
/**
* Returns the element query based on $elementType and $criteria
*
* @var string|ElementInterface $elementType
* @var array $criteria
*
* @return ElementQueryInterface
* @var array $criteria
*
* @var string|ElementInterface $elementType
*/
protected function getElementQuery($elementType, array $criteria): ElementQueryInterface
{
Expand Down

0 comments on commit 92ed5d4

Please sign in to comment.