From 92d816cca6c52ed70d66aeb4542c7000df683804 Mon Sep 17 00:00:00 2001 From: Denis Urban Date: Thu, 3 Aug 2023 14:56:47 +0200 Subject: [PATCH] update to PHP 8 & ES 8 --- .gitignore | 3 +- .travis.yml | 10 +- Makefile | 11 ++ README.md | 31 ++-- composer.json | 18 ++- phpunit.xml.dist | 53 +++---- src/Aggregation/AbstractAggregation.php | 20 +-- .../Bucketing/AdjacencyMatrixAggregation.php | 17 +-- .../AutoDateHistogramAggregation.php | 10 +- .../Bucketing/ChildrenAggregation.php | 39 ++--- .../Bucketing/CompositeAggregation.php | 60 ++------ .../Bucketing/DateHistogramAggregation.php | 40 +---- .../Bucketing/DateRangeAggregation.php | 61 ++------ .../DiversifiedSamplerAggregation.php | 14 +- .../Bucketing/FilterAggregation.php | 28 ++-- .../Bucketing/FiltersAggregation.php | 27 +--- .../Bucketing/GeoDistanceAggregation.php | 76 +++------ .../Bucketing/GeoHashGridAggregation.php | 6 +- .../Bucketing/GlobalAggregation.php | 8 +- .../Bucketing/HistogramAggregation.php | 23 ++- .../Bucketing/Ipv4RangeAggregation.php | 37 ++--- .../Bucketing/MissingAggregation.php | 10 +- .../Bucketing/NestedAggregation.php | 6 +- .../Bucketing/RangeAggregation.php | 59 ++----- .../Bucketing/ReverseNestedAggregation.php | 20 +-- .../Bucketing/SamplerAggregation.php | 10 +- .../Bucketing/SignificantTermsAggregation.php | 2 +- .../Bucketing/SignificantTextAggregation.php | 2 +- .../Bucketing/TermsAggregation.php | 10 +- .../Matrix/MatrixStatsAggregation.php | 16 +- src/Aggregation/Metric/AvgAggregation.php | 2 +- .../Metric/CardinalityAggregation.php | 11 +- .../Metric/ExtendedStatsAggregation.php | 14 +- .../Metric/GeoBoundsAggregation.php | 24 +-- .../Metric/GeoCentroidAggregation.php | 9 +- src/Aggregation/Metric/MaxAggregation.php | 2 +- src/Aggregation/Metric/MinAggregation.php | 2 +- .../Metric/PercentileRanksAggregation.php | 14 +- .../Metric/PercentilesAggregation.php | 14 +- .../Metric/ScriptedMetricAggregation.php | 27 ++-- src/Aggregation/Metric/StatsAggregation.php | 10 +- src/Aggregation/Metric/SumAggregation.php | 2 +- src/Aggregation/Metric/TopHitsAggregation.php | 104 +++---------- .../Metric/ValueCountAggregation.php | 2 +- .../Pipeline/AbstractPipelineAggregation.php | 4 +- .../Pipeline/AvgBucketAggregation.php | 2 +- .../Pipeline/BucketScriptAggregation.php | 29 +--- .../Pipeline/BucketSelectorAggregation.php | 2 +- .../Pipeline/BucketSortAggregation.php | 24 +-- .../Pipeline/CumulativeSumAggregation.php | 2 +- .../Pipeline/DerivativeAggregation.php | 2 +- .../ExtendedStatsBucketAggregation.php | 2 +- .../Pipeline/MaxBucketAggregation.php | 2 +- .../Pipeline/MinBucketAggregation.php | 2 +- .../Pipeline/MovingAverageAggregation.php | 2 +- .../Pipeline/MovingFunctionAggregation.php | 2 +- .../Pipeline/PercentilesBucketAggregation.php | 15 +- .../SerialDifferencingAggregation.php | 2 +- .../Pipeline/StatsBucketAggregation.php | 2 +- .../Pipeline/SumBucketAggregation.php | 2 +- src/Aggregation/Type/BucketingTrait.php | 4 +- src/Aggregation/Type/MetricTrait.php | 4 +- src/BuilderBag.php | 22 +-- src/BuilderInterface.php | 4 +- src/Highlight/Highlight.php | 27 +--- src/InnerHit/NestedInnerHit.php | 74 +++------ src/InnerHit/ParentInnerHit.php | 2 +- src/NameAwareTrait.php | 17 +-- src/NamedBuilderInterface.php | 2 +- src/ParametersTrait.php | 46 ++---- src/Query/Compound/BoolQuery.php | 29 ++-- src/Query/Compound/BoostingQuery.php | 26 +--- src/Query/Compound/ConstantScoreQuery.php | 16 +- src/Query/Compound/DisMaxQuery.php | 10 +- src/Query/Compound/FunctionScoreQuery.php | 81 ++++------ src/Query/FullText/CommonTermsQuery.php | 19 +-- src/Query/FullText/MatchPhrasePrefixQuery.php | 2 +- src/Query/FullText/MatchPhraseQuery.php | 2 +- src/Query/FullText/MatchQuery.php | 19 +-- src/Query/FullText/MultiMatchQuery.php | 23 +-- src/Query/FullText/QueryStringQuery.php | 13 +- src/Query/FullText/SimpleQueryStringQuery.php | 13 +- src/Query/Geo/GeoBoundingBoxQuery.php | 19 +-- src/Query/Geo/GeoDistanceQuery.php | 27 +--- src/Query/Geo/GeoPolygonQuery.php | 20 +-- src/Query/Geo/GeoShapeQuery.php | 27 ++-- src/Query/Joining/HasChildQuery.php | 20 +-- src/Query/Joining/HasParentQuery.php | 20 +-- src/Query/Joining/NestedQuery.php | 24 +-- src/Query/Joining/ParentIdQuery.php | 20 +-- src/Query/MatchAllQuery.php | 6 +- src/Query/Span/FieldMaskingSpanQuery.php | 9 +- src/Query/Span/SpanContainingQuery.php | 26 +--- src/Query/Span/SpanFirstQuery.php | 20 +-- src/Query/Span/SpanMultiTermQuery.php | 15 +- src/Query/Span/SpanNearQuery.php | 5 +- src/Query/Span/SpanNotQuery.php | 23 +-- src/Query/Span/SpanOrQuery.php | 11 +- src/Query/Span/SpanTermQuery.php | 2 +- src/Query/Span/SpanWithinQuery.php | 2 +- src/Query/Specialized/MoreLikeThisQuery.php | 15 +- src/Query/Specialized/ScriptQuery.php | 12 +- src/Query/Specialized/TemplateQuery.php | 4 +- src/Query/TermLevel/ExistsQuery.php | 14 +- src/Query/TermLevel/FuzzyQuery.php | 19 +-- src/Query/TermLevel/IdsQuery.php | 16 +- src/Query/TermLevel/PrefixQuery.php | 18 +-- src/Query/TermLevel/RangeQuery.php | 23 ++- src/Query/TermLevel/RegexpQuery.php | 19 +-- src/Query/TermLevel/TermQuery.php | 23 +-- src/Query/TermLevel/TermsQuery.php | 18 +-- src/Query/TermLevel/TermsSetQuery.php | 21 +-- src/Query/TermLevel/TypeQuery.php | 12 +- src/Query/TermLevel/WildcardQuery.php | 19 +-- src/ScriptAwareTrait.php | 17 +-- src/Search.php | 144 ++++++------------ src/SearchEndpoint/AbstractSearchEndpoint.php | 8 +- src/SearchEndpoint/AggregationsEndpoint.php | 12 +- src/SearchEndpoint/HighlightEndpoint.php | 6 +- src/SearchEndpoint/InnerHitsEndpoint.php | 12 +- src/SearchEndpoint/PostFilterEndpoint.php | 6 +- src/SearchEndpoint/QueryEndpoint.php | 20 +-- src/SearchEndpoint/SearchEndpointFactory.php | 16 +- .../SearchEndpointInterface.php | 2 +- src/SearchEndpoint/SortEndpoint.php | 4 +- src/SearchEndpoint/SuggestEndpoint.php | 12 +- .../Normalizer/CustomReferencedNormalizer.php | 9 +- src/Serializer/OrderedSerializer.php | 22 +-- src/Sort/FieldSort.php | 73 ++------- src/Sort/NestedSort.php | 43 ++---- src/Suggest/Suggest.php | 89 ++--------- .../AbstractElasticsearchTestCase.php | 16 +- .../Query/FunctionScoreQueryTest.php | 10 +- tests/Functional/Query/MatchAllQueryTest.php | 6 +- .../AdjacencyMatrixAggregationTest.php | 31 ++-- .../AudoDateHistogramAggregationTest.php | 11 +- .../Bucketing/ChildrenAggregationTest.php | 16 +- .../Bucketing/CompositeAggregationTest.php | 23 +-- .../DateHistogramAggregationTest.php | 16 +- .../Bucketing/DateRangeAggregationTest.php | 40 +++-- .../Bucketing/FilterAggregationTest.php | 33 ++-- .../Bucketing/FiltersAggregationTest.php | 45 +++--- .../Bucketing/GeoDistanceAggregationTest.php | 39 +++-- .../Bucketing/GeoHashGridAggregationTest.php | 21 ++- .../Bucketing/GlobalAggregationTest.php | 26 ++-- .../Bucketing/Ipv4RangeAggregationTest.php | 10 +- .../Bucketing/MissingAggregationTest.php | 15 +- .../Bucketing/NestedAggregationTest.php | 5 +- .../Bucketing/RangeAggregationTest.php | 17 ++- .../ReverseNestedAggregationTest.php | 13 +- .../Bucketing/SamplerAggregationTest.php | 10 +- .../SignificantTermsAggregationTest.php | 11 +- .../SignificantTextAggregationTest.php | 11 +- .../Bucketing/TermsAggregationTest.php | 19 +-- .../Metric/CardinalityAggregationTest.php | 15 +- .../Metric/GeoBoundsAggregationTest.php | 13 +- .../Metric/GeoCentroidAggregationTest.php | 12 +- .../Metric/PercentileRanksAggregationTest.php | 24 ++- .../Metric/PercentilesAggregationTest.php | 14 +- .../Metric/StatsAggregationTest.php | 7 +- .../Metric/TopHitsAggregationTest.php | 7 +- .../Pipeline/AvgBucketAggregationTest.php | 7 +- .../Pipeline/BucketScriptAggregationTest.php | 12 +- .../BucketSelectorAggregationTest.php | 12 +- .../Pipeline/BucketSortAggregationTest.php | 5 +- .../Pipeline/CumulativeSumAggregationTest.php | 5 +- .../Pipeline/DerivativeAggregationTest.php | 5 +- .../ExtendedStatsBucketAggregationTest.php | 5 +- .../Pipeline/MaxBucketAggregationTest.php | 5 +- .../Pipeline/MinBucketAggregationTest.php | 5 +- .../MovingFunctionAggregationTest.php | 5 +- .../PercentilesBucketAggregationTest.php | 5 +- .../SerialDifferencingAggregationTest.php | 5 +- .../Pipeline/StatsBucketAggregationTest.php | 5 +- .../Pipeline/SumBucketAggregationTest.php | 5 +- tests/Unit/BuilderBagTest.php | 21 +-- tests/Unit/Highlight/HighlightTest.php | 21 ++- tests/Unit/InnerHit/NestedInnerHitTest.php | 17 +-- tests/Unit/InnerHit/ParentInnerHitTest.php | 5 +- tests/Unit/ParametersTraitTest.php | 12 +- tests/Unit/Query/Compound/BoolQueryTest.php | 48 +++--- .../Unit/Query/Compound/BoostingQueryTest.php | 8 +- .../Query/Compound/ConstantScoreQueryTest.php | 8 +- tests/Unit/Query/Compound/DisMaxQueryTest.php | 9 +- .../Query/Compound/FunctionScoreQueryTest.php | 30 ++-- .../Query/FullText/CommonTermsQueryTest.php | 5 +- .../FullText/MatchPhrasePrefixQueryTest.php | 4 +- .../Query/FullText/MatchPhraseQueryTest.php | 4 +- tests/Unit/Query/FullText/MatchQueryTest.php | 5 +- .../Query/FullText/MultiMatchQueryTest.php | 7 +- .../Query/FullText/QueryStringQueryTest.php | 5 +- .../FullText/SimpleQueryStringQueryTest.php | 5 +- .../Query/Geo/GeoBoundingBoxQueryTest.php | 18 +-- tests/Unit/Query/Geo/GeoDistanceQueryTest.php | 13 +- tests/Unit/Query/Geo/GeoPolygonQueryTest.php | 13 +- tests/Unit/Query/Geo/GeoShapeQueryTest.php | 7 +- .../Unit/Query/Joining/HasChildQueryTest.php | 8 +- .../Unit/Query/Joining/HasParentQueryTest.php | 8 +- tests/Unit/Query/Joining/NestedQueryTest.php | 14 +- tests/Unit/Query/MatchAllQueryTest.php | 9 +- .../Query/Span/FieldMaskingSpanQueryTest.php | 5 +- .../Query/Span/SpanContainingQueryTest.php | 12 +- tests/Unit/Query/Span/SpanFirstQueryTest.php | 8 +- .../Query/Span/SpanMultiTermQueryTest.php | 8 +- tests/Unit/Query/Span/SpanNearQueryTest.php | 9 +- tests/Unit/Query/Span/SpanNotQueryTest.php | 8 +- tests/Unit/Query/Span/SpanOrQueryTest.php | 11 +- tests/Unit/Query/Span/SpanTermQueryTest.php | 7 +- tests/Unit/Query/Span/SpanWithinQueryTest.php | 12 +- .../Specialized/MoreLikeThisQueryTest.php | 5 +- .../Query/Specialized/ScriptQueryTest.php | 13 +- .../Query/Specialized/TemplateQueryTest.php | 13 +- .../Unit/Query/TermLevel/ExistsQueryTest.php | 5 +- tests/Unit/Query/TermLevel/FuzzyQueryTest.php | 5 +- tests/Unit/Query/TermLevel/IdsQueryTest.php | 5 +- .../Unit/Query/TermLevel/PrefixQueryTest.php | 5 +- tests/Unit/Query/TermLevel/RangeQueryTest.php | 5 +- .../Unit/Query/TermLevel/RegexpQueryTest.php | 5 +- tests/Unit/Query/TermLevel/TermQueryTest.php | 5 +- tests/Unit/Query/TermLevel/TermsQueryTest.php | 5 +- .../Query/TermLevel/TermsSetQueryTest.php | 7 +- tests/Unit/Query/TermLevel/TypeQueryTest.php | 5 +- .../Query/TermLevel/WildcardQueryTest.php | 5 +- .../AggregationsEndpointTest.php | 10 +- .../SearchEndpoint/HighlightEndpointTest.php | 20 +-- .../SearchEndpoint/InnerHitsEndpointTest.php | 26 ++-- .../SearchEndpoint/PostFilterEndpointTest.php | 21 +-- .../Unit/SearchEndpoint/QueryEndpointTest.php | 17 ++- .../SearchEndpointFactoryTest.php | 12 +- .../Unit/SearchEndpoint/SortEndpointTest.php | 18 ++- .../SearchEndpoint/SuggestEndpointTest.php | 15 +- tests/Unit/SearchTest.php | 9 +- tests/Unit/Sort/FieldSortTest.php | 5 +- tests/Unit/Sort/NestedSortTest.php | 9 +- tests/Unit/Suggest/SuggestTest.php | 17 +-- 235 files changed, 1393 insertions(+), 2419 deletions(-) create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index 2a3d0d0b..48503b06 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /vendor/ /phpunit.xml /composer.lock -coverage.xml \ No newline at end of file +coverage.xml +.phpunit.cache \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 65d6b9db..c8934ae1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,10 @@ sudo: false language: php php: - - 7.1 - - 7.2 - - 7.3 - - 7.4snapshot -matrix: - allow_failures: - - php: 7.4snapshot + - 8.1 env: global: - - ES_VERSION=7.4.0 ES_DOWNLOAD_URL=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}-linux-x86_64.tar.gz + - ES_VERSION=8.9.0 ES_DOWNLOAD_URL=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}-linux-x86_64.tar.gz install: - wget ${ES_DOWNLOAD_URL} - tar -xzf elasticsearch-${ES_VERSION}-linux-x86_64.tar.gz diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..13345fc3 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +#!/usr/bin/make + +es:: + docker run \ + --name elasticsearch \ + -p 9200:9200 \ + -e discovery.type=single-node \ + -e ES_JAVA_OPTS="-Xms1g -Xmx1g"\ + -e xpack.security.enabled=false \ + -it \ + docker.elastic.co/elasticsearch/elasticsearch:8.9.0 \ No newline at end of file diff --git a/README.md b/README.md index aa8e0c65..ff034c38 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,7 @@ # ElasticsearchDSL -Introducing Elasticsearch DSL library to provide objective query builder for [Elasticsearch bundle](https://github.com/ongr-io/ElasticsearchBundle) and [elasticsearch-php](https://github.com/elastic/elasticsearch-php) client. You can easily build any Elasticsearch query and transform it to an array. +Introducing Elasticsearch DSL library to provide objective query builder for [elasticsearch-php](https://github.com/elastic/elasticsearch-php) client. You can easily build any Elasticsearch query and transform it to an array. -If you need any help, [stack overflow](http://stackoverflow.com/questions/tagged/ongr) -is the preferred and recommended way to ask ONGR support questions. - [![Build Status](https://travis-ci.org/ongr-io/ElasticsearchDSL.svg?branch=master)](https://travis-ci.org/ongr-io/ElasticsearchDSL) [![codecov](https://codecov.io/gh/ongr-io/ElasticsearchDSL/branch/master/graph/badge.svg)](https://codecov.io/gh/ongr-io/ElasticsearchDSL) [![Latest Stable Version](https://poser.pugx.org/ongr/elasticsearch-dsl/v/stable)](https://packagist.org/packages/ongr/elasticsearch-dsl) @@ -18,14 +15,15 @@ If you like this library, help me to develop it by buying a cup of coffee ## Version matrix -| Elasticsearch version | ElasticsearchDSL version | -| --------------------- | --------------------------- | -| >= 7.0 | >= 7.0 | -| >= 6.0, < 7.0 | >= 6.0 | -| >= 5.0, < 6.0 | >= 5.0 | -| >= 2.0, < 5.0 | >= 2.0 (not supported) | -| >= 1.0, < 2.0 | 1.x (not supported) | -| <= 0.90.x | not supported | +| Elasticsearch version | ElasticsearchDSL version | +|-----------------------|--------------------------| +| >= 8.0 | >= 8.0 | +| >= 7.0 | >= 7.0 | +| >= 6.0, < 7.0 | >= 6.0 | +| >= 5.0, < 6.0 | >= 5.0 | +| >= 2.0, < 5.0 | >= 2.0 (not supported) | +| >= 1.0, < 2.0 | 1.x (not supported) | +| <= 0.90.x | not supported | ## Documentation @@ -38,20 +36,13 @@ If you like this library, help me to develop it by buying a cup of coffee Install library with [composer](https://getcomposer.org): ```bash -$ composer require ongr/elasticsearch-dsl +$ composer require bistrosk/elasticsearch-dsl ``` > [elasticsearch-php](https://github.com/elastic/elasticsearch-php) client is defined in the composer requirements, no need to install it. ### Search -Elasticsearch DSL was extracted from [Elasticsearch Bundle](https://github.com/ongr-io/ElasticsearchBundle) to provide standalone query dsl for [elasticsearch-php](https://github.com/elastic/elasticsearch-php). Examples how to use it together with [Elasticsearch Bundle](https://github.com/ongr-io/ElasticsearchBundle) can be found in the [Elasticsearch Bundle docs](https://github.com/ongr-io/ElasticsearchBundle/blob/master/Resources/doc/search.md). - -If you dont want to use Symfony or Elasticsearch bundle, no worries, you can use it in any project together with [elasticsearch-php](https://github.com/elastic/elasticsearch-php). Here's the example: - -If you are using Symfony there is also the [ElasticsearchBundle](https://github.com/ongr-io/ElasticsearchBundle) -which provides full integration with Elasticsearch DSL. - The library is standalone and is not coupled with any framework. You can use it in any PHP project, the only requirement is composer. Here's the example: diff --git a/composer.json b/composer.json index 1cc93d8a..f96476ae 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "ongr/elasticsearch-dsl", + "name": "bistrosk/elasticsearch-dsl", "description": "Elasticsearch DSL library", "type": "library", "homepage": "http://ongr.io", @@ -11,13 +11,13 @@ } ], "require": { - "php": "^7.0", - "symfony/serializer": "^3.0|^4.0", + "php": ">=8.1", + "symfony/serializer": "^6.0", "paragonie/random_compat": "*" }, "require-dev": { - "elasticsearch/elasticsearch": "^7.0", - "phpunit/phpunit": "~6.0", + "elasticsearch/elasticsearch": "^8.0", + "phpunit/phpunit": "~10.0", "squizlabs/php_codesniffer": "^3.0" }, "suggest": { @@ -34,9 +34,15 @@ } }, "minimum-stability": "dev", + "prefer-stable": true, "extra": { "branch-alias": { - "dev-master": "7.0-dev" + "dev-master": "8.0-dev" + } + }, + "config": { + "allow-plugins": { + "php-http/discovery": true } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 17185e94..b3b52c08 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,38 +1,31 @@ - - + + + + + + ./tests/Unit/ - - ./tests/Functional/ - - - ./tests/ - + + + + + + - - - + + + ./ - - ./tests - ./vendor - - - - - - + + + ./tests + ./vendor + + diff --git a/src/Aggregation/AbstractAggregation.php b/src/Aggregation/AbstractAggregation.php index c2fbe984..a2dd1319 100644 --- a/src/Aggregation/AbstractAggregation.php +++ b/src/Aggregation/AbstractAggregation.php @@ -29,29 +29,26 @@ abstract class AbstractAggregation implements NamedBuilderInterface */ private $field; - /** - * @var BuilderBag - */ - private $aggregations; + private ?BuilderBag $aggregations = null; /** * Abstract supportsNesting method. * * @return bool */ - abstract protected function supportsNesting(); + abstract protected function supportsNesting(): bool; /** * @return array|\stdClass */ - abstract protected function getArray(); + abstract public function getArray(): array; /** * Inner aggregations container init. * * @param string $name */ - public function __construct($name) + public function __construct(string $name) { $this->setName($name); } @@ -79,13 +76,12 @@ public function getField() /** * Adds a sub-aggregation. * - * @param AbstractAggregation $abstractAggregation * * @return $this */ public function addAggregation(AbstractAggregation $abstractAggregation) { - if (!$this->aggregations) { + if (!$this->aggregations instanceof BuilderBag) { $this->aggregations = $this->createBuilderBag(); } @@ -101,7 +97,7 @@ public function addAggregation(AbstractAggregation $abstractAggregation) */ public function getAggregations() { - if ($this->aggregations) { + if ($this->aggregations instanceof BuilderBag) { return $this->aggregations->all(); } else { return []; @@ -126,11 +122,11 @@ public function getAggregation($name) /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $array = $this->getArray(); $result = [ - $this->getType() => is_array($array) ? $this->processArray($array) : $array, + $this->getType() => $this->processArray($array), ]; if ($this->supportsNesting()) { diff --git a/src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php b/src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php index 632f0b59..9bbcdc8d 100644 --- a/src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php +++ b/src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php @@ -22,14 +22,14 @@ */ class AdjacencyMatrixAggregation extends AbstractAggregation { - const FILTERS = 'filters'; + final public const FILTERS = 'filters'; use BucketingTrait; /** * @var BuilderInterface[] */ - private $filters = [ + private array $filters = [ self::FILTERS => [] ]; @@ -39,7 +39,7 @@ class AdjacencyMatrixAggregation extends AbstractAggregation * @param string $name * @param BuilderInterface[] $filters */ - public function __construct($name, $filters = []) + public function __construct(string $name, array $filters = []) { parent::__construct($name); @@ -49,14 +49,9 @@ public function __construct($name, $filters = []) } /** - * @param string $name - * @param BuilderInterface $filter - * * @throws \LogicException - * - * @return self */ - public function addFilter($name, BuilderInterface $filter) + public function addFilter(string $name, BuilderInterface $filter): static { $this->filters[self::FILTERS][$name] = $filter->toArray(); @@ -66,7 +61,7 @@ public function addFilter($name, BuilderInterface $filter) /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { return $this->filters; } @@ -74,7 +69,7 @@ public function getArray() /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'adjacency_matrix'; } diff --git a/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php b/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php index 6a204465..7e97b0de 100644 --- a/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php +++ b/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php @@ -32,7 +32,7 @@ class AutoDateHistogramAggregation extends AbstractAggregation * @param int $buckets * @param string $format */ - public function __construct($name, $field, $buckets = null, $format = null) + public function __construct(string $name, $field, $buckets = null, $format = null) { parent::__construct($name); @@ -50,21 +50,19 @@ public function __construct($name, $field, $buckets = null, $format = null) /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { - $data = array_filter( + return array_filter( [ 'field' => $this->getField(), ] ); - - return $data; } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'auto_date_histogram'; } diff --git a/src/Aggregation/Bucketing/ChildrenAggregation.php b/src/Aggregation/Bucketing/ChildrenAggregation.php index f36802aa..f81a192f 100644 --- a/src/Aggregation/Bucketing/ChildrenAggregation.php +++ b/src/Aggregation/Bucketing/ChildrenAggregation.php @@ -23,48 +23,31 @@ class ChildrenAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var string - */ - private $children; - - /** - * Return children. - * - * @return string - */ - public function getChildren() - { - return $this->children; - } + private ?string $children; - /** - * @param string $name - * @param string $children - */ - public function __construct($name, $children = null) + public function __construct(string $name, ?string $children = null) { parent::__construct($name); $this->setChildren($children); } - /** - * @param string $children - * - * @return $this - */ - public function setChildren($children) + public function setChildren(?string $children): static { $this->children = $children; return $this; } + public function getChildren(): ?string + { + return $this->children; + } + /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'children'; } @@ -72,10 +55,10 @@ public function getType() /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { if (count($this->getAggregations()) == 0) { - throw new \LogicException("Children aggregation `{$this->getName()}` has no aggregations added"); + throw new \LogicException(sprintf('Children aggregation `%s` has no aggregations added', $this->getName())); } return ['type' => $this->getChildren()]; diff --git a/src/Aggregation/Bucketing/CompositeAggregation.php b/src/Aggregation/Bucketing/CompositeAggregation.php index 8f1f8d8a..01d58b09 100644 --- a/src/Aggregation/Bucketing/CompositeAggregation.php +++ b/src/Aggregation/Bucketing/CompositeAggregation.php @@ -27,25 +27,15 @@ class CompositeAggregation extends AbstractAggregation /** * @var BuilderInterface[] */ - private $sources = []; + private array $sources = []; - /** - * @var int - */ - private $size; - - /** - * @var array - */ - private $after; + private ?int $size = null; + private ?array $after = null; /** * Inner aggregations container init. - * - * @param string $name - * @param AbstractAggregation[] $sources */ - public function __construct($name, $sources = []) + public function __construct(string $name, array $sources = []) { parent::__construct($name); @@ -55,13 +45,9 @@ public function __construct($name, $sources = []) } /** - * @param AbstractAggregation $agg - * * @throws \LogicException - * - * @return self */ - public function addSource(AbstractAggregation $agg) + public function addSource(AbstractAggregation $agg): static { $array = $agg->getArray(); @@ -77,7 +63,7 @@ public function addSource(AbstractAggregation $agg) /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { $array = [ 'sources' => $this->sources, @@ -97,55 +83,31 @@ public function getArray() /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'composite'; } - /** - * Sets size - * - * @param int $size Size - * - * @return $this - */ - public function setSize($size) + public function setSize(int $size): static { $this->size = $size; return $this; } - /** - * Returns size - * - * @return int - */ - public function getSize() + public function getSize(): ?int { return $this->size; } - /** - * Sets after - * - * @param array $after After - * - * @return $this - */ - public function setAfter(array $after) + public function setAfter(array $after): static { $this->after = $after; return $this; } - /** - * Returns after - * - * @return array - */ - public function getAfter() + public function getAfter(): ?array { return $this->after; } diff --git a/src/Aggregation/Bucketing/DateHistogramAggregation.php b/src/Aggregation/Bucketing/DateHistogramAggregation.php index 352b8167..abea7fc8 100644 --- a/src/Aggregation/Bucketing/DateHistogramAggregation.php +++ b/src/Aggregation/Bucketing/DateHistogramAggregation.php @@ -23,24 +23,13 @@ class DateHistogramAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var string - */ - protected $interval; - - /** - * @var string - */ - protected $format; + protected ?string $interval; + protected ?string $format; /** * Inner aggregations container init. - * - * @param string $name - * @param string $field - * @param string $interval */ - public function __construct($name, $field = null, $interval = null, $format = null) + public function __construct(string $name, ?string $field = null, ?string $interval = null, ?string $format = null) { parent::__construct($name); @@ -49,32 +38,19 @@ public function __construct($name, $field = null, $interval = null, $format = nu $this->setFormat($format); } - /** - * @return int - */ - public function getInterval() + public function getInterval(): ?string { return $this->interval; } - /** - * @param string $interval - * - * @return $this - */ - public function setInterval($interval) + public function setInterval(?string $interval): static { $this->interval = $interval; return $this; } - /** - * @param string $format - * - * @return $this - */ - public function setFormat($format) + public function setFormat(?string $format): static { $this->format = $format; @@ -84,7 +60,7 @@ public function setFormat($format) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'date_histogram'; } @@ -92,7 +68,7 @@ public function getType() /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { if (!$this->getField() || !$this->getInterval()) { throw new \LogicException('Date histogram aggregation must have field and interval set.'); diff --git a/src/Aggregation/Bucketing/DateRangeAggregation.php b/src/Aggregation/Bucketing/DateRangeAggregation.php index 3ca14e29..9197a9c7 100644 --- a/src/Aggregation/Bucketing/DateRangeAggregation.php +++ b/src/Aggregation/Bucketing/DateRangeAggregation.php @@ -23,29 +23,11 @@ class DateRangeAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var string - */ - private $format; - - /** - * @var array - */ - private $ranges = []; + private ?string $format; + private array $ranges = []; + private bool $keyed = false; - /** - * @var bool - */ - private $keyed = false; - - /** - * @param string $name - * @param string $field - * @param string $format - * @param array $ranges - * @param bool $keyed - */ - public function __construct($name, $field = null, $format = null, array $ranges = [], $keyed = false) + public function __construct(string $name, ?string $field = null, ?string $format = null, array $ranges = [], bool $keyed = false) { parent::__construct($name); @@ -53,9 +35,9 @@ public function __construct($name, $field = null, $format = null, array $ranges $this->setFormat($format); $this->setKeyed($keyed); foreach ($ranges as $range) { - $from = isset($range['from']) ? $range['from'] : null; - $to = isset($range['to']) ? $range['to'] : null; - $key = isset($range['key']) ? $range['key'] : null; + $from = $range['from'] ?? null; + $to = $range['to'] ?? null; + $key = $range['key'] ?? null; $this->addRange($from, $to, $key); } } @@ -63,29 +45,21 @@ public function __construct($name, $field = null, $format = null, array $ranges /** * Sets if result buckets should be keyed. * - * @param bool $keyed - * * @return DateRangeAggregation */ - public function setKeyed($keyed) + public function setKeyed(bool $keyed): static { $this->keyed = $keyed; return $this; } - /** - * @return string - */ - public function getFormat() + public function getFormat(): ?string { return $this->format; } - /** - * @param string $format - */ - public function setFormat($format) + public function setFormat(?string $format): void { $this->format = $format; } @@ -101,7 +75,7 @@ public function setFormat($format) * * @throws \LogicException */ - public function addRange($from = null, $to = null, $key = null) + public function addRange(mixed $from = null, mixed $to = null, mixed $key = null): static { $range = array_filter( [ @@ -109,9 +83,7 @@ public function addRange($from = null, $to = null, $key = null) 'to' => $to, 'key' => $key, ], - function ($v) { - return !is_null($v); - } + static fn($v): bool => !is_null($v) ); if (empty($range)) { @@ -126,25 +98,24 @@ function ($v) { /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { if ($this->getField() && $this->getFormat() && !empty($this->ranges)) { - $data = [ + return [ 'format' => $this->getFormat(), 'field' => $this->getField(), 'ranges' => $this->ranges, 'keyed' => $this->keyed, ]; - - return $data; } + throw new \LogicException('Date range aggregation must have field, format set and range added.'); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'date_range'; } diff --git a/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php b/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php index b16ae9de..b00793c4 100644 --- a/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php +++ b/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php @@ -36,7 +36,7 @@ class DiversifiedSamplerAggregation extends AbstractAggregation * @param string $field Elasticsearch field name * @param int $shardSize Shard size, by default it's 100 */ - public function __construct($name, $field = null, $shardSize = null) + public function __construct(string $name, $field = null, $shardSize = null) { parent::__construct($name); @@ -53,11 +53,9 @@ public function getShardSize() } /** - * @param mixed $shardSize - * * @return $this */ - public function setShardSize($shardSize) + public function setShardSize(mixed $shardSize) { $this->shardSize = $shardSize; @@ -67,7 +65,7 @@ public function setShardSize($shardSize) /** * @inheritdoc */ - public function getType() + public function getType(): string { return 'diversified_sampler'; } @@ -75,15 +73,13 @@ public function getType() /** * @inheritdoc */ - protected function getArray() + public function getArray(): array { - $out = array_filter( + return array_filter( [ 'field' => $this->getField(), 'shard_size' => $this->getShardSize(), ] ); - - return $out; } } diff --git a/src/Aggregation/Bucketing/FilterAggregation.php b/src/Aggregation/Bucketing/FilterAggregation.php index c7d1fa9d..dff3d33b 100644 --- a/src/Aggregation/Bucketing/FilterAggregation.php +++ b/src/Aggregation/Bucketing/FilterAggregation.php @@ -24,32 +24,24 @@ class FilterAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var BuilderInterface - */ - protected $filter; + protected ?BuilderInterface $filter = null; /** * Inner aggregations container init. - * - * @param string $name - * @param BuilderInterface $filter */ - public function __construct($name, BuilderInterface $filter = null) + public function __construct(string $name, ?BuilderInterface $filter = null) { parent::__construct($name); - if ($filter !== null) { + if ($filter instanceof BuilderInterface) { $this->setFilter($filter); } } /** - * @param BuilderInterface $filter - * * @return $this */ - public function setFilter(BuilderInterface $filter) + public function setFilter(BuilderInterface $filter): static { $this->filter = $filter; @@ -58,10 +50,8 @@ public function setFilter(BuilderInterface $filter) /** * Returns a filter. - * - * @return BuilderInterface */ - public function getFilter() + public function getFilter(): ?BuilderInterface { return $this->filter; } @@ -69,7 +59,7 @@ public function getFilter() /** * {@inheritdoc} */ - public function setField($field) + public function setField($field): void { throw new \LogicException("Filter aggregation, doesn't support `field` parameter"); } @@ -77,10 +67,10 @@ public function setField($field) /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { if (!$this->filter) { - throw new \LogicException("Filter aggregation `{$this->getName()}` has no filter added"); + throw new \LogicException(sprintf('Filter aggregation `%s` has no filter added', $this->getName())); } return $this->getFilter()->toArray(); @@ -89,7 +79,7 @@ public function getArray() /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'filter'; } diff --git a/src/Aggregation/Bucketing/FiltersAggregation.php b/src/Aggregation/Bucketing/FiltersAggregation.php index 9bd52c6a..ead56bba 100644 --- a/src/Aggregation/Bucketing/FiltersAggregation.php +++ b/src/Aggregation/Bucketing/FiltersAggregation.php @@ -27,12 +27,9 @@ class FiltersAggregation extends AbstractAggregation /** * @var BuilderInterface[] */ - private $filters = []; + private ?array $filters = []; - /** - * @var bool - */ - private $anonymous = false; + private bool $anonymous = false; /** * Inner aggregations container init. @@ -41,7 +38,7 @@ class FiltersAggregation extends AbstractAggregation * @param BuilderInterface[] $filters * @param bool $anonymous */ - public function __construct($name, $filters = [], $anonymous = false) + public function __construct(string $name, array $filters = [], bool $anonymous = false) { parent::__construct($name); @@ -55,12 +52,7 @@ public function __construct($name, $filters = [], $anonymous = false) } } - /** - * @param bool $anonymous - * - * @return $this - */ - public function setAnonymous($anonymous) + public function setAnonymous(bool $anonymous): static { $this->anonymous = $anonymous; @@ -68,14 +60,9 @@ public function setAnonymous($anonymous) } /** - * @param BuilderInterface $filter - * @param string $name - * * @throws \LogicException - * - * @return FiltersAggregation */ - public function addFilter(BuilderInterface $filter, $name = '') + public function addFilter(BuilderInterface $filter, string $name = ''): static { if ($this->anonymous === false && empty($name)) { throw new \LogicException('In not anonymous filters filter name must be set.'); @@ -91,7 +78,7 @@ public function addFilter(BuilderInterface $filter, $name = '') /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { return $this->filters; } @@ -99,7 +86,7 @@ public function getArray() /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'filters'; } diff --git a/src/Aggregation/Bucketing/GeoDistanceAggregation.php b/src/Aggregation/Bucketing/GeoDistanceAggregation.php index 81db370d..f9857d8d 100644 --- a/src/Aggregation/Bucketing/GeoDistanceAggregation.php +++ b/src/Aggregation/Bucketing/GeoDistanceAggregation.php @@ -23,25 +23,10 @@ class GeoDistanceAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var mixed - */ - private $origin; - - /** - * @var string - */ - private $distanceType; - - /** - * @var string - */ - private $unit; - - /** - * @var array - */ - private $ranges = []; + private mixed $origin; + private ?string $distanceType; + private ?string $unit; + private array $ranges = []; /** * Inner aggregations container init. @@ -53,65 +38,47 @@ class GeoDistanceAggregation extends AbstractAggregation * @param string $unit * @param string $distanceType */ - public function __construct($name, $field = null, $origin = null, $ranges = [], $unit = null, $distanceType = null) + public function __construct(string $name, ?string $field = null, mixed $origin = null, array $ranges = [], ?string $unit = null, ?string $distanceType = null) { parent::__construct($name); $this->setField($field); $this->setOrigin($origin); foreach ($ranges as $range) { - $from = isset($range['from']) ? $range['from'] : null; - $to = isset($range['to']) ? $range['to'] : null; + $from = $range['from'] ?? null; + $to = $range['to'] ?? null; $this->addRange($from, $to); } + $this->setUnit($unit); $this->setDistanceType($distanceType); } - /** - * @return string - */ - public function getOrigin() + public function getOrigin(): ?string { return $this->origin; } - /** - * @param mixed $origin - * - * @return $this - */ - public function setOrigin($origin) + public function setOrigin(mixed $origin): static { $this->origin = $origin; return $this; } - /** - * @return string - */ - public function getDistanceType() + public function getDistanceType(): ?string { return $this->distanceType; } - /** - * @param string $distanceType - * - * @return $this - */ - public function setDistanceType($distanceType) + public function setDistanceType(?string $distanceType): static { $this->distanceType = $distanceType; return $this; } - /** - * @return string - */ - public function getUnit() + public function getUnit(): ?string { return $this->unit; } @@ -121,7 +88,7 @@ public function getUnit() * * @return $this */ - public function setUnit($unit) + public function setUnit(?string $unit): static { $this->unit = $unit; @@ -131,23 +98,16 @@ public function setUnit($unit) /** * Add range to aggregation. * - * @param int|float|null $from - * @param int|float|null $to - * * @throws \LogicException - * - * @return GeoDistanceAggregation */ - public function addRange($from = null, $to = null) + public function addRange(mixed $from = null, mixed $to = null): static { $range = array_filter( [ 'from' => $from, 'to' => $to, ], - function ($v) { - return !is_null($v); - } + static fn($v): bool => !is_null($v) ); if (empty($range)) { @@ -162,7 +122,7 @@ function ($v) { /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { $data = []; @@ -194,7 +154,7 @@ public function getArray() /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'geo_distance'; } diff --git a/src/Aggregation/Bucketing/GeoHashGridAggregation.php b/src/Aggregation/Bucketing/GeoHashGridAggregation.php index 613026a7..41993424 100644 --- a/src/Aggregation/Bucketing/GeoHashGridAggregation.php +++ b/src/Aggregation/Bucketing/GeoHashGridAggregation.php @@ -47,7 +47,7 @@ class GeoHashGridAggregation extends AbstractAggregation * @param int $size * @param int $shardSize */ - public function __construct($name, $field = null, $precision = null, $size = null, $shardSize = null) + public function __construct(string $name, $field = null, $precision = null, $size = null, $shardSize = null) { parent::__construct($name); @@ -120,7 +120,7 @@ public function setShardSize($shardSize) /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { $data = []; @@ -148,7 +148,7 @@ public function getArray() /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'geohash_grid'; } diff --git a/src/Aggregation/Bucketing/GlobalAggregation.php b/src/Aggregation/Bucketing/GlobalAggregation.php index badd85cd..4e4fac3f 100644 --- a/src/Aggregation/Bucketing/GlobalAggregation.php +++ b/src/Aggregation/Bucketing/GlobalAggregation.php @@ -26,7 +26,7 @@ class GlobalAggregation extends AbstractAggregation /** * {@inheritdoc} */ - public function setField($field) + public function setField($field): void { throw new \LogicException("Global aggregation, doesn't support `field` parameter"); } @@ -34,7 +34,7 @@ public function setField($field) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'global'; } @@ -42,8 +42,8 @@ public function getType() /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { - return new \stdClass(); + return []; } } diff --git a/src/Aggregation/Bucketing/HistogramAggregation.php b/src/Aggregation/Bucketing/HistogramAggregation.php index f2387585..eb842377 100644 --- a/src/Aggregation/Bucketing/HistogramAggregation.php +++ b/src/Aggregation/Bucketing/HistogramAggregation.php @@ -23,8 +23,8 @@ class HistogramAggregation extends AbstractAggregation { use BucketingTrait; - const DIRECTION_ASC = 'asc'; - const DIRECTION_DESC = 'desc'; + final public const DIRECTION_ASC = 'asc'; + final public const DIRECTION_DESC = 'desc'; /** * @var int @@ -198,13 +198,10 @@ public function getExtendedBounds() */ public function setExtendedBounds($min = null, $max = null) { - $bounds = array_filter( - [ - 'min' => $min, - 'max' => $max, - ], - 'strlen' - ); + $bounds = [ + 'min' => $min ?? '', + 'max' => $max ?? '', + ]; $this->extendedBounds = $bounds; return $this; @@ -213,7 +210,7 @@ public function setExtendedBounds($min = null, $max = null) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'histogram'; } @@ -221,7 +218,7 @@ public function getType() /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { $out = array_filter( [ @@ -232,9 +229,7 @@ public function getArray() 'keyed' => $this->isKeyed(), 'order' => $this->getOrder(), ], - function ($val) { - return ($val || is_numeric($val)); - } + static fn($val): bool => $val || is_numeric($val) ); $this->checkRequiredParameters($out, ['field', 'interval']); diff --git a/src/Aggregation/Bucketing/Ipv4RangeAggregation.php b/src/Aggregation/Bucketing/Ipv4RangeAggregation.php index 0579aaf8..7538896e 100644 --- a/src/Aggregation/Bucketing/Ipv4RangeAggregation.php +++ b/src/Aggregation/Bucketing/Ipv4RangeAggregation.php @@ -23,27 +23,20 @@ class Ipv4RangeAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var array - */ - private $ranges = []; + private array $ranges = []; /** * Inner aggregations container init. - * - * @param string $name - * @param string $field - * @param array $ranges */ - public function __construct($name, $field = null, $ranges = []) + public function __construct(string $name, ?string $field = null, array $ranges = []) { parent::__construct($name); $this->setField($field); foreach ($ranges as $range) { if (is_array($range)) { - $from = isset($range['from']) ? $range['from'] : null; - $to = isset($range['to']) ? $range['to'] : null; + $from = $range['from'] ?? null; + $to = $range['to'] ?? null; $this->addRange($from, $to); } else { $this->addMask($range); @@ -53,22 +46,15 @@ public function __construct($name, $field = null, $ranges = []) /** * Add range to aggregation. - * - * @param string|null $from - * @param string|null $to - * - * @return Ipv4RangeAggregation */ - public function addRange($from = null, $to = null) + public function addRange(mixed $from = null, mixed $to = null): static { $range = array_filter( [ 'from' => $from, 'to' => $to, ], - function ($v) { - return !is_null($v); - } + static fn($v): bool => !is_null($v) ); $this->ranges[] = $range; @@ -78,12 +64,8 @@ function ($v) { /** * Add ip mask to aggregation. - * - * @param string $mask - * - * @return Ipv4RangeAggregation */ - public function addMask($mask) + public function addMask(string $mask): static { $this->ranges[] = ['mask' => $mask]; @@ -93,7 +75,7 @@ public function addMask($mask) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'ip_range'; } @@ -101,7 +83,7 @@ public function getType() /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { if ($this->getField() && !empty($this->ranges)) { return [ @@ -109,6 +91,7 @@ public function getArray() 'ranges' => array_values($this->ranges), ]; } + throw new \LogicException('Ip range aggregation must have field set and range added.'); } } diff --git a/src/Aggregation/Bucketing/MissingAggregation.php b/src/Aggregation/Bucketing/MissingAggregation.php index 2cffa14b..c3eb5cc3 100644 --- a/src/Aggregation/Bucketing/MissingAggregation.php +++ b/src/Aggregation/Bucketing/MissingAggregation.php @@ -25,11 +25,8 @@ class MissingAggregation extends AbstractAggregation /** * Inner aggregations container init. - * - * @param string $name - * @param string $field */ - public function __construct($name, $field = null) + public function __construct(string $name, mixed $field = null) { parent::__construct($name); @@ -39,18 +36,19 @@ public function __construct($name, $field = null) /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { if ($this->getField()) { return ['field' => $this->getField()]; } + throw new \LogicException('Missing aggregation must have a field set.'); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'missing'; } diff --git a/src/Aggregation/Bucketing/NestedAggregation.php b/src/Aggregation/Bucketing/NestedAggregation.php index 39e3d9e2..e54ea12d 100644 --- a/src/Aggregation/Bucketing/NestedAggregation.php +++ b/src/Aggregation/Bucketing/NestedAggregation.php @@ -34,7 +34,7 @@ class NestedAggregation extends AbstractAggregation * @param string $name * @param string $path */ - public function __construct($name, $path = null) + public function __construct(string $name, $path = null) { parent::__construct($name); @@ -66,7 +66,7 @@ public function setPath($path) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'nested'; } @@ -74,7 +74,7 @@ public function getType() /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { return ['path' => $this->getPath()]; } diff --git a/src/Aggregation/Bucketing/RangeAggregation.php b/src/Aggregation/Bucketing/RangeAggregation.php index e86faedf..12aaf419 100644 --- a/src/Aggregation/Bucketing/RangeAggregation.php +++ b/src/Aggregation/Bucketing/RangeAggregation.php @@ -23,46 +23,30 @@ class RangeAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var array - */ - private $ranges = []; - - /** - * @var bool - */ - private $keyed = false; + private array $ranges = []; + private bool $keyed = false; /** * Inner aggregations container init. - * - * @param string $name - * @param string $field - * @param array $ranges - * @param bool $keyed */ - public function __construct($name, $field = null, $ranges = [], $keyed = false) + public function __construct(string $name, ?string $field = null, array $ranges = [], bool $keyed = false) { parent::__construct($name); $this->setField($field); $this->setKeyed($keyed); foreach ($ranges as $range) { - $from = isset($range['from']) ? $range['from'] : null; - $to = isset($range['to']) ? $range['to'] : null; - $key = isset($range['key']) ? $range['key'] : null; + $from = $range['from'] ?? null; + $to = $range['to'] ?? null; + $key = $range['key'] ?? null; $this->addRange($from, $to, $key); } } /** * Sets if result buckets should be keyed. - * - * @param bool $keyed - * - * @return $this */ - public function setKeyed($keyed) + public function setKeyed(bool $keyed): static { $this->keyed = $keyed; @@ -71,23 +55,15 @@ public function setKeyed($keyed) /** * Add range to aggregation. - * - * @param int|float|null $from - * @param int|float|null $to - * @param string $key - * - * @return RangeAggregation */ - public function addRange($from = null, $to = null, $key = '') + public function addRange(mixed $from = null, mixed $to = null, string $key = ''): static { $range = array_filter( [ 'from' => $from, 'to' => $to, ], - function ($v) { - return !is_null($v); - } + static fn($v): bool => !is_null($v) ); if (!empty($key)) { @@ -101,13 +77,8 @@ function ($v) { /** * Remove range from aggregation. Returns true on success. - * - * @param int|float|null $from - * @param int|float|null $to - * - * @return bool */ - public function removeRange($from, $to) + public function removeRange(mixed $from, mixed $to): bool { foreach ($this->ranges as $key => $range) { if (array_diff_assoc(array_filter(['from' => $from, 'to' => $to]), $range) === []) { @@ -122,12 +93,8 @@ public function removeRange($from, $to) /** * Removes range by key. - * - * @param string $key Range key. - * - * @return bool */ - public function removeRangeByKey($key) + public function removeRangeByKey(string $key): bool { if ($this->keyed) { foreach ($this->ranges as $rangeKey => $range) { @@ -145,7 +112,7 @@ public function removeRangeByKey($key) /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { $data = [ 'keyed' => $this->keyed, @@ -162,7 +129,7 @@ public function getArray() /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'range'; } diff --git a/src/Aggregation/Bucketing/ReverseNestedAggregation.php b/src/Aggregation/Bucketing/ReverseNestedAggregation.php index 6cda4b8c..c14b0e8e 100644 --- a/src/Aggregation/Bucketing/ReverseNestedAggregation.php +++ b/src/Aggregation/Bucketing/ReverseNestedAggregation.php @@ -23,18 +23,12 @@ class ReverseNestedAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var string - */ - private $path; + private ?string $path; /** * Inner aggregations container init. - * - * @param string $name - * @param string $path */ - public function __construct($name, $path = null) + public function __construct(string $name, ?string $path = null) { parent::__construct($name); @@ -46,7 +40,7 @@ public function __construct($name, $path = null) * * @return string */ - public function getPath() + public function getPath(): ?string { return $this->path; } @@ -56,7 +50,7 @@ public function getPath() * * @return $this */ - public function setPath($path) + public function setPath(?string $path): static { $this->path = $path; @@ -66,7 +60,7 @@ public function setPath($path) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'reverse_nested'; } @@ -74,9 +68,9 @@ public function getType() /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { - $output = new \stdClass(); + $output = []; if ($this->getPath()) { $output = ['path' => $this->getPath()]; } diff --git a/src/Aggregation/Bucketing/SamplerAggregation.php b/src/Aggregation/Bucketing/SamplerAggregation.php index aa90b460..3bdc0319 100644 --- a/src/Aggregation/Bucketing/SamplerAggregation.php +++ b/src/Aggregation/Bucketing/SamplerAggregation.php @@ -36,7 +36,7 @@ class SamplerAggregation extends AbstractAggregation * @param string $field * @param int $shardSize */ - public function __construct($name, $field = null, $shardSize = null) + public function __construct(string $name, $field = null, $shardSize = null) { parent::__construct($name); @@ -67,7 +67,7 @@ public function setShardSize($shardSize) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'sampler'; } @@ -75,15 +75,13 @@ public function getType() /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { - $out = array_filter( + return array_filter( [ 'field' => $this->getField(), 'shard_size' => $this->getShardSize(), ] ); - - return $out; } } diff --git a/src/Aggregation/Bucketing/SignificantTermsAggregation.php b/src/Aggregation/Bucketing/SignificantTermsAggregation.php index d3844dec..6f112e5d 100644 --- a/src/Aggregation/Bucketing/SignificantTermsAggregation.php +++ b/src/Aggregation/Bucketing/SignificantTermsAggregation.php @@ -21,7 +21,7 @@ class SignificantTermsAggregation extends TermsAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'significant_terms'; } diff --git a/src/Aggregation/Bucketing/SignificantTextAggregation.php b/src/Aggregation/Bucketing/SignificantTextAggregation.php index 4b6bd5a3..5e21de3e 100644 --- a/src/Aggregation/Bucketing/SignificantTextAggregation.php +++ b/src/Aggregation/Bucketing/SignificantTextAggregation.php @@ -21,7 +21,7 @@ class SignificantTextAggregation extends TermsAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'significant_text'; } diff --git a/src/Aggregation/Bucketing/TermsAggregation.php b/src/Aggregation/Bucketing/TermsAggregation.php index b1fe6c54..7ada6720 100644 --- a/src/Aggregation/Bucketing/TermsAggregation.php +++ b/src/Aggregation/Bucketing/TermsAggregation.php @@ -32,7 +32,7 @@ class TermsAggregation extends AbstractAggregation * @param string $field * @param string $script */ - public function __construct($name, $field = null, $script = null) + public function __construct(string $name, $field = null, $script = null) { parent::__construct($name); @@ -43,7 +43,7 @@ public function __construct($name, $field = null, $script = null) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'terms'; } @@ -51,15 +51,13 @@ public function getType() /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { - $data = array_filter( + return array_filter( [ 'field' => $this->getField(), 'script' => $this->getScript(), ] ); - - return $data; } } diff --git a/src/Aggregation/Matrix/MatrixStatsAggregation.php b/src/Aggregation/Matrix/MatrixStatsAggregation.php index 619aae90..f9cf983a 100644 --- a/src/Aggregation/Matrix/MatrixStatsAggregation.php +++ b/src/Aggregation/Matrix/MatrixStatsAggregation.php @@ -17,9 +17,9 @@ /** * Class representing Max Aggregation. * - * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-matrix-stats-aggregation.html */ -class MaxAggregation extends AbstractAggregation +class MatrixStatsAggregation extends AbstractAggregation { use MetricTrait; @@ -28,11 +28,6 @@ class MaxAggregation extends AbstractAggregation */ private $mode; - /** - * @var array Defines how documents that are missing a value should be treated. - */ - private $missing; - /** * Inner aggregations container init. * @@ -41,13 +36,12 @@ class MaxAggregation extends AbstractAggregation * @param array $missing * @param string $mode */ - public function __construct($name, $field, $missing = null, $mode = null) + public function __construct(string $name, $field, private $missing = null, $mode = null) { parent::__construct($name); $this->setField($field); $this->setMode($mode); - $this->missing = $missing; } /** @@ -93,12 +87,12 @@ public function setMissing($missing) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'matrix_stats'; } - protected function getArray() + public function getArray(): array { $out = []; if ($this->getField()) { diff --git a/src/Aggregation/Metric/AvgAggregation.php b/src/Aggregation/Metric/AvgAggregation.php index 412c6764..8931b771 100644 --- a/src/Aggregation/Metric/AvgAggregation.php +++ b/src/Aggregation/Metric/AvgAggregation.php @@ -21,7 +21,7 @@ class AvgAggregation extends StatsAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'avg'; } diff --git a/src/Aggregation/Metric/CardinalityAggregation.php b/src/Aggregation/Metric/CardinalityAggregation.php index bcaaaf65..51486d5e 100644 --- a/src/Aggregation/Metric/CardinalityAggregation.php +++ b/src/Aggregation/Metric/CardinalityAggregation.php @@ -38,7 +38,7 @@ class CardinalityAggregation extends AbstractAggregation /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { $out = array_filter( [ @@ -47,9 +47,7 @@ public function getArray() 'precision_threshold' => $this->getPrecisionThreshold(), 'rehash' => $this->isRehash(), ], - function ($val) { - return ($val || is_bool($val)); - } + static fn($val): bool => $val || is_bool($val) ); $this->checkRequiredFields($out); @@ -100,7 +98,7 @@ public function setRehash($rehash) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'cardinality'; } @@ -108,11 +106,10 @@ public function getType() /** * Checks if required fields are set. * - * @param array $fields * * @throws \LogicException */ - private function checkRequiredFields($fields) + private function checkRequiredFields(array $fields) { if (!array_key_exists('field', $fields) && !array_key_exists('script', $fields)) { throw new \LogicException('Cardinality aggregation must have field or script set.'); diff --git a/src/Aggregation/Metric/ExtendedStatsAggregation.php b/src/Aggregation/Metric/ExtendedStatsAggregation.php index 660dc0d0..5fdcb9c1 100644 --- a/src/Aggregation/Metric/ExtendedStatsAggregation.php +++ b/src/Aggregation/Metric/ExtendedStatsAggregation.php @@ -33,7 +33,7 @@ class ExtendedStatsAggregation extends AbstractAggregation * @param int $sigma * @param string $script */ - public function __construct($name, $field = null, $sigma = null, $script = null) + public function __construct(string $name, $field = null, $sigma = null, $script = null) { parent::__construct($name); @@ -70,7 +70,7 @@ public function setSigma($sigma) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'extended_stats'; } @@ -78,19 +78,15 @@ public function getType() /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { - $out = array_filter( + return array_filter( [ 'field' => $this->getField(), 'script' => $this->getScript(), 'sigma' => $this->getSigma(), ], - function ($val) { - return ($val || is_numeric($val)); - } + static fn($val): bool => $val || is_numeric($val) ); - - return $out; } } diff --git a/src/Aggregation/Metric/GeoBoundsAggregation.php b/src/Aggregation/Metric/GeoBoundsAggregation.php index 3f01a1dd..13c35039 100644 --- a/src/Aggregation/Metric/GeoBoundsAggregation.php +++ b/src/Aggregation/Metric/GeoBoundsAggregation.php @@ -23,19 +23,12 @@ class GeoBoundsAggregation extends AbstractAggregation { use MetricTrait; - /** - * @var bool - */ - private $wrapLongitude = true; + private bool $wrapLongitude = true; /** * Inner aggregations container init. - * - * @param string $name - * @param string $field - * @param bool $wrapLongitude */ - public function __construct($name, $field = null, $wrapLongitude = true) + public function __construct(string $name, ?string $field = null, bool $wrapLongitude = true) { parent::__construct($name); @@ -43,20 +36,15 @@ public function __construct($name, $field = null, $wrapLongitude = true) $this->setWrapLongitude($wrapLongitude); } - /** - * @return bool - */ - public function isWrapLongitude() + public function isWrapLongitude(): bool { return $this->wrapLongitude; } /** - * @param bool $wrapLongitude - * * @return $this */ - public function setWrapLongitude($wrapLongitude) + public function setWrapLongitude(bool $wrapLongitude): static { $this->wrapLongitude = $wrapLongitude; @@ -66,7 +54,7 @@ public function setWrapLongitude($wrapLongitude) /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { $data = []; if ($this->getField()) { @@ -83,7 +71,7 @@ public function getArray() /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'geo_bounds'; } diff --git a/src/Aggregation/Metric/GeoCentroidAggregation.php b/src/Aggregation/Metric/GeoCentroidAggregation.php index 807c81cb..3b0529a5 100644 --- a/src/Aggregation/Metric/GeoCentroidAggregation.php +++ b/src/Aggregation/Metric/GeoCentroidAggregation.php @@ -25,11 +25,8 @@ class GeoCentroidAggregation extends AbstractAggregation /** * Inner aggregations container init. - * - * @param string $name - * @param string $field */ - public function __construct($name, $field = null) + public function __construct(string $name, ?string $field = null) { parent::__construct($name); @@ -39,7 +36,7 @@ public function __construct($name, $field = null) /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { $data = []; if ($this->getField()) { @@ -54,7 +51,7 @@ public function getArray() /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'geo_centroid'; } diff --git a/src/Aggregation/Metric/MaxAggregation.php b/src/Aggregation/Metric/MaxAggregation.php index 7879e1d3..b2a20444 100644 --- a/src/Aggregation/Metric/MaxAggregation.php +++ b/src/Aggregation/Metric/MaxAggregation.php @@ -21,7 +21,7 @@ class MaxAggregation extends StatsAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'max'; } diff --git a/src/Aggregation/Metric/MinAggregation.php b/src/Aggregation/Metric/MinAggregation.php index a92f9432..14240e93 100644 --- a/src/Aggregation/Metric/MinAggregation.php +++ b/src/Aggregation/Metric/MinAggregation.php @@ -21,7 +21,7 @@ class MinAggregation extends StatsAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'min'; } diff --git a/src/Aggregation/Metric/PercentileRanksAggregation.php b/src/Aggregation/Metric/PercentileRanksAggregation.php index d0fb2c0a..2f7969f4 100644 --- a/src/Aggregation/Metric/PercentileRanksAggregation.php +++ b/src/Aggregation/Metric/PercentileRanksAggregation.php @@ -44,7 +44,7 @@ class PercentileRanksAggregation extends AbstractAggregation * @param string $script * @param int $compression */ - public function __construct($name, $field = null, $values = null, $script = null, $compression = null) + public function __construct(string $name, $field = null, $values = null, $script = null, $compression = null) { parent::__construct($name); @@ -97,7 +97,7 @@ public function setCompression($compression) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'percentile_ranks'; } @@ -105,7 +105,7 @@ public function getType() /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { $out = array_filter( [ @@ -114,9 +114,7 @@ public function getArray() 'values' => $this->getValues(), 'compression' => $this->getCompression(), ], - function ($val) { - return ($val || is_numeric($val)); - } + static fn($val): bool => $val || is_numeric($val) ); $this->isRequiredParametersSet($out); @@ -125,18 +123,18 @@ function ($val) { } /** - * @param array $a * * @return bool * @throws \LogicException */ - private function isRequiredParametersSet($a) + private function isRequiredParametersSet(array $a) { if (array_key_exists('field', $a) && array_key_exists('values', $a) || (array_key_exists('script', $a) && array_key_exists('values', $a)) ) { return true; } + throw new \LogicException('Percentile ranks aggregation must have field and values or script and values set.'); } } diff --git a/src/Aggregation/Metric/PercentilesAggregation.php b/src/Aggregation/Metric/PercentilesAggregation.php index 627bb568..385ef4bf 100644 --- a/src/Aggregation/Metric/PercentilesAggregation.php +++ b/src/Aggregation/Metric/PercentilesAggregation.php @@ -44,7 +44,7 @@ class PercentilesAggregation extends AbstractAggregation * @param string $script * @param int $compression */ - public function __construct($name, $field = null, $percents = null, $script = null, $compression = null) + public function __construct(string $name, $field = null, $percents = null, $script = null, $compression = null) { parent::__construct($name); @@ -97,7 +97,7 @@ public function setCompression($compression) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'percentiles'; } @@ -105,7 +105,7 @@ public function getType() /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { $out = array_filter( [ @@ -114,9 +114,7 @@ public function getArray() 'field' => $this->getField(), 'script' => $this->getScript(), ], - function ($val) { - return ($val || is_numeric($val)); - } + static fn($val): bool => $val || is_numeric($val) ); $this->isRequiredParametersSet($out); @@ -125,11 +123,9 @@ function ($val) { } /** - * @param array $a - * * @throws \LogicException */ - private function isRequiredParametersSet($a) + private function isRequiredParametersSet(array $a) { if (!array_key_exists('field', $a) && !array_key_exists('script', $a)) { throw new \LogicException('Percentiles aggregation must have field or script set.'); diff --git a/src/Aggregation/Metric/ScriptedMetricAggregation.php b/src/Aggregation/Metric/ScriptedMetricAggregation.php index a88412f7..903c6e9e 100644 --- a/src/Aggregation/Metric/ScriptedMetricAggregation.php +++ b/src/Aggregation/Metric/ScriptedMetricAggregation.php @@ -43,6 +43,7 @@ class ScriptedMetricAggregation extends AbstractAggregation * @var mixed */ private $reduceScript; + /** * ScriptedMetricAggregation constructor. * @param string $name @@ -58,7 +59,7 @@ public function __construct( $combineScript = null, $reduceScript = null ) { - + parent::__construct($name); $this->setInitScript($initScript); @@ -70,7 +71,7 @@ public function __construct( /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'scripted_metric'; } @@ -84,11 +85,9 @@ public function getInitScript() } /** - * @param mixed $initScript - * * @return $this */ - public function setInitScript($initScript) + public function setInitScript(mixed $initScript) { $this->initScript = $initScript; @@ -104,11 +103,9 @@ public function getMapScript() } /** - * @param mixed $mapScript - * * @return $this */ - public function setMapScript($mapScript) + public function setMapScript(mixed $mapScript) { $this->mapScript = $mapScript; @@ -124,11 +121,9 @@ public function getCombineScript() } /** - * @param mixed $combineScript - * * @return $this */ - public function setCombineScript($combineScript) + public function setCombineScript(mixed $combineScript) { $this->combineScript = $combineScript; @@ -144,11 +139,9 @@ public function getReduceScript() } /** - * @param mixed $reduceScript - * * @return $this */ - public function setReduceScript($reduceScript) + public function setReduceScript(mixed $reduceScript) { $this->reduceScript = $reduceScript; @@ -158,9 +151,9 @@ public function setReduceScript($reduceScript) /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { - $out = array_filter( + return array_filter( [ 'init_script' => $this->getInitScript(), 'map_script' => $this->getMapScript(), @@ -168,7 +161,5 @@ public function getArray() 'reduce_script' => $this->getReduceScript(), ] ); - - return $out; } } diff --git a/src/Aggregation/Metric/StatsAggregation.php b/src/Aggregation/Metric/StatsAggregation.php index c9fd2ebc..111c6481 100644 --- a/src/Aggregation/Metric/StatsAggregation.php +++ b/src/Aggregation/Metric/StatsAggregation.php @@ -27,12 +27,8 @@ class StatsAggregation extends AbstractAggregation /** * Inner aggregations container init. - * - * @param string $name - * @param string $field - * @param string $script */ - public function __construct($name, $field = null, $script = null) + public function __construct(string $name, ?string $field = null, mixed $script = null) { parent::__construct($name); @@ -43,7 +39,7 @@ public function __construct($name, $field = null, $script = null) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'stats'; } @@ -51,7 +47,7 @@ public function getType() /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { $out = []; if ($this->getField()) { diff --git a/src/Aggregation/Metric/SumAggregation.php b/src/Aggregation/Metric/SumAggregation.php index ba2ddbc5..6da8a8b9 100644 --- a/src/Aggregation/Metric/SumAggregation.php +++ b/src/Aggregation/Metric/SumAggregation.php @@ -21,7 +21,7 @@ class SumAggregation extends StatsAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'sum'; } diff --git a/src/Aggregation/Metric/TopHitsAggregation.php b/src/Aggregation/Metric/TopHitsAggregation.php index 01225666..d81ee3d9 100644 --- a/src/Aggregation/Metric/TopHitsAggregation.php +++ b/src/Aggregation/Metric/TopHitsAggregation.php @@ -25,52 +25,44 @@ class TopHitsAggregation extends AbstractAggregation use MetricTrait; /** - * @var int Number of top matching hits to return per bucket. + * Number of top matching hits to return per bucket. */ - private $size; + private ?int $size; /** - * @var int The offset from the first result you want to fetch. + * The offset from the first result you want to fetch. */ - private $from; + private ?int $from; /** * @var BuilderInterface[] How the top matching hits should be sorted. */ - private $sorts = []; + 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 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); $this->setSize($size); - $this->addSort($sort); + if ($sort instanceof BuilderInterface) { + $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($from): static { $this->from = $from; @@ -80,7 +72,7 @@ public function setFrom($from) /** * @return BuilderInterface[] */ - public function getSorts() + public function getSorts(): array { return $this->sorts; } @@ -90,41 +82,26 @@ public function getSorts() * * @return $this */ - public function setSorts(array $sorts) + public function setSorts(array $sorts): static { $this->sorts = $sorts; return $this; } - /** - * Add sort. - * - * @param BuilderInterface $sort - */ - public function addSort($sort) + public function addSort(BuilderInterface $sort): void { $this->sorts[] = $sort; } - /** - * @param int $size - * - * @return $this - */ - public function setSize($size) + public function setSize(int $size): static { $this->size = $size; return $this; } - /** - * Return size. - * - * @return int - */ - public function getSize() + public function getSize(): ?int { return $this->size; } @@ -132,7 +109,7 @@ public function getSize() /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'top_hits'; } @@ -140,16 +117,14 @@ public function getType() /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { - $sortsOutput = []; + $sortsOutput = null; $addedSorts = array_filter($this->getSorts()); if ($addedSorts) { foreach ($addedSorts as $sort) { $sortsOutput[] = $sort->toArray(); } - } else { - $sortsOutput = null; } $output = array_filter( @@ -158,40 +133,9 @@ public function getArray() 'size' => $this->getSize(), 'from' => $this->getFrom(), ], - function ($val) { - return (($val || is_array($val) || ($val || is_numeric($val)))); - } + static fn($val): bool => $val || is_array($val) || ($val || is_numeric($val)) ); - 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. - * - * @param BuilderInterface $sort - * - * @return $this - */ - public function setSort(BuilderInterface $sort) - { - $this->sort = $sort; - - return $this; + return empty($output) ? [] : $output; } } diff --git a/src/Aggregation/Metric/ValueCountAggregation.php b/src/Aggregation/Metric/ValueCountAggregation.php index 617a966e..06635412 100644 --- a/src/Aggregation/Metric/ValueCountAggregation.php +++ b/src/Aggregation/Metric/ValueCountAggregation.php @@ -21,7 +21,7 @@ class ValueCountAggregation extends StatsAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'value_count'; } diff --git a/src/Aggregation/Pipeline/AbstractPipelineAggregation.php b/src/Aggregation/Pipeline/AbstractPipelineAggregation.php index 4259c057..b797d0f1 100644 --- a/src/Aggregation/Pipeline/AbstractPipelineAggregation.php +++ b/src/Aggregation/Pipeline/AbstractPipelineAggregation.php @@ -18,7 +18,7 @@ abstract class AbstractPipelineAggregation extends AbstractAggregation * @param string $name * @param $bucketsPath */ - public function __construct($name, $bucketsPath = null) + public function __construct(string $name, $bucketsPath = null) { parent::__construct($name); $this->setBucketsPath($bucketsPath); @@ -47,7 +47,7 @@ public function setBucketsPath($bucketsPath) /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { return ['buckets_path' => $this->getBucketsPath()]; } diff --git a/src/Aggregation/Pipeline/AvgBucketAggregation.php b/src/Aggregation/Pipeline/AvgBucketAggregation.php index 69e0f93f..242b1dcf 100644 --- a/src/Aggregation/Pipeline/AvgBucketAggregation.php +++ b/src/Aggregation/Pipeline/AvgBucketAggregation.php @@ -21,7 +21,7 @@ class AvgBucketAggregation extends AbstractPipelineAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'avg_bucket'; } diff --git a/src/Aggregation/Pipeline/BucketScriptAggregation.php b/src/Aggregation/Pipeline/BucketScriptAggregation.php index f0f4c615..a39d7755 100644 --- a/src/Aggregation/Pipeline/BucketScriptAggregation.php +++ b/src/Aggregation/Pipeline/BucketScriptAggregation.php @@ -18,17 +18,9 @@ */ class BucketScriptAggregation extends AbstractPipelineAggregation { - /** - * @var string - */ - private $script; + private mixed $script; - /** - * @param string $name - * @param array $bucketsPath - * @param string $script - */ - public function __construct($name, $bucketsPath, $script = null) + public function __construct(string $name, $bucketsPath, mixed $script = null) { parent::__construct($name, $bucketsPath); $this->setScript($script); @@ -37,17 +29,12 @@ public function __construct($name, $bucketsPath, $script = null) /** * @return string */ - public function getScript() + public function getScript(): mixed { return $this->script; } - /** - * @param string $script - * - * @return $this - */ - public function setScript($script) + public function setScript(mixed $script): static { $this->script = $script; @@ -57,7 +44,7 @@ public function setScript($script) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'bucket_script'; } @@ -65,7 +52,7 @@ public function getType() /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { if (!$this->getScript()) { throw new \LogicException( @@ -76,11 +63,9 @@ public function getArray() ); } - $out = [ + return [ 'buckets_path' => $this->getBucketsPath(), 'script' => $this->getScript(), ]; - - return $out; } } diff --git a/src/Aggregation/Pipeline/BucketSelectorAggregation.php b/src/Aggregation/Pipeline/BucketSelectorAggregation.php index cd8c41b3..2c8de4e2 100644 --- a/src/Aggregation/Pipeline/BucketSelectorAggregation.php +++ b/src/Aggregation/Pipeline/BucketSelectorAggregation.php @@ -21,7 +21,7 @@ class BucketSelectorAggregation extends BucketScriptAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'bucket_selector'; } diff --git a/src/Aggregation/Pipeline/BucketSortAggregation.php b/src/Aggregation/Pipeline/BucketSortAggregation.php index 06dd5dc5..3c09af15 100644 --- a/src/Aggregation/Pipeline/BucketSortAggregation.php +++ b/src/Aggregation/Pipeline/BucketSortAggregation.php @@ -21,24 +21,18 @@ */ class BucketSortAggregation extends AbstractPipelineAggregation { - /** - * @var array - */ - private $sort = []; + private array $sort = []; /** * @param string $name * @param string $bucketsPath */ - public function __construct($name, $bucketsPath = null) + public function __construct(string $name, $bucketsPath = null) { parent::__construct($name, $bucketsPath); } - /** - * @return array - */ - public function getSort() + public function getSort(): array { return $this->sort; } @@ -46,7 +40,7 @@ public function getSort() /** * @return self */ - public function addSort(FieldSort $sort) + public function addSort(FieldSort $sort): void { $this->sort[] = $sort->toArray(); } @@ -56,7 +50,7 @@ public function addSort(FieldSort $sort) * * @return $this */ - public function setSort($sort) + public function setSort(array $sort) { $this->sort = $sort; @@ -66,7 +60,7 @@ public function setSort($sort) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'bucket_sort'; } @@ -74,15 +68,13 @@ public function getType() /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { - $out = array_filter( + return array_filter( [ 'buckets_path' => $this->getBucketsPath(), 'sort' => $this->getSort(), ] ); - - return $out; } } diff --git a/src/Aggregation/Pipeline/CumulativeSumAggregation.php b/src/Aggregation/Pipeline/CumulativeSumAggregation.php index dff8a9cb..0e059dd7 100644 --- a/src/Aggregation/Pipeline/CumulativeSumAggregation.php +++ b/src/Aggregation/Pipeline/CumulativeSumAggregation.php @@ -21,7 +21,7 @@ class CumulativeSumAggregation extends AbstractPipelineAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'cumulative_sum'; } diff --git a/src/Aggregation/Pipeline/DerivativeAggregation.php b/src/Aggregation/Pipeline/DerivativeAggregation.php index ad030b01..e37fdb06 100644 --- a/src/Aggregation/Pipeline/DerivativeAggregation.php +++ b/src/Aggregation/Pipeline/DerivativeAggregation.php @@ -21,7 +21,7 @@ class DerivativeAggregation extends AbstractPipelineAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'derivative'; } diff --git a/src/Aggregation/Pipeline/ExtendedStatsBucketAggregation.php b/src/Aggregation/Pipeline/ExtendedStatsBucketAggregation.php index aa8697a7..f3c00d52 100644 --- a/src/Aggregation/Pipeline/ExtendedStatsBucketAggregation.php +++ b/src/Aggregation/Pipeline/ExtendedStatsBucketAggregation.php @@ -21,7 +21,7 @@ class ExtendedStatsBucketAggregation extends AbstractPipelineAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'extended_stats_bucket'; } diff --git a/src/Aggregation/Pipeline/MaxBucketAggregation.php b/src/Aggregation/Pipeline/MaxBucketAggregation.php index ea51d682..202b89bf 100644 --- a/src/Aggregation/Pipeline/MaxBucketAggregation.php +++ b/src/Aggregation/Pipeline/MaxBucketAggregation.php @@ -21,7 +21,7 @@ class MaxBucketAggregation extends AbstractPipelineAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'max_bucket'; } diff --git a/src/Aggregation/Pipeline/MinBucketAggregation.php b/src/Aggregation/Pipeline/MinBucketAggregation.php index 8c4d64f8..8ed21408 100644 --- a/src/Aggregation/Pipeline/MinBucketAggregation.php +++ b/src/Aggregation/Pipeline/MinBucketAggregation.php @@ -21,7 +21,7 @@ class MinBucketAggregation extends AbstractPipelineAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'min_bucket'; } diff --git a/src/Aggregation/Pipeline/MovingAverageAggregation.php b/src/Aggregation/Pipeline/MovingAverageAggregation.php index e93fdc5d..e3ddf780 100644 --- a/src/Aggregation/Pipeline/MovingAverageAggregation.php +++ b/src/Aggregation/Pipeline/MovingAverageAggregation.php @@ -21,7 +21,7 @@ class MovingAverageAggregation extends AbstractPipelineAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'moving_avg'; } diff --git a/src/Aggregation/Pipeline/MovingFunctionAggregation.php b/src/Aggregation/Pipeline/MovingFunctionAggregation.php index 70d587e8..1f5ae999 100644 --- a/src/Aggregation/Pipeline/MovingFunctionAggregation.php +++ b/src/Aggregation/Pipeline/MovingFunctionAggregation.php @@ -21,7 +21,7 @@ class MovingFunctionAggregation extends AbstractPipelineAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'moving_fn'; } diff --git a/src/Aggregation/Pipeline/PercentilesBucketAggregation.php b/src/Aggregation/Pipeline/PercentilesBucketAggregation.php index 6c0c9a3c..83d7c434 100644 --- a/src/Aggregation/Pipeline/PercentilesBucketAggregation.php +++ b/src/Aggregation/Pipeline/PercentilesBucketAggregation.php @@ -18,30 +18,25 @@ */ class PercentilesBucketAggregation extends AbstractPipelineAggregation { - /** - * @var array - */ - private $percents; + private ?array $percents = null; /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'percentiles_bucket'; } /** - * @return array + * @return mixed[]|null */ - public function getPercents() + public function getPercents(): ?array { return $this->percents; } /** - * @param array $percents - * * @return $this */ public function setPercents(array $percents) @@ -54,7 +49,7 @@ public function setPercents(array $percents) /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { $data = ['buckets_path' => $this->getBucketsPath()]; diff --git a/src/Aggregation/Pipeline/SerialDifferencingAggregation.php b/src/Aggregation/Pipeline/SerialDifferencingAggregation.php index 715c0706..6c99c260 100644 --- a/src/Aggregation/Pipeline/SerialDifferencingAggregation.php +++ b/src/Aggregation/Pipeline/SerialDifferencingAggregation.php @@ -21,7 +21,7 @@ class SerialDifferencingAggregation extends AbstractPipelineAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'serial_diff'; } diff --git a/src/Aggregation/Pipeline/StatsBucketAggregation.php b/src/Aggregation/Pipeline/StatsBucketAggregation.php index 43d6b620..0ae26132 100644 --- a/src/Aggregation/Pipeline/StatsBucketAggregation.php +++ b/src/Aggregation/Pipeline/StatsBucketAggregation.php @@ -21,7 +21,7 @@ class StatsBucketAggregation extends AbstractPipelineAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'stats_bucket'; } diff --git a/src/Aggregation/Pipeline/SumBucketAggregation.php b/src/Aggregation/Pipeline/SumBucketAggregation.php index b03859ae..82789e21 100644 --- a/src/Aggregation/Pipeline/SumBucketAggregation.php +++ b/src/Aggregation/Pipeline/SumBucketAggregation.php @@ -21,7 +21,7 @@ class SumBucketAggregation extends AbstractPipelineAggregation /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'sum_bucket'; } diff --git a/src/Aggregation/Type/BucketingTrait.php b/src/Aggregation/Type/BucketingTrait.php index 6497ecb2..ab64d439 100644 --- a/src/Aggregation/Type/BucketingTrait.php +++ b/src/Aggregation/Type/BucketingTrait.php @@ -18,10 +18,8 @@ trait BucketingTrait { /** * Bucketing aggregations supports nesting. - * - * @return bool */ - protected function supportsNesting() + protected function supportsNesting(): bool { return true; } diff --git a/src/Aggregation/Type/MetricTrait.php b/src/Aggregation/Type/MetricTrait.php index bdfbd2d1..8bd5b5e6 100644 --- a/src/Aggregation/Type/MetricTrait.php +++ b/src/Aggregation/Type/MetricTrait.php @@ -18,10 +18,8 @@ trait MetricTrait { /** * Metric aggregations does not support nesting. - * - * @return bool */ - protected function supportsNesting() + protected function supportsNesting(): bool { return false; } diff --git a/src/BuilderBag.php b/src/BuilderBag.php index 997667fc..cdb6ea1a 100644 --- a/src/BuilderBag.php +++ b/src/BuilderBag.php @@ -19,7 +19,7 @@ class BuilderBag /** * @var BuilderInterface[] */ - private $bag = []; + private array $bag = []; /** * @param BuilderInterface[] $builders @@ -34,17 +34,11 @@ public function __construct($builders = []) /** * Adds a builder. * - * @param BuilderInterface $builder - * * @return string */ public function add(BuilderInterface $builder) { - if (method_exists($builder, 'getName')) { - $name = $builder->getName(); - } else { - $name = bin2hex(random_bytes(30)); - } + $name = method_exists($builder, 'getName') ? $builder->getName() : bin2hex(random_bytes(30)); $this->bag[$name] = $builder; @@ -68,7 +62,7 @@ public function has($name) * * @param string $name Builder name. */ - public function remove($name) + public function remove($name): void { unset($this->bag[$name]); } @@ -76,7 +70,7 @@ public function remove($name) /** * Clears contained builders. */ - public function clear() + public function clear(): void { $this->bag = []; } @@ -100,21 +94,19 @@ public function get($name) * * @return BuilderInterface[] */ - public function all($type = null) + public function all($type = null): array { return array_filter( $this->bag, /** @var BuilderInterface $builder */ - function (BuilderInterface $builder) use ($type) { - return $type === null || $builder->getType() == $type; - } + static fn(BuilderInterface $builder): bool => $type === null || $builder->getType() == $type ); } /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $output = []; foreach ($this->all() as $builder) { diff --git a/src/BuilderInterface.php b/src/BuilderInterface.php index de07683d..f57e0a0e 100644 --- a/src/BuilderInterface.php +++ b/src/BuilderInterface.php @@ -21,12 +21,12 @@ interface BuilderInterface * * @return array */ - public function toArray(); + public function toArray(): array; /** * Returns element type. * * @return string */ - public function getType(); + public function getType(): string; } diff --git a/src/Highlight/Highlight.php b/src/Highlight/Highlight.php index 171e6a16..8bc04c62 100644 --- a/src/Highlight/Highlight.php +++ b/src/Highlight/Highlight.php @@ -24,20 +24,11 @@ class Highlight implements BuilderInterface /** * @var array Holds fields for highlight. */ - private $fields = []; + private array $fields = []; - /** - * @var array - */ - private $tags; + private ?array $tags = null; - /** - * @param string $name Field name to highlight. - * @param array $params - * - * @return $this - */ - public function addField($name, array $params = []) + public function addField(string $name, array $params = []): static { $this->fields[$name] = $params; @@ -46,13 +37,9 @@ public function addField($name, array $params = []) /** * Sets html tag and its class used in highlighting. - * - * @param array $preTags - * @param array $postTags - * * @return $this */ - public function setTags(array $preTags, array $postTags) + public function setTags(array $preTags, array $postTags): static { $this->tags['pre_tags'] = $preTags; $this->tags['post_tags'] = $postTags; @@ -63,7 +50,7 @@ public function setTags(array $preTags, array $postTags) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'highlight'; } @@ -71,7 +58,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $output = []; @@ -82,7 +69,7 @@ public function toArray() $output = $this->processArray($output); foreach ($this->fields as $field => $params) { - $output['fields'][$field] = count($params) ? $params : new \stdClass(); + $output['fields'][$field] = count($params) ? $params : []; } return $output; diff --git a/src/InnerHit/NestedInnerHit.php b/src/InnerHit/NestedInnerHit.php index e52ca710..649ebad6 100644 --- a/src/InnerHit/NestedInnerHit.php +++ b/src/InnerHit/NestedInnerHit.php @@ -26,66 +26,40 @@ class NestedInnerHit implements NamedBuilderInterface use ParametersTrait; use NameAwareTrait; - /** - * @var string - */ - private $path; + private string $path; - /** - * @var Search - */ - private $search; + private ?Search $search = null; /** * Inner hits container init. - * - * @param string $name - * @param string $path - * @param Search $search */ - public function __construct($name, $path, Search $search = null) + public function __construct(string $name, string $path, Search $search = null) { $this->setName($name); $this->setPath($path); - if ($search) { + if ($search instanceof Search) { $this->setSearch($search); } } - /** - * @return string - */ - public function getPath() + public function getPath(): string { return $this->path; } - /** - * @param string $path - * - * @return $this - */ - public function setPath($path) + public function setPath(string $path): static { $this->path = $path; return $this; } - /** - * @return Search - */ - public function getSearch() + public function getSearch(): ?Search { return $this->search; } - /** - * @param Search $search - * - * @return $this - */ - public function setSearch(Search $search) + public function setSearch(Search $search): static { $this->search = $search; @@ -95,7 +69,7 @@ public function setSearch(Search $search) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'nested'; } @@ -103,36 +77,26 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { - $out = $this->getSearch() ? $this->getSearch()->toArray() : new \stdClass(); + $out = $this->getSearch() ? $this->getSearch()->toArray() : []; - $out = [ + return [ $this->getPathType() => [ - $this->getPath() => $out , + $this->getPath() => $out, ], ]; - - return $out; } /** * Returns 'path' for nested and 'type' for parent inner hits - * - * @return null|string */ - private function getPathType() + private function getPathType(): ?string { - switch ($this->getType()) { - case 'nested': - $type = 'path'; - break; - case 'parent': - $type = 'type'; - break; - default: - $type = null; - } - return $type; + return match ($this->getType()) { + 'nested' => 'path', + 'parent' => 'type', + default => null, + }; } } diff --git a/src/InnerHit/ParentInnerHit.php b/src/InnerHit/ParentInnerHit.php index 3f30c7d5..83a7e616 100644 --- a/src/InnerHit/ParentInnerHit.php +++ b/src/InnerHit/ParentInnerHit.php @@ -7,7 +7,7 @@ class ParentInnerHit extends NestedInnerHit /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'parent'; } diff --git a/src/NameAwareTrait.php b/src/NameAwareTrait.php index ccd620e2..9e85a55b 100644 --- a/src/NameAwareTrait.php +++ b/src/NameAwareTrait.php @@ -13,25 +13,14 @@ trait NameAwareTrait { - /** - * @var string - */ - private $name; + private string $name = ''; - /** - * @return mixed - */ - public function getName() + public function getName(): string { return $this->name; } - /** - * @param string $name - * - * @return $this - */ - public function setName($name) + public function setName(string $name): static { $this->name = $name; diff --git a/src/NamedBuilderInterface.php b/src/NamedBuilderInterface.php index b9df4322..ee791f80 100644 --- a/src/NamedBuilderInterface.php +++ b/src/NamedBuilderInterface.php @@ -21,5 +21,5 @@ interface NamedBuilderInterface extends BuilderInterface * * @return string */ - public function getName(); + public function getName(): string; } diff --git a/src/ParametersTrait.php b/src/ParametersTrait.php index 013b4b6d..08b72a4d 100644 --- a/src/ParametersTrait.php +++ b/src/ParametersTrait.php @@ -16,29 +16,20 @@ */ trait ParametersTrait { - /** - * @var array - */ - private $parameters = []; + private array $parameters = []; /** * Checks if parameter exists. - * - * @param string $name - * - * @return bool */ - public function hasParameter($name) + public function hasParameter(string $name): bool { return isset($this->parameters[$name]); } /** * Removes parameter. - * - * @param string $name */ - public function removeParameter($name) + public function removeParameter(string $name): void { if ($this->hasParameter($name)) { unset($this->parameters[$name]); @@ -47,41 +38,26 @@ public function removeParameter($name) /** * Returns one parameter by it's name. - * - * @param string $name - * - * @return array|string|int|float|bool|\stdClass */ - public function getParameter($name) + public function getParameter(string $name): mixed { - return $this->parameters[$name]; + return $this->parameters[$name] ?? null; } /** * Returns an array of all parameters. - * - * @return array */ - public function getParameters() + public function getParameters(): array { return $this->parameters; } - /** - * @param string $name - * @param array|string|int|float|bool|\stdClass $value - */ - public function addParameter($name, $value) + public function addParameter(string $name, mixed $value): void { $this->parameters[$name] = $value; } - /** - * @param array $parameters - * - * @return $this - */ - public function setParameters(array $parameters) + public function setParameters(array $parameters): static { $this->parameters = $parameters; @@ -90,12 +66,8 @@ public function setParameters(array $parameters) /** * Returns given array merged with parameters. - * - * @param array $array - * - * @return array */ - protected function processArray(array $array = []) + protected function processArray(array $array = []): array { return array_merge($array, $this->parameters); } diff --git a/src/Query/Compound/BoolQuery.php b/src/Query/Compound/BoolQuery.php index ecdebe6c..3bb46a41 100644 --- a/src/Query/Compound/BoolQuery.php +++ b/src/Query/Compound/BoolQuery.php @@ -23,27 +23,22 @@ class BoolQuery implements BuilderInterface { use ParametersTrait; - const MUST = 'must'; - const MUST_NOT = 'must_not'; - const SHOULD = 'should'; - const FILTER = 'filter'; + final public const MUST = 'must'; + final public const MUST_NOT = 'must_not'; + final public const SHOULD = 'should'; + final public const FILTER = 'filter'; - /** - * @var array - */ - private $container = []; + private array $container = []; /** * Constructor to prepare container. - * - * @param array $container */ public function __construct(array $container = []) { foreach ($container as $type => $queries) { $queries = is_array($queries) ? $queries : [$queries]; - array_walk($queries, function ($query) use ($type) { + array_walk($queries, function ($query) use ($type): void { $this->add($query, $type); }); } @@ -68,11 +63,7 @@ public function getQueries($boolType = null) return $queries; } - if (isset($this->container[$boolType])) { - return $this->container[$boolType]; - } - - return []; + return $this->container[$boolType] ?? []; } /** @@ -104,7 +95,7 @@ public function add(BuilderInterface $query, $type = self::MUST, $key = null) /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { if (count($this->container) === 1 && isset($this->container[self::MUST]) && count($this->container[self::MUST]) === 1) { @@ -125,7 +116,7 @@ public function toArray() $output = $this->processArray($output); if (empty($output)) { - $output = new \stdClass(); + $output = []; } return [$this->getType() => $output]; @@ -134,7 +125,7 @@ public function toArray() /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'bool'; } diff --git a/src/Query/Compound/BoostingQuery.php b/src/Query/Compound/BoostingQuery.php index 87d3523b..0df5fd29 100644 --- a/src/Query/Compound/BoostingQuery.php +++ b/src/Query/Compound/BoostingQuery.php @@ -21,36 +21,16 @@ class BoostingQuery implements BuilderInterface { /** - * @var BuilderInterface - */ - private $positive; - - /** - * @var BuilderInterface - */ - private $negative; - - /** - * @var int|float - */ - private $negativeBoost; - - /** - * @param BuilderInterface $positive - * @param BuilderInterface $negative * @param int|float $negativeBoost */ - public function __construct(BuilderInterface $positive, BuilderInterface $negative, $negativeBoost) + public function __construct(private readonly BuilderInterface $positive, private readonly BuilderInterface $negative, private $negativeBoost) { - $this->positive = $positive; - $this->negative = $negative; - $this->negativeBoost = $negativeBoost; } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'boosting'; } @@ -58,7 +38,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'positive' => $this->positive->toArray(), diff --git a/src/Query/Compound/ConstantScoreQuery.php b/src/Query/Compound/ConstantScoreQuery.php index ee3373a5..c1a19dae 100644 --- a/src/Query/Compound/ConstantScoreQuery.php +++ b/src/Query/Compound/ConstantScoreQuery.php @@ -23,25 +23,15 @@ class ConstantScoreQuery implements BuilderInterface { use ParametersTrait; - /** - * @var BuilderInterface - */ - private $query; - - /** - * @param BuilderInterface $query - * @param array $parameters - */ - public function __construct(BuilderInterface $query, array $parameters = []) + public function __construct(private readonly BuilderInterface $query, array $parameters = []) { - $this->query = $query; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'constant_score'; } @@ -49,7 +39,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'filter' => $this->query->toArray(), diff --git a/src/Query/Compound/DisMaxQuery.php b/src/Query/Compound/DisMaxQuery.php index adb77c36..4bde4c45 100644 --- a/src/Query/Compound/DisMaxQuery.php +++ b/src/Query/Compound/DisMaxQuery.php @@ -26,12 +26,10 @@ class DisMaxQuery implements BuilderInterface /** * @var BuilderInterface[] */ - private $queries = []; + private array $queries = []; /** * Initializes Dis Max query. - * - * @param array $parameters */ public function __construct(array $parameters = []) { @@ -41,7 +39,6 @@ public function __construct(array $parameters = []) /** * Add query. * - * @param BuilderInterface $query * * @return DisMaxQuery */ @@ -55,7 +52,7 @@ public function addQuery(BuilderInterface $query) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'dis_max'; } @@ -63,12 +60,13 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = []; foreach ($this->queries as $type) { $query[] = $type->toArray(); } + $output = $this->processArray(['queries' => $query]); return [$this->getType() => $output]; diff --git a/src/Query/Compound/FunctionScoreQuery.php b/src/Query/Compound/FunctionScoreQuery.php index e7d96c1b..323beb68 100644 --- a/src/Query/Compound/FunctionScoreQuery.php +++ b/src/Query/Compound/FunctionScoreQuery.php @@ -24,22 +24,12 @@ class FunctionScoreQuery implements BuilderInterface use ParametersTrait; /** - * @var BuilderInterface + * @var mixed[][]|null */ - private $query; + private ?array $functions = null; - /** - * @var array[] - */ - private $functions; - - /** - * @param BuilderInterface $query - * @param array $parameters - */ - public function __construct(BuilderInterface $query, array $parameters = []) + public function __construct(private readonly BuilderInterface $query, array $parameters = []) { - $this->query = $query; $this->setParameters($parameters); } @@ -48,7 +38,7 @@ public function __construct(BuilderInterface $query, array $parameters = []) * * @return BuilderInterface object */ - public function getQuery() + public function getQuery(): BuilderInterface { return $this->query; } @@ -56,11 +46,9 @@ public function getQuery() /** * Creates field_value_factor function. * - * @param string $field - * @param float $factor - * @param string $modifier - * @param BuilderInterface $query - * @param mixed $missing + * @param string $field + * @param float $factor + * @param string $modifier * @return $this */ public function addFieldValueFactorFunction( @@ -68,15 +56,17 @@ public function addFieldValueFactorFunction( $factor, $modifier = 'none', BuilderInterface $query = null, - $missing = null + mixed $missing = null ) { $function = [ - 'field_value_factor' => array_filter([ - 'field' => $field, - 'factor' => $factor, - 'modifier' => $modifier, - 'missing' => $missing - ]), + 'field_value_factor' => array_filter( + [ + 'field' => $field, + 'factor' => $factor, + 'modifier' => $modifier, + 'missing' => $missing, + ] + ), ]; $this->applyFilter($function, $query); @@ -88,13 +78,10 @@ public function addFieldValueFactorFunction( /** * Modifier to apply filter to the function score function. - * - * @param array $function - * @param BuilderInterface $query */ - private function applyFilter(array &$function, BuilderInterface $query = null) + private function applyFilter(array &$function, BuilderInterface $query = null): void { - if ($query) { + if ($query instanceof BuilderInterface) { $function['filter'] = $query->toArray(); } } @@ -102,12 +89,9 @@ private function applyFilter(array &$function, BuilderInterface $query = null) /** * Add decay function to function score. Weight and query are optional. * - * @param string $type - * @param string $field - * @param array $function - * @param array $options - * @param BuilderInterface $query - * @param int $weight + * @param string $type + * @param string $field + * @param int $weight * * @return $this */ @@ -139,8 +123,7 @@ public function addDecayFunction( /** * Adds function to function score without decay function. Influence search score only for specific query. * - * @param float $weight - * @param BuilderInterface $query + * @param float $weight * * @return $this */ @@ -160,15 +143,13 @@ public function addWeightFunction($weight, BuilderInterface $query = null) /** * Adds random score function. Seed is optional. * - * @param mixed $seed - * @param BuilderInterface $query * * @return $this */ - public function addRandomFunction($seed = null, BuilderInterface $query = null) + public function addRandomFunction(mixed $seed = null, BuilderInterface $query = null) { $function = [ - 'random_score' => $seed ? [ 'seed' => $seed ] : new \stdClass(), + 'random_score' => $seed ? ['seed' => $seed] : [], ]; $this->applyFilter($function, $query); @@ -181,10 +162,7 @@ public function addRandomFunction($seed = null, BuilderInterface $query = null) /** * Adds script score function. * - * @param string $inline - * @param array $params - * @param array $options - * @param BuilderInterface $query + * @param string $inline * * @return $this */ @@ -202,11 +180,11 @@ public function addScriptScoreFunction( [ 'lang' => 'painless', 'inline' => $inline, - 'params' => $params + 'params' => $params, ], $options ) - ) + ), ], ]; @@ -219,7 +197,6 @@ public function addScriptScoreFunction( /** * Adds custom simple function. You can add to the array whatever you want. * - * @param array $function * * @return $this */ @@ -233,7 +210,7 @@ public function addSimpleFunction(array $function) /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'query' => $this->query->toArray(), @@ -248,7 +225,7 @@ public function toArray() /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'function_score'; } diff --git a/src/Query/FullText/CommonTermsQuery.php b/src/Query/FullText/CommonTermsQuery.php index 2e31b13b..66c19c97 100644 --- a/src/Query/FullText/CommonTermsQuery.php +++ b/src/Query/FullText/CommonTermsQuery.php @@ -23,32 +23,19 @@ class CommonTermsQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $field; - - /** - * @var string - */ - private $query; - /** * @param string $field * @param string $query - * @param array $parameters */ - public function __construct($field, $query, array $parameters = []) + public function __construct(private $field, private $query, array $parameters = []) { - $this->field = $field; - $this->query = $query; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'common'; } @@ -56,7 +43,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'query' => $this->query, diff --git a/src/Query/FullText/MatchPhrasePrefixQuery.php b/src/Query/FullText/MatchPhrasePrefixQuery.php index 58f8a83a..42e78784 100644 --- a/src/Query/FullText/MatchPhrasePrefixQuery.php +++ b/src/Query/FullText/MatchPhrasePrefixQuery.php @@ -22,7 +22,7 @@ class MatchPhrasePrefixQuery extends MatchQuery /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'match_phrase_prefix'; } diff --git a/src/Query/FullText/MatchPhraseQuery.php b/src/Query/FullText/MatchPhraseQuery.php index 2560088f..c2f0baf1 100644 --- a/src/Query/FullText/MatchPhraseQuery.php +++ b/src/Query/FullText/MatchPhraseQuery.php @@ -22,7 +22,7 @@ class MatchPhraseQuery extends MatchQuery /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'match_phrase'; } diff --git a/src/Query/FullText/MatchQuery.php b/src/Query/FullText/MatchQuery.php index 6ce6801c..7f5398ac 100644 --- a/src/Query/FullText/MatchQuery.php +++ b/src/Query/FullText/MatchQuery.php @@ -23,32 +23,19 @@ class MatchQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $field; - - /** - * @var string - */ - private $query; - /** * @param string $field * @param string $query - * @param array $parameters */ - public function __construct($field, $query, array $parameters = []) + public function __construct(private $field, private $query, array $parameters = []) { - $this->field = $field; - $this->query = $query; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'match'; } @@ -56,7 +43,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'query' => $this->query, diff --git a/src/Query/FullText/MultiMatchQuery.php b/src/Query/FullText/MultiMatchQuery.php index 1450916e..89ee785f 100644 --- a/src/Query/FullText/MultiMatchQuery.php +++ b/src/Query/FullText/MultiMatchQuery.php @@ -29,32 +29,15 @@ class MultiMatchQuery implements BuilderInterface { use ParametersTrait; - /** - * @var array - */ - private $fields = []; - - /** - * @var string - */ - private $query; - - /** - * @param array $fields - * @param string $query - * @param array $parameters - */ - public function __construct(array $fields, $query, array $parameters = []) + public function __construct(private readonly array $fields, private ?string $query, array $parameters = []) { - $this->fields = $fields; - $this->query = $query; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'multi_match'; } @@ -62,7 +45,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'query' => $this->query, diff --git a/src/Query/FullText/QueryStringQuery.php b/src/Query/FullText/QueryStringQuery.php index 29eda3fa..9b90f021 100644 --- a/src/Query/FullText/QueryStringQuery.php +++ b/src/Query/FullText/QueryStringQuery.php @@ -23,25 +23,18 @@ class QueryStringQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string The actual query to be parsed. - */ - private $query; - /** * @param string $query - * @param array $parameters */ - public function __construct($query, array $parameters = []) + public function __construct(private $query, array $parameters = []) { - $this->query = $query; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'query_string'; } @@ -49,7 +42,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'query' => $this->query, diff --git a/src/Query/FullText/SimpleQueryStringQuery.php b/src/Query/FullText/SimpleQueryStringQuery.php index 976ffceb..efee2cd8 100644 --- a/src/Query/FullText/SimpleQueryStringQuery.php +++ b/src/Query/FullText/SimpleQueryStringQuery.php @@ -23,25 +23,18 @@ class SimpleQueryStringQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string The actual query to be parsed. - */ - private $query; - /** * @param string $query - * @param array $parameters */ - public function __construct($query, array $parameters = []) + public function __construct(private $query, array $parameters = []) { - $this->query = $query; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'simple_query_string'; } @@ -49,7 +42,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'query' => $this->query, diff --git a/src/Query/Geo/GeoBoundingBoxQuery.php b/src/Query/Geo/GeoBoundingBoxQuery.php index 98bb050d..e2db2fce 100644 --- a/src/Query/Geo/GeoBoundingBoxQuery.php +++ b/src/Query/Geo/GeoBoundingBoxQuery.php @@ -23,32 +23,19 @@ class GeoBoundingBoxQuery implements BuilderInterface { use ParametersTrait; - /** - * @var array - */ - private $values; - - /** - * @var string - */ - private $field; - /** * @param string $field * @param array $values - * @param array $parameters */ - public function __construct($field, $values, array $parameters = []) + public function __construct(private $field, private $values, array $parameters = []) { - $this->field = $field; - $this->values = $values; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'geo_bounding_box'; } @@ -56,7 +43,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { return [ $this->getType() => $this->processArray([$this->field => $this->points()]) diff --git a/src/Query/Geo/GeoDistanceQuery.php b/src/Query/Geo/GeoDistanceQuery.php index 03cf2021..df60072f 100644 --- a/src/Query/Geo/GeoDistanceQuery.php +++ b/src/Query/Geo/GeoDistanceQuery.php @@ -23,40 +23,19 @@ class GeoDistanceQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $field; - - /** - * @var string - */ - private $distance; - - /** - * @var mixed - */ - private $location; - /** * @param string $field * @param string $distance - * @param mixed $location - * @param array $parameters */ - public function __construct($field, $distance, $location, array $parameters = []) + public function __construct(private $field, private $distance, private readonly mixed $location, array $parameters = []) { - $this->field = $field; - $this->distance = $distance; - $this->location = $location; - $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'geo_distance'; } @@ -64,7 +43,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'distance' => $this->distance, diff --git a/src/Query/Geo/GeoPolygonQuery.php b/src/Query/Geo/GeoPolygonQuery.php index 51a9c4ea..656154dd 100644 --- a/src/Query/Geo/GeoPolygonQuery.php +++ b/src/Query/Geo/GeoPolygonQuery.php @@ -23,32 +23,18 @@ class GeoPolygonQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $field; - - /** - * @var array - */ - private $points; - /** * @param string $field - * @param array $points - * @param array $parameters */ - public function __construct($field, array $points = [], array $parameters = []) + public function __construct(private $field, private readonly array $points = [], array $parameters = []) { - $this->field = $field; - $this->points = $points; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'geo_polygon'; } @@ -56,7 +42,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [$this->field => ['points' => $this->points]]; $output = $this->processArray($query); diff --git a/src/Query/Geo/GeoShapeQuery.php b/src/Query/Geo/GeoShapeQuery.php index a2a5893a..029b2339 100644 --- a/src/Query/Geo/GeoShapeQuery.php +++ b/src/Query/Geo/GeoShapeQuery.php @@ -23,19 +23,16 @@ class GeoShapeQuery implements BuilderInterface { use ParametersTrait; - const INTERSECTS = 'intersects'; - const DISJOINT = 'disjoint'; - const WITHIN = 'within'; - const CONTAINS = 'contains'; + final public const INTERSECTS = 'intersects'; - /** - * @var array - */ - private $fields = []; + final public const DISJOINT = 'disjoint'; + + final public const WITHIN = 'within'; + + final public const CONTAINS = 'contains'; + + private array $fields = []; - /** - * @param array $parameters - */ public function __construct(array $parameters = []) { $this->setParameters($parameters); @@ -44,7 +41,7 @@ public function __construct(array $parameters = []) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'geo_shape'; } @@ -58,7 +55,7 @@ public function getType() * @param string $relation Spatial relation. * @param array $parameters Additional parameters. */ - public function addShape($field, $type, array $coordinates, $relation = self::INTERSECTS, array $parameters = []) + public function addShape($field, $type, array $coordinates, $relation = self::INTERSECTS, array $parameters = []): void { // TODO: remove this in the next major version if (is_array($relation)) { @@ -100,7 +97,7 @@ public function addPreIndexedShape( $path, $relation = self::INTERSECTS, array $parameters = [] - ) { + ): void { // TODO: remove this in the next major version if (is_array($relation)) { $parameters = $relation; @@ -127,7 +124,7 @@ public function addPreIndexedShape( /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $output = $this->processArray($this->fields); diff --git a/src/Query/Joining/HasChildQuery.php b/src/Query/Joining/HasChildQuery.php index 4682f8c5..809d59ae 100644 --- a/src/Query/Joining/HasChildQuery.php +++ b/src/Query/Joining/HasChildQuery.php @@ -23,32 +23,18 @@ class HasChildQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $type; - - /** - * @var BuilderInterface - */ - private $query; - /** * @param string $type - * @param BuilderInterface $query - * @param array $parameters */ - public function __construct($type, BuilderInterface $query, array $parameters = []) + public function __construct(private $type, private readonly BuilderInterface $query, array $parameters = []) { - $this->type = $type; - $this->query = $query; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'has_child'; } @@ -56,7 +42,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'type' => $this->type, diff --git a/src/Query/Joining/HasParentQuery.php b/src/Query/Joining/HasParentQuery.php index a575639b..37fe5e14 100644 --- a/src/Query/Joining/HasParentQuery.php +++ b/src/Query/Joining/HasParentQuery.php @@ -23,32 +23,18 @@ class HasParentQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $parentType; - - /** - * @var BuilderInterface - */ - private $query; - /** * @param string $parentType - * @param BuilderInterface $query - * @param array $parameters */ - public function __construct($parentType, BuilderInterface $query, array $parameters = []) + public function __construct(private $parentType, private readonly BuilderInterface $query, array $parameters = []) { - $this->parentType = $parentType; - $this->query = $query; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'has_parent'; } @@ -56,7 +42,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'parent_type' => $this->parentType, diff --git a/src/Query/Joining/NestedQuery.php b/src/Query/Joining/NestedQuery.php index e43ff18b..c77958ca 100644 --- a/src/Query/Joining/NestedQuery.php +++ b/src/Query/Joining/NestedQuery.php @@ -23,32 +23,18 @@ class NestedQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $path; - - /** - * @var BuilderInterface - */ - private $query; - /** * @param string $path - * @param BuilderInterface $query - * @param array $parameters */ - public function __construct($path, BuilderInterface $query, array $parameters = []) + public function __construct(private $path, private readonly BuilderInterface $query, array $parameters = []) { - $this->path = $path; - $this->query = $query; $this->parameters = $parameters; } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'nested'; } @@ -56,7 +42,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { return [ $this->getType() => $this->processArray( @@ -70,10 +56,8 @@ public function toArray() /** * Returns nested query object. - * - * @return BuilderInterface */ - public function getQuery() + public function getQuery(): BuilderInterface { return $this->query; } diff --git a/src/Query/Joining/ParentIdQuery.php b/src/Query/Joining/ParentIdQuery.php index f6edd9b2..8c2be24b 100644 --- a/src/Query/Joining/ParentIdQuery.php +++ b/src/Query/Joining/ParentIdQuery.php @@ -12,32 +12,18 @@ class ParentIdQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $childType; - - /** - * @var string - */ - private $parentId; - /** * @param string $parentId - * @param string $childType - * @param array $parameters */ - public function __construct($parentId, string $childType, array $parameters = []) + public function __construct(private $parentId, private readonly string $childType, array $parameters = []) { - $this->childType = $childType; - $this->parentId = $parentId; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'parent_id'; } @@ -45,7 +31,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'id' => $this->parentId, diff --git a/src/Query/MatchAllQuery.php b/src/Query/MatchAllQuery.php index 2529ac60..0a727aba 100644 --- a/src/Query/MatchAllQuery.php +++ b/src/Query/MatchAllQuery.php @@ -34,7 +34,7 @@ public function __construct(array $parameters = []) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'match_all'; } @@ -42,9 +42,9 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $params = $this->getParameters(); - return [$this->getType() => !empty($params) ? $params : new \stdClass()]; + return [$this->getType() => !empty($params) ? $params : []]; } } diff --git a/src/Query/Span/FieldMaskingSpanQuery.php b/src/Query/Span/FieldMaskingSpanQuery.php index f2cc4578..9cd641fc 100644 --- a/src/Query/Span/FieldMaskingSpanQuery.php +++ b/src/Query/Span/FieldMaskingSpanQuery.php @@ -34,7 +34,6 @@ class FieldMaskingSpanQuery implements SpanQueryInterface /** * @param string $field - * @param SpanQueryInterface $query */ public function __construct($field, SpanQueryInterface $query) { @@ -51,11 +50,9 @@ public function getQuery() } /** - * @param mixed $query - * * @return $this */ - public function setQuery($query) + public function setQuery(mixed $query) { $this->query = $query; @@ -85,7 +82,7 @@ public function setField($field) /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $output = [ 'query' => $this->getQuery()->toArray(), @@ -100,7 +97,7 @@ public function toArray() /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'field_masking_span'; } diff --git a/src/Query/Span/SpanContainingQuery.php b/src/Query/Span/SpanContainingQuery.php index aed6e67b..ed23bd0e 100644 --- a/src/Query/Span/SpanContainingQuery.php +++ b/src/Query/Span/SpanContainingQuery.php @@ -25,34 +25,25 @@ class SpanContainingQuery implements SpanQueryInterface /** * @param SpanQueryInterface */ - private $little; + private SpanQueryInterface $little; /** * @param SpanQueryInterface */ - private $big; + private SpanQueryInterface $big; - /** - * @param SpanQueryInterface $little - * @param SpanQueryInterface $big - */ public function __construct(SpanQueryInterface $little, SpanQueryInterface $big) { $this->setLittle($little); $this->setBig($big); } - /** - * @return SpanQueryInterface - */ - public function getLittle() + public function getLittle(): SpanQueryInterface { return $this->little; } /** - * @param SpanQueryInterface $little - * * @return $this */ public function setLittle(SpanQueryInterface $little) @@ -62,17 +53,12 @@ public function setLittle(SpanQueryInterface $little) return $this; } - /** - * @return SpanQueryInterface - */ - public function getBig() + public function getBig(): SpanQueryInterface { return $this->big; } /** - * @param SpanQueryInterface $big - * * @return $this */ public function setBig(SpanQueryInterface $big) @@ -85,7 +71,7 @@ public function setBig(SpanQueryInterface $big) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'span_containing'; } @@ -93,7 +79,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $output = [ 'little' => $this->getLittle()->toArray(), diff --git a/src/Query/Span/SpanFirstQuery.php b/src/Query/Span/SpanFirstQuery.php index 46f16ef9..61e33df2 100644 --- a/src/Query/Span/SpanFirstQuery.php +++ b/src/Query/Span/SpanFirstQuery.php @@ -23,33 +23,19 @@ class SpanFirstQuery implements SpanQueryInterface use ParametersTrait; /** - * @var SpanQueryInterface - */ - private $query; - - /** - * @var int - */ - private $end; - - /** - * @param SpanQueryInterface $query * @param int $end - * @param array $parameters * * @throws \LogicException */ - public function __construct(SpanQueryInterface $query, $end, array $parameters = []) + public function __construct(private readonly SpanQueryInterface $query, private $end, array $parameters = []) { - $this->query = $query; - $this->end = $end; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'span_first'; } @@ -57,7 +43,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = []; $query['match'] = $this->query->toArray(); diff --git a/src/Query/Span/SpanMultiTermQuery.php b/src/Query/Span/SpanMultiTermQuery.php index 9042c598..ac021692 100644 --- a/src/Query/Span/SpanMultiTermQuery.php +++ b/src/Query/Span/SpanMultiTermQuery.php @@ -23,27 +23,18 @@ class SpanMultiTermQuery implements SpanQueryInterface { use ParametersTrait; - /** - * @var BuilderInterface - */ - private $query; - /** * Accepts one of fuzzy, prefix, term range, wildcard, regexp query. - * - * @param BuilderInterface $query - * @param array $parameters */ - public function __construct(BuilderInterface $query, array $parameters = []) + public function __construct(private readonly BuilderInterface $query, array $parameters = []) { - $this->query = $query; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'span_multi'; } @@ -53,7 +44,7 @@ public function getType() * * @throws \InvalidArgumentException */ - public function toArray() + public function toArray(): array { $query = []; $query['match'] = $this->query->toArray(); diff --git a/src/Query/Span/SpanNearQuery.php b/src/Query/Span/SpanNearQuery.php index 086f414b..b0392ac6 100644 --- a/src/Query/Span/SpanNearQuery.php +++ b/src/Query/Span/SpanNearQuery.php @@ -46,7 +46,7 @@ public function setSlop($slop) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'span_near'; } @@ -54,12 +54,13 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = []; foreach ($this->getQueries() as $type) { $query['clauses'][] = $type->toArray(); } + $query['slop'] = $this->getSlop(); $output = $this->processArray($query); diff --git a/src/Query/Span/SpanNotQuery.php b/src/Query/Span/SpanNotQuery.php index 1492d1a1..2929b6ae 100644 --- a/src/Query/Span/SpanNotQuery.php +++ b/src/Query/Span/SpanNotQuery.php @@ -22,32 +22,15 @@ class SpanNotQuery implements SpanQueryInterface { use ParametersTrait; - /** - * @var SpanQueryInterface - */ - private $include; - - /** - * @var SpanQueryInterface - */ - private $exclude; - - /** - * @param SpanQueryInterface $include - * @param SpanQueryInterface $exclude - * @param array $parameters - */ - public function __construct(SpanQueryInterface $include, SpanQueryInterface $exclude, array $parameters = []) + public function __construct(private readonly SpanQueryInterface $include, private readonly SpanQueryInterface $exclude, array $parameters = []) { - $this->include = $include; - $this->exclude = $exclude; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'span_not'; } @@ -55,7 +38,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'include' => $this->include->toArray(), diff --git a/src/Query/Span/SpanOrQuery.php b/src/Query/Span/SpanOrQuery.php index 9f3ec54d..3ff25aaf 100644 --- a/src/Query/Span/SpanOrQuery.php +++ b/src/Query/Span/SpanOrQuery.php @@ -25,11 +25,8 @@ class SpanOrQuery implements SpanQueryInterface /** * @var SpanQueryInterface[] */ - private $queries = []; + private array $queries = []; - /** - * @param array $parameters - */ public function __construct(array $parameters = []) { $this->setParameters($parameters); @@ -38,7 +35,6 @@ public function __construct(array $parameters = []) /** * Add span query. * - * @param SpanQueryInterface $query * * @return $this */ @@ -60,7 +56,7 @@ public function getQueries() /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'span_or'; } @@ -68,12 +64,13 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = []; foreach ($this->queries as $type) { $query['clauses'][] = $type->toArray(); } + $output = $this->processArray($query); return [$this->getType() => $output]; diff --git a/src/Query/Span/SpanTermQuery.php b/src/Query/Span/SpanTermQuery.php index acfbf510..dbe63a0d 100644 --- a/src/Query/Span/SpanTermQuery.php +++ b/src/Query/Span/SpanTermQuery.php @@ -23,7 +23,7 @@ class SpanTermQuery extends TermQuery implements SpanQueryInterface /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'span_term'; } diff --git a/src/Query/Span/SpanWithinQuery.php b/src/Query/Span/SpanWithinQuery.php index c7d8ade1..2df2bb41 100644 --- a/src/Query/Span/SpanWithinQuery.php +++ b/src/Query/Span/SpanWithinQuery.php @@ -21,7 +21,7 @@ class SpanWithinQuery extends SpanContainingQuery /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'span_within'; } diff --git a/src/Query/Specialized/MoreLikeThisQuery.php b/src/Query/Specialized/MoreLikeThisQuery.php index af721ada..90d5d190 100644 --- a/src/Query/Specialized/MoreLikeThisQuery.php +++ b/src/Query/Specialized/MoreLikeThisQuery.php @@ -23,25 +23,18 @@ class MoreLikeThisQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string The text to find documents like it, required if ids or docs are not specified. - */ - private $like; - /** * @param string $like - * @param array $parameters */ - public function __construct($like, array $parameters = []) + public function __construct(private $like, array $parameters = []) { - $this->like = $like; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'more_like_this'; } @@ -49,11 +42,11 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = []; - if (($this->hasParameter('ids') === false) || ($this->hasParameter('docs') === false)) { + if ((!$this->hasParameter('ids')) || (!$this->hasParameter('docs'))) { $query['like'] = $this->like; } diff --git a/src/Query/Specialized/ScriptQuery.php b/src/Query/Specialized/ScriptQuery.php index 07ef289e..ce166d33 100644 --- a/src/Query/Specialized/ScriptQuery.php +++ b/src/Query/Specialized/ScriptQuery.php @@ -23,25 +23,19 @@ class ScriptQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $script; - /** * @param string $script Script * @param array $parameters Optional parameters */ - public function __construct($script, array $parameters = []) + public function __construct(private $script, array $parameters = []) { - $this->script = $script; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'script'; } @@ -49,7 +43,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = ['inline' => $this->script]; $output = $this->processArray($query); diff --git a/src/Query/Specialized/TemplateQuery.php b/src/Query/Specialized/TemplateQuery.php index ab541d37..de5c96e6 100644 --- a/src/Query/Specialized/TemplateQuery.php +++ b/src/Query/Specialized/TemplateQuery.php @@ -113,7 +113,7 @@ public function setParams($params) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'template'; } @@ -121,7 +121,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $output = array_filter( [ diff --git a/src/Query/TermLevel/ExistsQuery.php b/src/Query/TermLevel/ExistsQuery.php index f72e6e52..49a5d323 100644 --- a/src/Query/TermLevel/ExistsQuery.php +++ b/src/Query/TermLevel/ExistsQuery.php @@ -20,25 +20,17 @@ */ class ExistsQuery implements BuilderInterface { - /** - * @var string - */ - private $field; - /** * Constructor. - * - * @param string $field Field value */ - public function __construct($field) + public function __construct(private string $field) { - $this->field = $field; } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'exists'; } @@ -46,7 +38,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { return [ $this->getType() => [ diff --git a/src/Query/TermLevel/FuzzyQuery.php b/src/Query/TermLevel/FuzzyQuery.php index e7e7535b..418c9191 100644 --- a/src/Query/TermLevel/FuzzyQuery.php +++ b/src/Query/TermLevel/FuzzyQuery.php @@ -23,32 +23,19 @@ class FuzzyQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $field; - - /** - * @var string - */ - private $value; - /** * @param string $field * @param string $value - * @param array $parameters */ - public function __construct($field, $value, array $parameters = []) + public function __construct(private $field, private $value, array $parameters = []) { - $this->field = $field; - $this->value = $value; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'fuzzy'; } @@ -56,7 +43,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'value' => $this->value, diff --git a/src/Query/TermLevel/IdsQuery.php b/src/Query/TermLevel/IdsQuery.php index 29edd33d..749066be 100644 --- a/src/Query/TermLevel/IdsQuery.php +++ b/src/Query/TermLevel/IdsQuery.php @@ -23,25 +23,15 @@ class IdsQuery implements BuilderInterface { use ParametersTrait; - /** - * @var array - */ - private $values; - - /** - * @param array $values - * @param array $parameters - */ - public function __construct(array $values, array $parameters = []) + public function __construct(private readonly array $values, array $parameters = []) { - $this->values = $values; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'ids'; } @@ -49,7 +39,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'values' => $this->values, diff --git a/src/Query/TermLevel/PrefixQuery.php b/src/Query/TermLevel/PrefixQuery.php index c453121c..5b0a20bf 100644 --- a/src/Query/TermLevel/PrefixQuery.php +++ b/src/Query/TermLevel/PrefixQuery.php @@ -23,32 +23,20 @@ class PrefixQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - protected $field; - - /** - * @var string - */ - protected $value; - /** * @param string $field Field name. * @param string $value Value. * @param array $parameters Optional parameters. */ - public function __construct($field, $value, array $parameters = []) + public function __construct(protected $field, protected $value, array $parameters = []) { - $this->field = $field; - $this->value = $value; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'prefix'; } @@ -56,7 +44,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'value' => $this->value, diff --git a/src/Query/TermLevel/RangeQuery.php b/src/Query/TermLevel/RangeQuery.php index 9a33142d..338224bc 100644 --- a/src/Query/TermLevel/RangeQuery.php +++ b/src/Query/TermLevel/RangeQuery.php @@ -26,21 +26,18 @@ class RangeQuery implements BuilderInterface /** * Range control names. */ - const LT = 'lt'; - const GT = 'gt'; - const LTE = 'lte'; - const GTE = 'gte'; + final public const LT = 'lt'; - /** - * @var string Field name. - */ - private $field; + final public const GT = 'gt'; + + final public const LTE = 'lte'; + + final public const GTE = 'gte'; /** * @param string $field - * @param array $parameters */ - public function __construct($field, array $parameters = []) + public function __construct(private $field, array $parameters = []) { $this->setParameters($parameters); @@ -51,14 +48,12 @@ public function __construct($field, array $parameters = []) if ($this->hasParameter(self::LTE) && $this->hasParameter(self::LT)) { unset($this->parameters[self::LT]); } - - $this->field = $field; } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'range'; } @@ -66,7 +61,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $output = [ $this->field => $this->getParameters(), diff --git a/src/Query/TermLevel/RegexpQuery.php b/src/Query/TermLevel/RegexpQuery.php index 4fcee0f7..5ffde9e5 100644 --- a/src/Query/TermLevel/RegexpQuery.php +++ b/src/Query/TermLevel/RegexpQuery.php @@ -23,32 +23,19 @@ class RegexpQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string Field to be queried. - */ - private $field; - - /** - * @var string The actual regexp value to be used. - */ - private $regexpValue; - /** * @param string $field * @param string $regexpValue - * @param array $parameters */ - public function __construct($field, $regexpValue, array $parameters = []) + public function __construct(private $field, private $regexpValue, array $parameters = []) { - $this->field = $field; - $this->regexpValue = $regexpValue; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'regexp'; } @@ -56,7 +43,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'value' => $this->regexpValue, diff --git a/src/Query/TermLevel/TermQuery.php b/src/Query/TermLevel/TermQuery.php index 7cbbd78e..73afffac 100644 --- a/src/Query/TermLevel/TermQuery.php +++ b/src/Query/TermLevel/TermQuery.php @@ -23,32 +23,15 @@ class TermQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $field; - - /** - * @var string - */ - private $value; - - /** - * @param string $field - * @param string|int|float|bool $value - * @param array $parameters - */ - public function __construct($field, $value, array $parameters = []) + public function __construct(private string $field, private mixed $value, array $parameters = []) { - $this->field = $field; - $this->value = $value; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'term'; } @@ -56,7 +39,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = $this->processArray(); diff --git a/src/Query/TermLevel/TermsQuery.php b/src/Query/TermLevel/TermsQuery.php index 38332234..22697010 100644 --- a/src/Query/TermLevel/TermsQuery.php +++ b/src/Query/TermLevel/TermsQuery.php @@ -23,16 +23,6 @@ class TermsQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $field; - - /** - * @var array - */ - private $terms; - /** * Constructor. * @@ -40,17 +30,15 @@ class TermsQuery implements BuilderInterface * @param array $terms An array of terms * @param array $parameters Optional parameters */ - public function __construct($field, $terms, array $parameters = []) + public function __construct(private $field, private $terms, array $parameters = []) { - $this->field = $field; - $this->terms = $terms; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'terms'; } @@ -58,7 +46,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ $this->field => $this->terms, diff --git a/src/Query/TermLevel/TermsSetQuery.php b/src/Query/TermLevel/TermsSetQuery.php index 6f9ec80d..d15595ce 100644 --- a/src/Query/TermLevel/TermsSetQuery.php +++ b/src/Query/TermLevel/TermsSetQuery.php @@ -23,18 +23,9 @@ class TermsSetQuery implements BuilderInterface { use ParametersTrait; - const MINIMUM_SHOULD_MATCH_TYPE_FIELD = 'minimum_should_match_field'; - const MINIMUM_SHOULD_MATCH_TYPE_SCRIPT = 'minimum_should_match_script'; + final public const MINIMUM_SHOULD_MATCH_TYPE_FIELD = 'minimum_should_match_field'; - /** - * @var string - */ - private $field; - - /** - * @var array - */ - private $terms; + final public const MINIMUM_SHOULD_MATCH_TYPE_SCRIPT = 'minimum_should_match_script'; /** * Constructor. @@ -43,10 +34,8 @@ class TermsSetQuery implements BuilderInterface * @param array $terms An array of terms * @param array $parameters Parameters */ - public function __construct($field, $terms, array $parameters) + public function __construct(private $field, private $terms, array $parameters) { - $this->field = $field; - $this->terms = $terms; $this->validateParameters($parameters); $this->setParameters($parameters); } @@ -54,7 +43,7 @@ public function __construct($field, $terms, array $parameters) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'terms_set'; } @@ -62,7 +51,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'terms' => $this->terms, diff --git a/src/Query/TermLevel/TypeQuery.php b/src/Query/TermLevel/TypeQuery.php index a5ae21c8..533be8f2 100644 --- a/src/Query/TermLevel/TypeQuery.php +++ b/src/Query/TermLevel/TypeQuery.php @@ -20,25 +20,19 @@ */ class TypeQuery implements BuilderInterface { - /** - * @var string - */ - private $type; - /** * Constructor. * * @param string $type Type name */ - public function __construct($type) + public function __construct(private $type) { - $this->type = $type; } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'type'; } @@ -46,7 +40,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { return [ $this->getType() => [ diff --git a/src/Query/TermLevel/WildcardQuery.php b/src/Query/TermLevel/WildcardQuery.php index 8453c9b2..901dfb08 100644 --- a/src/Query/TermLevel/WildcardQuery.php +++ b/src/Query/TermLevel/WildcardQuery.php @@ -23,32 +23,19 @@ class WildcardQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $field; - - /** - * @var string - */ - private $value; - /** * @param string $field * @param string $value - * @param array $parameters */ - public function __construct($field, $value, array $parameters = []) + public function __construct(private $field, private $value, array $parameters = []) { - $this->field = $field; - $this->value = $value; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'wildcard'; } @@ -56,7 +43,7 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $query = [ 'value' => $this->value, diff --git a/src/ScriptAwareTrait.php b/src/ScriptAwareTrait.php index 6eb2148e..c389f5db 100644 --- a/src/ScriptAwareTrait.php +++ b/src/ScriptAwareTrait.php @@ -16,25 +16,14 @@ */ trait ScriptAwareTrait { - /** - * @var string - */ - private $script; + private mixed $script = null; - /** - * @return string - */ - public function getScript() + public function getScript(): mixed { return $this->script; } - /** - * @param string $script - * - * @return $this - */ - public function setScript($script) + public function setScript($script): static { $this->script = $script; diff --git a/src/Search.php b/src/Search.php index 89726af4..0d21cbf7 100644 --- a/src/Search.php +++ b/src/Search.php @@ -37,26 +37,20 @@ class Search /** * If you don’t need to track the total number of hits at all you can improve * query times by setting this option to false. Defaults to true. - * - * @var bool */ - private $trackTotalHits; + private ?bool $trackTotalHits = null; /** * To retrieve hits from a certain offset. Defaults to 0. - * - * @var int */ - private $from; + private ?int $from = null; /** * The number of hits to return. Defaults to 10. If you do not care about getting some * hits back but only about the number of matches and/or aggregations, setting the value * to 0 will help performance. - * - * @var int */ - private $size; + private ?int $size = null; /** * Allows to control how the _source field is returned with every hit. By default @@ -69,10 +63,8 @@ class Search /** * Allows to selectively load specific stored fields for each document represented by a search hit. - * - * @var array */ - private $storedFields; + private ?array $storedFields = null; /** * Allows to return a script evaluation (based on different fields) for each hit. @@ -80,10 +72,8 @@ class Search * values to be returned (the evaluated value of the script). Script fields can * also access the actual _source document indexed and extract specific elements * to be returned from it (can be an "object" type). - * - * @var array */ - private $scriptFields; + private ?array $scriptFields = null; /** * Allows to return the doc value representation of a field for each hit. Doc value @@ -91,40 +81,36 @@ class Search * specifies fields without docvalues it will try to load the value from the fielddata * cache causing the terms for that field to be loaded to memory (cached), which will * result in more memory consumption. - * - * @var array */ - private $docValueFields; + private ?array $docValueFields = null; /** * Enables explanation for each hit on how its score was computed. * * @var bool */ - private $explain; + private ?bool $explain = null; /** * Returns a version for each search hit. * * @var bool */ - private $version; + private ?bool $version = null; /** * Allows to configure different boost level per index when searching across more * than one indices. This is very handy when hits coming from one index matter more * than hits coming from another index (think social graph where each user has an index). - * - * @var array */ - private $indicesBoost; + private ?array $indicesBoost = null; /** * Exclude documents which have a _score less than the minimum specified in min_score. * * @var int */ - private $minScore; + private ?int $minScore = null; /** * Pagination of results can be done by using the from and size but the cost becomes @@ -135,19 +121,15 @@ class Search * real time user requests. The search_after parameter circumvents this problem by * providing a live cursor. The idea is to use the results from the previous page to * help the retrieval of the next page. - * - * @var array */ - private $searchAfter; + private ?array $searchAfter = null; /** * URI parameters alongside Request body search. * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html - * - * @var array */ - private $uriParams = []; + private ?array $uriParams = null; /** * While a search request returns a single “page” of results, the scroll API can be used to retrieve @@ -155,20 +137,15 @@ class Search * as you would use a cursor on a traditional database. Scrolling is not intended for real time user * requests, but rather for processing large amounts of data, e.g. in order to reindex the contents * of one index into a new index with a different configuration. - * - * @var string */ - private $scroll; + private ?string $scroll = null; - /** - * @var OrderedSerializer - */ - private static $serializer; + private static ?OrderedSerializer $serializer = null; /** * @var SearchEndpointInterface[] */ - private $endpoints = []; + private ?array $endpoints = null; /** * Constructor to initialize static properties @@ -189,9 +166,9 @@ public function __wakeup() /** * Initializes the serializer */ - private function initializeSerializer() + private function initializeSerializer(): void { - if (static::$serializer === null) { + if (!static::$serializer instanceof OrderedSerializer) { static::$serializer = new OrderedSerializer( [ new CustomReferencedNormalizer(), @@ -206,21 +183,15 @@ private function initializeSerializer() * * @param string $type Endpoint type. */ - public function destroyEndpoint($type) + public function destroyEndpoint($type): void { unset($this->endpoints[$type]); } /** * Adds query to the search. - * - * @param BuilderInterface $query - * @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, mixed $key = null): static { $endpoint = $this->getEndpoint(QueryEndpoint::NAME); $endpoint->addToBool($query, $boolType, $key); @@ -237,7 +208,7 @@ public function addQuery(BuilderInterface $query, $boolType = BoolQuery::MUST, $ */ private function getEndpoint($type) { - if (!array_key_exists($type, $this->endpoints)) { + if (!array_key_exists($type, $this->endpoints ?? [])) { $this->endpoints[$type] = SearchEndpointFactory::get($type); } @@ -259,11 +230,10 @@ public function getQueries() /** * Sets query endpoint parameters. * - * @param array $parameters * * @return $this */ - public function setQueryParameters(array $parameters) + public function setQueryParameters(array $parameters): static { $this->setEndpointParameters(QueryEndpoint::NAME, $parameters); @@ -274,11 +244,10 @@ public function setQueryParameters(array $parameters) * Sets parameters to the endpoint. * * @param string $endpointName - * @param array $parameters * * @return $this */ - public function setEndpointParameters($endpointName, array $parameters) + public function setEndpointParameters($endpointName, array $parameters): static { /** @var AbstractSearchEndpoint $endpoint */ $endpoint = $this->getEndpoint($endpointName); @@ -299,7 +268,7 @@ public function setEndpointParameters($endpointName, array $parameters) * * @return $this. */ - public function addPostFilter(BuilderInterface $filter, $boolType = BoolQuery::MUST, $key = null) + public function addPostFilter(BuilderInterface $filter, $boolType = BoolQuery::MUST, $key = null): static { $this ->getEndpoint(PostFilterEndpoint::NAME) @@ -323,11 +292,10 @@ public function getPostFilters() /** * Sets post filter endpoint parameters. * - * @param array $parameters * * @return $this */ - public function setPostFilterParameters(array $parameters) + public function setPostFilterParameters(array $parameters): static { $this->setEndpointParameters(PostFilterEndpoint::NAME, $parameters); @@ -337,11 +305,10 @@ public function setPostFilterParameters(array $parameters) /** * Adds aggregation into search. * - * @param AbstractAggregation $aggregation * * @return $this */ - public function addAggregation(AbstractAggregation $aggregation) + public function addAggregation(AbstractAggregation $aggregation): static { $this->getEndpoint(AggregationsEndpoint::NAME)->add($aggregation, $aggregation->getName()); @@ -361,11 +328,10 @@ public function getAggregations() /** * Adds inner hit into search. * - * @param NestedInnerHit $innerHit * * @return $this */ - public function addInnerHit(NestedInnerHit $innerHit) + public function addInnerHit(NestedInnerHit $innerHit): static { $this->getEndpoint(InnerHitsEndpoint::NAME)->add($innerHit, $innerHit->getName()); @@ -385,11 +351,10 @@ public function getInnerHits() /** * Adds sort to search. * - * @param BuilderInterface $sort * * @return $this */ - public function addSort(BuilderInterface $sort) + public function addSort(BuilderInterface $sort): static { $this->getEndpoint(SortEndpoint::NAME)->add($sort); @@ -413,7 +378,7 @@ public function getSorts() * * @return $this. */ - public function addHighlight($highlight) + public function addHighlight(BuilderInterface $highlight): static { $this->getEndpoint(HighlightEndpoint::NAME)->add($highlight); @@ -440,7 +405,7 @@ public function getHighlights() * * @return $this */ - public function addSuggest(NamedBuilderInterface $suggest) + public function addSuggest(NamedBuilderInterface $suggest): static { $this->getEndpoint(SuggestEndpoint::NAME)->add($suggest, $suggest->getName()); @@ -460,7 +425,7 @@ public function getSuggests() /** * @return null|int */ - public function getFrom() + public function getFrom(): ?int { return $this->from; } @@ -470,24 +435,19 @@ public function getFrom() * * @return $this */ - public function setFrom($from) + public function setFrom($from): static { $this->from = $from; return $this; } - /** - * @return bool - */ - public function isTrackTotalHits() + public function isTrackTotalHits(): ?bool { return $this->trackTotalHits; } /** - * @param bool $trackTotalHits - * * @return $this */ public function setTrackTotalHits(bool $trackTotalHits) @@ -497,20 +457,12 @@ public function setTrackTotalHits(bool $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): static { $this->size = $size; @@ -530,7 +482,7 @@ public function isSource() * * @return $this */ - public function setSource($source) + public function setSource($source): static { $this->source = $source; @@ -550,7 +502,7 @@ public function getStoredFields() * * @return $this */ - public function setStoredFields($storedFields) + public function setStoredFields($storedFields): static { $this->storedFields = $storedFields; @@ -570,7 +522,7 @@ public function getScriptFields() * * @return $this */ - public function setScriptFields($scriptFields) + public function setScriptFields($scriptFields): static { $this->scriptFields = $scriptFields; @@ -590,7 +542,7 @@ public function getDocValueFields() * * @return $this */ - public function setDocValueFields($docValueFields) + public function setDocValueFields($docValueFields): static { $this->docValueFields = $docValueFields; @@ -610,7 +562,7 @@ public function isExplain() * * @return $this */ - public function setExplain($explain) + public function setExplain($explain): static { $this->explain = $explain; @@ -630,7 +582,7 @@ public function isVersion() * * @return $this */ - public function setVersion($version) + public function setVersion($version): static { $this->version = $version; @@ -650,7 +602,7 @@ public function getIndicesBoost() * * @return $this */ - public function setIndicesBoost($indicesBoost) + public function setIndicesBoost($indicesBoost): static { $this->indicesBoost = $indicesBoost; @@ -670,7 +622,7 @@ public function getMinScore() * * @return $this */ - public function setMinScore($minScore) + public function setMinScore($minScore): static { $this->minScore = $minScore; @@ -690,7 +642,7 @@ public function getSearchAfter() * * @return $this */ - public function setSearchAfter($searchAfter) + public function setSearchAfter($searchAfter): static { $this->searchAfter = $searchAfter; @@ -710,7 +662,7 @@ public function getScroll() * * @return $this */ - public function setScroll($scroll = '5m') + public function setScroll(string $scroll = '5m'): static { $this->scroll = $scroll; @@ -725,7 +677,7 @@ public function setScroll($scroll = '5m') * * @return $this */ - public function addUriParam($name, $value) + public function addUriParam($name, $value): static { if (in_array($name, [ 'q', @@ -763,10 +715,8 @@ public function addUriParam($name, $value) /** * Returns query url parameters. - * - * @return array */ - public function getUriParams() + public function getUriParams(): array { return $this->uriParams; } @@ -774,7 +724,7 @@ public function getUriParams() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $output = array_filter(static::$serializer->normalize($this->endpoints)); diff --git a/src/SearchEndpoint/AbstractSearchEndpoint.php b/src/SearchEndpoint/AbstractSearchEndpoint.php index d3e851dc..1a5b436d 100644 --- a/src/SearchEndpoint/AbstractSearchEndpoint.php +++ b/src/SearchEndpoint/AbstractSearchEndpoint.php @@ -25,7 +25,7 @@ abstract class AbstractSearchEndpoint extends AbstractNormalizable implements Se /** * @var BuilderInterface[] */ - private $container = []; + private array $container = []; /** * {@inheritdoc} @@ -48,7 +48,7 @@ public function add(BuilderInterface $builder, $key = null) /** * {@inheritdoc} */ - public function addToBool(BuilderInterface $builder, $boolType = null, $key = null) + public function addToBool(BuilderInterface $builder, ?string $boolType = null, mixed $key = null) { throw new \BadFunctionCallException(sprintf("Endpoint %s doesn't support bool statements", static::NAME)); } @@ -69,10 +69,8 @@ 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($key): bool { return array_key_exists($key, $this->container); } diff --git a/src/SearchEndpoint/AggregationsEndpoint.php b/src/SearchEndpoint/AggregationsEndpoint.php index 079a6a5d..8eddd6b5 100644 --- a/src/SearchEndpoint/AggregationsEndpoint.php +++ b/src/SearchEndpoint/AggregationsEndpoint.php @@ -22,19 +22,17 @@ class AggregationsEndpoint extends AbstractSearchEndpoint /** * Endpoint name */ - const NAME = 'aggregations'; + final public const NAME = 'aggregations'; /** * {@inheritdoc} */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) + public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null { $output = []; - if (count($this->getAll()) > 0) { - /** @var AbstractAggregation $aggregation */ - foreach ($this->getAll() as $aggregation) { - $output[$aggregation->getName()] = $aggregation->toArray(); - } + /** @var AbstractAggregation $aggregation */ + foreach ($this->getAll() as $aggregation) { + $output[$aggregation->getName()] = $aggregation->toArray(); } return $output; diff --git a/src/SearchEndpoint/HighlightEndpoint.php b/src/SearchEndpoint/HighlightEndpoint.php index f2b8d8e9..b8380ba1 100644 --- a/src/SearchEndpoint/HighlightEndpoint.php +++ b/src/SearchEndpoint/HighlightEndpoint.php @@ -22,7 +22,7 @@ class HighlightEndpoint extends AbstractSearchEndpoint /** * Endpoint name */ - const NAME = 'highlight'; + final public const NAME = 'highlight'; /** * @var BuilderInterface @@ -37,7 +37,7 @@ class HighlightEndpoint extends AbstractSearchEndpoint /** * {@inheritdoc} */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) + public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null { if ($this->highlight) { return $this->highlight->toArray(); @@ -62,7 +62,7 @@ public function add(BuilderInterface $builder, $key = null) /** * {@inheritdoc} */ - public function getAll($boolType = null) + public function getAll($boolType = null): array { return [$this->key => $this->highlight]; } diff --git a/src/SearchEndpoint/InnerHitsEndpoint.php b/src/SearchEndpoint/InnerHitsEndpoint.php index f1562fe6..bcf78c80 100644 --- a/src/SearchEndpoint/InnerHitsEndpoint.php +++ b/src/SearchEndpoint/InnerHitsEndpoint.php @@ -22,19 +22,17 @@ class InnerHitsEndpoint extends AbstractSearchEndpoint /** * Endpoint name */ - const NAME = 'inner_hits'; + final public const NAME = 'inner_hits'; /** * {@inheritdoc} */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) + public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null { $output = []; - if (count($this->getAll()) > 0) { - /** @var NestedInnerHit $innerHit */ - foreach ($this->getAll() as $innerHit) { - $output[$innerHit->getName()] = $innerHit->toArray(); - } + /** @var NestedInnerHit $innerHit */ + foreach ($this->getAll() as $innerHit) { + $output[$innerHit->getName()] = $innerHit->toArray(); } return $output; diff --git a/src/SearchEndpoint/PostFilterEndpoint.php b/src/SearchEndpoint/PostFilterEndpoint.php index 6055b7ed..b35869df 100644 --- a/src/SearchEndpoint/PostFilterEndpoint.php +++ b/src/SearchEndpoint/PostFilterEndpoint.php @@ -21,12 +21,12 @@ class PostFilterEndpoint extends QueryEndpoint /** * Endpoint name */ - const NAME = 'post_filter'; + final public const NAME = 'post_filter'; /** * {@inheritdoc} */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) + public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null { if (!$this->getBool()) { return null; @@ -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 6212b05d..24c04770 100644 --- a/src/SearchEndpoint/QueryEndpoint.php +++ b/src/SearchEndpoint/QueryEndpoint.php @@ -24,22 +24,16 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI /** * Endpoint name */ - const NAME = 'query'; + public const NAME = 'query'; - /** - * @var BoolQuery - */ - private $bool; + private ?BoolQuery $bool = null; - /** - * @var bool - */ - private $filtersSet = false; + private bool $filtersSet = false; /** * {@inheritdoc} */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) + public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null { if (!$this->filtersSet && $this->hasReference('filter_query')) { /** @var BuilderInterface $filter */ @@ -48,7 +42,7 @@ public function normalize(NormalizerInterface $normalizer, $format = null, array $this->filtersSet = true; } - if (!$this->bool) { + if (!$this->bool instanceof BoolQuery) { return null; } @@ -66,9 +60,9 @@ public function add(BuilderInterface $builder, $key = null) /** * {@inheritdoc} */ - public function addToBool(BuilderInterface $builder, $boolType = null, $key = null) + public function addToBool(BuilderInterface $builder, ?string $boolType = null, mixed $key = null) { - if (!$this->bool) { + if (!$this->bool instanceof BoolQuery) { $this->bool = new BoolQuery(); } diff --git a/src/SearchEndpoint/SearchEndpointFactory.php b/src/SearchEndpoint/SearchEndpointFactory.php index 17e6838b..e3acf4a5 100644 --- a/src/SearchEndpoint/SearchEndpointFactory.php +++ b/src/SearchEndpoint/SearchEndpointFactory.php @@ -19,14 +19,14 @@ class SearchEndpointFactory /** * @var array Holds namespaces for endpoints. */ - private static $endpoints = [ - 'query' => 'ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint', - 'post_filter' => 'ONGR\ElasticsearchDSL\SearchEndpoint\PostFilterEndpoint', - 'sort' => 'ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint', - 'highlight' => 'ONGR\ElasticsearchDSL\SearchEndpoint\HighlightEndpoint', - 'aggregations' => 'ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint', - 'suggest' => 'ONGR\ElasticsearchDSL\SearchEndpoint\SuggestEndpoint', - 'inner_hits' => 'ONGR\ElasticsearchDSL\SearchEndpoint\InnerHitsEndpoint', + private static array $endpoints = [ + 'query' => QueryEndpoint::class, + 'post_filter' => PostFilterEndpoint::class, + 'sort' => SortEndpoint::class, + 'highlight' => HighlightEndpoint::class, + 'aggregations' => AggregationsEndpoint::class, + 'suggest' => SuggestEndpoint::class, + 'inner_hits' => InnerHitsEndpoint::class, ]; /** diff --git a/src/SearchEndpoint/SearchEndpointInterface.php b/src/SearchEndpoint/SearchEndpointInterface.php index 17ec4abf..0464cba1 100644 --- a/src/SearchEndpoint/SearchEndpointInterface.php +++ b/src/SearchEndpoint/SearchEndpointInterface.php @@ -40,7 +40,7 @@ public function add(BuilderInterface $builder, $key = null); * * @return string Key of added builder. */ - public function addToBool(BuilderInterface $builder, $boolType = null, $key = null); + public function addToBool(BuilderInterface $builder, ?string $boolType = null, mixed $key = null); /** * Removes contained builder. diff --git a/src/SearchEndpoint/SortEndpoint.php b/src/SearchEndpoint/SortEndpoint.php index 2461c858..ba2f7d1b 100644 --- a/src/SearchEndpoint/SortEndpoint.php +++ b/src/SearchEndpoint/SortEndpoint.php @@ -21,12 +21,12 @@ class SortEndpoint extends AbstractSearchEndpoint /** * Endpoint name */ - const NAME = 'sort'; + final public const NAME = 'sort'; /** * {@inheritdoc} */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) + public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null { $output = []; diff --git a/src/SearchEndpoint/SuggestEndpoint.php b/src/SearchEndpoint/SuggestEndpoint.php index 2e2e16ac..56a684b0 100644 --- a/src/SearchEndpoint/SuggestEndpoint.php +++ b/src/SearchEndpoint/SuggestEndpoint.php @@ -22,19 +22,17 @@ class SuggestEndpoint extends AbstractSearchEndpoint /** * Endpoint name */ - const NAME = 'suggest'; + final public const NAME = 'suggest'; /** * {@inheritdoc} */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) + public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null { $output = []; - if (count($this->getAll()) > 0) { - /** @var TermSuggest $suggest */ - foreach ($this->getAll() as $suggest) { - $output = array_merge($output, $suggest->toArray()); - } + /** @var TermSuggest $suggest */ + foreach ($this->getAll() as $suggest) { + $output = array_merge($output, $suggest->toArray()); } return $output; diff --git a/src/Serializer/Normalizer/CustomReferencedNormalizer.php b/src/Serializer/Normalizer/CustomReferencedNormalizer.php index 6cd24a8e..bd5501b1 100644 --- a/src/Serializer/Normalizer/CustomReferencedNormalizer.php +++ b/src/Serializer/Normalizer/CustomReferencedNormalizer.php @@ -18,15 +18,12 @@ */ class CustomReferencedNormalizer extends CustomNormalizer { - /** - * @var array - */ - private $references = []; + private array $references = []; /** * {@inheritdoc} */ - public function normalize($object, $format = null, array $context = []) + public function normalize(mixed $object, string $format = null, array $context = []): array|bool|string|int|float|null|\ArrayObject { $object->setReferences($this->references); $data = parent::normalize($object, $format, $context); @@ -38,7 +35,7 @@ public function normalize($object, $format = null, array $context = []) /** * {@inheritdoc} */ - public function supportsNormalization($data, $format = null) + public function supportsNormalization($data, $format = null): bool { return $data instanceof AbstractNormalizable; } diff --git a/src/Serializer/OrderedSerializer.php b/src/Serializer/OrderedSerializer.php index bcbb9c3d..90e33297 100644 --- a/src/Serializer/OrderedSerializer.php +++ b/src/Serializer/OrderedSerializer.php @@ -22,7 +22,7 @@ class OrderedSerializer extends Serializer /** * {@inheritdoc} */ - public function normalize($data, $format = null, array $context = []) + public function normalize($data, string $format = null, array $context = []): array|bool|string|int|float|null|\ArrayObject { return parent::normalize( is_array($data) ? $this->order($data) : $data, @@ -34,7 +34,7 @@ public function normalize($data, $format = null, array $context = []) /** * {@inheritdoc} */ - public function denormalize($data, $type, $format = null, array $context = []) + public function denormalize($data, string $type, string $format = null, array $context = []): mixed { return parent::denormalize( is_array($data) ? $this->order($data) : $data, @@ -46,21 +46,15 @@ public function denormalize($data, $type, $format = null, array $context = []) /** * Orders objects if can be done. - * - * @param array $data Data to order. - * - * @return array */ - private function order(array $data) + private function order(array $data): array { $filteredData = $this->filterOrderable($data); if (!empty($filteredData)) { uasort( $filteredData, - function (OrderedNormalizerInterface $a, OrderedNormalizerInterface $b) { - return $a->getOrder() > $b->getOrder(); - } + static fn(OrderedNormalizerInterface $a, OrderedNormalizerInterface $b): bool => $a->getOrder() > $b->getOrder() ); return array_merge($filteredData, array_diff_key($data, $filteredData)); @@ -73,16 +67,12 @@ function (OrderedNormalizerInterface $a, OrderedNormalizerInterface $b) { * Filters out data which can be ordered. * * @param array $array Data to filter out. - * - * @return array */ - private function filterOrderable($array) + private function filterOrderable(array $array): array { return array_filter( $array, - function ($value) { - return $value instanceof OrderedNormalizerInterface; - } + static fn($value): bool => $value instanceof OrderedNormalizerInterface ); } } diff --git a/src/Sort/FieldSort.php b/src/Sort/FieldSort.php index 36cb8908..fe7c1d5f 100644 --- a/src/Sort/FieldSort.php +++ b/src/Sort/FieldSort.php @@ -21,90 +21,51 @@ class FieldSort implements BuilderInterface { use ParametersTrait; - const ASC = 'asc'; - const DESC = 'desc'; + final public const ASC = 'asc'; + final public const DESC = 'desc'; - /** - * @var string - */ - private $field; - - /** - * @var string - */ - private $order; - - /** - * @var BuilderInterface - */ - private $nestedFilter; + private ?BuilderInterface $nestedFilter = null; /** * @param string $field Field name. * @param string $order Order direction. * @param array $params Params that can be set to field sort. */ - public function __construct($field, $order = null, $params = []) + public function __construct(private string $field, private ?string $order = null, array $params = []) { - $this->field = $field; - $this->order = $order; $this->setParameters($params); } - /** - * @return string - */ - public function getField() + public function getField(): string { return $this->field; } - /** - * @param string $field - * - * @return $this - */ - public function setField($field) + public function setField(string $field): static { $this->field = $field; return $this; } - /** - * @return string - */ - public function getOrder() + public function getOrder(): string { return $this->order; } - /** - * @param string $order - * - * @return $this - */ - public function setOrder($order) + public function setOrder(string $order): static { $this->order = $order; return $this; } - /** - * @return BuilderInterface - */ - public function getNestedFilter() + public function getNestedFilter(): ?BuilderInterface { return $this->nestedFilter; } - /** - * @param BuilderInterface $nestedFilter - * - * @return $this - */ - public function setNestedFilter(BuilderInterface $nestedFilter) + public function setNestedFilter(BuilderInterface $nestedFilter): static { $this->nestedFilter = $nestedFilter; @@ -113,10 +74,8 @@ public function setNestedFilter(BuilderInterface $nestedFilter) /** * Returns element type. - * - * @return string */ - public function getType() + public function getType(): string { return 'sort'; } @@ -124,20 +83,18 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { if ($this->order) { $this->addParameter('order', $this->order); } - if ($this->nestedFilter) { + if ($this->nestedFilter instanceof BuilderInterface) { $this->addParameter('nested', $this->nestedFilter->toArray()); } - $output = [ - $this->field => !$this->getParameters() ? new \stdClass() : $this->getParameters(), + return [ + $this->field => $this->getParameters() ?: [], ]; - - return $output; } } diff --git a/src/Sort/NestedSort.php b/src/Sort/NestedSort.php index 8954719f..355ece62 100644 --- a/src/Sort/NestedSort.php +++ b/src/Sort/NestedSort.php @@ -23,40 +23,24 @@ class NestedSort implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $path; - - /** - * @var BuilderInterface - */ - private $filter; - - /** - * @var BuilderInterface - */ - private $nestedFilter; + private ?BuilderInterface $nestedFilter = null; /** * @param string $path - * @param BuilderInterface $filter - * @param array $parameters + * @param BuilderInterface|null $filter */ public function __construct( - $path, - BuilderInterface $filter = null, + private $path, + private readonly ?BuilderInterface $filter = null, array $parameters = [] ) { - $this->path = $path; - $this->filter = $filter; $this->setParameters($parameters); } /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return 'nested'; } @@ -64,17 +48,17 @@ public function getType() /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { $output = [ 'path' => $this->path, ]; - if ($this->filter) { + if ($this->filter instanceof BuilderInterface) { $output['filter'] = $this->filter->toArray(); } - if ($this->nestedFilter) { + if ($this->nestedFilter instanceof BuilderInterface) { $output[$this->getType()] = $this->nestedFilter->toArray(); } @@ -83,10 +67,8 @@ public function toArray() /** * Returns nested filter object. - * - * @return BuilderInterface */ - public function getFilter() + public function getFilter(): ?BuilderInterface { return $this->filter; } @@ -101,17 +83,12 @@ public function getPath() return $this->path; } - /** - * @return BuilderInterface - */ - public function getNestedFilter() + public function getNestedFilter(): ?BuilderInterface { return $this->nestedFilter; } /** - * @param BuilderInterface $nestedFilter - * * @return $this */ public function setNestedFilter(BuilderInterface $nestedFilter) diff --git a/src/Suggest/Suggest.php b/src/Suggest/Suggest.php index 1738d84d..95fa710e 100644 --- a/src/Suggest/Suggest.php +++ b/src/Suggest/Suggest.php @@ -13,41 +13,20 @@ use ONGR\ElasticsearchDSL\NamedBuilderInterface; use ONGR\ElasticsearchDSL\ParametersTrait; -use Symfony\Component\Serializer\Exception\InvalidArgumentException; class Suggest implements NamedBuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $name; - - /** - * @var string - */ - private $type; - - /** - * @var string - */ - private $text; - - /** - * @var string - */ - private $field; + private string $name; + private string $type; + private string $text; + private string $field; /** * TermSuggest constructor. - * @param string $name - * @param string $type - * @param string $text - * @param string $field - * @param array $parameters */ - public function __construct($name, $type, $text, $field, $parameters = []) + public function __construct(string $name, string $type, string $text, string $field, array $parameters = []) { $this->setName($name); $this->setType($type); @@ -56,84 +35,48 @@ public function __construct($name, $type, $text, $field, $parameters = []) $this->setParameters($parameters); } - /** - * @param string $name - * - * @return $this - */ - public function setName($name) + public function setName(string $name): static { $this->name = $name; return $this; } - /** - * Returns suggest name - * - * @return string - */ - public function getName() + public function getName(): string { return $this->name; } - /** - * Returns element type. - * - * @return string - */ - public function getType() + public function getType(): string { return $this->type; } - /** - * @param string $type - * - * @return $this - */ - public function setType($type) + public function setType(string $type): static { $this->type = $type; return $this; } - /** - * @return string - */ - public function getText() + public function getText(): string { return $this->text; } - /** - * @param string $text - * - * @return $this - */ - public function setText($text) + public function setText(string $text): static { $this->text = $text; return $this; } - /** - * @return string - */ - public function getField() + public function getField(): string { return $this->field; } - /** - * @param string $field - * - * @return $this - */ - public function setField($field) + public function setField(string $field): static { $this->field = $field; @@ -143,15 +86,13 @@ public function setField($field) /** * {@inheritdoc} */ - public function toArray() + public function toArray(): array { - $output = [ + return [ $this->getName() => [ 'text' => $this->getText(), $this->getType() => $this->processArray(['field' => $this->getField()]), ] ]; - - return $output; } } diff --git a/tests/Functional/AbstractElasticsearchTestCase.php b/tests/Functional/AbstractElasticsearchTestCase.php index 3c023410..3df94835 100644 --- a/tests/Functional/AbstractElasticsearchTestCase.php +++ b/tests/Functional/AbstractElasticsearchTestCase.php @@ -11,8 +11,8 @@ namespace ONGR\ElasticsearchDSL\Tests\Functional; -use Elasticsearch\Client; -use Elasticsearch\ClientBuilder; +use Elastic\Elasticsearch\Client; +use Elastic\Elasticsearch\ClientBuilder; use ONGR\ElasticsearchDSL\Search; use PHPUnit\Framework\TestCase; @@ -21,7 +21,7 @@ abstract class AbstractElasticsearchTestCase extends TestCase /** * Test index name in the elasticsearch. */ - const INDEX_NAME = 'elasticsaerch-dsl-test'; + final public const INDEX_NAME = 'elasticsaerch-dsl-test'; /** * @var Client @@ -31,7 +31,7 @@ abstract class AbstractElasticsearchTestCase extends TestCase /** * {@inheritdoc} */ - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -109,7 +109,7 @@ protected function getDataArray() /** * {@inheritdoc} */ - protected function tearDown() + protected function tearDown(): void { parent::tearDown(); $this->deleteIndex(); @@ -143,7 +143,7 @@ protected function executeSearch(Search $search, $type = null, $returnRaw = fals foreach ($response['hits']['hits'] as $document) { $documents[$document['_id']] = $document['_source']; } - } catch (\Exception $e) { + } catch (\Exception) { return $documents; } @@ -153,11 +153,11 @@ protected function executeSearch(Search $search, $type = null, $returnRaw = fals /** * Deletes index from elasticsearch. */ - private function deleteIndex() + private function deleteIndex(): void { try { $this->client->indices()->delete(['index' => self::INDEX_NAME]); - } catch (\Exception $e) { + } catch (\Exception) { // Do nothing. } } diff --git a/tests/Functional/Query/FunctionScoreQueryTest.php b/tests/Functional/Query/FunctionScoreQueryTest.php index 63e89872..60e1055c 100644 --- a/tests/Functional/Query/FunctionScoreQueryTest.php +++ b/tests/Functional/Query/FunctionScoreQueryTest.php @@ -21,7 +21,7 @@ class FunctionScoreQueryTest extends AbstractElasticsearchTestCase /** * {@inheritdoc} */ - protected function getDataArray() + protected function getDataArray(): array { return [ 'product' => [ @@ -44,7 +44,7 @@ protected function getDataArray() /** * Match all test */ - public function testRandomScore() + public function testRandomScore(): void { $fquery = new FunctionScoreQuery(new MatchAllQuery()); $fquery->addRandomFunction(); @@ -52,12 +52,13 @@ public function testRandomScore() $search = new Search(); $search->addQuery($fquery); + $results = $this->executeSearch($search); - $this->assertEquals(count($this->getDataArray()['product']), count($results)); + $this->assertEquals(is_countable($this->getDataArray()['product']) ? count($this->getDataArray()['product']) : 0, count($results)); } - public function testScriptScore() + public function testScriptScore(): void { $fquery = new FunctionScoreQuery(new MatchAllQuery()); $fquery->addScriptScoreFunction( @@ -76,6 +77,7 @@ public function testScriptScore() $search = new Search(); $search->addQuery($fquery); + $results = $this->executeSearch($search); foreach ($results as $document) { diff --git a/tests/Functional/Query/MatchAllQueryTest.php b/tests/Functional/Query/MatchAllQueryTest.php index 49d3a48f..38c0a3ad 100644 --- a/tests/Functional/Query/MatchAllQueryTest.php +++ b/tests/Functional/Query/MatchAllQueryTest.php @@ -20,7 +20,7 @@ class MatchAllQueryTest extends AbstractElasticsearchTestCase /** * {@inheritdoc} */ - protected function getDataArray() + protected function getDataArray(): array { return [ 'product' => [ @@ -37,13 +37,13 @@ protected function getDataArray() /** * Match all test */ - public function testMatchAll() + public function testMatchAll(): void { $search = new Search(); $matchAll = new MatchAllQuery(); $search->addQuery($matchAll); - $q = $search->getQueries(); + $search->getQueries(); $results = $this->executeSearch($search); $this->assertEquals($this->getDataArray()['product'], $results); diff --git a/tests/Unit/Aggregation/Bucketing/AdjacencyMatrixAggregationTest.php b/tests/Unit/Aggregation/Bucketing/AdjacencyMatrixAggregationTest.php index f73cc612..02774511 100644 --- a/tests/Unit/Aggregation/Bucketing/AdjacencyMatrixAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/AdjacencyMatrixAggregationTest.php @@ -11,13 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\MockObject\MockObject; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\AdjacencyMatrixAggregation; use ONGR\ElasticsearchDSL\BuilderInterface; /** * Unit test for adjacency matrix aggregation. */ -class AdjacencyMatrixAggregationTest extends \PHPUnit\Framework\TestCase +class AdjacencyMatrixAggregationTest extends TestCase { // /** // * Test if exception is thrown when not anonymous filter is without name. @@ -35,11 +37,12 @@ class AdjacencyMatrixAggregationTest extends \PHPUnit\Framework\TestCase /** * Test GetArray method. */ - public function testFiltersAggregationGetArray() + public function testFiltersAggregationGetArray(): void { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); + $mock = $this->createMock(BuilderInterface::class); $aggregation = new AdjacencyMatrixAggregation('test_agg'); $aggregation->addFilter('name', $mock); + $result = $aggregation->getArray(); $this->assertArrayHasKey('filters', $result); } @@ -47,7 +50,7 @@ public function testFiltersAggregationGetArray() /** * Tests getType method. */ - public function testFiltersAggregationGetType() + public function testFiltersAggregationGetType(): void { $aggregation = new AdjacencyMatrixAggregation('foo'); $result = $aggregation->getType(); @@ -57,11 +60,11 @@ public function testFiltersAggregationGetType() /** * Test for filter aggregation toArray() method. */ - public function testToArray() + public function testToArray(): void { $aggregation = new AdjacencyMatrixAggregation('test_agg'); - $filter = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface') - ->setMethods(['toArray', 'getType']) + $filter = $this->getMockBuilder(BuilderInterface::class) + ->onlyMethods(['toArray', 'getType']) ->getMockForAbstractClass(); $filter->expects($this->any()) ->method('toArray') @@ -93,12 +96,12 @@ public function testToArray() /** * Tests if filters can be passed to the constructor. */ - public function testFilterConstructor() + public function testFilterConstructor(): void { - /** @var BuilderInterface|\PHPUnit_Framework_MockObject_MockObject $builderInterface1 */ - $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); - /** @var BuilderInterface|\PHPUnit_Framework_MockObject_MockObject $builderInterface2 */ - $builderInterface2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); + /** @var BuilderInterface|MockObject $builderInterface1 */ + $builderInterface1 = $this->getMockForAbstractClass(BuilderInterface::class); + /** @var BuilderInterface|MockObject $builderInterface2 */ + $builderInterface2 = $this->getMockForAbstractClass(BuilderInterface::class); $aggregation = new AdjacencyMatrixAggregation( 'test', @@ -112,8 +115,8 @@ public function testFilterConstructor() [ 'adjacency_matrix' => [ 'filters' => [ - 'filter1' => null, - 'filter2' => null, + 'filter1' => $builderInterface1->toArray(), + 'filter2' => $builderInterface2->toArray(), ], ], ], diff --git a/tests/Unit/Aggregation/Bucketing/AudoDateHistogramAggregationTest.php b/tests/Unit/Aggregation/Bucketing/AudoDateHistogramAggregationTest.php index bece77d3..c52b6a5e 100644 --- a/tests/Unit/Aggregation/Bucketing/AudoDateHistogramAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/AudoDateHistogramAggregationTest.php @@ -11,15 +11,16 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\AutoDateHistogramAggregation; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\TermsAggregation; -class AudoDateHistogramAggregationTest extends \PHPUnit\Framework\TestCase +class AudoDateHistogramAggregationTest extends TestCase { /** * Tests agg. */ - public function testAutoDateHistogramAggregationSetField() + public function testAutoDateHistogramAggregationSetField(): void { // Case #0 terms aggregation. $aggregation = new AutoDateHistogramAggregation('test_agg', 'test_field'); @@ -34,7 +35,7 @@ public function testAutoDateHistogramAggregationSetField() /** * Tests setSize method. */ - public function testAutoDateHistogramAggregationFormat() + public function testAutoDateHistogramAggregationFormat(): void { $date = '2020-12-25'; // Case #1 @@ -67,7 +68,7 @@ public function testAutoDateHistogramAggregationFormat() /** * Tests buckets. */ - public function testAutoDateHistogramAggregationBuckets() + public function testAutoDateHistogramAggregationBuckets(): void { // Case #1 $aggregation = new AutoDateHistogramAggregation('test_agg', 'wrong_field'); @@ -101,7 +102,7 @@ public function testAutoDateHistogramAggregationBuckets() /** * Tests getType method. */ - public function testAutoDateHistogramAggregationGetType() + public function testAutoDateHistogramAggregationGetType(): void { $aggregation = new AutoDateHistogramAggregation('foo', 'bar'); $result = $aggregation->getType(); diff --git a/tests/Unit/Aggregation/Bucketing/ChildrenAggregationTest.php b/tests/Unit/Aggregation/Bucketing/ChildrenAggregationTest.php index f4cb6b3a..7927c539 100644 --- a/tests/Unit/Aggregation/Bucketing/ChildrenAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/ChildrenAggregationTest.php @@ -11,20 +11,21 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\ChildrenAggregation; /** * Unit test for children aggregation. */ -class ChildrenAggregationTest extends \PHPUnit\Framework\TestCase +class ChildrenAggregationTest extends TestCase { /** * Tests if ChildrenAggregation#getArray throws exception when expected. - * - * @expectedException \LogicException */ - public function testGetArrayException() + public function testGetArrayException(): void { + $this->expectException(\LogicException::class); $aggregation = new ChildrenAggregation('foo'); $aggregation->getArray(); } @@ -32,7 +33,7 @@ public function testGetArrayException() /** * Tests getType method. */ - public function testChildrenAggregationGetType() + public function testChildrenAggregationGetType(): void { $aggregation = new ChildrenAggregation('foo'); $result = $aggregation->getType(); @@ -42,14 +43,15 @@ public function testChildrenAggregationGetType() /** * Tests getArray method. */ - public function testChildrenAggregationGetArray() + public function testChildrenAggregationGetArray(): void { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation') + $mock = $this->getMockBuilder(AbstractAggregation::class) ->disableOriginalConstructor() ->getMockForAbstractClass(); $aggregation = new ChildrenAggregation('foo'); $aggregation->addAggregation($mock); $aggregation->setChildren('question'); + $result = $aggregation->getArray(); $expected = ['type' => 'question']; $this->assertEquals($expected, $result); diff --git a/tests/Unit/Aggregation/Bucketing/CompositeAggregationTest.php b/tests/Unit/Aggregation/Bucketing/CompositeAggregationTest.php index b45c2233..eaf33757 100644 --- a/tests/Unit/Aggregation/Bucketing/CompositeAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/CompositeAggregationTest.php @@ -11,15 +11,16 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\CompositeAggregation; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\TermsAggregation; -class CompositeAggregationTest extends \PHPUnit\Framework\TestCase +class CompositeAggregationTest extends TestCase { /** * Test for composite aggregation toArray() method exception. */ - public function testToArray() + public function testToArray(): void { $compositeAgg = new CompositeAggregation('composite_test_agg'); $termsAgg = new TermsAggregation('test_term_agg', 'test_field'); @@ -41,7 +42,7 @@ public function testToArray() /** * Test for composite aggregation toArray() method with size and after part. */ - public function testToArrayWithSizeAndAfter() + public function testToArrayWithSizeAndAfter(): void { $compositeAgg = new CompositeAggregation('composite_test_agg'); $termsAgg = new TermsAggregation('test_term_agg', 'test_field'); @@ -67,7 +68,7 @@ public function testToArrayWithSizeAndAfter() /** * Test for composite aggregation getSize() method. */ - public function testGetSize() + public function testGetSize(): void { $compositeAgg = new CompositeAggregation('composite_test_agg'); $compositeAgg->setSize(5); @@ -78,7 +79,7 @@ public function testGetSize() /** * Test for composite aggregation getAfter() method. */ - public function testGetAfter() + public function testGetAfter(): void { $compositeAgg = new CompositeAggregation('composite_test_agg'); $compositeAgg->setAfter(['test_term_agg' => 'test']); @@ -89,18 +90,19 @@ public function testGetAfter() /** * Tests getType method. */ - public function testGetType() + public function testGetType(): void { $aggregation = new CompositeAggregation('foo'); $result = $aggregation->getType(); $this->assertEquals('composite', $result); } - public function testTermsSourceWithOrderParameter() + public function testTermsSourceWithOrderParameter(): void { $compositeAgg = new CompositeAggregation('composite_with_order'); $termsAgg = new TermsAggregation('test_term_agg', 'test_field'); $termsAgg->addParameter('order', 'asc'); + $compositeAgg->addSource($termsAgg); $expectedResult = [ @@ -117,11 +119,12 @@ public function testTermsSourceWithOrderParameter() } - public function testTermsSourceWithDescOrderParameter() + public function testTermsSourceWithDescOrderParameter(): void { $compositeAgg = new CompositeAggregation('composite_with_order'); $termsAgg = new TermsAggregation('test_term_agg', 'test_field'); $termsAgg->addParameter('order', 'desc'); + $compositeAgg->addSource($termsAgg); $expectedResult = [ @@ -138,16 +141,18 @@ public function testTermsSourceWithDescOrderParameter() } - public function testMultipleSourcesWithDifferentOrders() + public function testMultipleSourcesWithDifferentOrders(): void { $compositeAgg = new CompositeAggregation('composite_with_order'); $termsAgg = new TermsAggregation('test_term_agg_1', 'test_field'); $termsAgg->addParameter('order', 'desc'); + $compositeAgg->addSource($termsAgg); $termsAgg = new TermsAggregation('test_term_agg_2', 'test_field'); $termsAgg->addParameter('order', 'asc'); + $compositeAgg->addSource($termsAgg); $expectedResult = [ diff --git a/tests/Unit/Aggregation/Bucketing/DateHistogramAggregationTest.php b/tests/Unit/Aggregation/Bucketing/DateHistogramAggregationTest.php index 6f7544bb..0d4b55b5 100644 --- a/tests/Unit/Aggregation/Bucketing/DateHistogramAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/DateHistogramAggregationTest.php @@ -11,20 +11,21 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\DateHistogramAggregation; /** * Unit test for children aggregation. */ -class DateHistogramAggregationTest extends \PHPUnit\Framework\TestCase +class DateHistogramAggregationTest extends TestCase { /** * Tests if ChildrenAggregation#getArray throws exception when expected. - * - * @expectedException \LogicException */ - public function testGetArrayException() + public function testGetArrayException(): void { + $this->expectException(\LogicException::class); $aggregation = new DateHistogramAggregation('foo'); $aggregation->getArray(); } @@ -32,7 +33,7 @@ public function testGetArrayException() /** * Tests getType method. */ - public function testDateHistogramAggregationGetType() + public function testDateHistogramAggregationGetType(): void { $aggregation = new DateHistogramAggregation('foo'); $result = $aggregation->getType(); @@ -42,15 +43,16 @@ public function testDateHistogramAggregationGetType() /** * Tests getArray method. */ - public function testChildrenAggregationGetArray() + public function testChildrenAggregationGetArray(): void { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation') + $mock = $this->getMockBuilder(AbstractAggregation::class) ->disableOriginalConstructor() ->getMockForAbstractClass(); $aggregation = new DateHistogramAggregation('foo'); $aggregation->addAggregation($mock); $aggregation->setField('date'); $aggregation->setInterval('month'); + $result = $aggregation->getArray(); $expected = ['field' => 'date', 'interval' => 'month']; $this->assertEquals($expected, $result); diff --git a/tests/Unit/Aggregation/Bucketing/DateRangeAggregationTest.php b/tests/Unit/Aggregation/Bucketing/DateRangeAggregationTest.php index 6a3c5dbe..497b3139 100644 --- a/tests/Unit/Aggregation/Bucketing/DateRangeAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/DateRangeAggregationTest.php @@ -11,30 +11,31 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\MockObject\MockObject; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\DateRangeAggregation; -class DateRangeAggregationTest extends \PHPUnit\Framework\TestCase +class DateRangeAggregationTest extends TestCase { /** * Test if exception is thrown. - * - * @expectedException \LogicException - * @expectedExceptionMessage Date range aggregation must have field, format set and range added. */ - public function testIfExceptionIsThrownWhenNoParametersAreSet() + public function testIfExceptionIsThrownWhenNoParametersAreSet(): void { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Date range aggregation must have field, format set and range added.'); $agg = new DateRangeAggregation('test_agg'); $agg->getArray(); } /** * Test if exception is thrown when both range parameters are null. - * - * @expectedException \LogicException - * @expectedExceptionMessage Either from or to must be set. Both cannot be null. */ - public function testIfExceptionIsThrownWhenBothRangesAreNull() + public function testIfExceptionIsThrownWhenBothRangesAreNull(): void { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Either from or to must be set. Both cannot be null.'); $agg = new DateRangeAggregation('test_agg'); $agg->addRange(null, null); } @@ -42,12 +43,13 @@ public function testIfExceptionIsThrownWhenBothRangesAreNull() /** * Test getArray method. */ - public function testDateRangeAggregationGetArray() + public function testDateRangeAggregationGetArray(): void { $agg = new DateRangeAggregation('foo', 'baz'); $agg->addRange(10, 20); $agg->setFormat('bar'); $agg->setKeyed(true); + $result = $agg->getArray(); $expected = [ 'format' => 'bar', @@ -61,7 +63,7 @@ public function testDateRangeAggregationGetArray() /** * Tests getType method. */ - public function testDateRangeAggregationGetType() + public function testDateRangeAggregationGetType(): void { $aggregation = new DateRangeAggregation('foo'); $result = $aggregation->getType(); @@ -70,10 +72,8 @@ public function testDateRangeAggregationGetType() /** * Data provider for testDateRangeAggregationConstructor. - * - * @return array */ - public function testDateRangeAggregationConstructorProvider() + public static function dateRangeAggregationConstructorProvider(): array { return [ // Case #0. Minimum arguments. @@ -110,15 +110,13 @@ public function testDateRangeAggregationConstructorProvider() * * @param string $field * @param string $format - * @param array $ranges - * - * @dataProvider testDateRangeAggregationConstructorProvider */ - public function testDateRangeAggregationConstructor($field = null, $format = null, array $ranges = null) + #[DataProvider('dateRangeAggregationConstructorProvider')] + public function testDateRangeAggregationConstructor($field = null, $format = null, array $ranges = null): void { - /** @var DateRangeAggregation|\PHPUnit_Framework_MockObject_MockObject $aggregation */ - $aggregation = $this->getMockBuilder('ONGR\ElasticsearchDSL\Aggregation\Bucketing\DateRangeAggregation') - ->setMethods(['setField', 'setFormat', 'addRange']) + /** @var DateRangeAggregation|MockObject $aggregation */ + $aggregation = $this->getMockBuilder(DateRangeAggregation::class) + ->onlyMethods(['setField', 'setFormat', 'addRange']) ->disableOriginalConstructor() ->getMock(); $aggregation->expects($this->once())->method('setField')->with($field); diff --git a/tests/Unit/Aggregation/Bucketing/FilterAggregationTest.php b/tests/Unit/Aggregation/Bucketing/FilterAggregationTest.php index b453e014..89c1a93c 100644 --- a/tests/Unit/Aggregation/Bucketing/FilterAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/FilterAggregationTest.php @@ -11,6 +11,9 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\FilterAggregation; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\HistogramAggregation; use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery; @@ -18,14 +21,12 @@ use ONGR\ElasticsearchDSL\Query\MatchAllQuery; use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; -class FilterAggregationTest extends \PHPUnit\Framework\TestCase +class FilterAggregationTest extends TestCase { /** * Data provider for testToArray. - * - * @return array */ - public function getToArrayData() + public static function getToArrayData(): array { $out = []; @@ -91,34 +92,31 @@ public function getToArrayData() * * @param FilterAggregation $aggregation * @param array $expectedResult - * - * @dataProvider getToArrayData */ - public function testToArray($aggregation, $expectedResult) + #[DataProvider('getToArrayData')] + public function testToArray($aggregation, mixed $expectedResult): void { $this->assertEquals($expectedResult, $aggregation->toArray()); } /** * Test for setField(). - * - * @expectedException \LogicException - * @expectedExceptionMessage doesn't support `field` parameter */ - public function testSetField() + public function testSetField(): never { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage("doesn't support `field` parameter"); $aggregation = new FilterAggregation('test_agg'); $aggregation->setField('test_field'); } /** * Test for toArray() without setting a filter. - * - * @expectedException \LogicException - * @expectedExceptionMessage has no filter added */ - public function testToArrayNoFilter() + public function testToArrayNoFilter(): void { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('has no filter added'); $aggregation = new FilterAggregation('test_agg'); $aggregation->toArray(); } @@ -126,7 +124,8 @@ public function testToArrayNoFilter() /** * Test for toArray() with setting a filter. */ - public function testToArrayWithFilter() + #[DoesNotPerformAssertions] + public function testToArrayWithFilter(): void { $aggregation = new FilterAggregation('test_agg'); @@ -137,7 +136,7 @@ public function testToArrayWithFilter() /** * Tests if filter can be passed to constructor. */ - public function testConstructorFilter() + public function testConstructorFilter(): void { $matchAllFilter = new MatchAllQuery(); $aggregation = new FilterAggregation('test', $matchAllFilter); diff --git a/tests/Unit/Aggregation/Bucketing/FiltersAggregationTest.php b/tests/Unit/Aggregation/Bucketing/FiltersAggregationTest.php index b3032a31..de7e8093 100644 --- a/tests/Unit/Aggregation/Bucketing/FiltersAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/FiltersAggregationTest.php @@ -11,23 +11,24 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\MockObject\MockObject; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\FiltersAggregation; use ONGR\ElasticsearchDSL\BuilderInterface; /** * Unit test for filters aggregation. */ -class FiltersAggregationTest extends \PHPUnit\Framework\TestCase +class FiltersAggregationTest extends TestCase { /** * Test if exception is thrown when not anonymous filter is without name. - * - * @expectedException \LogicException - * @expectedExceptionMessage In not anonymous filters filter name must be set. */ - public function testIfExceptionIsThrown() + public function testIfExceptionIsThrown(): void { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('In not anonymous filters filter name must be set.'); + $mock = $this->createMock(BuilderInterface::class); $aggregation = new FiltersAggregation('test_agg'); $aggregation->addFilter($mock); } @@ -35,12 +36,13 @@ public function testIfExceptionIsThrown() /** * Test GetArray method. */ - public function testFiltersAggregationGetArray() + public function testFiltersAggregationGetArray(): void { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); + $mock = $this->createMock(BuilderInterface::class); $aggregation = new FiltersAggregation('test_agg'); $aggregation->setAnonymous(true); $aggregation->addFilter($mock, 'name'); + $result = $aggregation->getArray(); $this->assertArrayHasKey('filters', $result); } @@ -48,7 +50,7 @@ public function testFiltersAggregationGetArray() /** * Tests getType method. */ - public function testFiltersAggregationGetType() + public function testFiltersAggregationGetType(): void { $aggregation = new FiltersAggregation('foo'); $result = $aggregation->getType(); @@ -58,11 +60,11 @@ public function testFiltersAggregationGetType() /** * Test for filter aggregation toArray() method. */ - public function testToArray() + public function testToArray(): void { $aggregation = new FiltersAggregation('test_agg'); - $filter = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface') - ->setMethods(['toArray', 'getType']) + $filter = $this->getMockBuilder(BuilderInterface::class) + ->onlyMethods(['toArray', 'getType']) ->getMockForAbstractClass(); $filter->expects($this->any()) ->method('toArray') @@ -70,6 +72,7 @@ public function testToArray() $aggregation->addFilter($filter, 'first'); $aggregation->addFilter($filter, 'second'); + $results = $aggregation->toArray(); $expected = [ 'filters' => [ @@ -93,12 +96,12 @@ public function testToArray() /** * Tests if filters can be passed to constructor. */ - public function testConstructorFilter() + public function testConstructorFilter(): void { - /** @var BuilderInterface|\PHPUnit_Framework_MockObject_MockObject $builderInterface1 */ - $builderInterface1 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); - /** @var BuilderInterface|\PHPUnit_Framework_MockObject_MockObject $builderInterface2 */ - $builderInterface2 = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); + /** @var BuilderInterface|MockObject $builderInterface1 */ + $builderInterface1 = $this->getMockForAbstractClass(BuilderInterface::class); + /** @var BuilderInterface|MockObject $builderInterface2 */ + $builderInterface2 = $this->getMockForAbstractClass(BuilderInterface::class); $aggregation = new FiltersAggregation( 'test', @@ -112,8 +115,8 @@ public function testConstructorFilter() [ 'filters' => [ 'filters' => [ - 'filter1' => null, - 'filter2' => null, + 'filter1' => $builderInterface1->toArray(), + 'filter2' => $builderInterface2->toArray(), ], ], ], @@ -133,8 +136,8 @@ public function testConstructorFilter() [ 'filters' => [ 'filters' => [ - null, - null, + [], + [], ], ], ], diff --git a/tests/Unit/Aggregation/Bucketing/GeoDistanceAggregationTest.php b/tests/Unit/Aggregation/Bucketing/GeoDistanceAggregationTest.php index 89f2f5d5..b5935d66 100644 --- a/tests/Unit/Aggregation/Bucketing/GeoDistanceAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/GeoDistanceAggregationTest.php @@ -11,18 +11,19 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\GeoDistanceAggregation; -class GeoDistanceAggregationTest extends \PHPUnit\Framework\TestCase +class GeoDistanceAggregationTest extends TestCase { /** * Test if exception is thrown when field is not set. - * - * @expectedException \LogicException - * @expectedExceptionMessage Geo distance aggregation must have a field set. */ - public function testGeoDistanceAggregationExceptionWhenFieldIsNotSet() + public function testGeoDistanceAggregationExceptionWhenFieldIsNotSet(): void { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Geo distance aggregation must have a field set.'); $agg = new GeoDistanceAggregation('test_agg'); $agg->setOrigin('50, 70'); $agg->getArray(); @@ -30,12 +31,11 @@ public function testGeoDistanceAggregationExceptionWhenFieldIsNotSet() /** * Test if exception is thrown when origin is not set. - * - * @expectedException \LogicException - * @expectedExceptionMessage Geo distance aggregation must have an origin set. */ - public function testGeoDistanceAggregationExceptionWhenOriginIsNotSet() + public function testGeoDistanceAggregationExceptionWhenOriginIsNotSet(): void { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Geo distance aggregation must have an origin set.'); $agg = new GeoDistanceAggregation('test_agg'); $agg->setField('location'); $agg->getArray(); @@ -43,22 +43,19 @@ public function testGeoDistanceAggregationExceptionWhenOriginIsNotSet() /** * Test if exception is thrown when field is not set. - * - * @expectedException \LogicException - * @expectedExceptionMessage Either from or to must be set. Both cannot be null. */ - public function testGeoDistanceAggregationAddRangeException() + public function testGeoDistanceAggregationAddRangeException(): void { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Either from or to must be set. Both cannot be null.'); $agg = new GeoDistanceAggregation('test_agg'); $agg->addRange(); } /** * Data provider for testGeoDistanceAggregationGetArray(). - * - * @return array */ - public function testGeoDistanceAggregationGetArrayDataProvider() + public static function geoDistanceAggregationGetArrayDataProvider(): array { $out = []; $filterData = [ @@ -85,12 +82,10 @@ public function testGeoDistanceAggregationGetArrayDataProvider() /** * Tests getArray method. * - * @param array $filterData * @param array $expected - * - * @dataProvider testGeoDistanceAggregationGetArrayDataProvider */ - public function testGeoDistanceAggregationGetArray($filterData, $expected) + #[DataProvider('geoDistanceAggregationGetArrayDataProvider')] + public function testGeoDistanceAggregationGetArray(array $filterData, mixed $expected): void { $aggregation = new GeoDistanceAggregation('foo'); $aggregation->setOrigin($filterData['origin']); @@ -106,7 +101,7 @@ public function testGeoDistanceAggregationGetArray($filterData, $expected) /** * Tests getType method. */ - public function testGeoDistanceAggregationGetType() + public function testGeoDistanceAggregationGetType(): void { $aggregation = new GeoDistanceAggregation('foo'); $result = $aggregation->getType(); @@ -116,7 +111,7 @@ public function testGeoDistanceAggregationGetType() /** * Tests if parameters can be passed to constructor. */ - public function testConstructorFilter() + public function testConstructorFilter(): void { $aggregation = new GeoDistanceAggregation( 'test', diff --git a/tests/Unit/Aggregation/Bucketing/GeoHashGridAggregationTest.php b/tests/Unit/Aggregation/Bucketing/GeoHashGridAggregationTest.php index 1ef28d34..98356026 100644 --- a/tests/Unit/Aggregation/Bucketing/GeoHashGridAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/GeoHashGridAggregationTest.php @@ -11,30 +11,29 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\GeoHashGridAggregation; /** * Unit test for geohash grid aggregation. */ -class GeoHashGridAggregationTest extends \PHPUnit\Framework\TestCase +class GeoHashGridAggregationTest extends TestCase { /** * Test if exception is thrown. - * - * @expectedException \LogicException */ - public function testGeoHashGridAggregationException() + public function testGeoHashGridAggregationException(): void { + $this->expectException(\LogicException::class); $agg = new GeoHashGridAggregation('test_agg'); $agg->getArray(); } /** * Data provider for testGeoHashGridAggregationGetArray(). - * - * @return array */ - public function getArrayDataProvider() + public static function getArrayDataProvider(): array { $out = []; @@ -60,12 +59,10 @@ public function getArrayDataProvider() /** * Tests getArray method. * - * @param array $filterData * @param array $expected - * - * @dataProvider getArrayDataProvider */ - public function testGeoHashGridAggregationGetArray($filterData, $expected) + #[DataProvider('getArrayDataProvider')] + public function testGeoHashGridAggregationGetArray(array $filterData, mixed $expected): void { $aggregation = new GeoHashGridAggregation('foo'); $aggregation->setPrecision($filterData['precision']); @@ -80,7 +77,7 @@ public function testGeoHashGridAggregationGetArray($filterData, $expected) /** * Tests getType method. */ - public function testGeoHashGridAggregationGetType() + public function testGeoHashGridAggregationGetType(): void { $aggregation = new GeoHashGridAggregation('foo'); $result = $aggregation->getType(); diff --git a/tests/Unit/Aggregation/Bucketing/GlobalAggregationTest.php b/tests/Unit/Aggregation/Bucketing/GlobalAggregationTest.php index 725ed283..107c2ea5 100644 --- a/tests/Unit/Aggregation/Bucketing/GlobalAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/GlobalAggregationTest.php @@ -11,16 +11,16 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\GlobalAggregation; -class GlobalAggregationTest extends \PHPUnit\Framework\TestCase +class GlobalAggregationTest extends TestCase { /** * Data provider for testToArray(). - * - * @return array */ - public function getToArrayData() + public static function getToArrayData(): array { $out = []; @@ -28,7 +28,7 @@ public function getToArrayData() $aggregation = new GlobalAggregation('test_agg'); $result = [ - 'global' => new \stdClass(), + 'global' => [], ]; $out[] = [ @@ -42,7 +42,7 @@ public function getToArrayData() $aggregation->addAggregation($aggregation2); $result = [ - 'global' => new \stdClass(), + 'global' => [], 'aggregations' => [ $aggregation2->getName() => $aggregation2->toArray(), ], @@ -61,24 +61,22 @@ public function getToArrayData() * * @param GlobalAggregation $aggregation * @param array $expectedResult - * - * @dataProvider getToArrayData */ - public function testToArray($aggregation, $expectedResult) + #[DataProvider('getToArrayData')] + public function testToArray($aggregation, $expectedResult): void { $this->assertEquals( - json_encode($expectedResult), - json_encode($aggregation->toArray()) + json_encode($expectedResult, JSON_THROW_ON_ERROR), + json_encode($aggregation->toArray(), JSON_THROW_ON_ERROR) ); } /** * Test for setField method on global aggregation. - * - * @expectedException \LogicException */ - public function testSetField() + public function testSetField(): never { + $this->expectException(\LogicException::class); $aggregation = new GlobalAggregation('test_agg'); $aggregation->setField('test_field'); } diff --git a/tests/Unit/Aggregation/Bucketing/Ipv4RangeAggregationTest.php b/tests/Unit/Aggregation/Bucketing/Ipv4RangeAggregationTest.php index 7566efe8..c4bb7fec 100644 --- a/tests/Unit/Aggregation/Bucketing/Ipv4RangeAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/Ipv4RangeAggregationTest.php @@ -11,17 +11,17 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\Ipv4RangeAggregation; -class Ipv4RangeAggregationTest extends \PHPUnit\Framework\TestCase +class Ipv4RangeAggregationTest extends TestCase { /** * Test exception when field and range are not set. - * - * @expectedException \LogicException */ - public function testIfExceptionIsThrownWhenFieldAndRangeAreNotSet() + public function testIfExceptionIsThrownWhenFieldAndRangeAreNotSet(): void { + $this->expectException(\LogicException::class); $agg = new Ipv4RangeAggregation('foo'); $agg->toArray(); } @@ -29,7 +29,7 @@ public function testIfExceptionIsThrownWhenFieldAndRangeAreNotSet() /** * Tests if field and range can be passed to constructor. */ - public function testConstructorFilter() + public function testConstructorFilter(): void { $aggregation = new Ipv4RangeAggregation('test', 'fieldName', [['from' => 'fromValue']]); $this->assertSame( diff --git a/tests/Unit/Aggregation/Bucketing/MissingAggregationTest.php b/tests/Unit/Aggregation/Bucketing/MissingAggregationTest.php index 613d97ba..1b87dadd 100644 --- a/tests/Unit/Aggregation/Bucketing/MissingAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/MissingAggregationTest.php @@ -11,18 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\MissingAggregation; -class MissingAggregationTest extends \PHPUnit\Framework\TestCase +class MissingAggregationTest extends TestCase { /** * Test if exception is thrown when field is not set. - * - * @expectedException \LogicException - * @expectedExceptionMessage Missing aggregation must have a field set. */ - public function testIfExceptionIsThrown() + public function testIfExceptionIsThrown(): void { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Missing aggregation must have a field set.'); $agg = new MissingAggregation('test_agg'); $agg->getArray(); } @@ -30,10 +30,11 @@ public function testIfExceptionIsThrown() /** * Test getArray method. */ - public function testMissingAggregationGetArray() + public function testMissingAggregationGetArray(): void { $aggregation = new MissingAggregation('foo'); $aggregation->setField('bar'); + $result = $aggregation->getArray(); $this->assertEquals('bar', $result['field']); } @@ -41,7 +42,7 @@ public function testMissingAggregationGetArray() /** * Test getType method. */ - public function testMissingAggregationGetType() + public function testMissingAggregationGetType(): void { $aggregation = new MissingAggregation('bar'); $result = $aggregation->getType(); diff --git a/tests/Unit/Aggregation/Bucketing/NestedAggregationTest.php b/tests/Unit/Aggregation/Bucketing/NestedAggregationTest.php index b4458f12..53abc5dd 100644 --- a/tests/Unit/Aggregation/Bucketing/NestedAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/NestedAggregationTest.php @@ -11,15 +11,16 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\NestedAggregation; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\TermsAggregation; -class NestedAggregationTest extends \PHPUnit\Framework\TestCase +class NestedAggregationTest extends TestCase { /** * Test for nested aggregation toArray() method exception. */ - public function testToArray() + public function testToArray(): void { $termAggregation = new TermsAggregation('acme'); diff --git a/tests/Unit/Aggregation/Bucketing/RangeAggregationTest.php b/tests/Unit/Aggregation/Bucketing/RangeAggregationTest.php index 55134a86..778e67ec 100644 --- a/tests/Unit/Aggregation/Bucketing/RangeAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/RangeAggregationTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\RangeAggregation; -class RangeAggregationTest extends \PHPUnit\Framework\TestCase +class RangeAggregationTest extends TestCase { /** * Test addRange method. */ - public function testRangeAggregationAddRange() + public function testRangeAggregationAddRange(): void { $aggregation = new RangeAggregation('test_agg'); $aggregation->setField('test_field'); @@ -43,7 +44,7 @@ public function testRangeAggregationAddRange() /** * Test addRange method with multiple values. */ - public function testRangeAggregationAddRangeMultiple() + public function testRangeAggregationAddRangeMultiple(): void { $aggregation = new RangeAggregation('test_agg'); $aggregation->setField('test_field'); @@ -74,7 +75,7 @@ public function testRangeAggregationAddRangeMultiple() /** * Test addRange method with nested values. */ - public function testRangeAggregationAddRangeNested() + public function testRangeAggregationAddRangeNested(): void { $aggregation = new RangeAggregation('test_agg'); $aggregation->setField('test_field'); @@ -117,7 +118,7 @@ public function testRangeAggregationAddRangeNested() /** * Tests getType method. */ - public function testRangeAggregationGetType() + public function testRangeAggregationGetType(): void { $agg = new RangeAggregation('foo'); $result = $agg->getType(); @@ -127,7 +128,7 @@ public function testRangeAggregationGetType() /** * Tests removeRangeByKey method. */ - public function testRangeAggregationRemoveRangeByKey() + public function testRangeAggregationRemoveRangeByKey(): void { $aggregation = new RangeAggregation('foo'); $aggregation->setField('price'); @@ -167,7 +168,7 @@ public function testRangeAggregationRemoveRangeByKey() /** * Tests removeRange method. */ - public function testRangeAggregationRemoveRange() + public function testRangeAggregationRemoveRange(): void { $aggregation = new RangeAggregation('foo'); $aggregation->setField('price'); @@ -197,7 +198,7 @@ public function testRangeAggregationRemoveRange() /** * Tests if parameter can be passed to constructor. */ - public function testConstructor() + public function testConstructor(): void { $aggregation = new RangeAggregation('foo', 'fieldValue', [['from' => 'now', 'key' => 'nowkey']], true); $this->assertSame( diff --git a/tests/Unit/Aggregation/Bucketing/ReverseNestedAggregationTest.php b/tests/Unit/Aggregation/Bucketing/ReverseNestedAggregationTest.php index 11ed18f7..62d711d2 100644 --- a/tests/Unit/Aggregation/Bucketing/ReverseNestedAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/ReverseNestedAggregationTest.php @@ -11,15 +11,16 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\ReverseNestedAggregation; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\TermsAggregation; -class ReverseNestedAggregationTest extends \PHPUnit\Framework\TestCase +class ReverseNestedAggregationTest extends TestCase { /** * Test for reverse_nested aggregation toArray() method exception. */ - public function testToArray() + public function testToArray(): void { $termAggregation = new TermsAggregation('acme'); @@ -40,7 +41,7 @@ public function testToArray() /** * Test for reverse_nested aggregation toArray() without path. */ - public function testToArrayNoPath() + public function testToArrayNoPath(): void { $termAggregation = new TermsAggregation('acme'); @@ -48,15 +49,15 @@ public function testToArrayNoPath() $aggregation->addAggregation($termAggregation); $expectedResult = [ - 'reverse_nested' => new \stdClass(), + 'reverse_nested' => [], 'aggregations' => [ $termAggregation->getName() => $termAggregation->toArray(), ], ]; $this->assertEquals( - json_encode($expectedResult), - json_encode($aggregation->toArray()) + json_encode($expectedResult, JSON_THROW_ON_ERROR), + json_encode($aggregation->toArray(), JSON_THROW_ON_ERROR) ); } } diff --git a/tests/Unit/Aggregation/Bucketing/SamplerAggregationTest.php b/tests/Unit/Aggregation/Bucketing/SamplerAggregationTest.php index 0bbb209b..b429e2f8 100644 --- a/tests/Unit/Aggregation/Bucketing/SamplerAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/SamplerAggregationTest.php @@ -11,18 +11,19 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\SamplerAggregation; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\TermsAggregation; /** * Unit test for children aggregation. */ -class SamplerAggregationTest extends \PHPUnit\Framework\TestCase +class SamplerAggregationTest extends TestCase { /** * Tests getType method. */ - public function testGetType() + public function testGetType(): void { $aggregation = new SamplerAggregation('foo'); $result = $aggregation->getType(); @@ -32,7 +33,7 @@ public function testGetType() /** * Tests toArray method. */ - public function testToArray() + public function testToArray(): void { $termAggregation = new TermsAggregation('acme'); @@ -40,6 +41,7 @@ public function testToArray() $aggregation->addAggregation($termAggregation); $aggregation->setField('name'); $aggregation->setShardSize(200); + $result = $aggregation->toArray(); $expected = [ 'sampler' => [ @@ -56,7 +58,7 @@ public function testToArray() /** * Tests getArray method without provided shard size. */ - public function testGetArrayNoShardSize() + public function testGetArrayNoShardSize(): void { $aggregation = new SamplerAggregation('foo', 'bar'); $this->assertEquals(['field' => 'bar'], $aggregation->getArray()); diff --git a/tests/Unit/Aggregation/Bucketing/SignificantTermsAggregationTest.php b/tests/Unit/Aggregation/Bucketing/SignificantTermsAggregationTest.php index c6cf2133..2b9bcd5b 100644 --- a/tests/Unit/Aggregation/Bucketing/SignificantTermsAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/SignificantTermsAggregationTest.php @@ -11,17 +11,19 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\SignificantTermsAggregation; /** * Unit test for children aggregation. */ -class SignificantTermsAggregationTest extends \PHPUnit\Framework\TestCase +class SignificantTermsAggregationTest extends TestCase { /** * Tests getType method. */ - public function testSignificantTermsAggregationGetType() + public function testSignificantTermsAggregationGetType(): void { $aggregation = new SignificantTermsAggregation('foo'); $result = $aggregation->getType(); @@ -31,13 +33,14 @@ public function testSignificantTermsAggregationGetType() /** * Tests getArray method. */ - public function testSignificantTermsAggregationGetArray() + public function testSignificantTermsAggregationGetArray(): void { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation') + $mock = $this->getMockBuilder(AbstractAggregation::class) ->disableOriginalConstructor() ->getMockForAbstractClass(); $aggregation = new SignificantTermsAggregation('foo', 'title'); $aggregation->addAggregation($mock); + $result = $aggregation->getArray(); $expected = ['field' => 'title']; $this->assertEquals($expected, $result); diff --git a/tests/Unit/Aggregation/Bucketing/SignificantTextAggregationTest.php b/tests/Unit/Aggregation/Bucketing/SignificantTextAggregationTest.php index 23b96f3f..8fea54c4 100644 --- a/tests/Unit/Aggregation/Bucketing/SignificantTextAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/SignificantTextAggregationTest.php @@ -11,17 +11,19 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\SignificantTextAggregation; /** * Unit test for children aggregation. */ -class SignificantTextAggregationTest extends \PHPUnit\Framework\TestCase +class SignificantTextAggregationTest extends TestCase { /** * Tests getType method. */ - public function testSignificantTextAggregationGetType() + public function testSignificantTextAggregationGetType(): void { $aggregation = new SignificantTextAggregation('foo'); $result = $aggregation->getType(); @@ -31,13 +33,14 @@ public function testSignificantTextAggregationGetType() /** * Tests getArray method. */ - public function testSignificantTermsAggregationGetArray() + public function testSignificantTermsAggregationGetArray(): void { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation') + $mock = $this->getMockBuilder(AbstractAggregation::class) ->disableOriginalConstructor() ->getMockForAbstractClass(); $aggregation = new SignificantTextAggregation('foo', 'title'); $aggregation->addAggregation($mock); + $result = $aggregation->getArray(); $expected = ['field' => 'title']; $this->assertEquals($expected, $result); diff --git a/tests/Unit/Aggregation/Bucketing/TermsAggregationTest.php b/tests/Unit/Aggregation/Bucketing/TermsAggregationTest.php index 47924419..ecbb009a 100644 --- a/tests/Unit/Aggregation/Bucketing/TermsAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/TermsAggregationTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\TermsAggregation; -class TermsAggregationTest extends \PHPUnit\Framework\TestCase +class TermsAggregationTest extends TestCase { /** * Tests setField method. */ - public function testTermsAggregationSetField() + public function testTermsAggregationSetField(): void { // Case #0 terms aggregation. $aggregation = new TermsAggregation('test_agg'); @@ -34,7 +35,7 @@ public function testTermsAggregationSetField() /** * Tests setSize method. */ - public function testTermsAggregationSetSize() + public function testTermsAggregationSetSize(): void { // Case #1 terms aggregation with size. $aggregation = new TermsAggregation('test_agg'); @@ -69,7 +70,7 @@ public function testTermsAggregationSetSize() /** * Tests minDocumentCount method. */ - public function testTermsAggregationMinDocumentCount() + public function testTermsAggregationMinDocumentCount(): void { // Case #3 terms aggregation with size and min document count. $aggregation = new TermsAggregation('test_agg'); @@ -91,7 +92,7 @@ public function testTermsAggregationMinDocumentCount() /** * Tests include, exclude method. */ - public function testTermsAggregationSimpleIncludeExclude() + public function testTermsAggregationSimpleIncludeExclude(): void { // Case #4 terms aggregation with simple include, exclude. $aggregation = new TermsAggregation('test_agg'); @@ -113,7 +114,7 @@ public function testTermsAggregationSimpleIncludeExclude() /** * Tests include, exclude with flags method. */ - public function testTermsAggregationIncludeExcludeFlags() + public function testTermsAggregationIncludeExcludeFlags(): void { // Case #5 terms aggregation with include, exclude and flags. $aggregation = new TermsAggregation('test_agg'); @@ -153,7 +154,7 @@ public function testTermsAggregationIncludeExcludeFlags() /** * Tests setOrder method. */ - public function testTermsAggregationSetOrder() + public function testTermsAggregationSetOrder(): void { // Case #6 terms aggregation with order default direction. $aggregation = new TermsAggregation('test_agg'); @@ -173,7 +174,7 @@ public function testTermsAggregationSetOrder() /** * Tests setOrder DESC method. */ - public function testTermsAggregationSetOrderDESC() + public function testTermsAggregationSetOrderDESC(): void { // Case #7 terms aggregation with order term mode, desc direction. $aggregation = new TermsAggregation('test_agg'); @@ -193,7 +194,7 @@ public function testTermsAggregationSetOrderDESC() /** * Tests getType method. */ - public function testTermsAggregationGetType() + public function testTermsAggregationGetType(): void { $aggregation = new TermsAggregation('foo'); $result = $aggregation->getType(); diff --git a/tests/Unit/Aggregation/Metric/CardinalityAggregationTest.php b/tests/Unit/Aggregation/Metric/CardinalityAggregationTest.php index 8ec9f2cf..5f16d927 100644 --- a/tests/Unit/Aggregation/Metric/CardinalityAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/CardinalityAggregationTest.php @@ -11,21 +11,23 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Metric\Aggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Metric\CardinalityAggregation; /** * Unit test for cardinality aggregation. */ -class CardinalityAggregationTest extends \PHPUnit\Framework\TestCase +class CardinalityAggregationTest extends TestCase { /** * Tests getArray method. */ - public function testGetArray() + public function testGetArray(): void { $aggregation = new CardinalityAggregation('bar'); $aggregation->setScript('foo'); + $result = $aggregation->getArray(); $this->assertArrayHasKey('script', $result, 'key=script when script is set'); @@ -52,12 +54,11 @@ public function testGetArray() /** * Tests if CardinalityAggregation#getArray throws exception when expected. - * - * @expectedException \LogicException - * @expectedExceptionMessage Cardinality aggregation must have field or script set. */ - public function testGetArrayException() + public function testGetArrayException(): void { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Cardinality aggregation must have field or script set.'); $aggregation = new CardinalityAggregation('bar'); $aggregation->getArray(); } @@ -65,7 +66,7 @@ public function testGetArrayException() /** * Tests getType method. */ - public function testCardinallyAggregationGetType() + public function testCardinallyAggregationGetType(): void { $aggregation = new CardinalityAggregation('foo'); $result = $aggregation->getType(); diff --git a/tests/Unit/Aggregation/Metric/GeoBoundsAggregationTest.php b/tests/Unit/Aggregation/Metric/GeoBoundsAggregationTest.php index efccb9ba..62bfd269 100644 --- a/tests/Unit/Aggregation/Metric/GeoBoundsAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/GeoBoundsAggregationTest.php @@ -11,20 +11,20 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Metric\Aggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Metric\GeoBoundsAggregation; /** * Unit test for geo bounds aggregation. */ -class GeoBoundsAggregationTest extends \PHPUnit\Framework\TestCase +class GeoBoundsAggregationTest extends TestCase { /** * Test if exception is thrown. - * - * @expectedException \LogicException */ - public function testGeoBoundsAggregationException() + public function testGeoBoundsAggregationException(): void { + $this->expectException(\LogicException::class); $agg = new GeoBoundsAggregation('test_agg'); $agg->getArray(); } @@ -32,7 +32,7 @@ public function testGeoBoundsAggregationException() /** * Tests getType method. */ - public function testGeoBoundsAggregationGetType() + public function testGeoBoundsAggregationGetType(): void { $agg = new GeoBoundsAggregation('foo'); $result = $agg->getType(); @@ -42,11 +42,12 @@ public function testGeoBoundsAggregationGetType() /** * Tests getArray method. */ - public function testGeoBoundsAggregationGetArray() + public function testGeoBoundsAggregationGetArray(): void { $agg = new GeoBoundsAggregation('foo'); $agg->setField('bar'); $agg->setWrapLongitude(true); + $result = [ 'geo_bounds' => [ 'field' => 'bar', diff --git a/tests/Unit/Aggregation/Metric/GeoCentroidAggregationTest.php b/tests/Unit/Aggregation/Metric/GeoCentroidAggregationTest.php index dca14573..640feaf7 100644 --- a/tests/Unit/Aggregation/Metric/GeoCentroidAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/GeoCentroidAggregationTest.php @@ -11,20 +11,20 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Metric\Aggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Metric\GeoCentroidAggregation; /** * Unit test for children aggregation. */ -class GeoCentroidAggregationTest extends \PHPUnit\Framework\TestCase +class GeoCentroidAggregationTest extends TestCase { /** * Test if exception is thrown when field is not provided - * - * @expectedException \LogicException */ - public function testGetArrayException() + public function testGetArrayException(): void { + $this->expectException(\LogicException::class); $aggregation = new GeoCentroidAggregation('foo'); $aggregation->getArray(); } @@ -32,7 +32,7 @@ public function testGetArrayException() /** * Tests getType method. */ - public function testGeoCentroidAggregationGetType() + public function testGeoCentroidAggregationGetType(): void { $aggregation = new GeoCentroidAggregation('foo'); $this->assertEquals('geo_centroid', $aggregation->getType()); @@ -41,7 +41,7 @@ public function testGeoCentroidAggregationGetType() /** * Tests getArray method. */ - public function testGeoCentroidAggregationGetArray() + public function testGeoCentroidAggregationGetArray(): void { $aggregation = new GeoCentroidAggregation('foo', 'location'); $this->assertEquals(['field' => 'location'], $aggregation->getArray()); diff --git a/tests/Unit/Aggregation/Metric/PercentileRanksAggregationTest.php b/tests/Unit/Aggregation/Metric/PercentileRanksAggregationTest.php index 12aeffcc..775f2dff 100644 --- a/tests/Unit/Aggregation/Metric/PercentileRanksAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/PercentileRanksAggregationTest.php @@ -11,12 +11,13 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Metric\Aggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Metric\PercentileRanksAggregation; /** * Percentile ranks aggregation unit tests. */ -class PercentileRanksAggregationTest extends \PHPUnit\Framework\TestCase +class PercentileRanksAggregationTest extends TestCase { /** * @var PercentileRanksAggregation @@ -26,39 +27,36 @@ class PercentileRanksAggregationTest extends \PHPUnit\Framework\TestCase /** * Phpunit setup. */ - public function setUp() + protected function setUp(): void { $this->agg = new PercentileRanksAggregation('foo'); } /** * Tests if exception is thrown when required parameters not set. - * - * @expectedException \LogicException */ - public function testIfPercentileRanksAggregationThrowsAnException() + public function testIfPercentileRanksAggregationThrowsAnException(): void { + $this->expectException(\LogicException::class); $this->agg->toArray(); } /** * Tests exception when only field is set. - * - * @expectedException \LogicException */ - public function testIfExceptionIsThrownWhenFieldSetAndValueNotSet() + public function testIfExceptionIsThrownWhenFieldSetAndValueNotSet(): void { + $this->expectException(\LogicException::class); $this->agg->setField('bar'); $this->agg->toArray(); } /** * Tests exception when only value is set. - * - * @expectedException \LogicException */ - public function testIfExceptionIsThrownWhenScriptSetAndValueNotSet() + public function testIfExceptionIsThrownWhenScriptSetAndValueNotSet(): void { + $this->expectException(\LogicException::class); $this->agg->setScript('bar'); $this->agg->toArray(); } @@ -66,7 +64,7 @@ public function testIfExceptionIsThrownWhenScriptSetAndValueNotSet() /** * Test getType method. */ - public function testGetType() + public function testGetType(): void { $this->assertEquals('percentile_ranks', $this->agg->getType()); } @@ -74,7 +72,7 @@ public function testGetType() /** * Test toArray method. */ - public function testToArray() + public function testToArray(): void { $this->agg->setField('bar'); $this->agg->setValues(['bar']); diff --git a/tests/Unit/Aggregation/Metric/PercentilesAggregationTest.php b/tests/Unit/Aggregation/Metric/PercentilesAggregationTest.php index aa98aa3e..5ed758e4 100644 --- a/tests/Unit/Aggregation/Metric/PercentilesAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/PercentilesAggregationTest.php @@ -11,18 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Metric\Aggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Metric\PercentilesAggregation; -class PercentilesAggregationTest extends \PHPUnit\Framework\TestCase +class PercentilesAggregationTest extends TestCase { /** * Tests if PercentilesAggregation#getArray throws exception when expected. - * - * @expectedException \LogicException - * @expectedExceptionMessage Percentiles aggregation must have field or script set. */ - public function testPercentilesAggregationGetArrayException() + public function testPercentilesAggregationGetArrayException(): void { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Percentiles aggregation must have field or script set.'); $aggregation = new PercentilesAggregation('bar'); $aggregation->getArray(); } @@ -30,7 +30,7 @@ public function testPercentilesAggregationGetArrayException() /** * Test getType method. */ - public function testGetType() + public function testGetType(): void { $aggregation = new PercentilesAggregation('bar'); $this->assertEquals('percentiles', $aggregation->getType()); @@ -39,7 +39,7 @@ public function testGetType() /** * Test getArray method. */ - public function testGetArray() + public function testGetArray(): void { $aggregation = new PercentilesAggregation('bar', 'fieldValue', ['percentsValue']); $this->assertSame( diff --git a/tests/Unit/Aggregation/Metric/StatsAggregationTest.php b/tests/Unit/Aggregation/Metric/StatsAggregationTest.php index e130641e..4ae9e661 100644 --- a/tests/Unit/Aggregation/Metric/StatsAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/StatsAggregationTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Metric\Aggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Metric\StatsAggregation; -class StatsAggregationTest extends \PHPUnit\Framework\TestCase +class StatsAggregationTest extends TestCase { /** * Test for stats aggregation toArray() method. */ - public function testToArray() + public function testToArray(): void { $aggregation = new StatsAggregation('test_agg'); $aggregation->setField('test_field'); @@ -33,7 +34,7 @@ public function testToArray() /** * Tests if parameter can be passed to constructor. */ - public function testConstructor() + public function testConstructor(): void { $aggregation = new StatsAggregation('foo', 'fieldValue', 'scriptValue'); $this->assertSame( diff --git a/tests/Unit/Aggregation/Metric/TopHitsAggregationTest.php b/tests/Unit/Aggregation/Metric/TopHitsAggregationTest.php index f6018dc8..271fac54 100644 --- a/tests/Unit/Aggregation/Metric/TopHitsAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/TopHitsAggregationTest.php @@ -11,18 +11,19 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Metric\Aggregation; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Metric\TopHitsAggregation; use ONGR\ElasticsearchDSL\Sort\FieldSort; /** * Unit tests for top hits aggregation. */ -class TopHitsAggregationTest extends \PHPUnit\Framework\TestCase +class TopHitsAggregationTest extends TestCase { /** * Check if aggregation returns the expected array. */ - public function testToArray() + public function testToArray(): void { $sort = new FieldSort('acme', FieldSort::ASC); $aggregation = new TopHitsAggregation('acme', 1, 1, $sort); @@ -43,7 +44,7 @@ public function testToArray() /** * Check if parameters can be set to agg. */ - public function testParametersAddition() + public function testParametersAddition(): void { $aggregation = new TopHitsAggregation('acme', 0, 1); $aggregation->addParameter('_source', ['include' => ['title']]); diff --git a/tests/Unit/Aggregation/Pipeline/AvgBucketAggregationTest.php b/tests/Unit/Aggregation/Pipeline/AvgBucketAggregationTest.php index d4d26af1..7d06cabf 100644 --- a/tests/Unit/Aggregation/Pipeline/AvgBucketAggregationTest.php +++ b/tests/Unit/Aggregation/Pipeline/AvgBucketAggregationTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\AvgBucketAggregation; /** * Unit test for avg_bucket aggregation. */ -class AvgBucketAggregationTest extends \PHPUnit\Framework\TestCase +class AvgBucketAggregationTest extends TestCase { /** * Tests getArray method. */ - public function testGetArray() + public function testGetArray(): void { $aggregation = new AvgBucketAggregation('foo', 'foo>bar'); @@ -31,7 +32,7 @@ public function testGetArray() /** * Tests getType method. */ - public function testAvgBucketAggregationGetType() + public function testAvgBucketAggregationGetType(): void { $aggregation = new AvgBucketAggregation('foo', 'foo>bar'); $this->assertEquals('avg_bucket', $aggregation->getType()); diff --git a/tests/Unit/Aggregation/Pipeline/BucketScriptAggregationTest.php b/tests/Unit/Aggregation/Pipeline/BucketScriptAggregationTest.php index 16c85b62..ac3da94d 100644 --- a/tests/Unit/Aggregation/Pipeline/BucketScriptAggregationTest.php +++ b/tests/Unit/Aggregation/Pipeline/BucketScriptAggregationTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\BucketScriptAggregation; /** * Unit test for bucket script pipeline aggregation. */ -class BucketScriptAggregationTest extends \PHPUnit\Framework\TestCase +class BucketScriptAggregationTest extends TestCase { /** * Tests toArray method. */ - public function testToArray() + public function testToArray(): void { $aggregation = new BucketScriptAggregation( 'test', @@ -50,12 +51,11 @@ public function testToArray() /** * Tests if the exception is thrown in getArray method if no * buckets_path or script is set - * - * @expectedException \LogicException - * @expectedExceptionMessage `test` aggregation must have script set. */ - public function testGetArrayException() + public function testGetArrayException(): void { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('`test` aggregation must have script set.'); $agg = new BucketScriptAggregation('test', []); $agg->getArray(); diff --git a/tests/Unit/Aggregation/Pipeline/BucketSelectorAggregationTest.php b/tests/Unit/Aggregation/Pipeline/BucketSelectorAggregationTest.php index c8edbc9c..07f4fc05 100644 --- a/tests/Unit/Aggregation/Pipeline/BucketSelectorAggregationTest.php +++ b/tests/Unit/Aggregation/Pipeline/BucketSelectorAggregationTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\BucketSelectorAggregation; /** * Unit test for bucket selector pipeline aggregation. */ -class BucketSelectorAggregationTest extends \PHPUnit\Framework\TestCase +class BucketSelectorAggregationTest extends TestCase { /** * Tests toArray method. */ - public function testToArray() + public function testToArray(): void { $aggregation = new BucketSelectorAggregation( 'test', @@ -48,12 +49,11 @@ public function testToArray() /** * Tests if the exception is thrown in getArray method if no * buckets_path or script is set - * - * @expectedException \LogicException - * @expectedExceptionMessage `test` aggregation must have script set. */ - public function testGetArrayException() + public function testGetArrayException(): void { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('`test` aggregation must have script set.'); $agg = new BucketSelectorAggregation('test', []); $agg->getArray(); diff --git a/tests/Unit/Aggregation/Pipeline/BucketSortAggregationTest.php b/tests/Unit/Aggregation/Pipeline/BucketSortAggregationTest.php index 098c7d92..9ab5e6bf 100644 --- a/tests/Unit/Aggregation/Pipeline/BucketSortAggregationTest.php +++ b/tests/Unit/Aggregation/Pipeline/BucketSortAggregationTest.php @@ -11,6 +11,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\BucketSortAggregation; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\MovingFunctionAggregation; use ONGR\ElasticsearchDSL\Sort\FieldSort; @@ -18,12 +19,12 @@ /** * Unit test for the bucket sort aggregation. */ -class BucketSortAggregationTest extends \PHPUnit\Framework\TestCase +class BucketSortAggregationTest extends TestCase { /** * Tests toArray method. */ - public function testToArray() + public function testToArray(): void { $aggregation = new BucketSortAggregation('acme', 'test'); diff --git a/tests/Unit/Aggregation/Pipeline/CumulativeSumAggregationTest.php b/tests/Unit/Aggregation/Pipeline/CumulativeSumAggregationTest.php index 2a904949..019a50ec 100644 --- a/tests/Unit/Aggregation/Pipeline/CumulativeSumAggregationTest.php +++ b/tests/Unit/Aggregation/Pipeline/CumulativeSumAggregationTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\CumulativeSumAggregation; /** * Unit test for cumulative sum aggregation. */ -class CumulativeSumAggregationTest extends \PHPUnit\Framework\TestCase +class CumulativeSumAggregationTest extends TestCase { /** * Tests toArray method. */ - public function testToArray() + public function testToArray(): void { $aggregation = new CumulativeSumAggregation('acme', 'test'); diff --git a/tests/Unit/Aggregation/Pipeline/DerivativeAggregationTest.php b/tests/Unit/Aggregation/Pipeline/DerivativeAggregationTest.php index 2d37b5ac..70ff4ba2 100644 --- a/tests/Unit/Aggregation/Pipeline/DerivativeAggregationTest.php +++ b/tests/Unit/Aggregation/Pipeline/DerivativeAggregationTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\DerivativeAggregation; /** * Unit test for derivative aggregation. */ -class DerivativeAggregationTest extends \PHPUnit\Framework\TestCase +class DerivativeAggregationTest extends TestCase { /** * Tests toArray method. */ - public function testToArray() + public function testToArray(): void { $aggregation = new DerivativeAggregation('foo', 'foo>bar'); $aggregation->addParameter('gap_policy', 'skip'); diff --git a/tests/Unit/Aggregation/Pipeline/ExtendedStatsBucketAggregationTest.php b/tests/Unit/Aggregation/Pipeline/ExtendedStatsBucketAggregationTest.php index 69f3efc1..05f077c5 100644 --- a/tests/Unit/Aggregation/Pipeline/ExtendedStatsBucketAggregationTest.php +++ b/tests/Unit/Aggregation/Pipeline/ExtendedStatsBucketAggregationTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\ExtendedStatsBucketAggregation; /** * Unit test for stats bucket aggregation. */ -class ExtendedStatsBucketAggregationTest extends \PHPUnit\Framework\TestCase +class ExtendedStatsBucketAggregationTest extends TestCase { /** * Tests toArray method. */ - public function testToArray() + public function testToArray(): void { $aggregation = new ExtendedStatsBucketAggregation('acme', 'test'); diff --git a/tests/Unit/Aggregation/Pipeline/MaxBucketAggregationTest.php b/tests/Unit/Aggregation/Pipeline/MaxBucketAggregationTest.php index db8af080..b66bee7c 100644 --- a/tests/Unit/Aggregation/Pipeline/MaxBucketAggregationTest.php +++ b/tests/Unit/Aggregation/Pipeline/MaxBucketAggregationTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\MaxBucketAggregation; /** * Unit test for max bucket aggregation. */ -class MaxBucketAggregationTest extends \PHPUnit\Framework\TestCase +class MaxBucketAggregationTest extends TestCase { /** * Tests toArray method. */ - public function testToArray() + public function testToArray(): void { $aggregation = new MaxBucketAggregation('acme', 'test'); diff --git a/tests/Unit/Aggregation/Pipeline/MinBucketAggregationTest.php b/tests/Unit/Aggregation/Pipeline/MinBucketAggregationTest.php index cee5c0c1..03f3a6f4 100644 --- a/tests/Unit/Aggregation/Pipeline/MinBucketAggregationTest.php +++ b/tests/Unit/Aggregation/Pipeline/MinBucketAggregationTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\MinBucketAggregation; /** * Unit test for min bucket aggregation. */ -class MinBucketAggregationTest extends \PHPUnit\Framework\TestCase +class MinBucketAggregationTest extends TestCase { /** * Tests toArray method. */ - public function testToArray() + public function testToArray(): void { $aggregation = new MinBucketAggregation('acme', 'test'); diff --git a/tests/Unit/Aggregation/Pipeline/MovingFunctionAggregationTest.php b/tests/Unit/Aggregation/Pipeline/MovingFunctionAggregationTest.php index 0f769cf1..4d5a4a4e 100644 --- a/tests/Unit/Aggregation/Pipeline/MovingFunctionAggregationTest.php +++ b/tests/Unit/Aggregation/Pipeline/MovingFunctionAggregationTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\MovingFunctionAggregation; /** * Unit test for sum bucket aggregation. */ -class MovingFunctionAggregationTest extends \PHPUnit\Framework\TestCase +class MovingFunctionAggregationTest extends TestCase { /** * Tests toArray method. */ - public function testToArray() + public function testToArray(): void { $aggregation = new MovingFunctionAggregation('acme', 'test'); diff --git a/tests/Unit/Aggregation/Pipeline/PercentilesBucketAggregationTest.php b/tests/Unit/Aggregation/Pipeline/PercentilesBucketAggregationTest.php index 21ecc1b2..fb8dc4e2 100644 --- a/tests/Unit/Aggregation/Pipeline/PercentilesBucketAggregationTest.php +++ b/tests/Unit/Aggregation/Pipeline/PercentilesBucketAggregationTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\PercentilesBucketAggregation; /** * Unit test for percentiles bucket aggregation. */ -class PercentilesBucketAggregationTest extends \PHPUnit\Framework\TestCase +class PercentilesBucketAggregationTest extends TestCase { /** * Tests toArray method. */ - public function testToArray() + public function testToArray(): void { $aggregation = new PercentilesBucketAggregation('acme', 'test'); $aggregation->setPercents([25.0, 50.0, 75.0]); diff --git a/tests/Unit/Aggregation/Pipeline/SerialDifferencingAggregationTest.php b/tests/Unit/Aggregation/Pipeline/SerialDifferencingAggregationTest.php index 41c0bac8..7f720c46 100644 --- a/tests/Unit/Aggregation/Pipeline/SerialDifferencingAggregationTest.php +++ b/tests/Unit/Aggregation/Pipeline/SerialDifferencingAggregationTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\SerialDifferencingAggregation; /** * Unit test for serial differencing aggregation. */ -class SerialDifferencingAggregationTest extends \PHPUnit\Framework\TestCase +class SerialDifferencingAggregationTest extends TestCase { /** * Tests toArray method. */ - public function testToArray() + public function testToArray(): void { $aggregation = new SerialDifferencingAggregation('acme', 'test'); $aggregation->addParameter('lag', '7'); diff --git a/tests/Unit/Aggregation/Pipeline/StatsBucketAggregationTest.php b/tests/Unit/Aggregation/Pipeline/StatsBucketAggregationTest.php index 3bf5dd45..4c4c0c2d 100644 --- a/tests/Unit/Aggregation/Pipeline/StatsBucketAggregationTest.php +++ b/tests/Unit/Aggregation/Pipeline/StatsBucketAggregationTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\StatsBucketAggregation; /** * Unit test for stats bucket aggregation. */ -class StatsBucketAggregationTest extends \PHPUnit\Framework\TestCase +class StatsBucketAggregationTest extends TestCase { /** * Tests toArray method. */ - public function testToArray() + public function testToArray(): void { $aggregation = new StatsBucketAggregation('acme', 'test'); diff --git a/tests/Unit/Aggregation/Pipeline/SumBucketAggregationTest.php b/tests/Unit/Aggregation/Pipeline/SumBucketAggregationTest.php index 2e117543..a658c5fc 100644 --- a/tests/Unit/Aggregation/Pipeline/SumBucketAggregationTest.php +++ b/tests/Unit/Aggregation/Pipeline/SumBucketAggregationTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\SumBucketAggregation; /** * Unit test for sum bucket aggregation. */ -class SumBucketAggregationTest extends \PHPUnit\Framework\TestCase +class SumBucketAggregationTest extends TestCase { /** * Tests toArray method. */ - public function testToArray() + public function testToArray(): void { $aggregation = new SumBucketAggregation('acme', 'test'); diff --git a/tests/Unit/BuilderBagTest.php b/tests/Unit/BuilderBagTest.php index 49d6a2ff..a8199e9d 100644 --- a/tests/Unit/BuilderBagTest.php +++ b/tests/Unit/BuilderBagTest.php @@ -11,15 +11,17 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\MockObject\MockObject; use ONGR\ElasticsearchDSL\BuilderBag; use ONGR\ElasticsearchDSL\BuilderInterface; -class BuilderBagTest extends \PHPUnit\Framework\TestCase +class BuilderBagTest extends TestCase { /** * Tests if bag knows if he has a builder. */ - public function testHas() + public function testHas(): void { $bag = new BuilderBag(); $fooBuilder = $this->getBuilder('foo'); @@ -30,7 +32,7 @@ public function testHas() /** * Tests if bag can remove a builder. */ - public function testRemove() + public function testRemove(): void { $bag = new BuilderBag(); $fooBuilder = $this->getBuilder('foo'); @@ -48,7 +50,7 @@ public function testRemove() /** * Tests if bag can clear it's builders. */ - public function testClear() + public function testClear(): void { $bag = new BuilderBag( [ @@ -65,7 +67,7 @@ public function testClear() /** * Tests if bag can get a builder. */ - public function testGet() + public function testGet(): void { $bag = new BuilderBag(); $bazBuilder = $this->getBuilder('baz'); @@ -79,12 +81,13 @@ public function testGet() * * @param string $name * - * @return \PHPUnit_Framework_MockObject_MockObject|BuilderInterface + * @return MockObject|BuilderInterface */ - private function getBuilder($name) + private function getBuilder(mixed $name) { - $friendlyBuilderMock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface') - ->setMethods(['getName', 'toArray', 'getType']) + $friendlyBuilderMock = $this->getMockBuilder(BuilderInterface::class) + ->addMethods(['getName']) + ->onlyMethods(['toArray', 'getType']) ->disableOriginalConstructor() ->getMock(); diff --git a/tests/Unit/Highlight/HighlightTest.php b/tests/Unit/Highlight/HighlightTest.php index bc5ddaa1..c4761d8a 100644 --- a/tests/Unit/Highlight/HighlightTest.php +++ b/tests/Unit/Highlight/HighlightTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Highlight; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Highlight\Highlight; -class HighlightTest extends \PHPUnit\Framework\TestCase +class HighlightTest extends TestCase { /** * Tests GetType method, it should return 'highlight'. */ - public function testGetType() + public function testGetType(): void { $highlight = new Highlight(); $result = $highlight->getType(); @@ -28,10 +29,11 @@ public function testGetType() /** * Tests ParametersTrait hasParameter method. */ - public function testTraitHasParameter() + public function testTraitHasParameter(): void { $highlight = new Highlight(); $highlight->addParameter('_source', ['include' => ['title']]); + $result = $highlight->hasParameter('_source'); $this->assertTrue($result); } @@ -39,11 +41,12 @@ public function testTraitHasParameter() /** * Tests ParametersTrait removeParameter method. */ - public function testTraitRemoveParameter() + public function testTraitRemoveParameter(): void { $highlight = new Highlight(); $highlight->addParameter('_source', ['include' => ['title']]); $highlight->removeParameter('_source'); + $result = $highlight->hasParameter('_source'); $this->assertFalse($result); } @@ -51,10 +54,11 @@ public function testTraitRemoveParameter() /** * Tests ParametersTrait getParameter method. */ - public function testTraitGetParameter() + public function testTraitGetParameter(): void { $highlight = new Highlight(); $highlight->addParameter('_source', ['include' => 'title']); + $expectedResult = ['include' => 'title']; $this->assertEquals($expectedResult, $highlight->getParameter('_source')); } @@ -62,7 +66,7 @@ public function testTraitGetParameter() /** * Tests ParametersTrait getParameters and setParameters methods. */ - public function testTraitSetGetParameters() + public function testTraitSetGetParameters(): void { $highlight = new Highlight(); $highlight->setParameters( @@ -85,16 +89,17 @@ public function testTraitSetGetParameters() /** * Test toArray method. */ - public function testToArray() + public function testToArray(): void { $highlight = new Highlight(); $highlight->addField('ok'); $highlight->addParameter('_source', ['include' => ['title']]); $highlight->setTags([''], ['']); + $result = $highlight->toArray(); $expectedResult = [ 'fields' => [ - 'ok' => new \StdClass, + 'ok' => [], ], '_source' => [ 'include' => [ diff --git a/tests/Unit/InnerHit/NestedInnerHitTest.php b/tests/Unit/InnerHit/NestedInnerHitTest.php index 0e1ee2cb..cbddec82 100644 --- a/tests/Unit/InnerHit/NestedInnerHitTest.php +++ b/tests/Unit/InnerHit/NestedInnerHitTest.php @@ -2,19 +2,19 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\InnerHit; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; use ONGR\ElasticsearchDSL\InnerHit\NestedInnerHit; use ONGR\ElasticsearchDSL\Query\FullText\MatchQuery; use ONGR\ElasticsearchDSL\Query\Joining\NestedQuery; use ONGR\ElasticsearchDSL\Search; -class NestedInnerHitTest extends \PHPUnit\Framework\TestCase +class NestedInnerHitTest extends TestCase { /** * Data provider for testToArray(). - * - * @return array */ - public function getTestToArrayData() + public static function getTestToArrayData(): array { $out = []; @@ -38,7 +38,7 @@ public function getTestToArrayData() $emptyInnerHit, [ 'path' => [ - 'foo' => new \stdClass(), + 'foo' => [], ], ], ]; @@ -90,10 +90,9 @@ public function getTestToArrayData() * * @param NestedInnerHit $innerHit * @param array $expected - * - * @dataProvider getTestToArrayData */ - public function testToArray($innerHit, $expected) + #[DataProvider('getTestToArrayData')] + public function testToArray($innerHit, mixed $expected): void { $this->assertEquals($expected, $innerHit->toArray()); } @@ -102,7 +101,7 @@ public function testToArray($innerHit, $expected) /** * Tests getters and setters for $name, $path and $query */ - public function testGettersAndSetters() + public function testGettersAndSetters(): void { $query = new MatchQuery('acme', 'test'); $search = new Search(); diff --git a/tests/Unit/InnerHit/ParentInnerHitTest.php b/tests/Unit/InnerHit/ParentInnerHitTest.php index 7794b9b4..bc1783ca 100644 --- a/tests/Unit/InnerHit/ParentInnerHitTest.php +++ b/tests/Unit/InnerHit/ParentInnerHitTest.php @@ -2,13 +2,14 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\InnerHit; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\InnerHit\ParentInnerHit; use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; use ONGR\ElasticsearchDSL\Search; -class ParentInnerHitTest extends \PHPUnit\Framework\TestCase +class ParentInnerHitTest extends TestCase { - public function testToArray() + public function testToArray(): void { $query = new TermQuery('foo', 'bar'); $search = new Search(); diff --git a/tests/Unit/ParametersTraitTest.php b/tests/Unit/ParametersTraitTest.php index d456e288..d4035dc9 100644 --- a/tests/Unit/ParametersTraitTest.php +++ b/tests/Unit/ParametersTraitTest.php @@ -11,30 +11,32 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\ParametersTrait; /** * Test for ParametersTrait. */ -class ParametersTraitTest extends \PHPUnit\Framework\TestCase +class ParametersTraitTest extends TestCase { /** * @var ParametersTrait */ - private $parametersTraitMock; + private MockObject $parametersTraitMock; /** * {@inheritdoc} */ - public function setUp() + protected function setUp(): void { - $this->parametersTraitMock = $this->getMockForTrait('ONGR\ElasticsearchDSL\ParametersTrait'); + $this->parametersTraitMock = $this->getMockForTrait(ParametersTrait::class); } /** * Tests addParameter method. */ - public function testGetAndAddParameter() + public function testGetAndAddParameter(): void { $this->parametersTraitMock->addParameter('acme', 123); $this->assertEquals(123, $this->parametersTraitMock->getParameter('acme')); diff --git a/tests/Unit/Query/Compound/BoolQueryTest.php b/tests/Unit/Query/Compound/BoolQueryTest.php index 93fe2db7..708f0169 100644 --- a/tests/Unit/Query/Compound/BoolQueryTest.php +++ b/tests/Unit/Query/Compound/BoolQueryTest.php @@ -11,6 +11,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Compound; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery; use ONGR\ElasticsearchDSL\Query\MatchAllQuery; use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; @@ -18,16 +19,15 @@ /** * Unit test for Bool. */ -class BoolQueryTest extends \PHPUnit\Framework\TestCase +class BoolQueryTest extends TestCase { /** * Test for addToBool() without setting a correct bool operator. - * - * @expectedException \UnexpectedValueException - * @expectedExceptionMessage The bool operator acme is not supported */ - public function testBoolAddToBoolException() + public function testBoolAddToBoolException(): void { + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionMessage('The bool operator acme is not supported'); $bool = new BoolQuery(); $bool->add(new MatchAllQuery(), 'acme'); } @@ -35,7 +35,7 @@ public function testBoolAddToBoolException() /** * Tests constructor builds container */ - public function testBoolConstructor() + public function testBoolConstructor(): void { $bool = new BoolQuery([ BoolQuery::SHOULD => [new TermQuery('key1', 'value1')], @@ -81,12 +81,11 @@ public function testBoolConstructor() /** * Tests exception thrown if invalid BoolQuery type key is specified - * - * @expectedException \UnexpectedValueException - * @expectedExceptionMessage The bool operator acme is not supported */ - public function testBoolConstructorException() + public function testBoolConstructorException(): void { + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionMessage('The bool operator acme is not supported'); new BoolQuery([ 'acme' => [new TermQuery('key1', 'value1')], ]); @@ -95,12 +94,13 @@ public function testBoolConstructorException() /** * Tests toArray() method. */ - public function testBoolToArray() + public function testBoolToArray(): void { $bool = new BoolQuery(); $bool->add(new TermQuery('key1', 'value1'), BoolQuery::SHOULD); $bool->add(new TermQuery('key2', 'value2'), BoolQuery::MUST); $bool->add(new TermQuery('key3', 'value3'), BoolQuery::MUST_NOT); + $expected = [ 'bool' => [ 'should' => [ @@ -132,21 +132,22 @@ public function testBoolToArray() /** * Tests bool query with empty body if it forms \stdObject */ - public function testEmptyBoolQuery() + public function testEmptyBoolQuery(): void { $bool = new BoolQuery(); - $this->assertEquals(['bool' => new \stdClass()], $bool->toArray()); + $this->assertEquals(['bool' => []], $bool->toArray()); } /** * Tests bool query in filter context. */ - public function testBoolInFilterContext() + public function testBoolInFilterContext(): void { $bool = new BoolQuery(); $bool->add(new TermQuery('key1', 'value1'), BoolQuery::FILTER); $bool->add(new TermQuery('key2', 'value2'), BoolQuery::MUST); + $expected = [ 'bool' => [ 'filter' => [ @@ -171,10 +172,11 @@ public function testBoolInFilterContext() /** * Test if simplified structure is returned in case single MUST query given. */ - public function testSingleMust() + public function testSingleMust(): void { $bool = new BoolQuery(); $bool->add(new TermQuery('key2', 'value2'), BoolQuery::MUST); + $expected = [ 'term' => [ 'key2' => 'value2', @@ -186,17 +188,17 @@ public function testSingleMust() /** * Tests if BoolQuery::getQueries returns an empty array. */ - public function testGetQueriesEmpty() + public function testGetQueriesEmpty(): void { $bool = new BoolQuery(); - $this->assertInternalType('array', $bool->getQueries()); + $this->assertIsArray($bool->getQueries()); } /** * Tests if BoolQuery::getQueries returns an array with the added queries of all bool types. */ - public function testGetQueries() + public function testGetQueries(): void { $query = new TermQuery('key1', 'value1'); $query2 = new TermQuery('key2', 'value2'); @@ -205,23 +207,23 @@ public function testGetQueries() $bool->add($query, BoolQuery::MUST, 'query'); $bool->add($query2, BoolQuery::SHOULD, 'query2'); - $this->assertSame(array('query' => $query, 'query2' => $query2), $bool->getQueries()); + $this->assertSame(['query' => $query, 'query2' => $query2], $bool->getQueries()); } /** * Tests if BoolQuery::getQueries with specified bool type returns an empty array. */ - public function testGetQueriesByBoolTypeEmpty() + public function testGetQueriesByBoolTypeEmpty(): void { $bool = new BoolQuery(); - $this->assertInternalType('array', $bool->getQueries(BoolQuery::MUST)); + $this->assertIsArray($bool->getQueries(BoolQuery::MUST)); } /** * Tests if BoolQuery::getQueries with specified bool type returns an array with added queries. */ - public function testGetQueriesByBoolTypeWithQueryAddedToBoolType() + public function testGetQueriesByBoolTypeWithQueryAddedToBoolType(): void { $query = new TermQuery('key1', 'value1'); $query2 = new TermQuery('key2', 'value2'); @@ -230,6 +232,6 @@ public function testGetQueriesByBoolTypeWithQueryAddedToBoolType() $bool->add($query, BoolQuery::MUST, 'query'); $bool->add($query2, BoolQuery::SHOULD, 'query2'); - $this->assertSame(array('query' => $query), $bool->getQueries(BoolQuery::MUST)); + $this->assertSame(['query' => $query], $bool->getQueries(BoolQuery::MUST)); } } diff --git a/tests/Unit/Query/Compound/BoostingQueryTest.php b/tests/Unit/Query/Compound/BoostingQueryTest.php index 7fa687b6..823c83bc 100644 --- a/tests/Unit/Query/Compound/BoostingQueryTest.php +++ b/tests/Unit/Query/Compound/BoostingQueryTest.php @@ -11,16 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Compound; +use ONGR\ElasticsearchDSL\BuilderInterface; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Compound\BoostingQuery; -class BoostingQueryTest extends \PHPUnit\Framework\TestCase +class BoostingQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); + $mock = $this->createMock(BuilderInterface::class); $mock ->expects($this->any()) ->method('toArray') diff --git a/tests/Unit/Query/Compound/ConstantScoreQueryTest.php b/tests/Unit/Query/Compound/ConstantScoreQueryTest.php index c1a11dcc..6e7e96ee 100644 --- a/tests/Unit/Query/Compound/ConstantScoreQueryTest.php +++ b/tests/Unit/Query/Compound/ConstantScoreQueryTest.php @@ -11,16 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Compound; +use ONGR\ElasticsearchDSL\BuilderInterface; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Compound\ConstantScoreQuery; -class ConstantScoreQueryTest extends \PHPUnit\Framework\TestCase +class ConstantScoreQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); + $mock = $this->createMock(BuilderInterface::class); $mock ->expects($this->any()) ->method('toArray') diff --git a/tests/Unit/Query/Compound/DisMaxQueryTest.php b/tests/Unit/Query/Compound/DisMaxQueryTest.php index cbdda1fe..7e366f3e 100644 --- a/tests/Unit/Query/Compound/DisMaxQueryTest.php +++ b/tests/Unit/Query/Compound/DisMaxQueryTest.php @@ -11,16 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Compound; +use ONGR\ElasticsearchDSL\BuilderInterface; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Compound\DisMaxQuery; -class DisMaxQueryTest extends \PHPUnit\Framework\TestCase +class DisMaxQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); + $mock = $this->createMock(BuilderInterface::class); $mock ->expects($this->any()) ->method('toArray') @@ -29,6 +31,7 @@ public function testToArray() $query = new DisMaxQuery(['boost' => 1.2]); $query->addQuery($mock); $query->addQuery($mock); + $expected = [ 'dis_max' => [ 'queries' => [ diff --git a/tests/Unit/Query/Compound/FunctionScoreQueryTest.php b/tests/Unit/Query/Compound/FunctionScoreQueryTest.php index 9997b0b7..8270a411 100644 --- a/tests/Unit/Query/Compound/FunctionScoreQueryTest.php +++ b/tests/Unit/Query/Compound/FunctionScoreQueryTest.php @@ -11,32 +11,32 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Compound; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\Query\Compound\FunctionScoreQuery; use ONGR\ElasticsearchDSL\Query\MatchAllQuery; -use PHPUnit_Framework_MockObject_MockObject as MockObject; /** * Tests for FunctionScoreQuery. */ -class FunctionScoreQueryTest extends \PHPUnit\Framework\TestCase +class FunctionScoreQueryTest extends TestCase { /** * Data provider for testAddRandomFunction. - * - * @return array */ - public function addRandomFunctionProvider() + public static function addRandomFunctionProvider(): array { return [ // Case #0. No seed. [ 'seed' => null, 'expectedArray' => [ - 'query' => null, + 'query' => [], 'functions' => [ [ - 'random_score' => new \stdClass(), + 'random_score' => [], ], ], ], @@ -45,7 +45,7 @@ public function addRandomFunctionProvider() [ 'seed' => 'someSeed', 'expectedArray' => [ - 'query' => null, + 'query' => [], 'functions' => [ [ 'random_score' => [ 'seed' => 'someSeed'], @@ -59,15 +59,13 @@ public function addRandomFunctionProvider() /** * Tests addRandomFunction method. * - * @param mixed $seed * @param array $expectedArray - * - * @dataProvider addRandomFunctionProvider */ - public function testAddRandomFunction($seed, $expectedArray) + #[DataProvider('addRandomFunctionProvider')] + public function testAddRandomFunction(mixed $seed, $expectedArray): void { /** @var MatchAllQuery|MockObject $matchAllQuery */ - $matchAllQuery = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\MatchAllQuery')->getMock(); + $matchAllQuery = $this->createMock(MatchAllQuery::class); $functionScoreQuery = new FunctionScoreQuery($matchAllQuery); $functionScoreQuery->addRandomFunction($seed); @@ -78,17 +76,17 @@ public function testAddRandomFunction($seed, $expectedArray) /** * Tests default argument values. */ - public function testAddFieldValueFactorFunction() + public function testAddFieldValueFactorFunction(): void { /** @var BuilderInterface|MockObject $builderInterface */ - $builderInterface = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); + $builderInterface = $this->getMockForAbstractClass(BuilderInterface::class); $functionScoreQuery = new FunctionScoreQuery($builderInterface); $functionScoreQuery->addFieldValueFactorFunction('field1', 2); $functionScoreQuery->addFieldValueFactorFunction('field2', 1.5, 'ln'); $this->assertEquals( [ - 'query' => null, + 'query' => [], 'functions' => [ [ 'field_value_factor' => [ diff --git a/tests/Unit/Query/FullText/CommonTermsQueryTest.php b/tests/Unit/Query/FullText/CommonTermsQueryTest.php index 8a387132..05fd5dc4 100644 --- a/tests/Unit/Query/FullText/CommonTermsQueryTest.php +++ b/tests/Unit/Query/FullText/CommonTermsQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\FullText; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\FullText\CommonTermsQuery; -class CommonTermsQueryTest extends \PHPUnit\Framework\TestCase +class CommonTermsQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new CommonTermsQuery('body', 'this is bonsai cool', ['cutoff_frequency' => 0.01]); $expected = [ diff --git a/tests/Unit/Query/FullText/MatchPhrasePrefixQueryTest.php b/tests/Unit/Query/FullText/MatchPhrasePrefixQueryTest.php index 9a0d651e..f01774c5 100644 --- a/tests/Unit/Query/FullText/MatchPhrasePrefixQueryTest.php +++ b/tests/Unit/Query/FullText/MatchPhrasePrefixQueryTest.php @@ -14,12 +14,12 @@ use ONGR\ElasticsearchDSL\Query\FullText\MatchPhrasePrefixQuery; use PHPUnit\Framework\TestCase; -class MatchPhrasePrefixQueryTest extends \PHPUnit\Framework\TestCase +class MatchPhrasePrefixQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new MatchPhrasePrefixQuery('message', 'this is a test'); $expected = [ diff --git a/tests/Unit/Query/FullText/MatchPhraseQueryTest.php b/tests/Unit/Query/FullText/MatchPhraseQueryTest.php index 0bd142c0..4790cbb4 100644 --- a/tests/Unit/Query/FullText/MatchPhraseQueryTest.php +++ b/tests/Unit/Query/FullText/MatchPhraseQueryTest.php @@ -14,12 +14,12 @@ use ONGR\ElasticsearchDSL\Query\FullText\MatchPhraseQuery; use PHPUnit\Framework\TestCase; -class MatchPhraseQueryTest extends \PHPUnit\Framework\TestCase +class MatchPhraseQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new MatchPhraseQuery('message', 'this is a test'); $expected = [ diff --git a/tests/Unit/Query/FullText/MatchQueryTest.php b/tests/Unit/Query/FullText/MatchQueryTest.php index e1b664f2..048d7dd2 100644 --- a/tests/Unit/Query/FullText/MatchQueryTest.php +++ b/tests/Unit/Query/FullText/MatchQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\FullText; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\FullText\MatchQuery; -class MatchQueryTest extends \PHPUnit\Framework\TestCase +class MatchQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new MatchQuery('message', 'this is a test'); $expected = [ diff --git a/tests/Unit/Query/FullText/MultiMatchQueryTest.php b/tests/Unit/Query/FullText/MultiMatchQueryTest.php index 833bfb43..ee26796e 100644 --- a/tests/Unit/Query/FullText/MultiMatchQueryTest.php +++ b/tests/Unit/Query/FullText/MultiMatchQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\FullText; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\FullText\MultiMatchQuery; -class MultiMatchQueryTest extends \PHPUnit\Framework\TestCase +class MultiMatchQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new MultiMatchQuery(['message', 'title'], 'this is a test'); $expected = [ @@ -34,7 +35,7 @@ public function testToArray() /** * Tests multi-match query with no fields. */ - public function testToArrayWithNoFields() + public function testToArrayWithNoFields(): void { $query = new MultiMatchQuery([], 'this is a test'); $expected = [ diff --git a/tests/Unit/Query/FullText/QueryStringQueryTest.php b/tests/Unit/Query/FullText/QueryStringQueryTest.php index 7d8337d4..2a7b2180 100644 --- a/tests/Unit/Query/FullText/QueryStringQueryTest.php +++ b/tests/Unit/Query/FullText/QueryStringQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\FullText; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\FullText\QueryStringQuery; -class QueryStringQueryTest extends \PHPUnit\Framework\TestCase +class QueryStringQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new QueryStringQuery('this AND that OR thus'); $expected = [ diff --git a/tests/Unit/Query/FullText/SimpleQueryStringQueryTest.php b/tests/Unit/Query/FullText/SimpleQueryStringQueryTest.php index fd6d10c4..bcaffe9d 100644 --- a/tests/Unit/Query/FullText/SimpleQueryStringQueryTest.php +++ b/tests/Unit/Query/FullText/SimpleQueryStringQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\FullText; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\FullText\SimpleQueryStringQuery; -class SimpleQueryStringQueryTest extends \PHPUnit\Framework\TestCase +class SimpleQueryStringQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new SimpleQueryStringQuery('"fried eggs" +(eggplant | potato) -frittata'); $expected = [ diff --git a/tests/Unit/Query/Geo/GeoBoundingBoxQueryTest.php b/tests/Unit/Query/Geo/GeoBoundingBoxQueryTest.php index bc9c01b1..11b1ff92 100644 --- a/tests/Unit/Query/Geo/GeoBoundingBoxQueryTest.php +++ b/tests/Unit/Query/Geo/GeoBoundingBoxQueryTest.php @@ -11,27 +11,26 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Geo; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; use ONGR\ElasticsearchDSL\Query\Geo\GeoBoundingBoxQuery; -class GeoBoundingBoxQueryTest extends \PHPUnit\Framework\TestCase +class GeoBoundingBoxQueryTest extends TestCase { /** * Test if exception is thrown when geo points are not set. - * - * @expectedException \LogicException */ - public function testGeoBoundBoxQueryException() + public function testGeoBoundBoxQueryException(): void { + $this->expectException(\LogicException::class); $query = new GeoBoundingBoxQuery('location', []); $query->toArray(); } /** * Data provider for testToArray(). - * - * @return array */ - public function getArrayDataProvider() + public static function getArrayDataProvider(): array { return [ // Case #1 (2 values). @@ -112,10 +111,9 @@ public function getArrayDataProvider() * @param array $values Bounding box values. * @param array $parameters Optional parameters. * @param array $expected Expected result. - * - * @dataProvider getArrayDataProvider */ - public function testToArray($field, $values, $parameters, $expected) + #[DataProvider('getArrayDataProvider')] + public function testToArray($field, $values, $parameters, $expected): void { $query = new GeoBoundingBoxQuery($field, $values, $parameters); $result = $query->toArray(); diff --git a/tests/Unit/Query/Geo/GeoDistanceQueryTest.php b/tests/Unit/Query/Geo/GeoDistanceQueryTest.php index b7f416da..71d3e560 100644 --- a/tests/Unit/Query/Geo/GeoDistanceQueryTest.php +++ b/tests/Unit/Query/Geo/GeoDistanceQueryTest.php @@ -11,16 +11,16 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Geo; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; use ONGR\ElasticsearchDSL\Query\Geo\GeoDistanceQuery; -class GeoDistanceQueryTest extends \PHPUnit\Framework\TestCase +class GeoDistanceQueryTest extends TestCase { /** * Data provider for testToArray(). - * - * @return array */ - public function getArrayDataProvider() + public static function getArrayDataProvider(): array { return [ // Case #1. @@ -50,10 +50,9 @@ public function getArrayDataProvider() * @param array $location Location. * @param array $parameters Optional parameters. * @param array $expected Expected result. - * - * @dataProvider getArrayDataProvider */ - public function testToArray($field, $distance, $location, $parameters, $expected) + #[DataProvider('getArrayDataProvider')] + public function testToArray($field, $distance, $location, $parameters, $expected): void { $query = new GeoDistanceQuery($field, $distance, $location, $parameters); $result = $query->toArray(); diff --git a/tests/Unit/Query/Geo/GeoPolygonQueryTest.php b/tests/Unit/Query/Geo/GeoPolygonQueryTest.php index 1edcaf97..b15fa778 100644 --- a/tests/Unit/Query/Geo/GeoPolygonQueryTest.php +++ b/tests/Unit/Query/Geo/GeoPolygonQueryTest.php @@ -11,16 +11,16 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Geo; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; use ONGR\ElasticsearchDSL\Query\Geo\GeoPolygonQuery; -class GeoPolygonQueryTest extends \PHPUnit\Framework\TestCase +class GeoPolygonQueryTest extends TestCase { /** * Data provider to testToArray. - * - * @return array */ - public function getArrayDataProvider() + public static function getArrayDataProvider(): array { return [ // Case #1. @@ -76,10 +76,9 @@ public function getArrayDataProvider() * @param array $points Polygon's points. * @param array $parameters Optional parameters. * @param array $expected Expected result. - * - * @dataProvider getArrayDataProvider */ - public function testToArray($field, $points, $parameters, $expected) + #[DataProvider('getArrayDataProvider')] + public function testToArray($field, $points, $parameters, $expected): void { $filter = new GeoPolygonQuery($field, $points, $parameters); $result = $filter->toArray(); diff --git a/tests/Unit/Query/Geo/GeoShapeQueryTest.php b/tests/Unit/Query/Geo/GeoShapeQueryTest.php index 1779129d..94289cbe 100644 --- a/tests/Unit/Query/Geo/GeoShapeQueryTest.php +++ b/tests/Unit/Query/Geo/GeoShapeQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Geo; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Geo\GeoShapeQuery; -class GeoShapeQueryTest extends \PHPUnit\Framework\TestCase +class GeoShapeQueryTest extends TestCase { /** * Tests toArray() method. */ - public function testToArray() + public function testToArray(): void { $filter = new GeoShapeQuery(['param1' => 'value1']); $filter->addShape('location', 'envelope', [[13, 53], [14, 52]], GeoShapeQuery::INTERSECTS); @@ -42,7 +43,7 @@ public function testToArray() /** * Test for toArray() in case of pre-indexed shape. */ - public function testToArrayIndexed() + public function testToArrayIndexed(): void { $filter = new GeoShapeQuery(['param1' => 'value1']); $filter->addPreIndexedShape('location', 'DEU', 'countries', 'shapes', 'location', GeoShapeQuery::WITHIN); diff --git a/tests/Unit/Query/Joining/HasChildQueryTest.php b/tests/Unit/Query/Joining/HasChildQueryTest.php index 147ca674..e5e6e171 100644 --- a/tests/Unit/Query/Joining/HasChildQueryTest.php +++ b/tests/Unit/Query/Joining/HasChildQueryTest.php @@ -11,16 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Joining; +use ONGR\ElasticsearchDSL\BuilderInterface; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Joining\HasChildQuery; -class HasChildQueryTest extends \PHPUnit\Framework\TestCase +class HasChildQueryTest extends TestCase { /** * Tests whether __constructor calls setParameters method. */ - public function testConstructor() + public function testConstructor(): void { - $parentQuery = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); + $parentQuery = $this->createMock(BuilderInterface::class); $query = new HasChildQuery('test_type', $parentQuery, ['test_parameter1']); $this->assertEquals(['test_parameter1'], $query->getParameters()); } diff --git a/tests/Unit/Query/Joining/HasParentQueryTest.php b/tests/Unit/Query/Joining/HasParentQueryTest.php index 7601815e..c1402691 100644 --- a/tests/Unit/Query/Joining/HasParentQueryTest.php +++ b/tests/Unit/Query/Joining/HasParentQueryTest.php @@ -11,16 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Joining; +use ONGR\ElasticsearchDSL\BuilderInterface; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Joining\HasParentQuery; -class HasParentQueryTest extends \PHPUnit\Framework\TestCase +class HasParentQueryTest extends TestCase { /** * Tests whether __constructor calls setParameters method. */ - public function testConstructor() + public function testConstructor(): void { - $parentQuery = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); + $parentQuery = $this->createMock(BuilderInterface::class); $query = new HasParentQuery('test_type', $parentQuery, ['test_parameter1']); $this->assertEquals(['test_parameter1'], $query->getParameters()); } diff --git a/tests/Unit/Query/Joining/NestedQueryTest.php b/tests/Unit/Query/Joining/NestedQueryTest.php index 0291e726..5c161da8 100644 --- a/tests/Unit/Query/Joining/NestedQueryTest.php +++ b/tests/Unit/Query/Joining/NestedQueryTest.php @@ -11,17 +11,17 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Joining; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; use ONGR\ElasticsearchDSL\Query\Joining\NestedQuery; use ONGR\ElasticsearchDSL\Query\TermLevel\TermsQuery; -class NestedQueryTest extends \PHPUnit\Framework\TestCase +class NestedQueryTest extends TestCase { /** * Data provider to testGetToArray. - * - * @return array */ - public function getArrayDataProvider() + public static function getArrayDataProvider(): array { $query = [ 'terms' => [ @@ -54,13 +54,13 @@ public function getArrayDataProvider() * @param string $path * @param array $parameters * @param array $expected - * - * @dataProvider getArrayDataProvider */ - public function testToArray($path, $parameters, $expected) + #[DataProvider('getArrayDataProvider')] + public function testToArray($path, $parameters, $expected): void { $query = new TermsQuery('foo', 'bar'); $query = new NestedQuery($path, $query, $parameters); + $result = $query->toArray(); $this->assertEquals(['nested' => $expected], $result); } diff --git a/tests/Unit/Query/MatchAllQueryTest.php b/tests/Unit/Query/MatchAllQueryTest.php index bab4cc08..a1e040e5 100644 --- a/tests/Unit/Query/MatchAllQueryTest.php +++ b/tests/Unit/Query/MatchAllQueryTest.php @@ -11,23 +11,24 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\MatchAllQuery; -class MatchAllQueryTest extends \PHPUnit\Framework\TestCase +class MatchAllQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArrayWhenThereAreNoParams() + public function testToArrayWhenThereAreNoParams(): void { $query = new MatchAllQuery(); - $this->assertEquals(['match_all' => new \stdClass()], $query->toArray()); + $this->assertEquals(['match_all' => []], $query->toArray()); } /** * Tests toArray(). */ - public function testToArrayWithParams() + public function testToArrayWithParams(): void { $params = ['boost' => 5]; $query = new MatchAllQuery($params); diff --git a/tests/Unit/Query/Span/FieldMaskingSpanQueryTest.php b/tests/Unit/Query/Span/FieldMaskingSpanQueryTest.php index 48ef28a5..b56fa6a2 100644 --- a/tests/Unit/Query/Span/FieldMaskingSpanQueryTest.php +++ b/tests/Unit/Query/Span/FieldMaskingSpanQueryTest.php @@ -11,6 +11,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Span\FieldMaskingSpanQuery; use ONGR\ElasticsearchDSL\Query\Span\SpanNearQuery; use ONGR\ElasticsearchDSL\Query\Span\SpanTermQuery; @@ -18,12 +19,12 @@ /** * Unit test for FieldMaskingSpanQuery. */ -class FieldMaskingSpanQueryTest extends \PHPUnit\Framework\TestCase +class FieldMaskingSpanQueryTest extends TestCase { /** * Tests for toArray(). */ - public function testToArray() + public function testToArray(): void { $spanTermQuery = new SpanTermQuery('text', 'quick brown'); diff --git a/tests/Unit/Query/Span/SpanContainingQueryTest.php b/tests/Unit/Query/Span/SpanContainingQueryTest.php index 71411abf..2e8d2b72 100644 --- a/tests/Unit/Query/Span/SpanContainingQueryTest.php +++ b/tests/Unit/Query/Span/SpanContainingQueryTest.php @@ -11,17 +11,19 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span; +use ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Span\SpanContainingQuery; /** * Unit test for SpanContainingQuery. */ -class SpanContainingQueryTest extends \PHPUnit\Framework\TestCase +class SpanContainingQueryTest extends TestCase { /** * Tests for toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new SpanContainingQuery( $this->getSpanQueryMock('foo'), @@ -41,13 +43,11 @@ public function testToArray() } /** - * @param string $value - * * @returns \PHPUnit_Framework_MockObject_MockObject */ - private function getSpanQueryMock($value) + private function getSpanQueryMock(string $value) { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock(); + $mock = $this->createMock(SpanQueryInterface::class); $mock ->expects($this->once()) ->method('toArray') diff --git a/tests/Unit/Query/Span/SpanFirstQueryTest.php b/tests/Unit/Query/Span/SpanFirstQueryTest.php index fbff909a..09583bed 100644 --- a/tests/Unit/Query/Span/SpanFirstQueryTest.php +++ b/tests/Unit/Query/Span/SpanFirstQueryTest.php @@ -11,19 +11,21 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span; +use ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Span\SpanFirstQuery; /** * Unit test for SpanFirstQuery. */ -class SpanFirstQueryTest extends \PHPUnit\Framework\TestCase +class SpanFirstQueryTest extends TestCase { /** * Tests for toArray(). */ - public function testToArray() + public function testToArray(): void { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock(); + $mock = $this->createMock(SpanQueryInterface::class); $mock ->expects($this->once()) ->method('toArray') diff --git a/tests/Unit/Query/Span/SpanMultiTermQueryTest.php b/tests/Unit/Query/Span/SpanMultiTermQueryTest.php index e6f4f803..27254475 100644 --- a/tests/Unit/Query/Span/SpanMultiTermQueryTest.php +++ b/tests/Unit/Query/Span/SpanMultiTermQueryTest.php @@ -11,19 +11,21 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span; +use ONGR\ElasticsearchDSL\BuilderInterface; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Span\SpanMultiTermQuery; /** * Unit test for SpanMultiTermQuery. */ -class SpanMultiTermQueryTest extends \PHPUnit\Framework\TestCase +class SpanMultiTermQueryTest extends TestCase { /** * Test for toArray(). */ - public function testToArray() + public function testToArray(): void { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); + $mock = $this->createMock(BuilderInterface::class); $mock ->expects($this->once()) ->method('toArray') diff --git a/tests/Unit/Query/Span/SpanNearQueryTest.php b/tests/Unit/Query/Span/SpanNearQueryTest.php index a71d0733..847f2107 100644 --- a/tests/Unit/Query/Span/SpanNearQueryTest.php +++ b/tests/Unit/Query/Span/SpanNearQueryTest.php @@ -11,19 +11,21 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span; +use ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Span\SpanNearQuery; /** * Unit test for SpanNearQuery. */ -class SpanNearQueryTest extends \PHPUnit\Framework\TestCase +class SpanNearQueryTest extends TestCase { /** * Tests for toArray(). */ - public function testToArray() + public function testToArray(): void { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock(); + $mock = $this->createMock(SpanQueryInterface::class); $mock ->expects($this->once()) ->method('toArray') @@ -32,6 +34,7 @@ public function testToArray() $query = new SpanNearQuery(['in_order' => false]); $query->setSlop(5); $query->addQuery($mock); + $result = [ 'span_near' => [ 'clauses' => [ diff --git a/tests/Unit/Query/Span/SpanNotQueryTest.php b/tests/Unit/Query/Span/SpanNotQueryTest.php index 0fb002f2..74178399 100644 --- a/tests/Unit/Query/Span/SpanNotQueryTest.php +++ b/tests/Unit/Query/Span/SpanNotQueryTest.php @@ -11,19 +11,21 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span; +use ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Span\SpanNotQuery; /** * Unit test for SpanNotQuery. */ -class SpanNotQueryTest extends \PHPUnit\Framework\TestCase +class SpanNotQueryTest extends TestCase { /** * Tests for toArray(). */ - public function testSpanNotQueryToArray() + public function testSpanNotQueryToArray(): void { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock(); + $mock = $this->createMock(SpanQueryInterface::class); $mock ->expects($this->exactly(2)) ->method('toArray') diff --git a/tests/Unit/Query/Span/SpanOrQueryTest.php b/tests/Unit/Query/Span/SpanOrQueryTest.php index 537caf44..6bd5ecdd 100644 --- a/tests/Unit/Query/Span/SpanOrQueryTest.php +++ b/tests/Unit/Query/Span/SpanOrQueryTest.php @@ -11,19 +11,21 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span; +use ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Span\SpanOrQuery; /** * Unit test for SpanOrQuery. */ -class SpanOrQueryTest extends \PHPUnit\Framework\TestCase +class SpanOrQueryTest extends TestCase { /** * Tests for toArray(). */ - public function testToArray() + public function testToArray(): void { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock(); + $mock = $this->createMock(SpanQueryInterface::class); $mock ->expects($this->once()) ->method('toArray') @@ -31,6 +33,7 @@ public function testToArray() $query = new SpanOrQuery(); $query->addQuery($mock); + $result = [ 'span_or' => [ 'clauses' => [ @@ -43,7 +46,7 @@ public function testToArray() $this->assertEquals($result, $query->toArray()); $result = $query->getQueries(); - $this->assertInternalType('array', $result); + $this->assertIsArray($result); $this->assertEquals(1, count($result)); } } diff --git a/tests/Unit/Query/Span/SpanTermQueryTest.php b/tests/Unit/Query/Span/SpanTermQueryTest.php index d260a619..91f7b37f 100644 --- a/tests/Unit/Query/Span/SpanTermQueryTest.php +++ b/tests/Unit/Query/Span/SpanTermQueryTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Span\SpanTermQuery; /** * Unit test for SpanTermQuery. */ -class SpanTermQueryTest extends \PHPUnit\Framework\TestCase +class SpanTermQueryTest extends TestCase { /** * Tests for toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new SpanTermQuery('user', 'bob'); $expected = [ @@ -34,7 +35,7 @@ public function testToArray() /** * Tests for toArray() with parameters. */ - public function testToArrayWithParameters() + public function testToArrayWithParameters(): void { $query = new SpanTermQuery('user', 'bob', ['boost' => 2]); $expected = [ diff --git a/tests/Unit/Query/Span/SpanWithinQueryTest.php b/tests/Unit/Query/Span/SpanWithinQueryTest.php index d0a7c160..a3bf213b 100644 --- a/tests/Unit/Query/Span/SpanWithinQueryTest.php +++ b/tests/Unit/Query/Span/SpanWithinQueryTest.php @@ -11,17 +11,19 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span; +use ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Span\SpanWithinQuery; /** * Unit test for SpanWithinQuery. */ -class SpanWithinQueryTest extends \PHPUnit\Framework\TestCase +class SpanWithinQueryTest extends TestCase { /** * Tests for toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new SpanWithinQuery( $this->getSpanQueryMock('foo'), @@ -41,13 +43,11 @@ public function testToArray() } /** - * @param string $value - * * @returns \PHPUnit_Framework_MockObject_MockObject */ - private function getSpanQueryMock($value) + private function getSpanQueryMock(string $value) { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\Span\SpanQueryInterface')->getMock(); + $mock = $this->createMock(SpanQueryInterface::class); $mock ->expects($this->once()) ->method('toArray') diff --git a/tests/Unit/Query/Specialized/MoreLikeThisQueryTest.php b/tests/Unit/Query/Specialized/MoreLikeThisQueryTest.php index 557b40ee..6961ff35 100644 --- a/tests/Unit/Query/Specialized/MoreLikeThisQueryTest.php +++ b/tests/Unit/Query/Specialized/MoreLikeThisQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Specialized; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Specialized\MoreLikeThisQuery; -class MoreLikeThisQueryTest extends \PHPUnit\Framework\TestCase +class MoreLikeThisQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new MoreLikeThisQuery('this is a test', ['fields' => ['title', 'description']]); $expected = [ diff --git a/tests/Unit/Query/Specialized/ScriptQueryTest.php b/tests/Unit/Query/Specialized/ScriptQueryTest.php index 7327f159..2c55ec43 100644 --- a/tests/Unit/Query/Specialized/ScriptQueryTest.php +++ b/tests/Unit/Query/Specialized/ScriptQueryTest.php @@ -11,16 +11,16 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Specialized; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; use ONGR\ElasticsearchDSL\Query\Specialized\ScriptQuery; -class ScriptQueryTest extends \PHPUnit\Framework\TestCase +class ScriptQueryTest extends TestCase { /** * Data provider for testToArray(). - * - * @return array */ - public function getArrayDataProvider() + public static function getArrayDataProvider(): array { return [ 'simple_script' => [ @@ -42,10 +42,9 @@ public function getArrayDataProvider() * @param string $script Script * @param array $parameters Optional parameters * @param array $expected Expected values - * - * @dataProvider getArrayDataProvider */ - public function testToArray($script, $parameters, $expected) + #[DataProvider('getArrayDataProvider')] + public function testToArray($script, $parameters, $expected): void { $filter = new ScriptQuery($script, $parameters); $result = $filter->toArray(); diff --git a/tests/Unit/Query/Specialized/TemplateQueryTest.php b/tests/Unit/Query/Specialized/TemplateQueryTest.php index 80e497a5..618b23f9 100644 --- a/tests/Unit/Query/Specialized/TemplateQueryTest.php +++ b/tests/Unit/Query/Specialized/TemplateQueryTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Specialized; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\Specialized\TemplateQuery; /** * Unit test for Template. */ -class TemplateQueryTest extends \PHPUnit\Framework\TestCase +class TemplateQueryTest extends TestCase { /** * Tests toArray() method with inline. */ - public function testToArrayInline() + public function testToArrayInline(): void { $inline = '"term": {"field": "{{query_string}}"}'; $params = ['query_string' => 'all about search']; @@ -38,13 +39,14 @@ public function testToArrayInline() /** * Tests toArray() method with file */ - public function testToArrayFile() + public function testToArrayFile(): void { $file = 'my_template'; $params = ['query_string' => 'all about search']; $query = new TemplateQuery(); $query->setFile($file); $query->setParams($params); + $expected = [ 'template' => [ 'file' => $file, @@ -56,11 +58,10 @@ public function testToArrayFile() /** * Tests toArray() exception - * - * @expectedException \InvalidArgumentException */ - public function testToArrayException() + public function testToArrayException(): void { + $this->expectException(\InvalidArgumentException::class); $query = new TemplateQuery(); $query->toArray(); } diff --git a/tests/Unit/Query/TermLevel/ExistsQueryTest.php b/tests/Unit/Query/TermLevel/ExistsQueryTest.php index 9ffd57e0..d8cda8ed 100644 --- a/tests/Unit/Query/TermLevel/ExistsQueryTest.php +++ b/tests/Unit/Query/TermLevel/ExistsQueryTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\TermLevel\ExistsQuery; /** * Unit test for ExistsQuery. */ -class ExistsQueryTest extends \PHPUnit\Framework\TestCase +class ExistsQueryTest extends TestCase { /** * Tests toArray() method. */ - public function testToArray() + public function testToArray(): void { $query = new ExistsQuery('bar'); $this->assertEquals(['exists' => ['field' => 'bar']], $query->toArray()); diff --git a/tests/Unit/Query/TermLevel/FuzzyQueryTest.php b/tests/Unit/Query/TermLevel/FuzzyQueryTest.php index 8eeb5998..2cd491df 100644 --- a/tests/Unit/Query/TermLevel/FuzzyQueryTest.php +++ b/tests/Unit/Query/TermLevel/FuzzyQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\TermLevel\FuzzyQuery; -class FuzzyQueryTest extends \PHPUnit\Framework\TestCase +class FuzzyQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new FuzzyQuery('user', 'ki', ['boost' => 1.2]); $expected = [ diff --git a/tests/Unit/Query/TermLevel/IdsQueryTest.php b/tests/Unit/Query/TermLevel/IdsQueryTest.php index eda356f1..4e8a4804 100644 --- a/tests/Unit/Query/TermLevel/IdsQueryTest.php +++ b/tests/Unit/Query/TermLevel/IdsQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\TermLevel\IdsQuery; -class IdsQueryTest extends \PHPUnit\Framework\TestCase +class IdsQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new IdsQuery(['foo', 'bar']); $expected = [ diff --git a/tests/Unit/Query/TermLevel/PrefixQueryTest.php b/tests/Unit/Query/TermLevel/PrefixQueryTest.php index 54604b52..00035dda 100644 --- a/tests/Unit/Query/TermLevel/PrefixQueryTest.php +++ b/tests/Unit/Query/TermLevel/PrefixQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\TermLevel\PrefixQuery; -class PrefixQueryTest extends \PHPUnit\Framework\TestCase +class PrefixQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new PrefixQuery('user', 'ki'); $expected = [ diff --git a/tests/Unit/Query/TermLevel/RangeQueryTest.php b/tests/Unit/Query/TermLevel/RangeQueryTest.php index 9506b06e..7a7fc367 100644 --- a/tests/Unit/Query/TermLevel/RangeQueryTest.php +++ b/tests/Unit/Query/TermLevel/RangeQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\TermLevel\RangeQuery; -class RangeQueryTest extends \PHPUnit\Framework\TestCase +class RangeQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new RangeQuery('age', ['gte' => 10, 'lte' => 20]); $expected = [ diff --git a/tests/Unit/Query/TermLevel/RegexpQueryTest.php b/tests/Unit/Query/TermLevel/RegexpQueryTest.php index 37035df5..d2f29a32 100644 --- a/tests/Unit/Query/TermLevel/RegexpQueryTest.php +++ b/tests/Unit/Query/TermLevel/RegexpQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\TermLevel\RegexpQuery; -class RegexpQueryTest extends \PHPUnit\Framework\TestCase +class RegexpQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new RegexpQuery('user', 's.*y'); $expected = [ diff --git a/tests/Unit/Query/TermLevel/TermQueryTest.php b/tests/Unit/Query/TermLevel/TermQueryTest.php index 70bb264a..25d365a4 100644 --- a/tests/Unit/Query/TermLevel/TermQueryTest.php +++ b/tests/Unit/Query/TermLevel/TermQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; -class TermQueryTest extends \PHPUnit\Framework\TestCase +class TermQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new TermQuery('user', 'bob'); $expected = [ diff --git a/tests/Unit/Query/TermLevel/TermsQueryTest.php b/tests/Unit/Query/TermLevel/TermsQueryTest.php index a7c128e1..7e5776d1 100644 --- a/tests/Unit/Query/TermLevel/TermsQueryTest.php +++ b/tests/Unit/Query/TermLevel/TermsQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\TermLevel\TermsQuery; -class TermsQueryTest extends \PHPUnit\Framework\TestCase +class TermsQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { $query = new TermsQuery('user', ['bob', 'elasticsearch']); $expected = [ diff --git a/tests/Unit/Query/TermLevel/TermsSetQueryTest.php b/tests/Unit/Query/TermLevel/TermsSetQueryTest.php index c51fa904..c2c9b42f 100644 --- a/tests/Unit/Query/TermLevel/TermsSetQueryTest.php +++ b/tests/Unit/Query/TermLevel/TermsSetQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\TermLevel\TermsSetQuery; -class TermsSetQueryTest extends \PHPUnit\Framework\TestCase +class TermsSetQueryTest extends TestCase { /** * Tests toArray(). */ - public function testToArray() + public function testToArray(): void { $terms = ['php', 'c++', 'java']; $parameters = ['minimum_should_match_field' => 'required_matches']; @@ -35,7 +36,7 @@ public function testToArray() $this->assertEquals($expected, $query->toArray()); } - public function testItThrowsAaExceptionWhenMinimumShouldMatchFieldOrMinimumShouldMatchScriptIsNotGiven() + public function testItThrowsAaExceptionWhenMinimumShouldMatchFieldOrMinimumShouldMatchScriptIsNotGiven(): void { $message = "Either minimum_should_match_field or minimum_should_match_script must be set."; $this->expectException(\InvalidArgumentException::class); diff --git a/tests/Unit/Query/TermLevel/TypeQueryTest.php b/tests/Unit/Query/TermLevel/TypeQueryTest.php index 6a0cd7f6..6b5873ad 100644 --- a/tests/Unit/Query/TermLevel/TypeQueryTest.php +++ b/tests/Unit/Query/TermLevel/TypeQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\TermLevel\TypeQuery; -class TypeQueryTest extends \PHPUnit\Framework\TestCase +class TypeQueryTest extends TestCase { /** * Test for query toArray() method. */ - public function testToArray() + public function testToArray(): void { $query = new TypeQuery('foo'); $expectedResult = [ diff --git a/tests/Unit/Query/TermLevel/WildcardQueryTest.php b/tests/Unit/Query/TermLevel/WildcardQueryTest.php index cbc95189..e40b8e53 100644 --- a/tests/Unit/Query/TermLevel/WildcardQueryTest.php +++ b/tests/Unit/Query/TermLevel/WildcardQueryTest.php @@ -11,14 +11,15 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\TermLevel\WildcardQuery; -class WildcardQueryTest extends \PHPUnit\Framework\TestCase +class WildcardQueryTest extends TestCase { /** * Test for query toArray() method. */ - public function testToArray() + public function testToArray(): void { $query = new WildcardQuery('user', 'ki*y'); $expectedResult = [ diff --git a/tests/Unit/SearchEndpoint/AggregationsEndpointTest.php b/tests/Unit/SearchEndpoint/AggregationsEndpointTest.php index 016c1ff7..3d66c911 100644 --- a/tests/Unit/SearchEndpoint/AggregationsEndpointTest.php +++ b/tests/Unit/SearchEndpoint/AggregationsEndpointTest.php @@ -11,21 +11,22 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\MissingAggregation; use ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint; /** * Class AggregationsEndpointTest. */ -class AggregationsEndpointTest extends \PHPUnit\Framework\TestCase +class AggregationsEndpointTest extends TestCase { /** * Tests constructor. */ - public function testItCanBeInstantiated() + public function testItCanBeInstantiated(): void { $this->assertInstanceOf( - 'ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint', + AggregationsEndpoint::class, new AggregationsEndpoint() ); } @@ -33,12 +34,13 @@ public function testItCanBeInstantiated() /** * Tests if endpoint returns builders. */ - public function testEndpointGetter() + public function testEndpointGetter(): void { $aggName = 'acme_agg'; $agg = new MissingAggregation('acme'); $endpoint = new AggregationsEndpoint(); $endpoint->add($agg, $aggName); + $builders = $endpoint->getAll(); $this->assertCount(1, $builders); diff --git a/tests/Unit/SearchEndpoint/HighlightEndpointTest.php b/tests/Unit/SearchEndpoint/HighlightEndpointTest.php index 5b1a623f..0f074297 100644 --- a/tests/Unit/SearchEndpoint/HighlightEndpointTest.php +++ b/tests/Unit/SearchEndpoint/HighlightEndpointTest.php @@ -11,51 +11,52 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Highlight\Highlight; use ONGR\ElasticsearchDSL\SearchEndpoint\HighlightEndpoint; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -use PHPUnit_Framework_MockObject_MockObject as MockObject; /** * Class HighlightEndpointTest. */ -class HighlightEndpointTest extends \PHPUnit\Framework\TestCase +class HighlightEndpointTest extends TestCase { /** * Tests constructor. */ - public function testItCanBeInstantiated() + public function testItCanBeInstantiated(): void { - $this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\HighlightEndpoint', new HighlightEndpoint()); + $this->assertInstanceOf(HighlightEndpoint::class, new HighlightEndpoint()); } /** * Tests adding builder. */ - public function testNormalization() + public function testNormalization(): void { $instance = new HighlightEndpoint(); /** @var NormalizerInterface|MockObject $normalizerInterface */ $normalizerInterface = $this->getMockForAbstractClass( - 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' + NormalizerInterface::class ); $this->assertNull($instance->normalize($normalizerInterface)); $highlight = new Highlight(); $highlight->addField('acme'); + $instance->add($highlight); $this->assertEquals( - json_encode($highlight->toArray()), - json_encode($instance->normalize($normalizerInterface)) + json_encode($highlight->toArray(), JSON_THROW_ON_ERROR), + json_encode($instance->normalize($normalizerInterface), JSON_THROW_ON_ERROR) ); } /** * Tests if endpoint returns builders. */ - public function testEndpointGetter() + public function testEndpointGetter(): void { $highlightName = 'acme_highlight'; $highlight = new Highlight(); @@ -63,6 +64,7 @@ public function testEndpointGetter() $endpoint = new HighlightEndpoint(); $endpoint->add($highlight, $highlightName); + $builders = $endpoint->getAll(); $this->assertCount(1, $builders); diff --git a/tests/Unit/SearchEndpoint/InnerHitsEndpointTest.php b/tests/Unit/SearchEndpoint/InnerHitsEndpointTest.php index 26e2a7a3..5d90d263 100644 --- a/tests/Unit/SearchEndpoint/InnerHitsEndpointTest.php +++ b/tests/Unit/SearchEndpoint/InnerHitsEndpointTest.php @@ -11,20 +11,23 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +use ONGR\ElasticsearchDSL\BuilderInterface; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\SearchEndpoint\InnerHitsEndpoint; /** * Class AggregationsEndpointTest. */ -class InnerHitsEndpointTest extends \PHPUnit\Framework\TestCase +class InnerHitsEndpointTest extends TestCase { /** * Tests constructor. */ - public function testItCanBeInstantiated() + public function testItCanBeInstantiated(): void { $this->assertInstanceOf( - 'ONGR\ElasticsearchDSL\SearchEndpoint\InnerHitsEndpoint', + InnerHitsEndpoint::class, new InnerHitsEndpoint() ); } @@ -32,12 +35,13 @@ public function testItCanBeInstantiated() /** * Tests if endpoint returns builders. */ - public function testEndpointGetter() + public function testEndpointGetter(): void { $hitName = 'foo'; - $innerHit = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); + $innerHit = $this->createMock(BuilderInterface::class); $endpoint = new InnerHitsEndpoint(); $endpoint->add($innerHit, $hitName); + $builders = $endpoint->getAll(); $this->assertCount(1, $builders); @@ -47,20 +51,20 @@ public function testEndpointGetter() /** * Tests normalize method */ - public function testNormalization() + public function testNormalization(): void { - $normalizer = $this - ->getMockBuilder('Symfony\Component\Serializer\Normalizer\NormalizerInterface') - ->getMock(); + $normalizer = $this->createMock(NormalizerInterface::class); $innerHit = $this - ->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface') - ->setMethods(['getName', 'toArray', 'getType']) + ->getMockBuilder(BuilderInterface::class) + ->addMethods(['getName']) + ->onlyMethods(['toArray', 'getType']) ->getMock(); $innerHit->expects($this->any())->method('getName')->willReturn('foo'); $innerHit->expects($this->any())->method('toArray')->willReturn(['foo' => 'bar']); $endpoint = new InnerHitsEndpoint(); $endpoint->add($innerHit, 'foo'); + $expected = [ 'foo' => [ 'foo' => 'bar' diff --git a/tests/Unit/SearchEndpoint/PostFilterEndpointTest.php b/tests/Unit/SearchEndpoint/PostFilterEndpointTest.php index 36adfc3e..c6993d51 100644 --- a/tests/Unit/SearchEndpoint/PostFilterEndpointTest.php +++ b/tests/Unit/SearchEndpoint/PostFilterEndpointTest.php @@ -11,28 +11,28 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\MatchAllQuery; use ONGR\ElasticsearchDSL\SearchEndpoint\PostFilterEndpoint; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -use PHPUnit_Framework_MockObject_MockObject as MockObject; /** * Class PostFilterEndpointTest. */ -class PostFilterEndpointTest extends \PHPUnit\Framework\TestCase +class PostFilterEndpointTest extends TestCase { /** * Tests constructor. */ - public function testItCanBeInstantiated() + public function testItCanBeInstantiated(): void { - $this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\PostFilterEndpoint', new PostFilterEndpoint()); + $this->assertInstanceOf(PostFilterEndpoint::class, new PostFilterEndpoint()); } /** * Tests if correct order is returned. It's very important that filters must be executed second. */ - public function testGetOrder() + public function testGetOrder(): void { $instance = new PostFilterEndpoint(); $this->assertEquals(1, $instance->getOrder()); @@ -41,12 +41,12 @@ public function testGetOrder() /** * Test normalization. */ - public function testNormalization() + public function testNormalization(): void { $instance = new PostFilterEndpoint(); /** @var NormalizerInterface|MockObject $normalizerInterface */ $normalizerInterface = $this->getMockForAbstractClass( - 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' + NormalizerInterface::class ); $this->assertNull($instance->normalize($normalizerInterface)); @@ -54,21 +54,22 @@ public function testNormalization() $instance->add($matchAll); $this->assertEquals( - json_encode($matchAll->toArray()), - json_encode($instance->normalize($normalizerInterface)) + json_encode($matchAll->toArray(), JSON_THROW_ON_ERROR), + json_encode($instance->normalize($normalizerInterface), JSON_THROW_ON_ERROR) ); } /** * Tests if endpoint returns builders. */ - public function testEndpointGetter() + public function testEndpointGetter(): void { $filterName = 'acme_post_filter'; $filter = new MatchAllQuery(); $endpoint = new PostFilterEndpoint(); $endpoint->add($filter, $filterName); + $builders = $endpoint->getAll(); $this->assertCount(1, $builders); diff --git a/tests/Unit/SearchEndpoint/QueryEndpointTest.php b/tests/Unit/SearchEndpoint/QueryEndpointTest.php index 5d28e152..4ce4a8e5 100644 --- a/tests/Unit/SearchEndpoint/QueryEndpointTest.php +++ b/tests/Unit/SearchEndpoint/QueryEndpointTest.php @@ -11,28 +11,28 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\MatchAllQuery; use ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint; -use PHPUnit_Framework_MockObject_MockObject as MockObject; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; /** * Unit test class for the QueryEndpoint. */ -class QueryEndpointTest extends \PHPUnit\Framework\TestCase +class QueryEndpointTest extends TestCase { /** * Tests constructor. */ - public function testItCanBeInstantiated() + public function testItCanBeInstantiated(): void { - $this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint', new QueryEndpoint()); + $this->assertInstanceOf(QueryEndpoint::class, new QueryEndpoint()); } /** * Tests if correct order is returned. Query must be executed after filter and post filter. */ - public function testGetOrder() + public function testGetOrder(): void { $instance = new QueryEndpoint(); $this->assertEquals(2, $instance->getOrder()); @@ -41,12 +41,12 @@ public function testGetOrder() /** * Tests if endpoint return correct normalized data. */ - public function testEndpoint() + public function testEndpoint(): void { $instance = new QueryEndpoint(); /** @var NormalizerInterface|MockObject $normalizerInterface */ $normalizerInterface = $this->getMockForAbstractClass( - 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' + NormalizerInterface::class ); $this->assertNull($instance->normalize($normalizerInterface)); @@ -63,12 +63,13 @@ public function testEndpoint() /** * Tests if endpoint returns builders. */ - public function testEndpointGetter() + public function testEndpointGetter(): void { $queryName = 'acme_query'; $query = new MatchAllQuery(); $endpoint = new QueryEndpoint(); $endpoint->add($query, $queryName); + $builders = $endpoint->getAll(); $this->assertCount(1, $builders); diff --git a/tests/Unit/SearchEndpoint/SearchEndpointFactoryTest.php b/tests/Unit/SearchEndpoint/SearchEndpointFactoryTest.php index b33028bb..e81263e5 100644 --- a/tests/Unit/SearchEndpoint/SearchEndpointFactoryTest.php +++ b/tests/Unit/SearchEndpoint/SearchEndpointFactoryTest.php @@ -11,28 +11,30 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointFactory; /** * Unit test class for search endpoint factory. */ -class SearchEndpointFactoryTest extends \PHPUnit\Framework\TestCase +class SearchEndpointFactoryTest extends TestCase { /** * Tests get method exception. - * - * @expectedException \RuntimeException */ - public function testGet() + public function testGet(): void { + $this->expectException(\RuntimeException::class); SearchEndpointFactory::get('foo'); } /** * Tests if factory can create endpoint. */ - public function testFactory() + #[DoesNotPerformAssertions] + public function testFactory(): void { SearchEndpointFactory::get(AggregationsEndpoint::NAME); } diff --git a/tests/Unit/SearchEndpoint/SortEndpointTest.php b/tests/Unit/SearchEndpoint/SortEndpointTest.php index 7f825bcf..3af8d44a 100644 --- a/tests/Unit/SearchEndpoint/SortEndpointTest.php +++ b/tests/Unit/SearchEndpoint/SortEndpointTest.php @@ -11,37 +11,38 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint; use ONGR\ElasticsearchDSL\Sort\FieldSort; -use PHPUnit_Framework_MockObject_MockObject as MockObject; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; /** * Class SortEndpointTest. */ -class SortEndpointTest extends \PHPUnit\Framework\TestCase +class SortEndpointTest extends TestCase { /** * Tests constructor. */ - public function testItCanBeInstantiated() + public function testItCanBeInstantiated(): void { - $this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint', new SortEndpoint()); + $this->assertInstanceOf(SortEndpoint::class, new SortEndpoint()); } /** * Tests endpoint normalization. */ - public function testNormalize() + public function testNormalize(): void { $instance = new SortEndpoint(); /** @var NormalizerInterface|MockObject $normalizerInterface */ $normalizerInterface = $this->getMockForAbstractClass( - 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' + NormalizerInterface::class ); - $sort = new FieldSort('acme', ['order' => FieldSort::ASC]); + $sort = new FieldSort('acme', FieldSort::ASC); $instance->add($sort); $this->assertEquals( @@ -53,12 +54,13 @@ public function testNormalize() /** * Tests if endpoint returns builders. */ - public function testEndpointGetter() + public function testEndpointGetter(): void { $sortName = 'acme_sort'; $sort = new FieldSort('acme'); $endpoint = new SortEndpoint(); $endpoint->add($sort, $sortName); + $builders = $endpoint->getAll(); $this->assertCount(1, $builders); diff --git a/tests/Unit/SearchEndpoint/SuggestEndpointTest.php b/tests/Unit/SearchEndpoint/SuggestEndpointTest.php index 34c8704b..00dc3077 100644 --- a/tests/Unit/SearchEndpoint/SuggestEndpointTest.php +++ b/tests/Unit/SearchEndpoint/SuggestEndpointTest.php @@ -11,31 +11,32 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\SearchEndpoint\SuggestEndpoint; use ONGR\ElasticsearchDSL\Suggest\Suggest; -use PHPUnit_Framework_MockObject_MockObject as MockObject; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -class SuggestEndpointTest extends \PHPUnit\Framework\TestCase +class SuggestEndpointTest extends TestCase { /** * Tests constructor. */ - public function testItCanBeInstantiated() + public function testItCanBeInstantiated(): void { - $this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\SuggestEndpoint', new SuggestEndpoint()); + $this->assertInstanceOf(SuggestEndpoint::class, new SuggestEndpoint()); } /** * Tests if endpoint returns builders. */ - public function testEndpointGetter() + public function testEndpointGetter(): void { $suggestName = 'acme_suggest'; $text = 'foo'; $suggest = new Suggest($suggestName, 'text', $text, 'acme'); $endpoint = new SuggestEndpoint(); $endpoint->add($suggest, $suggestName); + $builders = $endpoint->getAll(); $this->assertCount(1, $builders); @@ -45,13 +46,13 @@ public function testEndpointGetter() /** * Tests endpoint normalization. */ - public function testNormalize() + public function testNormalize(): void { $instance = new SuggestEndpoint(); /** @var NormalizerInterface|MockObject $normalizerInterface */ $normalizerInterface = $this->getMockForAbstractClass( - 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' + NormalizerInterface::class ); $suggest = new Suggest('foo', 'bar', 'acme', 'foo'); diff --git a/tests/Unit/SearchTest.php b/tests/Unit/SearchTest.php index bfb2f4d3..1c0a2916 100644 --- a/tests/Unit/SearchTest.php +++ b/tests/Unit/SearchTest.php @@ -11,6 +11,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\TermLevel\ExistsQuery; use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; use ONGR\ElasticsearchDSL\Search; @@ -20,17 +21,17 @@ /** * Test for Search. */ -class SearchTest extends \PHPUnit\Framework\TestCase +class SearchTest extends TestCase { /** * Tests Search constructor. */ - public function testItCanBeInstantiated() + public function testItCanBeInstantiated(): void { - $this->assertInstanceOf('ONGR\ElasticsearchDSL\Search', new Search()); + $this->assertInstanceOf(Search::class, new Search()); } - public function testScrollUriParameter() + public function testScrollUriParameter(): void { $search = new Search(); $search->setScroll('5m'); diff --git a/tests/Unit/Sort/FieldSortTest.php b/tests/Unit/Sort/FieldSortTest.php index d8863785..5ec31c06 100644 --- a/tests/Unit/Sort/FieldSortTest.php +++ b/tests/Unit/Sort/FieldSortTest.php @@ -11,17 +11,18 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Sort; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; use ONGR\ElasticsearchDSL\Sort\FieldSort; use ONGR\ElasticsearchDSL\Sort\NestedSort; -class FieldSortTest extends \PHPUnit\Framework\TestCase +class FieldSortTest extends TestCase { /** * Test for toArray() method. * */ - public function testToArray() + public function testToArray(): void { $nestedFilter = new NestedSort('somePath', new TermQuery('somePath.id', 10)); $sort = new FieldSort('someField', 'asc'); diff --git a/tests/Unit/Sort/NestedSortTest.php b/tests/Unit/Sort/NestedSortTest.php index 5fb970f1..3bbfe8b2 100644 --- a/tests/Unit/Sort/NestedSortTest.php +++ b/tests/Unit/Sort/NestedSortTest.php @@ -11,16 +11,17 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Sort; +use PHPUnit\Framework\TestCase; use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; use ONGR\ElasticsearchDSL\Sort\NestedSort; -class NestedSortTest extends \PHPUnit\Framework\TestCase +class NestedSortTest extends TestCase { /** * Test for single nested. * */ - public function testSingle() + public function testSingle(): void { $query = new NestedSort('somePath', new TermQuery('somePath.id', 10)); $expected = [ @@ -39,7 +40,7 @@ public function testSingle() * Test for single nested, no filter. * */ - public function testNoFilter() + public function testNoFilter(): void { $query = new NestedSort('somePath'); $expected = [ @@ -53,7 +54,7 @@ public function testNoFilter() * Test for single nested. * */ - public function testMultipleNesting() + public function testMultipleNesting(): void { $query = new NestedSort('somePath', new TermQuery('somePath.id', 10)); $nestedFilter1 = new NestedSort('secondPath', new TermQuery('secondPath.foo', 'bar')); diff --git a/tests/Unit/Suggest/SuggestTest.php b/tests/Unit/Suggest/SuggestTest.php index 7ae17892..3ba31b97 100644 --- a/tests/Unit/Suggest/SuggestTest.php +++ b/tests/Unit/Suggest/SuggestTest.php @@ -11,14 +11,16 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Suggest; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\Attributes\DataProvider; use ONGR\ElasticsearchDSL\Suggest\Suggest; -class SuggestTest extends \PHPUnit\Framework\TestCase +class SuggestTest extends TestCase { /** * Tests getType method. */ - public function testSuggestGetType() + public function testSuggestGetType(): void { $suggest = new Suggest('foo', 'term', 'acme', 'bar'); $this->assertEquals('term', $suggest->getType()); @@ -29,7 +31,7 @@ public function testSuggestGetType() * * @return array[] */ - public function getTestToArrayData() + public static function getTestToArrayData(): array { return [ [ @@ -110,13 +112,8 @@ public function getTestToArrayData() ]; } - /** - * @param Suggest $suggest - * @param array $expected - * - * @dataProvider getTestToArrayData() - */ - public function testToArray(Suggest $suggest, array $expected) + #[DataProvider('getTestToArrayData')] + public function testToArray(Suggest $suggest, array $expected): void { $this->assertEquals($expected, $suggest->toArray()); }