Skip to content

Commit

Permalink
refactor: PHPstan code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Apr 28, 2024
1 parent 6b3e993 commit afe7e48
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
12 changes: 0 additions & 12 deletions src/Similar.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,6 @@ class Similar extends Plugin
*/
public bool $hasCpSettings = false;

/**
* @inheritdoc
*/
public function __construct($id, $parent = null, array $config = [])
{
$config['components'] = [
'similar' => SimilarService::class,
];

parent::__construct($id, $parent, $config);
}

// Public Methods
// =========================================================================

Expand Down
8 changes: 5 additions & 3 deletions src/services/Similar.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function find(array $data): array|ElementInterface
throw new Exception('Required parameter `context` was not supplied to `craft.similar.find`.');
}

/** @var Element $element */
/** @var class-string|Element $element */
$element = $data['element'];
$context = $data['context'];
$criteria = $data['criteria'] ?? [];
Expand Down Expand Up @@ -151,9 +151,10 @@ public function find(array $data): array|ElementInterface

foreach ($queryConditions as $siteId => $elementIds) {
$method = $first ? 'where' : 'orWhere';
$first = false;
$query->subQuery->$method(['and', [
'elements_sites.siteId' => $siteId,
'elements.id' => $elementIds, ],
'elements.id' => $elementIds,],
]);
}
});
Expand All @@ -164,12 +165,13 @@ public function find(array $data): array|ElementInterface
// The `count` property is added dynamically by our CountBehavior behavior
$key = $element->siteId . '-' . $element->id;
if (!empty($similarCounts[$key])) {
/** @noinspection PhpUndefinedFieldInspection */
/** @phpstan-ignore-next-line */
$element->count = $similarCounts[$key];
}
}

if (empty($criteria['orderBy'])) {
/** @phpstan-ignore-next-line */
usort($elements, static fn($a, $b) => $a->count < $b->count ? 1 : ($a->count == $b->count ? 0 : -1));
}

Expand Down

0 comments on commit afe7e48

Please sign in to comment.