Skip to content

Commit

Permalink
IBX-8534: Changed typehint from abstract Criterion to CriterionInterf…
Browse files Browse the repository at this point in the history
…ace (#80)

* IBX-8534: Changed typehint from abstract Criterion to CriterionInterface

* Cleaned up baseline

* fixed codestyle

* Dropped removed isPublished property

* added status property

* added more concrete ternary operator check

* added null check for query::query
  • Loading branch information
ViniTou authored Dec 10, 2024
1 parent 1485af2 commit 4248a31
Show file tree
Hide file tree
Showing 72 changed files with 294 additions and 173 deletions.
20 changes: 0 additions & 20 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,6 @@ parameters:
count: 1
path: src/lib/CoreFilter/NativeCoreFilter.php

-
message: "#^Method Ibexa\\\\Solr\\\\CoreFilter\\\\NativeCoreFilter\\:\\:getLanguageFilter\\(\\) should return Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Query\\\\Criterion but returns Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Query\\\\Criterion\\\\CustomField\\|Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Query\\\\Criterion\\\\LogicalAnd\\|Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Query\\\\Criterion\\\\LogicalNot\\|Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Query\\\\Criterion\\\\LogicalOr\\|false\\.$#"
count: 1
path: src/lib/CoreFilter/NativeCoreFilter.php

-
message: "#^Parameter \\#2 \\$languageCode of method Ibexa\\\\Solr\\\\DocumentMapper\\\\NativeDocumentMapper\\:\\:generateContentDocumentId\\(\\) expects string\\|null, int\\|string given\\.$#"
count: 1
Expand Down Expand Up @@ -785,16 +780,6 @@ parameters:
count: 1
path: src/lib/Query/Common/CriterionVisitor/Aggregate.php

-
message: "#^Access to an undefined property Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Query\\\\Criterion\\:\\:\\$criteria\\.$#"
count: 1
path: src/lib/Query/Common/CriterionVisitor/CompositeCriterion.php

-
message: "#^Cannot call method visit\\(\\) on Ibexa\\\\Contracts\\\\Solr\\\\Query\\\\CriterionVisitor\\|null\\.$#"
count: 1
path: src/lib/Query/Common/CriterionVisitor/CompositeCriterion.php

-
message: "#^Parameter \\#2 \\$array of function array_map expects array, array\\<bool\\|float\\|int\\|string\\>\\|bool\\|float\\|int\\|string given\\.$#"
count: 1
Expand Down Expand Up @@ -1080,11 +1065,6 @@ parameters:
count: 1
path: src/lib/Query/Content/CriterionVisitor/ParentLocationIdIn.php

-
message: "#^Instanceof between Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Query\\\\Criterion and Ibexa\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Query\\\\Criterion\\\\PermissionSubtree will always evaluate to false\\.$#"
count: 1
path: src/lib/Query/Content/CriterionVisitor/SubtreeIn.php

-
message: "#^Parameter \\#2 \\$array of function array_map expects array, array\\<bool\\|float\\|int\\|string\\>\\|bool\\|float\\|int\\|string given\\.$#"
count: 1
Expand Down
7 changes: 3 additions & 4 deletions src/contracts/Query/CriterionVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\CriterionInterface;

/**
* Visits the criterion tree into a Solr query.
Expand All @@ -20,16 +21,14 @@ abstract class CriterionVisitor
*
* @return bool
*/
abstract public function canVisit(Criterion $criterion);
abstract public function canVisit(CriterionInterface $criterion);

/**
* Map field value to a proper Solr representation.
*
* @param CriterionVisitor $subVisitor
*
* @return string
*/
abstract public function visit(Criterion $criterion, self $subVisitor = null);
abstract public function visit(CriterionInterface $criterion, self $subVisitor = null);

/**
* Get Solr range.
Expand Down
14 changes: 8 additions & 6 deletions src/lib/CoreFilter/NativeCoreFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
namespace Ibexa\Solr\CoreFilter;

use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\CustomField;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\LogicalAnd;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\LogicalNot;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\LogicalOr;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
use Ibexa\Solr\CoreFilter;
use Ibexa\Solr\Gateway\EndpointResolver;
use RuntimeException;

/**
* Native core filter handles:.
Expand Down Expand Up @@ -132,7 +132,7 @@ private function getCoreCriterion(
array $languageCodes,
bool $useAlwaysAvailable,
bool $excludeTranslationsFromAlwaysAvailable = true
): Criterion {
): Query\CriterionInterface {
// Handle languages if given
if (!empty($languageCodes)) {
// Get condition for prioritized languages fallback
Expand Down Expand Up @@ -164,10 +164,8 @@ private function getCoreCriterion(
* Returns criteria for prioritized languages fallback.
*
* @param string[] $languageCodes
*
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion
*/
private function getLanguageFilter(array $languageCodes)
private function getLanguageFilter(array $languageCodes): Query\CriterionInterface
{
$languageFilters = [];

Expand Down Expand Up @@ -209,6 +207,10 @@ private function getLanguageFilter(array $languageCodes)
return new LogicalAnd($languageFilters);
}

if (empty($languageFilters)) {
throw new RuntimeException('$languageFilters is empty');
}

return reset($languageFilters);
}

