diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index 93d54dcc..39bfaaf8 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -8,8 +8,72 @@ on: - '[0-9]+.x' jobs: - php: - name: 'Run tests with php ${{ matrix.php-version }} with symfony ${{ matrix.symfony-version}}' + code-style: + runs-on: ubuntu-latest + + steps: + - name: Checkout project + uses: actions/checkout@v3 + + - name: Install and configure PHP + uses: shivammathur/setup-php@v2 + with: + coverage: none + php-version: 8.1 + + - name: Get composer cache directory + id: composer-cache-dir + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Cache dependencies + uses: actions/cache@v2 + id: composer-cache + with: + path: ${{ steps.composer-cache-dir.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }} + restore-keys: | + ${{ runner.os }}-composer- + + - name: Install dependencies + run: composer install --no-interaction --prefer-dist + + - name: Fix code style + run: vendor/bin/phpcs -p --standard=PSR2 --ignore=vendor/ ./ + + phpstan: + runs-on: ubuntu-latest + + steps: + - name: Checkout project + uses: actions/checkout@v3 + + - name: Install and configure PHP + uses: shivammathur/setup-php@v2 + with: + coverage: none + php-version: 8.1 + + - name: Get composer cache directory + id: composer-cache-dir + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Cache dependencies + uses: actions/cache@v2 + id: composer-cache + with: + path: ${{ steps.composer-cache-dir.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }} + restore-keys: | + ${{ runner.os }}-composer- + + - name: Install dependencies + run: composer install --no-interaction --prefer-dist + + - name: Fix code style + run: vendor/bin/phpstan analyse -l3 src/ + + unit-test: + name: 'Run unit tests with php ${{ matrix.php-version }} with symfony ${{ matrix.symfony-version}}' runs-on: ubuntu-latest strategy: @@ -17,19 +81,15 @@ jobs: matrix: include: - php-version: '8.0' - lint: true symfony-version: '5.4.*' - php-version: '8.1' - lint: false symfony-version: '5.4.*' - php-version: '8.1' - lint: false symfony-version: '6.0.*' - php-version: '8.1' - lint: false symfony-version: '6.1.*' steps: @@ -61,14 +121,51 @@ jobs: composer require --no-update symfony/serializer:${{ matrix.symfony-version }} composer install --no-interaction --prefer-dist - - name: Fix code style - if: ${{ matrix.lint }} - run: vendor/bin/phpcs -p --standard=PSR2 --ignore=vendor/ ./ - - name: Setup OpenSearch uses: ankane/setup-opensearch@v1 with: opensearch-version: '1.2' - name: Run tests - run: vendor/bin/phpunit --coverage-clover=coverage.xml + run: vendor/bin/phpunit --testsuite=unit --coverage-clover=coverage.xml + + functional-test: + name: 'Run functional tests' + runs-on: ubuntu-latest + + steps: + - name: Checkout project + uses: actions/checkout@v3 + + - name: Install and configure PHP + uses: shivammathur/setup-php@v2 + with: + coverage: none + php-version: 8.1 + + - name: Get composer cache directory + id: composer-cache-dir + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Cache dependencies + uses: actions/cache@v2 + id: composer-cache + with: + path: ${{ steps.composer-cache-dir.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }} + restore-keys: | + ${{ runner.os }}-composer- + + - name: Install dependencies + run: | + composer validate --strict + composer require --no-update symfony/serializer:${{ matrix.symfony-version }} + composer install --no-interaction --prefer-dist + + - name: Setup OpenSearch + uses: ankane/setup-opensearch@v1 + with: + opensearch-version: '1.2' + + - name: Run tests + run: vendor/bin/phpunit --testsuite=functional \ No newline at end of file diff --git a/composer.json b/composer.json index 9e6ccf54..ccb3d4c9 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ }, "require-dev": { "phpunit/phpunit": "^9.5", - "squizlabs/php_codesniffer": "^3.0" + "squizlabs/php_codesniffer": "^3.0", + "phpstan/phpstan": "^1.8" }, "autoload": { "psr-4": { diff --git a/src/Aggregation/AbstractAggregation.php b/src/Aggregation/AbstractAggregation.php index 99a1e8ce..6f5fc041 100644 --- a/src/Aggregation/AbstractAggregation.php +++ b/src/Aggregation/AbstractAggregation.php @@ -12,6 +12,7 @@ namespace OpenSearchDSL\Aggregation; use OpenSearchDSL\BuilderBag; +use OpenSearchDSL\BuilderInterface; use OpenSearchDSL\NameAwareTrait; use OpenSearchDSL\NamedBuilderInterface; use OpenSearchDSL\ParametersTrait; @@ -63,7 +64,7 @@ public function addAggregation(AbstractAggregation $abstractAggregation): static /** * Returns all sub aggregations. * - * @return BuilderBag[]|NamedBuilderInterface[] + * @return BuilderInterface[] */ public function getAggregations(): array { @@ -76,11 +77,8 @@ public function getAggregations(): array /** * Returns sub aggregation. - * @param string $name Aggregation name to return. - * - * @return AbstractAggregation|NamedBuilderInterface|null */ - public function getAggregation($name) + public function getAggregation(string $name): ?BuilderInterface { if ($this->aggregations && $this->aggregations->has($name)) { return $this->aggregations->get($name); @@ -112,7 +110,7 @@ public function toArray(): array * * @return array */ - protected function collectNestedAggregations() + protected function collectNestedAggregations(): array { $result = []; /** @var AbstractAggregation $aggregation */ diff --git a/src/Aggregation/Metric/TopHitsAggregation.php b/src/Aggregation/Metric/TopHitsAggregation.php index 907a90f8..4c016ac8 100644 --- a/src/Aggregation/Metric/TopHitsAggregation.php +++ b/src/Aggregation/Metric/TopHitsAggregation.php @@ -23,18 +23,17 @@ */ class TopHitsAggregation extends AbstractAggregation { - public BuilderInterface $sort; use MetricTrait; /** - * @var int Number of top matching hits to return per bucket. + * @var int|null Number of top matching hits to return per bucket. */ - private int $size; + private ?int $size; /** - * @var int The offset from the first result you want to fetch. + * @var int|null The offset from the first result you want to fetch. */ - private int $from; + private ?int $from; /** * @var BuilderInterface[] How the top matching hits should be sorted. @@ -42,14 +41,12 @@ class TopHitsAggregation extends AbstractAggregation private array $sorts = []; /** - * Constructor for top hits. - * * @param string $name Aggregation name. * @param null|int $size Number of top matching hits to return per bucket. * @param null|int $from The offset from the first result you want to fetch. * @param null|BuilderInterface $sort How the top matching hits should be sorted. */ - public function __construct($name, $size = null, $from = null, $sort = null) + public function __construct(string $name, ?int $size = null, ?int $from = null, ?BuilderInterface $sort = null) { parent::__construct($name); $this->setFrom($from); @@ -57,22 +54,12 @@ public function __construct($name, $size = null, $from = null, $sort = null) $this->addSort($sort); } - /** - * Return from. - * - * @return int - */ - public function getFrom() + public function getFrom(): ?int { return $this->from; } - /** - * @param int $from - * - * @return $this - */ - public function setFrom($from) + public function setFrom(?int $from): self { $this->from = $from; @@ -82,51 +69,36 @@ public function setFrom($from) /** * @return BuilderInterface[] */ - public function getSorts() + public function getSorts(): array { return $this->sorts; } /** * @param BuilderInterface[] $sorts - * - * @return $this */ - public function setSorts(array $sorts) + public function setSorts(array $sorts): self { $this->sorts = $sorts; return $this; } - /** - * Add sort. - * - * @param BuilderInterface $sort - */ - public function addSort($sort) + public function addSort(BuilderInterface $sort): self { $this->sorts[] = $sort; + + return $this; } - /** - * @param int $size - * - * @return $this - */ - public function setSize($size) + public function setSize(?int $size): self { $this->size = $size; return $this; } - /** - * Return size. - * - * @return int - */ - public function getSize() + public function getSize(): ?int { return $this->size; } @@ -139,10 +111,7 @@ public function getType(): string return 'top_hits'; } - /** - * {@inheritdoc} - */ - public function getArray(): array + public function getArray(): array|stdClass { $sortsOutput = []; $addedSorts = array_filter($this->getSorts()); @@ -154,43 +123,12 @@ public function getArray(): array $sortsOutput = null; } - $output = array_filter( - [ - 'sort' => $sortsOutput, - 'size' => $this->getSize(), - 'from' => $this->getFrom(), - ], - fn($val) => $val || is_array($val) || ($val || is_numeric($val)) - ); + $output = array_filter([ + 'sort' => $sortsOutput, + 'size' => $this->getSize(), + 'from' => $this->getFrom(), + ]); return empty($output) ? new stdClass() : $output; } - - /** - * @deprecated sorts now is a container, use `getSorts()`instead. - * Return sort. - * - * @return BuilderInterface - */ - public function getSort() - { - if (isset($this->sorts[0])) { - return $this->sorts[0]; - } - - return null; - } - - /** - * @deprecated sorts now is a container, use `addSort()`instead. - * - * - * @return $this - */ - public function setSort(BuilderInterface $sort) - { - $this->sort = $sort; - - return $this; - } } diff --git a/src/ParametersTrait.php b/src/ParametersTrait.php index 9040e8a7..48a54f75 100644 --- a/src/ParametersTrait.php +++ b/src/ParametersTrait.php @@ -18,6 +18,9 @@ */ trait ParametersTrait { + /** + * @var array + */ private array $parameters = []; public function hasParameter(string $name): bool @@ -39,6 +42,9 @@ public function getParameter(string $name): array|string|int|float|bool|stdClass return $this->parameters[$name]; } + /** + * @return array + */ public function getParameters(): array { return $this->parameters; diff --git a/src/Query/Compound/BoolQuery.php b/src/Query/Compound/BoolQuery.php index 1203882d..8362ff0e 100644 --- a/src/Query/Compound/BoolQuery.php +++ b/src/Query/Compound/BoolQuery.php @@ -30,12 +30,15 @@ class BoolQuery implements BuilderInterface public const SHOULD = 'should'; public const FILTER = 'filter'; + /** + * @var array> + */ private array $container = []; /** * Constructor to prepare container. * - * @param array $container + * @param array $container */ public function __construct(array $container = []) { @@ -51,11 +54,9 @@ public function __construct(array $container = []) /** * Returns the query instances (by bool type). * - * @param string|null $boolType - * - * @return array + * @return array */ - public function getQueries($boolType = null) + public function getQueries(?string $boolType = null): array { if ($boolType === null) { $queries = []; @@ -79,19 +80,19 @@ public function getQueries($boolType = null) * * @param BuilderInterface $query Query add to the bool. * @param string $type Bool type. Example: must, must_not, should. - * @param string $key Key that indicates a builder id. + * @param string|null $key Key that indicates a builder id. * * @return string Key of added builder. * * @throws UnexpectedValueException */ - public function add(BuilderInterface $query, $type = self::MUST, $key = null) + public function add(BuilderInterface $query, string $type = self::MUST, ?string $key = null): string { if (!in_array($type, [self::MUST, self::MUST_NOT, self::SHOULD, self::FILTER])) { throw new UnexpectedValueException(sprintf('The bool operator %s is not supported', $type)); } - if (!$key) { + if ($key === null) { $key = bin2hex(random_bytes(30)); } @@ -115,7 +116,6 @@ public function toArray(): array $output = []; foreach ($this->container as $boolType => $builders) { - /** @var BuilderInterface $builder */ foreach ($builders as $builder) { $output[$boolType][] = $builder->toArray(); } @@ -130,9 +130,6 @@ public function toArray(): array return [$this->getType() => $output]; } - /** - * {@inheritdoc} - */ public function getType(): string { return 'bool'; diff --git a/src/Query/Span/SpanContainingQuery.php b/src/Query/Span/SpanContainingQuery.php index 162fa07c..1b806ba6 100644 --- a/src/Query/Span/SpanContainingQuery.php +++ b/src/Query/Span/SpanContainingQuery.php @@ -22,62 +22,22 @@ class SpanContainingQuery implements SpanQueryInterface { use ParametersTrait; - /** - * @param SpanQueryInterface - */ - private $little; - - /** - * @param SpanQueryInterface - */ - private $big; - - /** - * @param SpanQueryInterface $little - * @param SpanQueryInterface $big - */ - public function __construct(SpanQueryInterface $little, SpanQueryInterface $big) - { - $this->setLittle($little); - $this->setBig($big); + public function __construct( + private SpanQueryInterface $little, + private SpanQueryInterface $big + ) { } - /** - * @return SpanQueryInterface - */ - public function getLittle() + public function getLittle(): SpanQueryInterface { return $this->little; } - /** - * @return $this - */ - public function setLittle(SpanQueryInterface $little) - { - $this->little = $little; - - return $this; - } - - /** - * @return SpanQueryInterface - */ - public function getBig() + public function getBig(): SpanQueryInterface { return $this->big; } - /** - * @return $this - */ - public function setBig(SpanQueryInterface $big) - { - $this->big = $big; - - return $this; - } - /** * {@inheritdoc} */ diff --git a/src/Search.php b/src/Search.php index b9d00705..cb09912d 100644 --- a/src/Search.php +++ b/src/Search.php @@ -28,6 +28,7 @@ use OpenSearchDSL\SearchEndpoint\SuggestEndpoint; use OpenSearchDSL\Serializer\Normalizer\CustomReferencedNormalizer; use OpenSearchDSL\Serializer\OrderedSerializer; +use stdClass; use Symfony\Component\Serializer\Normalizer\CustomNormalizer; /** @@ -121,6 +122,8 @@ class Search * URI parameters alongside Request body search. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html + * + * @var array */ private array $uriParams = []; @@ -140,29 +143,20 @@ class Search */ private array $endpoints = []; - /** - * Constructor to initialize static properties - */ public function __construct() { $this->initializeSerializer(); } - /** - * Wakeup method to initialize static properties - */ public function __wakeup() { $this->initializeSerializer(); } - /** - * Initializes the serializer - */ - private function initializeSerializer() + private function initializeSerializer(): void { - if (static::$serializer === null) { - static::$serializer = new OrderedSerializer( + if (self::$serializer === null) { + self::$serializer = new OrderedSerializer( [ new CustomReferencedNormalizer(), new CustomNormalizer(), @@ -176,19 +170,12 @@ private function initializeSerializer() * * @param string $type Endpoint type. */ - public function destroyEndpoint($type) + public function destroyEndpoint(string $type): void { unset($this->endpoints[$type]); } - /** - * Adds query to the search. - * - * @param string $boolType - * @param string $key - * @return $this - */ - public function addQuery(BuilderInterface $query, $boolType = BoolQuery::MUST, $key = null) + public function addQuery(BuilderInterface $query, string $boolType = BoolQuery::MUST, ?string $key = null): self { $endpoint = $this->getEndpoint(QueryEndpoint::NAME); $endpoint->addToBool($query, $boolType, $key); @@ -196,28 +183,15 @@ public function addQuery(BuilderInterface $query, $boolType = BoolQuery::MUST, $ return $this; } - /** - * Returns endpoint instance. - * - * @param string $type Endpoint type. - * - * @return SearchEndpointInterface - */ - private function getEndpoint($type) + private function getEndpoint(string $type): SearchEndpointInterface { - if (!array_key_exists($type, $this->endpoints)) { - $this->endpoints[$type] = SearchEndpointFactory::get($type); - } - - return $this->endpoints[$type]; + return $this->endpoints[$type] ??= SearchEndpointFactory::get($type); } /** * Returns queries inside BoolQuery instance. - * - * @return BoolQuery */ - public function getQueries() + public function getQueries(): ?BoolQuery { $endpoint = $this->getEndpoint(QueryEndpoint::NAME); @@ -226,11 +200,8 @@ public function getQueries() /** * Sets query endpoint parameters. - * - * - * @return $this */ - public function setQueryParameters(array $parameters) + public function setQueryParameters(array $parameters): self { $this->setEndpointParameters(QueryEndpoint::NAME, $parameters); @@ -240,11 +211,9 @@ public function setQueryParameters(array $parameters) /** * Sets parameters to the endpoint. * - * @param string $endpointName - * - * @return $this + * @param array $parameters */ - public function setEndpointParameters($endpointName, array $parameters) + public function setEndpointParameters(string $endpointName, array $parameters): self { /** @var AbstractSearchEndpoint $endpoint */ $endpoint = $this->getEndpoint($endpointName); @@ -255,18 +224,12 @@ public function setEndpointParameters($endpointName, array $parameters) /** * Adds a post filter to search. - * - * @param BuilderInterface $filter Filter. - * @param string $boolType Example boolType values: - * - must - * - must_not - * - should. - * @param string $key - * - * @return $this. */ - public function addPostFilter(BuilderInterface $filter, $boolType = BoolQuery::MUST, $key = null) - { + public function addPostFilter( + BuilderInterface $filter, + string $boolType = BoolQuery::MUST, + ?string $key = null + ): self { $this ->getEndpoint(PostFilterEndpoint::NAME) ->addToBool($filter, $boolType, $key); @@ -276,10 +239,8 @@ public function addPostFilter(BuilderInterface $filter, $boolType = BoolQuery::M /** * Returns queries inside BoolFilter instance. - * - * @return BoolQuery */ - public function getPostFilters() + public function getPostFilters(): ?BoolQuery { $endpoint = $this->getEndpoint(PostFilterEndpoint::NAME); @@ -288,11 +249,8 @@ public function getPostFilters() /** * Sets post filter endpoint parameters. - * - * - * @return $this */ - public function setPostFilterParameters(array $parameters) + public function setPostFilterParameters(array $parameters): self { $this->setEndpointParameters(PostFilterEndpoint::NAME, $parameters); @@ -301,11 +259,8 @@ public function setPostFilterParameters(array $parameters) /** * Adds aggregation into search. - * - * - * @return $this */ - public function addAggregation(AbstractAggregation $aggregation) + public function addAggregation(AbstractAggregation $aggregation): self { $this->getEndpoint(AggregationsEndpoint::NAME)->add($aggregation, $aggregation->getName()); @@ -317,18 +272,15 @@ public function addAggregation(AbstractAggregation $aggregation) * * @return BuilderInterface[] */ - public function getAggregations() + public function getAggregations(): array { return $this->getEndpoint(AggregationsEndpoint::NAME)->getAll(); } /** * Adds inner hit into search. - * - * - * @return $this */ - public function addInnerHit(NestedInnerHit $innerHit) + public function addInnerHit(NestedInnerHit $innerHit): self { $this->getEndpoint(InnerHitsEndpoint::NAME)->add($innerHit, $innerHit->getName()); @@ -338,20 +290,17 @@ public function addInnerHit(NestedInnerHit $innerHit) /** * Returns all inner hits. * - * @return BuilderInterface[] + * @return array */ - public function getInnerHits() + public function getInnerHits(): array { return $this->getEndpoint(InnerHitsEndpoint::NAME)->getAll(); } /** * Adds sort to search. - * - * - * @return $this */ - public function addSort(BuilderInterface $sort) + public function addSort(BuilderInterface $sort): self { $this->getEndpoint(SortEndpoint::NAME)->add($sort); @@ -363,19 +312,15 @@ public function addSort(BuilderInterface $sort) * * @return BuilderInterface[] */ - public function getSorts() + public function getSorts(): array { return $this->getEndpoint(SortEndpoint::NAME)->getAll(); } /** * Allows to highlight search results on one or more fields. - * - * @param Highlight $highlight - * - * @return $this. */ - public function addHighlight($highlight) + public function addHighlight(Highlight $highlight): self { $this->getEndpoint(HighlightEndpoint::NAME)->add($highlight); @@ -384,10 +329,8 @@ public function addHighlight($highlight) /** * Returns highlight builder. - * - * @return BuilderInterface */ - public function getHighlights() + public function getHighlights(): ?BuilderInterface { /** @var HighlightEndpoint $highlightEndpoint */ $highlightEndpoint = $this->getEndpoint(HighlightEndpoint::NAME); @@ -397,12 +340,8 @@ public function getHighlights() /** * Adds suggest into search. - * - * @param BuilderInterface $suggest - * - * @return $this */ - public function addSuggest(NamedBuilderInterface $suggest) + public function addSuggest(NamedBuilderInterface $suggest): self { $this->getEndpoint(SuggestEndpoint::NAME)->add($suggest, $suggest->getName()); @@ -414,258 +353,161 @@ public function addSuggest(NamedBuilderInterface $suggest) * * @return BuilderInterface[] */ - public function getSuggests() + public function getSuggests(): array { return $this->getEndpoint(SuggestEndpoint::NAME)->getAll(); } - /** - * @return null|int - */ - public function getFrom() + public function getFrom(): ?int { return $this->from; } - /** - * @param null|int $from - * - * @return $this - */ - public function setFrom($from) + public function setFrom(?int $from): self { $this->from = $from; return $this; } - /** - * @return bool - */ - public function isTrackTotalHits() + public function isTrackTotalHits(): ?bool { return $this->trackTotalHits; } - /** - * @return $this - */ - public function setTrackTotalHits(bool $trackTotalHits) + public function setTrackTotalHits(bool $trackTotalHits): self { $this->trackTotalHits = $trackTotalHits; return $this; } - /** - * @return null|int - */ - public function getSize() + public function getSize(): ?int { return $this->size; } - /** - * @param null|int $size - * - * @return $this - */ - public function setSize($size) + public function setSize(?int $size): self { $this->size = $size; return $this; } - public function isSource(): array|bool|string + public function isSource(): array|bool|string|null { return $this->source; } - /** - * @return $this - */ - public function setSource(array|bool|string $source) + public function setSource(array|bool|string $source): self { $this->source = $source; return $this; } - /** - * @return array - */ - public function getStoredFields() + public function getStoredFields(): ?array { return $this->storedFields; } - /** - * @param array $storedFields - * - * @return $this - */ - public function setStoredFields($storedFields) + public function setStoredFields(array $storedFields): self { $this->storedFields = $storedFields; return $this; } - /** - * @return array - */ - public function getScriptFields() + public function getScriptFields(): ?array { return $this->scriptFields; } - /** - * @param array $scriptFields - * - * @return $this - */ - public function setScriptFields($scriptFields) + public function setScriptFields(array $scriptFields): self { $this->scriptFields = $scriptFields; return $this; } - /** - * @return array - */ - public function getDocValueFields() + public function getDocValueFields(): ?array { return $this->docValueFields; } - /** - * @param array $docValueFields - * - * @return $this - */ - public function setDocValueFields($docValueFields) + public function setDocValueFields(array $docValueFields): self { $this->docValueFields = $docValueFields; return $this; } - /** - * @return bool - */ - public function isExplain() + public function isExplain(): ?bool { return $this->explain; } - /** - * @param bool $explain - * - * @return $this - */ - public function setExplain($explain) + public function setExplain(bool $explain): self { $this->explain = $explain; return $this; } - /** - * @return bool - */ - public function isVersion() + public function isVersion(): ?bool { return $this->version; } - /** - * @param bool $version - * - * @return $this - */ - public function setVersion($version) + public function setVersion(bool $version): self { $this->version = $version; return $this; } - /** - * @return array - */ - public function getIndicesBoost() + public function getIndicesBoost(): ?array { return $this->indicesBoost; } - /** - * @param array $indicesBoost - * - * @return $this - */ - public function setIndicesBoost($indicesBoost) + public function setIndicesBoost(array $indicesBoost): self { $this->indicesBoost = $indicesBoost; return $this; } - /** - * @return int - */ - public function getMinScore() + public function getMinScore(): ?int { return $this->minScore; } - /** - * @param int $minScore - * - * @return $this - */ - public function setMinScore($minScore) + public function setMinScore(int $minScore): self { $this->minScore = $minScore; return $this; } - /** - * @return array - */ - public function getSearchAfter() + public function getSearchAfter(): ?array { return $this->searchAfter; } - /** - * @param array $searchAfter - * - * @return $this - */ - public function setSearchAfter($searchAfter) + public function setSearchAfter(array $searchAfter): self { $this->searchAfter = $searchAfter; return $this; } - /** - * @return string - */ - public function getScroll() + public function getScroll(): ?string { return $this->scroll; } - /** - * @param string $scroll - * - * @return $this - */ - public function setScroll($scroll = '5m') + public function setScroll(string $scroll = '5m'): self { $this->scroll = $scroll; @@ -674,12 +516,7 @@ public function setScroll($scroll = '5m') return $this; } - /** - * @param string $name - * - * @return $this - */ - public function addUriParam($name, string|array|bool $value) + public function addUriParam(string $name, string|array|bool $value): self { if (in_array($name, [ 'q', @@ -719,19 +556,16 @@ public function addUriParam($name, string|array|bool $value) /** * Returns query url parameters. * - * @return array + * @return array */ - public function getUriParams() + public function getUriParams(): array { return $this->uriParams; } - /** - * {@inheritdoc} - */ public function toArray(): array { - $output = array_filter(static::$serializer->normalize($this->endpoints)); + $output = array_filter(self::$serializer->normalize($this->endpoints)); $params = [ 'from' => 'from', diff --git a/src/SearchEndpoint/AbstractSearchEndpoint.php b/src/SearchEndpoint/AbstractSearchEndpoint.php index cf563a78..7f15ee4c 100644 --- a/src/SearchEndpoint/AbstractSearchEndpoint.php +++ b/src/SearchEndpoint/AbstractSearchEndpoint.php @@ -14,6 +14,7 @@ use BadFunctionCallException; use OpenSearchDSL\BuilderInterface; use OpenSearchDSL\ParametersTrait; +use OpenSearchDSL\Query\Compound\BoolQuery; use OpenSearchDSL\Serializer\Normalizer\AbstractNormalizable; use OverflowException; @@ -25,14 +26,14 @@ abstract class AbstractSearchEndpoint extends AbstractNormalizable implements Se use ParametersTrait; /** - * @var BuilderInterface[] + * @var array */ private array $container = []; /** * {@inheritdoc} */ - public function add(BuilderInterface $builder, $key = null) + public function add(BuilderInterface $builder, $key = null): string { if (array_key_exists($key, $this->container)) { throw new OverflowException(sprintf('Builder with %s name for endpoint has already been added!', $key)); @@ -47,18 +48,12 @@ public function add(BuilderInterface $builder, $key = null) return $key; } - /** - * {@inheritdoc} - */ - public function addToBool(BuilderInterface $builder, $boolType = null, $key = null) + public function addToBool(BuilderInterface $builder, string $boolType = BoolQuery::MUST, ?string $key = null): string { throw new BadFunctionCallException(sprintf("Endpoint %s doesn't support bool statements", static::NAME)); } - /** - * {@inheritdoc} - */ - public function remove($key) + public function remove(string $key): static { if ($this->has($key)) { unset($this->container[$key]); @@ -69,40 +64,23 @@ public function remove($key) /** * Checks if builder with specific key exists. - * - * @param string $key Key to check if it exists in container. - * - * @return bool */ - public function has($key) + public function has(string $key): bool { return array_key_exists($key, $this->container); } - /** - * {@inheritdoc} - */ - public function get($key) + public function get(string $key): ?BuilderInterface { - if ($this->has($key)) { - return $this->container[$key]; - } - - return null; + return $this->container[$key] ?? null; } - /** - * {@inheritdoc} - */ - public function getAll($boolType = null) + public function getAll(?string $boolType = null): array { return $this->container; } - /** - * {@inheritdoc} - */ - public function getBool() + public function getBool(): ?BoolQuery { throw new BadFunctionCallException(sprintf("Endpoint %s doesn't support bool statements", static::NAME)); } diff --git a/src/SearchEndpoint/HighlightEndpoint.php b/src/SearchEndpoint/HighlightEndpoint.php index b5e33f58..9e5ef910 100644 --- a/src/SearchEndpoint/HighlightEndpoint.php +++ b/src/SearchEndpoint/HighlightEndpoint.php @@ -44,10 +44,7 @@ public function normalize(NormalizerInterface $normalizer, $format = null, array return false; } - /** - * {@inheritdoc} - */ - public function add(BuilderInterface $builder, $key = null) + public function add(BuilderInterface $builder, ?string $key = null): string { if ($this->highlight !== null) { throw new OverflowException('Only one highlight can be set'); @@ -55,20 +52,16 @@ public function add(BuilderInterface $builder, $key = null) $this->key = $key; $this->highlight = $builder; + + return $this->key; } - /** - * {@inheritdoc} - */ - public function getAll($boolType = null) + public function getAll(?string $boolType = null): array { return [$this->key => $this->highlight]; } - /** - * @return BuilderInterface - */ - public function getHighlight() + public function getHighlight(): ?BuilderInterface { return $this->highlight; } diff --git a/src/SearchEndpoint/PostFilterEndpoint.php b/src/SearchEndpoint/PostFilterEndpoint.php index 85e877b5..5060ecf7 100644 --- a/src/SearchEndpoint/PostFilterEndpoint.php +++ b/src/SearchEndpoint/PostFilterEndpoint.php @@ -38,7 +38,7 @@ public function normalize(NormalizerInterface $normalizer, $format = null, array /** * {@inheritdoc} */ - public function getOrder() + public function getOrder(): int { return 1; } diff --git a/src/SearchEndpoint/QueryEndpoint.php b/src/SearchEndpoint/QueryEndpoint.php index bd586f69..d6d58f4f 100644 --- a/src/SearchEndpoint/QueryEndpoint.php +++ b/src/SearchEndpoint/QueryEndpoint.php @@ -27,13 +27,9 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI public const NAME = 'query'; private ?BoolQuery $bool = null; - private bool $filtersSet = false; - /** - * {@inheritdoc} - */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool + public function normalize(NormalizerInterface $normalizer, string $format = null, array $context = []): array|string|int|float|bool { if (!$this->filtersSet && $this->hasReference('filter_query')) { /** @var BuilderInterface $filter */ @@ -49,18 +45,12 @@ public function normalize(NormalizerInterface $normalizer, $format = null, array return $this->bool->toArray(); } - /** - * {@inheritdoc} - */ - public function add(BuilderInterface $builder, $key = null) + public function add(BuilderInterface $builder, ?string $key = null): string { return $this->addToBool($builder, BoolQuery::MUST, $key); } - /** - * {@inheritdoc} - */ - public function addToBool(BuilderInterface $builder, $boolType = null, $key = null) + public function addToBool(BuilderInterface $builder, string $boolType = BoolQuery::MUST, ?string $key = null): string { if ($this->bool === null) { $this->bool = new BoolQuery(); @@ -69,26 +59,17 @@ public function addToBool(BuilderInterface $builder, $boolType = null, $key = nu return $this->bool->add($builder, $boolType, $key); } - /** - * {@inheritdoc} - */ - public function getOrder() + public function getOrder(): int { return 2; } - /** - * @return BoolQuery - */ - public function getBool() + public function getBool(): ?BoolQuery { return $this->bool; } - /** - * {@inheritdoc} - */ - public function getAll($boolType = null) + public function getAll(?string $boolType = null): array { return $this->bool->getQueries($boolType); } diff --git a/src/SearchEndpoint/SearchEndpointFactory.php b/src/SearchEndpoint/SearchEndpointFactory.php index f0ae3d84..a0c6794e 100644 --- a/src/SearchEndpoint/SearchEndpointFactory.php +++ b/src/SearchEndpoint/SearchEndpointFactory.php @@ -40,7 +40,7 @@ class SearchEndpointFactory * * @throws RuntimeException Endpoint does not exist. */ - public static function get($type) + public static function get(string $type): SearchEndpointInterface { if (!array_key_exists($type, self::$endpoints)) { throw new RuntimeException('Endpoint does not exist.'); diff --git a/src/SearchEndpoint/SearchEndpointInterface.php b/src/SearchEndpoint/SearchEndpointInterface.php index fe83d7f5..b3ef1216 100644 --- a/src/SearchEndpoint/SearchEndpointInterface.php +++ b/src/SearchEndpoint/SearchEndpointInterface.php @@ -24,55 +24,44 @@ interface SearchEndpointInterface extends NormalizableInterface * Adds builder to search endpoint. * * @param BuilderInterface $builder Builder to add. - * @param array $key Additional parameters relevant to builder. + * @param string|null $key Optional key for builder to add. * * @return string Key of added builder. */ - public function add(BuilderInterface $builder, $key = null); + public function add(BuilderInterface $builder, ?string $key = null): string; /** * Adds builder to search endpoint's specific bool type container. * * @param BuilderInterface $builder Builder to add. - * @param array $boolType Bool type for query or filter. If bool type is left null - * it will be treated as MUST. - * @param array $key Additional parameters relevant to builder. + * @param string $boolType Bool type for query or filter. + * @param string|null $key Additional parameters relevant to builder. * * @return string Key of added builder. */ - public function addToBool(BuilderInterface $builder, $boolType = null, $key = null); + public function addToBool(BuilderInterface $builder, string $boolType = BoolQuery::MUST, ?string $key = null): string; /** * Removes contained builder. - * - * @param int $key - * - * @return $this */ - public function remove($key); + public function remove(string $key): static; /** * Returns contained builder or null if Builder is not found. - * - * @param int $key - * - * @return BuilderInterface|null */ - public function get($key); + public function get(string $key): ?BuilderInterface; /** * Returns contained builder or null if Builder is not found. * * @param string|null $boolType If bool type is left null it will return all builders from container. * - * @return array + * @return array */ - public function getAll($boolType = null); + public function getAll(?string $boolType = null): array; /** * Returns Bool filter or query instance with all builder objects inside. - * - * @return BoolQuery */ - public function getBool(); + public function getBool(): ?BoolQuery; } diff --git a/src/SearchEndpoint/SuggestEndpoint.php b/src/SearchEndpoint/SuggestEndpoint.php index f17ed17e..688e6ddf 100644 --- a/src/SearchEndpoint/SuggestEndpoint.php +++ b/src/SearchEndpoint/SuggestEndpoint.php @@ -31,7 +31,6 @@ public function normalize(NormalizerInterface $normalizer, $format = null, array { $output = []; if ($this->getAll() !== []) { - /** @var TermSuggest $suggest */ foreach ($this->getAll() as $suggest) { $output = array_merge($output, $suggest->toArray()); } diff --git a/src/Serializer/Normalizer/OrderedNormalizerInterface.php b/src/Serializer/Normalizer/OrderedNormalizerInterface.php index ee471ccf..f31fdb1e 100644 --- a/src/Serializer/Normalizer/OrderedNormalizerInterface.php +++ b/src/Serializer/Normalizer/OrderedNormalizerInterface.php @@ -18,8 +18,6 @@ interface OrderedNormalizerInterface { /** * Returns normalization priority. - * - * @return int */ - public function getOrder(); + public function getOrder(): int; }