Expand All @@ -220,7 +222,7 @@ private function getLanguageFilter(array $languageCodes)
private function getAlwaysAvailableFilter(
array $languageCodes,
bool $excludeTranslationsFromAlwaysAvailable = true
): Criterion {
): Query\CriterionInterface {
$excludeOnField = $excludeTranslationsFromAlwaysAvailable
// Exclude all translations by given languages
? self::FIELD_LANGUAGES
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function findContent(Query $query, array $languageFilter = [])
*
* @return \Ibexa\Contracts\Core\Persistence\Content
*/
public function findSingle(Criterion $filter, array $languageFilter = [])
public function findSingle(Query\CriterionInterface $filter, array $languageFilter = [])
{
$query = new Query();
$query->filter = $filter;
Expand Down
7 changes: 4 additions & 3 deletions src/lib/Query/Common/CriterionVisitor/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Ibexa\Contracts\Core\Repository\Exceptions\NotImplementedException;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\CriterionInterface;
use Ibexa\Contracts\Solr\Query\CriterionVisitor;

/**
Expand Down Expand Up @@ -48,7 +49,7 @@ public function addVisitor(CriterionVisitor $visitor)
*
* @return bool
*/
public function canVisit(Criterion $criterion)
public function canVisit(CriterionInterface $criterion)
{
return true;
}
Expand All @@ -62,14 +63,14 @@ public function canVisit(Criterion $criterion)
*
* @return string
*/
public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null)
public function visit(CriterionInterface $criterion, CriterionVisitor $subVisitor = null)
{
foreach ($this->visitors as $visitor) {
if ($visitor->canVisit($criterion)) {
return $visitor->visit($criterion, $this);
}
}

throw new NotImplementedException('No visitor available for: ' . \get_class($criterion) . ' with operator ' . $criterion->operator);
throw new NotImplementedException('No visitor available for: ' . \get_class($criterion));
}
}
8 changes: 6 additions & 2 deletions src/lib/Query/Common/CriterionVisitor/BaseIsContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\CriterionInterface;
use Ibexa\Contracts\Solr\Query\CriterionVisitor;

/**
Expand All @@ -19,12 +20,15 @@ abstract class BaseIsContainer extends CriterionVisitor
{
abstract protected function getTargetField(): string;

public function canVisit(Criterion $criterion): bool
public function canVisit(CriterionInterface $criterion): bool
{
return $criterion instanceof Criterion\IsContainer && $criterion->operator === Operator::EQ;
}

public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null): string
/**
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\IsContainer $criterion
*/
public function visit(CriterionInterface $criterion, CriterionVisitor $subVisitor = null): string
{
$value = $criterion->value;

Expand Down
10 changes: 7 additions & 3 deletions src/lib/Query/Common/CriterionVisitor/CompositeCriterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@
namespace Ibexa\Solr\Query\Common\CriterionVisitor;

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\CriterionInterface;
use Ibexa\Contracts\Solr\Query\CriterionVisitor;

final class CompositeCriterion extends CriterionVisitor
{
public function canVisit(Criterion $criterion): bool
public function canVisit(CriterionInterface $criterion): bool
{
return $criterion instanceof Criterion\CompositeCriterion;
}

public function visit(Criterion $criterion, ?CriterionVisitor $subVisitor = null): string
/**
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\CompositeCriterion $criterion
*/
public function visit(CriterionInterface $criterion, ?CriterionVisitor $subVisitor = null): string
{
return $subVisitor->visit($criterion->criteria, $subVisitor);
return $subVisitor?->visit($criterion->criteria, $subVisitor) ?? '';
}
}
6 changes: 4 additions & 2 deletions src/lib/Query/Common/CriterionVisitor/ContentIdIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\CriterionInterface;
use Ibexa\Contracts\Solr\Query\CriterionVisitor;

/**
Expand All @@ -21,7 +22,7 @@ class ContentIdIn extends CriterionVisitor
*
* @return bool
*/
public function canVisit(Criterion $criterion)
public function canVisit(CriterionInterface $criterion)
{
return
$criterion instanceof Criterion\ContentId &&
Expand All @@ -34,11 +35,12 @@ public function canVisit(Criterion $criterion)
/**
* Map field value to a proper Solr representation.
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\ContentId $criterion
* @param \Ibexa\Contracts\Solr\Query\CriterionVisitor $subVisitor
*
* @return string
*/
public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null)
public function visit(CriterionInterface $criterion, CriterionVisitor $subVisitor = null)
{
return '(' .
implode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\CriterionInterface;
use Ibexa\Contracts\Solr\Query\CriterionVisitor;

/**
Expand All @@ -21,7 +22,7 @@ class ContentTypeGroupIdIn extends CriterionVisitor
*
* @return bool
*/
public function canVisit(Criterion $criterion)
public function canVisit(CriterionInterface $criterion)
{
return
$criterion instanceof Criterion\ContentTypeGroupId &&
Expand All @@ -34,11 +35,12 @@ public function canVisit(Criterion $criterion)
/**
* Map field value to a proper Solr representation.
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\ContentTypeGroupId $criterion
* @param \Ibexa\Contracts\Solr\Query\CriterionVisitor $subVisitor
*
* @return string
*/
public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null)
public function visit(CriterionInterface $criterion, CriterionVisitor $subVisitor = null)
{
return '(' .
implode(
Expand Down
6 changes: 4 additions & 2 deletions src/lib/Query/Common/CriterionVisitor/ContentTypeIdIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\CriterionInterface;
use Ibexa\Contracts\Solr\Query\CriterionVisitor;

/**
Expand All @@ -21,7 +22,7 @@ class ContentTypeIdIn extends CriterionVisitor
*
* @return bool
*/
public function canVisit(Criterion $criterion)
public function canVisit(CriterionInterface $criterion)
{
return
$criterion instanceof Criterion\ContentTypeId &&
Expand All @@ -34,11 +35,12 @@ public function canVisit(Criterion $criterion)
/**
* Map field value to a proper Solr representation.
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\ContentTypeId $criterion
* @param \Ibexa\Contracts\Solr\Query\CriterionVisitor $subVisitor
*
* @return string
*/
public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null)
public function visit(CriterionInterface $criterion, CriterionVisitor $subVisitor = null)
{
return '(' .
implode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\CriterionInterface;
use Ibexa\Contracts\Solr\Query\CriterionVisitor;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function __construct(Handler $contentTypeHandler, LoggerInterface $logger
*
* @return bool
*/
public function canVisit(Criterion $criterion)
public function canVisit(CriterionInterface $criterion)
{
return
$criterion instanceof Criterion\ContentTypeIdentifier
Expand All @@ -59,11 +60,12 @@ public function canVisit(Criterion $criterion)
/**
* Map field value to a proper Solr representation.
*
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\ContentTypeIdentifier $criterion
* @param \Ibexa\Contracts\Solr\Query\CriterionVisitor $subVisitor
*
* @return string
*/
public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null)
public function visit(CriterionInterface $criterion, CriterionVisitor $subVisitor = null)
{
$validIds = [];
$invalidIdentifiers = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\CriterionInterface;
use Ibexa\Contracts\Solr\Query\CriterionVisitor;

/**
Expand All @@ -21,7 +22,7 @@ class CustomFieldIn extends CriterionVisitor
*
* @return bool
*/
public function canVisit(Criterion $criterion)
public function canVisit(CriterionInterface $criterion)
{
return
$criterion instanceof Criterion\CustomField &&
Expand All @@ -36,10 +37,11 @@ public function canVisit(Criterion $criterion)
* Map field value to a proper Solr representation.
*
* @param \Ibexa\Contracts\Solr\Query\CriterionVisitor $subVisitor
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\CustomField $criterion
*
* @return string
*/
public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null)
public function visit(CriterionInterface $criterion, CriterionVisitor $subVisitor = null)
{
$queries = [];
$values = (array)$criterion->value;
Expand Down
Loading

0 comments on commit 4248a31

Please sign in to comment.