From 811d177ab708288b354cb2246152b43f87040f23 Mon Sep 17 00:00:00 2001 From: Khalimov Renato Date: Wed, 27 Jan 2021 21:26:25 +0300 Subject: [PATCH 1/3] implemented requirements of the PHP 8 --- src/Aggregation/AbstractAggregation.php | 107 +--- .../Bucketing/AdjacencyMatrixAggregation.php | 32 +- .../AutoDateHistogramAggregation.php | 32 +- .../Bucketing/ChildrenAggregation.php | 45 +- .../Bucketing/CompositeAggregation.php | 80 +-- .../Bucketing/DateHistogramAggregation.php | 56 +- .../Bucketing/DateRangeAggregation.php | 100 +--- .../DiversifiedSamplerAggregation.php | 41 +- .../Bucketing/FilterAggregation.php | 48 +- .../Bucketing/FiltersAggregation.php | 53 +- .../Bucketing/GeoDistanceAggregation.php | 99 +--- .../Bucketing/GeoHashGridAggregation.php | 81 +-- .../Bucketing/GlobalAggregation.php | 20 +- .../Bucketing/HistogramAggregation.php | 162 ++---- .../Bucketing/Ipv4RangeAggregation.php | 47 +- .../Bucketing/MissingAggregation.php | 21 +- .../Bucketing/NestedAggregation.php | 39 +- .../Bucketing/RangeAggregation.php | 82 +-- .../Bucketing/ReverseNestedAggregation.php | 42 +- .../Bucketing/SamplerAggregation.php | 47 +- .../Bucketing/SignificantTermsAggregation.php | 7 +- .../Bucketing/SignificantTextAggregation.php | 7 +- .../Bucketing/TermsAggregation.php | 31 +- .../Matrix/MatrixStatsAggregation.php | 62 +-- src/Aggregation/Metric/AvgAggregation.php | 7 +- .../Metric/CardinalityAggregation.php | 57 +-- .../Metric/ExtendedStatsAggregation.php | 52 +- .../Metric/GeoBoundsAggregation.php | 38 +- .../Metric/GeoCentroidAggregation.php | 27 +- src/Aggregation/Metric/MaxAggregation.php | 7 +- src/Aggregation/Metric/MinAggregation.php | 7 +- .../Metric/PercentileRanksAggregation.php | 79 +-- .../Metric/PercentilesAggregation.php | 76 +-- .../Metric/ScriptedMetricAggregation.php | 102 +--- src/Aggregation/Metric/StatsAggregation.php | 29 +- src/Aggregation/Metric/SumAggregation.php | 7 +- src/Aggregation/Metric/TopHitsAggregation.php | 118 +---- .../Metric/ValueCountAggregation.php | 7 +- .../Pipeline/AbstractPipelineAggregation.php | 30 +- .../Pipeline/AvgBucketAggregation.php | 7 +- .../Pipeline/BucketScriptAggregation.php | 45 +- .../Pipeline/BucketSelectorAggregation.php | 7 +- .../Pipeline/BucketSortAggregation.php | 44 +- .../Pipeline/CumulativeSumAggregation.php | 7 +- .../Pipeline/DerivativeAggregation.php | 7 +- .../ExtendedStatsBucketAggregation.php | 7 +- .../Pipeline/MaxBucketAggregation.php | 7 +- .../Pipeline/MinBucketAggregation.php | 7 +- .../Pipeline/MovingAverageAggregation.php | 7 +- .../Pipeline/MovingFunctionAggregation.php | 7 +- .../Pipeline/PercentilesBucketAggregation.php | 29 +- .../SerialDifferencingAggregation.php | 7 +- .../Pipeline/StatsBucketAggregation.php | 7 +- .../Pipeline/SumBucketAggregation.php | 7 +- src/Aggregation/Type/BucketingTrait.php | 9 +- src/Aggregation/Type/MetricTrait.php | 9 +- src/BuilderBag.php | 74 +-- src/BuilderInterface.php | 21 +- src/FieldAwareTrait.php | 19 +- src/Highlight/Highlight.php | 44 +- src/InnerHit/NestedInnerHit.php | 92 +--- src/InnerHit/ParentInnerHit.php | 7 +- src/NameAwareTrait.php | 19 +- src/NamedBuilderInterface.php | 12 +- src/ParametersTrait.php | 64 +-- src/Query/Compound/BoolQuery.php | 68 +-- src/Query/Compound/BoostingQuery.php | 41 +- src/Query/Compound/ConstantScoreQuery.php | 28 +- src/Query/Compound/DisMaxQuery.php | 31 +- src/Query/Compound/FunctionScoreQuery.php | 145 ++---- src/Query/FullText/CommonTermsQuery.php | 36 +- src/Query/FullText/MatchPhrasePrefixQuery.php | 4 +- src/Query/FullText/MatchPhraseQuery.php | 4 +- src/Query/FullText/MatchQuery.php | 36 +- src/Query/FullText/MultiMatchQuery.php | 36 +- src/Query/FullText/QueryStringQuery.php | 24 +- src/Query/FullText/SimpleQueryStringQuery.php | 24 +- src/Query/Geo/GeoBoundingBoxQuery.php | 41 +- src/Query/Geo/GeoDistanceQuery.php | 45 +- src/Query/Geo/GeoPolygonQuery.php | 36 +- src/Query/Geo/GeoShapeQuery.php | 88 +--- src/Query/Joining/HasChildQuery.php | 34 +- src/Query/Joining/HasParentQuery.php | 36 +- src/Query/Joining/NestedQuery.php | 50 +- src/Query/Joining/ParentIdQuery.php | 36 +- src/Query/MatchAllQuery.php | 19 +- src/Query/Span/FieldMaskingSpanQuery.php | 56 +- src/Query/Span/SpanContainingQuery.php | 52 +- src/Query/Span/SpanFirstQuery.php | 39 +- src/Query/Span/SpanMultiTermQuery.php | 32 +- src/Query/Span/SpanNearQuery.php | 29 +- src/Query/Span/SpanNotQuery.php | 36 +- src/Query/Span/SpanOrQuery.php | 34 +- src/Query/Span/SpanQueryInterface.php | 5 +- src/Query/Span/SpanTermQuery.php | 7 +- src/Query/Span/SpanWithinQuery.php | 7 +- src/Query/Specialized/MoreLikeThisQuery.php | 28 +- src/Query/Specialized/ScriptQuery.php | 24 +- src/Query/Specialized/TemplateQuery.php | 73 +-- src/Query/TermLevel/ExistsQuery.php | 25 +- src/Query/TermLevel/FuzzyQuery.php | 34 +- src/Query/TermLevel/IdsQuery.php | 22 +- src/Query/TermLevel/PrefixQuery.php | 31 +- src/Query/TermLevel/RangeQuery.php | 37 +- src/Query/TermLevel/RegexpQuery.php | 34 +- src/Query/TermLevel/TermQuery.php | 34 +- src/Query/TermLevel/TermsQuery.php | 39 +- src/Query/TermLevel/TermsSetQuery.php | 52 +- src/Query/TermLevel/TypeQuery.php | 25 +- src/Query/TermLevel/WildcardQuery.php | 36 +- src/ScriptAwareTrait.php | 22 +- src/Search.php | 479 ++++-------------- src/SearchEndpoint/AbstractSearchEndpoint.php | 47 +- src/SearchEndpoint/AggregationsEndpoint.php | 15 +- src/SearchEndpoint/HighlightEndpoint.php | 42 +- src/SearchEndpoint/InnerHitsEndpoint.php | 13 +- src/SearchEndpoint/PostFilterEndpoint.php | 22 +- src/SearchEndpoint/QueryEndpoint.php | 56 +- src/SearchEndpoint/SearchEndpointFactory.php | 16 +- .../SearchEndpointInterface.php | 61 +-- src/SearchEndpoint/SortEndpoint.php | 17 +- src/SearchEndpoint/SuggestEndpoint.php | 12 +- .../Normalizer/AbstractNormalizable.php | 5 +- .../Normalizer/CustomReferencedNormalizer.php | 22 +- .../Normalizer/OrderedNormalizerInterface.php | 12 +- src/Serializer/OrderedSerializer.php | 30 +- src/Sort/FieldSort.php | 89 +--- src/Sort/NestedSort.php | 68 +-- src/Suggest/Suggest.php | 102 +--- .../AbstractElasticsearchTestCase.php | 4 +- .../AdjacencyMatrixAggregationTest.php | 35 +- .../AudoDateHistogramAggregationTest.php | 6 +- .../Bucketing/ChildrenAggregationTest.php | 7 +- .../DateHistogramAggregationTest.php | 21 +- .../Bucketing/DateRangeAggregationTest.php | 20 +- .../Bucketing/FilterAggregationTest.php | 29 +- .../Bucketing/FiltersAggregationTest.php | 22 +- .../Bucketing/GeoDistanceAggregationTest.php | 49 +- .../Bucketing/GeoHashGridAggregationTest.php | 27 +- .../Bucketing/GlobalAggregationTest.php | 21 +- .../Bucketing/Ipv4RangeAggregationTest.php | 15 +- .../Bucketing/MissingAggregationTest.php | 24 +- .../Metric/CardinalityAggregationTest.php | 21 +- .../Metric/GeoBoundsAggregationTest.php | 22 +- .../Metric/GeoCentroidAggregationTest.php | 22 +- .../Metric/PercentileRanksAggregationTest.php | 42 +- .../Metric/PercentilesAggregationTest.php | 24 +- .../Pipeline/BucketScriptAggregationTest.php | 20 +- .../BucketSelectorAggregationTest.php | 20 +- tests/Unit/ParametersTraitTest.php | 2 +- tests/Unit/Query/Compound/BoolQueryTest.php | 76 +-- .../Query/Compound/FunctionScoreQueryTest.php | 13 +- .../Query/Geo/GeoBoundingBoxQueryTest.php | 29 +- tests/Unit/Query/Span/SpanOrQueryTest.php | 2 +- .../Query/Specialized/TemplateQueryTest.php | 21 +- .../SearchEndpointFactoryTest.php | 17 +- 156 files changed, 1478 insertions(+), 4472 deletions(-) diff --git a/src/Aggregation/AbstractAggregation.php b/src/Aggregation/AbstractAggregation.php index c2fbe984..eb86e707 100644 --- a/src/Aggregation/AbstractAggregation.php +++ b/src/Aggregation/AbstractAggregation.php @@ -9,12 +9,16 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation; use ONGR\ElasticsearchDSL\BuilderBag; +use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\NameAwareTrait; use ONGR\ElasticsearchDSL\NamedBuilderInterface; use ONGR\ElasticsearchDSL\ParametersTrait; +use stdClass; /** * AbstractAggregation class. @@ -22,111 +26,64 @@ abstract class AbstractAggregation implements NamedBuilderInterface { use ParametersTrait; + use NameAwareTrait; - /** - * @var string - */ - private $field; - - /** - * @var BuilderBag - */ - private $aggregations; - - /** - * Abstract supportsNesting method. - * - * @return bool - */ - abstract protected function supportsNesting(); - - /** - * @return array|\stdClass - */ - abstract protected function getArray(); - - /** - * Inner aggregations container init. - * - * @param string $name - */ - public function __construct($name) + private ?string $field = null; + + private ?BuilderBag $aggregations = null; + + abstract protected function supportsNesting(): bool; + + abstract protected function getArray(): array | stdClass; + + public function __construct(string $name) { $this->setName($name); } - /** - * @param string $field - * - * @return $this - */ - public function setField($field) + public function setField(?string $field): static { $this->field = $field; return $this; } - /** - * @return string - */ - public function getField() + public function getField(): ?string { return $this->field; } - /** - * Adds a sub-aggregation. - * - * @param AbstractAggregation $abstractAggregation - * - * @return $this - */ - public function addAggregation(AbstractAggregation $abstractAggregation) + public function addAggregation(AbstractAggregation $abstractAggregation): static { if (!$this->aggregations) { $this->aggregations = $this->createBuilderBag(); } $this->aggregations->add($abstractAggregation); - + return $this; } - /** - * Returns all sub aggregations. - * - * @return BuilderBag[]|NamedBuilderInterface[] - */ - public function getAggregations() + public function getAggregations(): array { if ($this->aggregations) { return $this->aggregations->all(); - } else { - return []; } + + return []; } - /** - * Returns sub aggregation. - * @param string $name Aggregation name to return. - * - * @return AbstractAggregation|NamedBuilderInterface|null - */ - public function getAggregation($name) + public function getAggregation(string $name): AbstractAggregation|BuilderInterface|null { if ($this->aggregations && $this->aggregations->has($name)) { return $this->aggregations->get($name); - } else { - return null; } + + return null; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array { $array = $this->getArray(); $result = [ @@ -144,12 +101,7 @@ public function toArray() return $result; } - /** - * Process all nested aggregations. - * - * @return array - */ - protected function collectNestedAggregations() + protected function collectNestedAggregations(): array { $result = []; /** @var AbstractAggregation $aggregation */ @@ -160,12 +112,7 @@ protected function collectNestedAggregations() return $result; } - /** - * Creates BuilderBag new instance. - * - * @return BuilderBag - */ - private function createBuilderBag() + private function createBuilderBag(): BuilderBag { return new BuilderBag(); } diff --git a/src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php b/src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php index 632f0b59..79a2535d 100644 --- a/src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php +++ b/src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -26,20 +28,11 @@ class AdjacencyMatrixAggregation extends AbstractAggregation use BucketingTrait; - /** - * @var BuilderInterface[] - */ - private $filters = [ + private array $filters = [ self::FILTERS => [] ]; - /** - * Inner aggregations container init. - * - * @param string $name - * @param BuilderInterface[] $filters - */ - public function __construct($name, $filters = []) + public function __construct(string $name, array $filters = []) { parent::__construct($name); @@ -49,32 +42,21 @@ 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(); return $this; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { return $this->filters; } - /** - * {@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..233f0eaf 100644 --- a/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php +++ b/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -24,16 +26,12 @@ class AutoDateHistogramAggregation extends AbstractAggregation { use BucketingTrait; - /** - * Inner aggregations container init. - * - * @param string $name - * @param string $field - * @param int $buckets - * @param string $format - */ - public function __construct($name, $field, $buckets = null, $format = null) - { + public function __construct( + private string $name, + private string $field, + private ?int $buckets = null, + private ?string $format = null + ) { parent::__construct($name); $this->setField($field); @@ -47,24 +45,16 @@ 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..2e0a586b 100644 --- a/src/Aggregation/Bucketing/ChildrenAggregation.php +++ b/src/Aggregation/Bucketing/ChildrenAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,56 +25,31 @@ class ChildrenAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var string - */ - private $children; - - /** - * Return children. - * - * @return string - */ - public function getChildren() - { - return $this->children; - } - - /** - * @param string $name - * @param string $children - */ - public function __construct($name, $children = null) + public function __construct(private string $name, private ?string $children = null) { parent::__construct($name); $this->setChildren($children); } - /** - * @param string $children - * - * @return $this - */ - public function setChildren($children) + public function getChildren(): ?string + { + return $this->children; + } + + public function setChildren(?string $children): static { $this->children = $children; return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'children'; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { if (count($this->getAggregations()) == 0) { throw new \LogicException("Children aggregation `{$this->getName()}` has no aggregations added"); diff --git a/src/Aggregation/Bucketing/CompositeAggregation.php b/src/Aggregation/Bucketing/CompositeAggregation.php index 8f1f8d8a..820d8289 100644 --- a/src/Aggregation/Bucketing/CompositeAggregation.php +++ b/src/Aggregation/Bucketing/CompositeAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -24,28 +26,11 @@ class CompositeAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var BuilderInterface[] - */ - private $sources = []; - - /** - * @var int - */ - private $size; - - /** - * @var array - */ - private $after; - - /** - * Inner aggregations container init. - * - * @param string $name - * @param AbstractAggregation[] $sources - */ - public function __construct($name, $sources = []) + private ?int $size = null; + + private array $after = []; + + public function __construct(private string $name, private array $sources = []) { parent::__construct($name); @@ -54,14 +39,7 @@ 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(); @@ -74,10 +52,7 @@ public function addSource(AbstractAggregation $agg) return $this; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { $array = [ 'sources' => $this->sources, @@ -94,58 +69,31 @@ public function getArray() return $array; } - /** - * {@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..f111d25a 100644 --- a/src/Aggregation/Bucketing/DateHistogramAggregation.php +++ b/src/Aggregation/Bucketing/DateHistogramAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,25 +25,12 @@ class DateHistogramAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var string - */ - protected $interval; - - /** - * @var string - */ - protected $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( + private string $name, + private ?string $field = null, + private mixed $interval = null, + private $format = null + ) { parent::__construct($name); $this->setField($field); @@ -49,50 +38,31 @@ public function __construct($name, $field = null, $interval = null, $format = nu $this->setFormat($format); } - /** - * @return int - */ - public function getInterval() + public function getInterval(): mixed { return $this->interval; } - /** - * @param string $interval - * - * @return $this - */ - public function setInterval($interval) + public function setInterval(mixed $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; return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'date_histogram'; } - /** - * {@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..ea5aea8a 100644 --- a/src/Aggregation/Bucketing/DateRangeAggregation.php +++ b/src/Aggregation/Bucketing/DateRangeAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,30 +25,13 @@ class DateRangeAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var string - */ - private $format; - - /** - * @var array - */ - private $ranges = []; - - /** - * @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( + private string $name, + private ?string $field = null, + private ?string $format = null, + private array $ranges = [], + private bool $keyed = false + ) { parent::__construct($name); $this->setField($field); @@ -60,48 +45,26 @@ 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($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($format): static { $this->format = $format; + + return $this; } - /** - * Add range to aggregation. - * - * @param string|null $from - * @param string|null $to - * @param string|null $key - * - * @return $this - * - * @throws \LogicException - */ - public function addRange($from = null, $to = null, $key = null) + public function addRange(?string $from = null, ?string $to = null, ?string $key = null): static { $range = array_filter( [ @@ -109,9 +72,7 @@ public function addRange($from = null, $to = null, $key = null) 'to' => $to, 'key' => $key, ], - function ($v) { - return !is_null($v); - } + fn(mixed $v): bool => !is_null($v) ); if (empty($range)) { @@ -123,28 +84,21 @@ function ($v) { return $this; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { - if ($this->getField() && $this->getFormat() && !empty($this->ranges)) { - $data = [ - 'format' => $this->getFormat(), - 'field' => $this->getField(), - 'ranges' => $this->ranges, - 'keyed' => $this->keyed, - ]; - - return $data; + if (!$this->getField() || !$this->getFormat() || empty($this->ranges)) { + throw new \LogicException('Date range aggregation must have field, format set and range added.'); } - throw new \LogicException('Date range aggregation must have field, format set and range added.'); + + return [ + 'format' => $this->getFormat(), + 'field' => $this->getField(), + 'ranges' => $this->ranges, + 'keyed' => $this->keyed, + ]; } - /** - * {@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..3aae71ba 100644 --- a/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php +++ b/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,12 +25,6 @@ class DiversifiedSamplerAggregation extends AbstractAggregation { use BucketingTrait; - /** - * Defines how many results will be received from each shard - * @param integer $shardSize - */ - private $shardSize; - /** * DiversifiedSamplerAggregation constructor. * @@ -36,54 +32,41 @@ 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( + protected string $name, + protected ?string $field = null, + protected ?int $shardSize = null + ) { parent::__construct($name); $this->setField($field); $this->setShardSize($shardSize); } - /** - * @return mixed - */ - public function getShardSize() + public function getShardSize(): int { return $this->shardSize; } - /** - * @param mixed $shardSize - * - * @return $this - */ - public function setShardSize($shardSize) + public function setShardSize(int $shardSize): static { $this->shardSize = $shardSize; return $this; } - /** - * @inheritdoc - */ - public function getType() + public function getType(): string { return 'diversified_sampler'; } - /** - * @inheritdoc - */ - protected function getArray() + protected 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..99d1525b 100644 --- a/src/Aggregation/Bucketing/FilterAggregation.php +++ b/src/Aggregation/Bucketing/FilterAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -24,19 +26,10 @@ class FilterAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var BuilderInterface - */ - protected $filter; - - /** - * Inner aggregations container init. - * - * @param string $name - * @param BuilderInterface $filter - */ - public function __construct($name, BuilderInterface $filter = null) - { + public function __construct( + private string $name, + private ?BuilderInterface $filter = null + ) { parent::__construct($name); if ($filter !== null) { @@ -44,40 +37,24 @@ public function __construct($name, BuilderInterface $filter = null) } } - /** - * @param BuilderInterface $filter - * - * @return $this - */ - public function setFilter(BuilderInterface $filter) + public function setFilter(BuilderInterface $filter): static { $this->filter = $filter; return $this; } - /** - * Returns a filter. - * - * @return BuilderInterface - */ - public function getFilter() + public function getFilter(): BuilderInterface { return $this->filter; } - /** - * {@inheritdoc} - */ - public function setField($field) + public function setField($field): static { throw new \LogicException("Filter aggregation, doesn't support `field` parameter"); } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { if (!$this->filter) { throw new \LogicException("Filter aggregation `{$this->getName()}` has no filter added"); @@ -86,10 +63,7 @@ public function getArray() return $this->getFilter()->toArray(); } - /** - * {@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..3644884d 100644 --- a/src/Aggregation/Bucketing/FiltersAggregation.php +++ b/src/Aggregation/Bucketing/FiltersAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -24,25 +26,11 @@ class FiltersAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var BuilderInterface[] - */ - private $filters = []; - - /** - * @var bool - */ - private $anonymous = false; - - /** - * Inner aggregations container init. - * - * @param string $name - * @param BuilderInterface[] $filters - * @param bool $anonymous - */ - public function __construct($name, $filters = [], $anonymous = false) - { + public function __construct( + private string $name, + private array $filters = [], + private bool $anonymous = false + ) { parent::__construct($name); $this->setAnonymous($anonymous); @@ -55,27 +43,14 @@ 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; return $this; } - /** - * @param BuilderInterface $filter - * @param string $name - * - * @throws \LogicException - * - * @return FiltersAggregation - */ - public function addFilter(BuilderInterface $filter, $name = '') + public function addFilter(?BuilderInterface $filter = null, string $name = ''): static { if ($this->anonymous === false && empty($name)) { throw new \LogicException('In not anonymous filters filter name must be set.'); @@ -88,18 +63,12 @@ public function addFilter(BuilderInterface $filter, $name = '') return $this; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { return $this->filters; } - /** - * {@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..1c3484fb 100644 --- a/src/Aggregation/Bucketing/GeoDistanceAggregation.php +++ b/src/Aggregation/Bucketing/GeoDistanceAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,38 +25,17 @@ class GeoDistanceAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var mixed - */ - private $origin; - - /** - * @var string - */ - private $distanceType; - - /** - * @var string - */ - private $unit; - - /** - * @var array - */ - private $ranges = []; - /** * Inner aggregations container init. - * - * @param string $name - * @param string $field - * @param mixed $origin - * @param array $ranges - * @param string $unit - * @param string $distanceType */ - public function __construct($name, $field = null, $origin = null, $ranges = [], $unit = null, $distanceType = null) - { + public function __construct( + protected string $name, + protected ?string $field = null, + protected ?string $origin = null, + protected array $ranges = [], + protected ?string $unit = null, + protected ?string $distanceType = null + ) { parent::__construct($name); $this->setField($field); @@ -68,60 +49,36 @@ public function __construct($name, $field = null, $origin = null, $ranges = [], $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(?string $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; } - /** - * @param string $unit - * - * @return $this - */ - public function setUnit($unit) + public function setUnit(?string $unit): static { $this->unit = $unit; @@ -130,24 +87,14 @@ 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(null|int|float|string $from = null, null|int|float|string $to = null): static { $range = array_filter( [ 'from' => $from, 'to' => $to, - ], - function ($v) { - return !is_null($v); - } + ] ); if (empty($range)) { @@ -159,10 +106,7 @@ function ($v) { return $this; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { $data = []; @@ -191,10 +135,7 @@ public function getArray() return $data; } - /** - * {@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..bc242840 100644 --- a/src/Aggregation/Bucketing/GeoHashGridAggregation.php +++ b/src/Aggregation/Bucketing/GeoHashGridAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,32 +25,13 @@ class GeoHashGridAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var int - */ - private $precision; - - /** - * @var int - */ - private $size; - - /** - * @var int - */ - private $shardSize; - - /** - * Inner aggregations container init. - * - * @param string $name - * @param string $field - * @param int $precision - * @param int $size - * @param int $shardSize - */ - public function __construct($name, $field = null, $precision = null, $size = null, $shardSize = null) - { + public function __construct( + private string $name, + private ?string $field = null, + private ?int $precision = null, + private ?int $size = null, + private ?int $shardSize = null + ) { parent::__construct($name); $this->setField($field); @@ -57,70 +40,43 @@ public function __construct($name, $field = null, $precision = null, $size = nul $this->setShardSize($shardSize); } - /** - * @return int - */ - public function getPrecision() + public function getPrecision(): ?int { return $this->precision; } - /** - * @param int $precision - * - * @return $this - */ - public function setPrecision($precision) + public function setPrecision(?int $precision): static { $this->precision = $precision; return $this; } - /** - * @return int - */ - public function getSize() + public function getSize(): ?int { return $this->size; } - /** - * @param int $size - * - * @return $this - */ - public function setSize($size) + public function setSize(?int $size): static { $this->size = $size; return $this; } - /** - * @return int - */ - public function getShardSize() + public function getShardSize(): ?int { return $this->shardSize; } - /** - * @param int $shardSize - * - * @return $this - */ - public function setShardSize($shardSize) + public function setShardSize(?int $shardSize): static { $this->shardSize = $shardSize; return $this; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { $data = []; @@ -145,10 +101,7 @@ public function getArray() return $data; } - /** - * {@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..47cfd47d 100644 --- a/src/Aggregation/Bucketing/GlobalAggregation.php +++ b/src/Aggregation/Bucketing/GlobalAggregation.php @@ -9,10 +9,13 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait; +use stdClass; /** * Class representing GlobalAggregation. @@ -23,27 +26,18 @@ class GlobalAggregation extends AbstractAggregation { use BucketingTrait; - /** - * {@inheritdoc} - */ - public function setField($field) + public function setField(?string $field): static { throw new \LogicException("Global aggregation, doesn't support `field` parameter"); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'global'; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): stdClass { - return new \stdClass(); + return new stdClass(); } } diff --git a/src/Aggregation/Bucketing/HistogramAggregation.php b/src/Aggregation/Bucketing/HistogramAggregation.php index f2387585..9d79562f 100644 --- a/src/Aggregation/Bucketing/HistogramAggregation.php +++ b/src/Aggregation/Bucketing/HistogramAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,62 +25,22 @@ class HistogramAggregation extends AbstractAggregation { use BucketingTrait; - const DIRECTION_ASC = 'asc'; - const DIRECTION_DESC = 'desc'; - - /** - * @var int - */ - protected $interval; - - /** - * @var int - */ - protected $minDocCount; + public const DIRECTION_ASC = 'asc'; - /** - * @var array - */ - protected $extendedBounds; + public const DIRECTION_DESC = 'desc'; - /** - * @var string - */ - protected $orderMode; + protected array $extendedBounds = []; - /** - * @var string - */ - protected $orderDirection; - - /** - * @var bool - */ - protected $keyed; - - /** - * Inner aggregations container init. - * - * @param string $name - * @param string $field - * @param int $interval - * @param int $minDocCount - * @param string $orderMode - * @param string $orderDirection - * @param int $extendedBoundsMin - * @param int $extendedBoundsMax - * @param bool $keyed - */ public function __construct( - $name, - $field = null, - $interval = null, - $minDocCount = null, - $orderMode = null, - $orderDirection = self::DIRECTION_ASC, - $extendedBoundsMin = null, - $extendedBoundsMax = null, - $keyed = null + private string $name, + private ?string $field = null, + private ?int $interval = null, + private ?int $minDocCount = null, + private ?string $orderMode = null, + private string $orderDirection = self::DIRECTION_ASC, + private ?int $extendedBoundsMin = null, + private ?int $extendedBoundsMax = null, + private ?bool $keyed = null ) { parent::__construct($name); @@ -90,37 +52,19 @@ public function __construct( $this->setKeyed($keyed); } - /** - * @return bool - */ - public function isKeyed() + public function isKeyed(): ?bool { return $this->keyed; } - /** - * Get response as a hash instead keyed by the buckets keys. - * - * @param bool $keyed - * - * @return $this - */ - public function setKeyed($keyed) + public function setKeyed(?bool $keyed): static { $this->keyed = $keyed; return $this; } - /** - * Sets buckets ordering. - * - * @param string $mode - * @param string $direction - * - * @return $this - */ - public function setOrder($mode, $direction = self::DIRECTION_ASC) + public function setOrder(?string $mode, string $direction = self::DIRECTION_ASC): static { $this->orderMode = $mode; $this->orderDirection = $direction; @@ -128,75 +72,45 @@ public function setOrder($mode, $direction = self::DIRECTION_ASC) return $this; } - /** - * @return array - */ - public function getOrder() + public function getOrder(): ?array { if ($this->orderMode && $this->orderDirection) { return [$this->orderMode => $this->orderDirection]; - } else { - return null; } + + return null; } - /** - * @return int - */ - public function getInterval() + public function getInterval(): int { return $this->interval; } - /** - * @param int $interval - * - * @return $this - */ - public function setInterval($interval) + public function setInterval(?int $interval): static { $this->interval = $interval; return $this; } - /** - * @return int - */ - public function getMinDocCount() + public function getMinDocCount(): ?int { return $this->minDocCount; } - /** - * Set limit for document count buckets should have. - * - * @param int $minDocCount - * - * @return $this - */ - public function setMinDocCount($minDocCount) + public function setMinDocCount(?int $minDocCount): static { $this->minDocCount = $minDocCount; return $this; } - /** - * @return array - */ - public function getExtendedBounds() + public function getExtendedBounds(): array { return $this->extendedBounds; } - /** - * @param int $min - * @param int $max - * - * @return $this - */ - public function setExtendedBounds($min = null, $max = null) + public function setExtendedBounds(?int $min = null, ?int $max = null): static { $bounds = array_filter( [ @@ -210,18 +124,12 @@ public function setExtendedBounds($min = null, $max = null) return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'histogram'; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { $out = array_filter( [ @@ -232,24 +140,14 @@ public function getArray() 'keyed' => $this->isKeyed(), 'order' => $this->getOrder(), ], - function ($val) { - return ($val || is_numeric($val)); - } + fn(mixed $val): bool => ($val || is_numeric($val)) ); $this->checkRequiredParameters($out, ['field', 'interval']); return $out; } - /** - * Checks if all required parameters are set. - * - * @param array $data - * @param array $required - * - * @throws \LogicException - */ - protected function checkRequiredParameters($data, $required) + protected function checkRequiredParameters(array $data, array $required): void { if (count(array_intersect_key(array_flip($required), $data)) !== count($required)) { throw new \LogicException('Histogram aggregation must have field and interval set.'); diff --git a/src/Aggregation/Bucketing/Ipv4RangeAggregation.php b/src/Aggregation/Bucketing/Ipv4RangeAggregation.php index 0579aaf8..60b8783f 100644 --- a/src/Aggregation/Bucketing/Ipv4RangeAggregation.php +++ b/src/Aggregation/Bucketing/Ipv4RangeAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,20 +25,11 @@ class Ipv4RangeAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var array - */ - private $ranges = []; - - /** - * Inner aggregations container init. - * - * @param string $name - * @param string $field - * @param array $ranges - */ - public function __construct($name, $field = null, $ranges = []) - { + public function __construct( + private string $name, + private ?string $field = null, + private array $ranges = [] + ) { parent::__construct($name); $this->setField($field); @@ -59,16 +52,13 @@ public function __construct($name, $field = null, $ranges = []) * * @return Ipv4RangeAggregation */ - public function addRange($from = null, $to = null) + public function addRange(?string $from = null, ?string $to = null): static { $range = array_filter( [ 'from' => $from, 'to' => $to, - ], - function ($v) { - return !is_null($v); - } + ] ); $this->ranges[] = $range; @@ -76,32 +66,19 @@ function ($v) { return $this; } - /** - * Add ip mask to aggregation. - * - * @param string $mask - * - * @return Ipv4RangeAggregation - */ - public function addMask($mask) + public function addMask(string $mask): static { $this->ranges[] = ['mask' => $mask]; return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'ip_range'; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { if ($this->getField() && !empty($this->ranges)) { return [ diff --git a/src/Aggregation/Bucketing/MissingAggregation.php b/src/Aggregation/Bucketing/MissingAggregation.php index 2cffa14b..e7867b13 100644 --- a/src/Aggregation/Bucketing/MissingAggregation.php +++ b/src/Aggregation/Bucketing/MissingAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,34 +25,23 @@ class MissingAggregation extends AbstractAggregation { use BucketingTrait; - /** - * Inner aggregations container init. - * - * @param string $name - * @param string $field - */ - public function __construct($name, $field = null) + public function __construct(private string $name, private ?string $field = null) { parent::__construct($name); $this->setField($field); } - /** - * {@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..0d188fc6 100644 --- a/src/Aggregation/Bucketing/NestedAggregation.php +++ b/src/Aggregation/Bucketing/NestedAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,58 +25,31 @@ class NestedAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var string - */ - private $path; - - /** - * Inner aggregations container init. - * - * @param string $name - * @param string $path - */ - public function __construct($name, $path = null) + public function __construct(private string $name, private ?string $path = null) { parent::__construct($name); $this->setPath($path); } - /** - * Return path. - * - * @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; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'nested'; } - /** - * {@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..2c82c2ce 100644 --- a/src/Aggregation/Bucketing/RangeAggregation.php +++ b/src/Aggregation/Bucketing/RangeAggregation.php @@ -1,5 +1,7 @@ setField($field); @@ -55,39 +43,20 @@ public function __construct($name, $field = null, $ranges = [], $keyed = false) } } - /** - * 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; return $this; } - /** - * 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); - } + ] ); if (!empty($key)) { @@ -99,15 +68,7 @@ function ($v) { return $this; } - /** - * 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(int|float|null $from, int|float|null $to): bool { foreach ($this->ranges as $key => $range) { if (array_diff_assoc(array_filter(['from' => $from, 'to' => $to]), $range) === []) { @@ -120,14 +81,7 @@ public function removeRange($from, $to) return false; } - /** - * 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) { @@ -142,10 +96,7 @@ public function removeRangeByKey($key) return false; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { $data = [ 'keyed' => $this->keyed, @@ -159,10 +110,7 @@ public function getArray() return $data; } - /** - * {@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..cc537c38 100644 --- a/src/Aggregation/Bucketing/ReverseNestedAggregation.php +++ b/src/Aggregation/Bucketing/ReverseNestedAggregation.php @@ -9,10 +9,13 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait; +use stdClass; /** * Class representing ReverseNestedAggregation. @@ -23,60 +26,33 @@ class ReverseNestedAggregation extends AbstractAggregation { use BucketingTrait; - /** - * @var string - */ - private $path; - - /** - * Inner aggregations container init. - * - * @param string $name - * @param string $path - */ - public function __construct($name, $path = null) + public function __construct(private string $name, private ?string $path = null) { parent::__construct($name); $this->setPath($path); } - /** - * Return path. - * - * @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; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'reverse_nested'; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): stdClass|array { - $output = new \stdClass(); + $output = new stdClass(); if ($this->getPath()) { $output = ['path' => $this->getPath()]; } diff --git a/src/Aggregation/Bucketing/SamplerAggregation.php b/src/Aggregation/Bucketing/SamplerAggregation.php index aa90b460..5b43657e 100644 --- a/src/Aggregation/Bucketing/SamplerAggregation.php +++ b/src/Aggregation/Bucketing/SamplerAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,67 +25,42 @@ class SamplerAggregation extends AbstractAggregation { use BucketingTrait; - /** - * Defines how many results will be received from each shard - * @param string $shardSize - */ - private $shardSize; - - /** - * Inner aggregations container init. - * - * @param string $name - * @param string $field - * @param int $shardSize - */ - public function __construct($name, $field = null, $shardSize = null) - { + public function __construct( + private string $name, + private ?string $field = null, + private ?int $shardSize = null + ) { parent::__construct($name); $this->setField($field); $this->setShardSize($shardSize); } - /** - * @return int - */ - public function getShardSize() + public function getShardSize(): ?int { return $this->shardSize; } - /** - * @param int $shardSize - * - * @return $this - */ - public function setShardSize($shardSize) + public function setShardSize(?int $shardSize): static { $this->shardSize = $shardSize; return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'sampler'; } - /** - * {@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..f83d7822 100644 --- a/src/Aggregation/Bucketing/SignificantTermsAggregation.php +++ b/src/Aggregation/Bucketing/SignificantTermsAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; /** @@ -18,10 +20,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..7aed7d6f 100644 --- a/src/Aggregation/Bucketing/SignificantTextAggregation.php +++ b/src/Aggregation/Bucketing/SignificantTextAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; /** @@ -18,10 +20,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..5b92dd74 100644 --- a/src/Aggregation/Bucketing/TermsAggregation.php +++ b/src/Aggregation/Bucketing/TermsAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,43 +25,32 @@ class TermsAggregation extends AbstractAggregation { use BucketingTrait; + use ScriptAwareTrait; - /** - * Inner aggregations container init. - * - * @param string $name - * @param string $field - * @param string $script - */ - public function __construct($name, $field = null, $script = null) - { + public function __construct( + private string $name, + private ?string $field = null, + ?string $script = null + ) { parent::__construct($name); $this->setField($field); $this->setScript($script); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'terms'; } - /** - * {@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..38205081 100644 --- a/src/Aggregation/Matrix/MatrixStatsAggregation.php +++ b/src/Aggregation/Matrix/MatrixStatsAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Matrix; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -19,86 +21,52 @@ * * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html */ -class MaxAggregation extends AbstractAggregation +class MatrixStatsAggregation extends AbstractAggregation { use MetricTrait; - /** - * @var string Used for multi value aggregation fields to pick a value. - */ - private $mode; - - /** - * @var array Defines how documents that are missing a value should be treated. - */ - private $missing; - - /** - * Inner aggregations container init. - * - * @param string $name - * @param string|array $field Fields list to aggregate. - * @param array $missing - * @param string $mode - */ - public function __construct($name, $field, $missing = null, $mode = null) - { + public function __construct( + private string $name, + private string|array $field, + private ?array $missing = null, + private ?string $mode = null + ) { parent::__construct($name); $this->setField($field); $this->setMode($mode); - $this->missing = $missing; } - /** - * @return string - */ - public function getMode() + public function getMode(): string { return $this->mode; } - /** - * @param string $mode - * - * @return $this - */ - public function setMode($mode) + public function setMode(?string $mode): static { $this->mode = $mode; return $this; } - /** - * @return array - */ - public function getMissing() + public function getMissing(): ?array { return $this->missing; } - /** - * @param array $missing - * - * @return $this - */ - public function setMissing($missing) + public function setMissing(?array $missing): static { $this->missing = $missing; return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'matrix_stats'; } - protected function getArray() + protected function getArray(): array { $out = []; if ($this->getField()) { diff --git a/src/Aggregation/Metric/AvgAggregation.php b/src/Aggregation/Metric/AvgAggregation.php index 412c6764..1f3df0ff 100644 --- a/src/Aggregation/Metric/AvgAggregation.php +++ b/src/Aggregation/Metric/AvgAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Metric; /** @@ -18,10 +20,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..1996584d 100644 --- a/src/Aggregation/Metric/CardinalityAggregation.php +++ b/src/Aggregation/Metric/CardinalityAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Metric; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,22 +25,17 @@ class CardinalityAggregation extends AbstractAggregation { use MetricTrait; + use ScriptAwareTrait; - /** - * @var int - */ - private $precisionThreshold; + private ?int $precisionThreshold = null; - /** - * @var bool - */ - private $rehash; + private ?bool $rehash = null; /** * {@inheritdoc} */ - public function getArray() + public function getArray(): array { $out = array_filter( [ @@ -47,9 +44,7 @@ public function getArray() 'precision_threshold' => $this->getPrecisionThreshold(), 'rehash' => $this->isRehash(), ], - function ($val) { - return ($val || is_bool($val)); - } + fn(mixed $val): bool => $val || is_bool($val) ); $this->checkRequiredFields($out); @@ -57,62 +52,36 @@ function ($val) { return $out; } - /** - * @param int $precision - * - * @return $this - */ - public function setPrecisionThreshold($precision) + public function setPrecisionThreshold(?int $precision): static { $this->precisionThreshold = $precision; return $this; } - /** - * @return int - */ - public function getPrecisionThreshold() + public function getPrecisionThreshold(): ?int { return $this->precisionThreshold; } - /** - * @return bool - */ - public function isRehash() + public function isRehash(): ?bool { return $this->rehash; } - /** - * @param bool $rehash - * - * @return $this - */ - public function setRehash($rehash) + public function setRehash(?bool $rehash): static { $this->rehash = $rehash; return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'cardinality'; } - /** - * Checks if required fields are set. - * - * @param array $fields - * - * @throws \LogicException - */ - private function checkRequiredFields($fields) + private function checkRequiredFields(array $fields): void { 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..cf6d320e 100644 --- a/src/Aggregation/Metric/ExtendedStatsAggregation.php +++ b/src/Aggregation/Metric/ExtendedStatsAggregation.php @@ -23,18 +23,15 @@ class ExtendedStatsAggregation extends AbstractAggregation { use MetricTrait; + use ScriptAwareTrait; - /** - * Inner aggregations container init. - * - * @param string $name - * @param string $field - * @param int $sigma - * @param string $script - */ - public function __construct($name, $field = null, $sigma = null, $script = null) - { + public function __construct( + private string $name, + private ?string $field = null, + private ?int $sigma = null, + private ?string $script = null + ) { parent::__construct($name); $this->setField($field); @@ -42,55 +39,32 @@ public function __construct($name, $field = null, $sigma = null, $script = null) $this->setScript($script); } - /** - * @var int - */ - private $sigma; - - /** - * @return int - */ - public function getSigma() + public function getSigma(): ?int { return $this->sigma; } - /** - * @param int $sigma - * - * @return $this - */ - public function setSigma($sigma) + public function setSigma(?int $sigma): static { $this->sigma = $sigma; return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'extended_stats'; } - /** - * {@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)); - } + fn(mixed $val): bool => $val || is_numeric($val) ); - - return $out; } } diff --git a/src/Aggregation/Metric/GeoBoundsAggregation.php b/src/Aggregation/Metric/GeoBoundsAggregation.php index 3f01a1dd..f1a2d323 100644 --- a/src/Aggregation/Metric/GeoBoundsAggregation.php +++ b/src/Aggregation/Metric/GeoBoundsAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Metric; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,19 +25,7 @@ class GeoBoundsAggregation extends AbstractAggregation { use MetricTrait; - /** - * @var bool - */ - private $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(private string $name, private ?string $field = null, private bool $wrapLongitude = true) { parent::__construct($name); @@ -43,30 +33,19 @@ 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; return $this; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { $data = []; if ($this->getField()) { @@ -80,10 +59,7 @@ public function getArray() return $data; } - /** - * {@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..b431572b 100644 --- a/src/Aggregation/Metric/GeoCentroidAggregation.php +++ b/src/Aggregation/Metric/GeoCentroidAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Metric; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,38 +25,27 @@ class GeoCentroidAggregation extends AbstractAggregation { use MetricTrait; - /** - * 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); $this->setField($field); } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { $data = []; + if ($this->getField()) { $data['field'] = $this->getField(); - } else { - throw new \LogicException('Geo centroid aggregation must have a field set.'); + + return $data; } - return $data; + throw new \LogicException('Geo centroid aggregation must have a field set.'); } - /** - * {@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..5be38ebc 100644 --- a/src/Aggregation/Metric/MaxAggregation.php +++ b/src/Aggregation/Metric/MaxAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Metric; /** @@ -18,10 +20,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..d2160756 100644 --- a/src/Aggregation/Metric/MinAggregation.php +++ b/src/Aggregation/Metric/MinAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Metric; /** @@ -18,10 +20,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..004e6641 100644 --- a/src/Aggregation/Metric/PercentileRanksAggregation.php +++ b/src/Aggregation/Metric/PercentileRanksAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Metric; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,29 +25,16 @@ class PercentileRanksAggregation extends AbstractAggregation { use MetricTrait; + use ScriptAwareTrait; - /** - * @var array - */ - private $values; - - /** - * @var int - */ - private $compression; - - /** - * Inner aggregations container init. - * - * @param string $name - * @param string $field - * @param array $values - * @param string $script - * @param int $compression - */ - public function __construct($name, $field = null, $values = null, $script = null, $compression = null) - { + public function __construct( + private string $name, + private ?string $field = null, + private ?array $values = null, + ?string $script = null, + ?int $compression = null + ) { parent::__construct($name); $this->setField($field); @@ -54,58 +43,36 @@ public function __construct($name, $field = null, $values = null, $script = null $this->setCompression($compression); } - /** - * @return array - */ - public function getValues() + public function getValues(): ?array { return $this->values; } - /** - * @param array $values - * - * @return $this - */ - public function setValues($values) + public function setValues(?array $values): static { $this->values = $values; return $this; } - /** - * @return int - */ - public function getCompression() + public function getCompression(): ?int { return $this->compression; } - /** - * @param int $compression - * - * @return $this - */ - public function setCompression($compression) + public function setCompression(?int $compression): static { $this->compression = $compression; return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'percentile_ranks'; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { $out = array_filter( [ @@ -114,9 +81,7 @@ public function getArray() 'values' => $this->getValues(), 'compression' => $this->getCompression(), ], - function ($val) { - return ($val || is_numeric($val)); - } + fn(mixed $val): bool => $val || is_numeric($val) ); $this->isRequiredParametersSet($out); @@ -124,18 +89,12 @@ function ($val) { return $out; } - /** - * @param array $a - * - * @return bool - * @throws \LogicException - */ - private function isRequiredParametersSet($a) + private function isRequiredParametersSet(array $a): void { if (array_key_exists('field', $a) && array_key_exists('values', $a) || (array_key_exists('script', $a) && array_key_exists('values', $a)) ) { - return true; + return; } 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..f9ddfc28 100644 --- a/src/Aggregation/Metric/PercentilesAggregation.php +++ b/src/Aggregation/Metric/PercentilesAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Metric; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,29 +25,16 @@ class PercentilesAggregation extends AbstractAggregation { use MetricTrait; + use ScriptAwareTrait; - /** - * @var array - */ - private $percents; - - /** - * @var int - */ - private $compression; - - /** - * Inner aggregations container init. - * - * @param string $name - * @param string $field - * @param array $percents - * @param string $script - * @param int $compression - */ - public function __construct($name, $field = null, $percents = null, $script = null, $compression = null) - { + public function __construct( + private string $name, + private ?string $field = null, + private ?array $percents = null, + ?string $script = null, + ?int $compression = null + ) { parent::__construct($name); $this->setField($field); @@ -54,58 +43,36 @@ public function __construct($name, $field = null, $percents = null, $script = nu $this->setCompression($compression); } - /** - * @return array - */ - public function getPercents() + public function getPercents(): ?array { return $this->percents; } - /** - * @param array $percents - * - * @return $this - */ - public function setPercents($percents) + public function setPercents(?array $percents): static { $this->percents = $percents; return $this; } - /** - * @return int - */ - public function getCompression() + public function getCompression(): ?int { return $this->compression; } - /** - * @param int $compression - * - * @return $this - */ - public function setCompression($compression) + public function setCompression(?int $compression): static { $this->compression = $compression; return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'percentiles'; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { $out = array_filter( [ @@ -114,9 +81,7 @@ public function getArray() 'field' => $this->getField(), 'script' => $this->getScript(), ], - function ($val) { - return ($val || is_numeric($val)); - } + fn(mixed $val): bool => $val || is_numeric($val) ); $this->isRequiredParametersSet($out); @@ -124,12 +89,7 @@ function ($val) { return $out; } - /** - * @param array $a - * - * @throws \LogicException - */ - private function isRequiredParametersSet($a) + private function isRequiredParametersSet(array $a): void { 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..18583c88 100644 --- a/src/Aggregation/Metric/ScriptedMetricAggregation.php +++ b/src/Aggregation/Metric/ScriptedMetricAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Metric; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -24,39 +26,12 @@ class ScriptedMetricAggregation extends AbstractAggregation { use MetricTrait; - /** - * @var mixed - */ - private $initScript; - - /** - * @var mixed - */ - private $mapScript; - - /** - * @var mixed - */ - private $combineScript; - - /** - * @var mixed - */ - private $reduceScript; - /** - * ScriptedMetricAggregation constructor. - * @param string $name - * @param mixed $initScript - * @param mixed $mapScript - * @param mixed $combineScript - * @param mixed $reduceScript - */ public function __construct( - $name, - $initScript = null, - $mapScript = null, - $combineScript = null, - $reduceScript = null + private string $name, + private mixed $initScript = null, + private mixed $mapScript = null, + private mixed $combineScript = null, + private mixed $reduceScript = null ) { parent::__construct($name); @@ -67,100 +42,63 @@ public function __construct( $this->setReduceScript($reduceScript); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'scripted_metric'; } - /** - * @return mixed - */ - public function getInitScript() + public function getInitScript(): mixed { return $this->initScript; } - /** - * @param mixed $initScript - * - * @return $this - */ - public function setInitScript($initScript) + public function setInitScript(mixed $initScript): static { $this->initScript = $initScript; return $this; } - /** - * @return mixed - */ - public function getMapScript() + public function getMapScript(): mixed { return $this->mapScript; } - /** - * @param mixed $mapScript - * - * @return $this - */ - public function setMapScript($mapScript) + public function setMapScript(mixed $mapScript): static { $this->mapScript = $mapScript; return $this; } - /** - * @return mixed - */ - public function getCombineScript() + public function getCombineScript(): mixed { return $this->combineScript; } - /** - * @param mixed $combineScript - * - * @return $this - */ - public function setCombineScript($combineScript) + public function setCombineScript(mixed $combineScript): static { $this->combineScript = $combineScript; return $this; } - /** - * @return mixed - */ - public function getReduceScript() + public function getReduceScript(): mixed { return $this->reduceScript; } - /** - * @param mixed $reduceScript - * - * @return $this - */ - public function setReduceScript($reduceScript) + public function setReduceScript(mixed $reduceScript): static { $this->reduceScript = $reduceScript; return $this; } - /** - * {@inheritdoc} - */ - public function getArray() + + public function getArray(): array { - $out = array_filter( + return array_filter( [ 'init_script' => $this->getInitScript(), 'map_script' => $this->getMapScript(), @@ -168,7 +106,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..aec861f3 100644 --- a/src/Aggregation/Metric/StatsAggregation.php +++ b/src/Aggregation/Metric/StatsAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Metric; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -23,40 +25,33 @@ class StatsAggregation extends AbstractAggregation { use MetricTrait; + use ScriptAwareTrait; - /** - * Inner aggregations container init. - * - * @param string $name - * @param string $field - * @param string $script - */ - public function __construct($name, $field = null, $script = null) - { + public function __construct( + private string $name, + private ?string $field = null, + ?string $script = null + ) { parent::__construct($name); $this->setField($field); $this->setScript($script); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'stats'; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { $out = []; + if ($this->getField()) { $out['field'] = $this->getField(); } + if ($this->getScript()) { $out['script'] = $this->getScript(); } diff --git a/src/Aggregation/Metric/SumAggregation.php b/src/Aggregation/Metric/SumAggregation.php index ba2ddbc5..1589c07c 100644 --- a/src/Aggregation/Metric/SumAggregation.php +++ b/src/Aggregation/Metric/SumAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Metric; /** @@ -18,10 +20,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..b0eeae2e 100644 --- a/src/Aggregation/Metric/TopHitsAggregation.php +++ b/src/Aggregation/Metric/TopHitsAggregation.php @@ -9,11 +9,14 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Metric; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; use ONGR\ElasticsearchDSL\Aggregation\Type\MetricTrait; use ONGR\ElasticsearchDSL\BuilderInterface; +use stdClass; /** * Top hits aggregation. @@ -24,123 +27,67 @@ class TopHitsAggregation extends AbstractAggregation { use MetricTrait; - /** - * @var int Number of top matching hits to return per bucket. - */ - private $size; - - /** - * @var int The offset from the first result you want to fetch. - */ - private $from; - - /** - * @var BuilderInterface[] How the top matching hits should be sorted. - */ - private $sorts = []; - - /** - * Constructor for top hits. - * - * @param string $name Aggregation name. - * @param null|int $size Number of top matching hits to return per bucket. - * @param null|int $from The offset from the first result you want to fetch. - * @param null|BuilderInterface $sort How the top matching hits should be sorted. - */ - public function __construct($name, $size = null, $from = null, $sort = null) - { + private array $sorts = []; + + public function __construct( + private string $name, + private ?int $size = null, + private ?int $from = null, + ?BuilderInterface $sort = null + ) { parent::__construct($name); $this->setFrom($from); $this->setSize($size); $this->addSort($sort); } - /** - * Return from. - * - * @return int - */ - public function getFrom() + public function getFrom(): ?int { return $this->from; } - /** - * @param int $from - * - * @return $this - */ - public function setFrom($from) + public function setFrom(?int $from): static { $this->from = $from; return $this; } - /** - * @return BuilderInterface[] - */ - public function getSorts() + public function getSorts(): array { return $this->sorts; } - /** - * @param BuilderInterface[] $sorts - * - * @return $this - */ - public function setSorts(array $sorts) + public function setSorts(array $sorts): 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; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'top_hits'; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array|stdClass { $sortsOutput = []; $addedSorts = array_filter($this->getSorts()); @@ -148,8 +95,6 @@ public function getArray() foreach ($addedSorts as $sort) { $sortsOutput[] = $sort->toArray(); } - } else { - $sortsOutput = null; } $output = array_filter( @@ -158,21 +103,13 @@ public function getArray() 'size' => $this->getSize(), 'from' => $this->getFrom(), ], - function ($val) { - return (($val || is_array($val) || ($val || is_numeric($val)))); - } + fn(mixed $val): bool => (($val || is_array($val) || ($val || is_numeric($val)))) ); - return empty($output) ? new \stdClass() : $output; + return $output ?: new stdClass(); } - /** - * @deprecated sorts now is a container, use `getSorts()`instead. - * Return sort. - * - * @return BuilderInterface - */ - public function getSort() + public function getSort(): ?BuilderInterface { if (isset($this->sorts[0])) { return $this->sorts[0]; @@ -181,14 +118,7 @@ public function getSort() return null; } - /** - * @deprecated sorts now is a container, use `addSort()`instead. - * - * @param BuilderInterface $sort - * - * @return $this - */ - public function setSort(BuilderInterface $sort) + public function setSort(BuilderInterface $sort): static { $this->sort = $sort; diff --git a/src/Aggregation/Metric/ValueCountAggregation.php b/src/Aggregation/Metric/ValueCountAggregation.php index 617a966e..38d437e5 100644 --- a/src/Aggregation/Metric/ValueCountAggregation.php +++ b/src/Aggregation/Metric/ValueCountAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Metric; /** @@ -18,10 +20,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..cf7e3e95 100644 --- a/src/Aggregation/Pipeline/AbstractPipelineAggregation.php +++ b/src/Aggregation/Pipeline/AbstractPipelineAggregation.php @@ -1,5 +1,7 @@ setBucketsPath($bucketsPath); } - /** - * @return string - */ - public function getBucketsPath() + public function getBucketsPath(): mixed { return $this->bucketsPath; } - /** - * @param string $bucketsPath - * - * @return $this - */ - public function setBucketsPath($bucketsPath) + public function setBucketsPath(mixed $bucketsPath): static { $this->bucketsPath = $bucketsPath; return $this; } - /** - * {@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..86f769be 100644 --- a/src/Aggregation/Pipeline/AvgBucketAggregation.php +++ b/src/Aggregation/Pipeline/AvgBucketAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; /** @@ -18,10 +20,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..8d3d28e7 100644 --- a/src/Aggregation/Pipeline/BucketScriptAggregation.php +++ b/src/Aggregation/Pipeline/BucketScriptAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; /** @@ -18,54 +20,33 @@ */ class BucketScriptAggregation extends AbstractPipelineAggregation { - /** - * @var string - */ - private $script; - - /** - * @param string $name - * @param array $bucketsPath - * @param string $script - */ - public function __construct($name, $bucketsPath, $script = null) - { + public function __construct( + private string $name, + private ?array $bucketsPath, + private ?string $script = null + ) { parent::__construct($name, $bucketsPath); $this->setScript($script); } - /** - * @return string - */ - public function getScript() + public function getScript(): ?string { return $this->script; } - /** - * @param string $script - * - * @return $this - */ - public function setScript($script) + public function setScript(?string $script): static { $this->script = $script; return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'bucket_script'; } - /** - * {@inheritdoc} - */ - public function getArray() + public function getArray(): array { if (!$this->getScript()) { throw new \LogicException( @@ -76,11 +57,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..a6e69022 100644 --- a/src/Aggregation/Pipeline/BucketSelectorAggregation.php +++ b/src/Aggregation/Pipeline/BucketSelectorAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; /** @@ -18,10 +20,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..ca25b596 100644 --- a/src/Aggregation/Pipeline/BucketSortAggregation.php +++ b/src/Aggregation/Pipeline/BucketSortAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -21,68 +23,42 @@ */ 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, ?string $bucketsPath = null) { parent::__construct($name, $bucketsPath); } - /** - * @return array - */ - public function getSort() + public function getSort(): array { return $this->sort; } - /** - * @return self - */ - public function addSort(FieldSort $sort) + public function addSort(FieldSort $sort): void { $this->sort[] = $sort->toArray(); } - /** - * @param string $sort - * - * @return $this - */ - public function setSort($sort) + public function setSort(array $sort): static { $this->sort = $sort; return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'bucket_sort'; } - /** - * {@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..d5a3c80a 100644 --- a/src/Aggregation/Pipeline/CumulativeSumAggregation.php +++ b/src/Aggregation/Pipeline/CumulativeSumAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; /** @@ -18,10 +20,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..6ea90780 100644 --- a/src/Aggregation/Pipeline/DerivativeAggregation.php +++ b/src/Aggregation/Pipeline/DerivativeAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; /** @@ -18,10 +20,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..2b7d3e63 100644 --- a/src/Aggregation/Pipeline/ExtendedStatsBucketAggregation.php +++ b/src/Aggregation/Pipeline/ExtendedStatsBucketAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; /** @@ -18,10 +20,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..abbb915e 100644 --- a/src/Aggregation/Pipeline/MaxBucketAggregation.php +++ b/src/Aggregation/Pipeline/MaxBucketAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; /** @@ -18,10 +20,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..a1c3393f 100644 --- a/src/Aggregation/Pipeline/MinBucketAggregation.php +++ b/src/Aggregation/Pipeline/MinBucketAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; /** @@ -18,10 +20,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..fc41e9cf 100644 --- a/src/Aggregation/Pipeline/MovingAverageAggregation.php +++ b/src/Aggregation/Pipeline/MovingAverageAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; /** @@ -18,10 +20,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..33fa1792 100644 --- a/src/Aggregation/Pipeline/MovingFunctionAggregation.php +++ b/src/Aggregation/Pipeline/MovingFunctionAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; /** @@ -18,10 +20,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..59b4a9a4 100644 --- a/src/Aggregation/Pipeline/PercentilesBucketAggregation.php +++ b/src/Aggregation/Pipeline/PercentilesBucketAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; /** @@ -18,43 +20,26 @@ */ class PercentilesBucketAggregation extends AbstractPipelineAggregation { - /** - * @var array - */ - private $percents; + private array $percents; - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'percentiles_bucket'; } - /** - * @return array - */ - public function getPercents() + public function getPercents(): array { return $this->percents; } - /** - * @param array $percents - * - * @return $this - */ - public function setPercents(array $percents) + public function setPercents(array $percents): static { $this->percents = $percents; return $this; } - /** - * {@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..1710bfaf 100644 --- a/src/Aggregation/Pipeline/SerialDifferencingAggregation.php +++ b/src/Aggregation/Pipeline/SerialDifferencingAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; /** @@ -18,10 +20,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..5107937d 100644 --- a/src/Aggregation/Pipeline/StatsBucketAggregation.php +++ b/src/Aggregation/Pipeline/StatsBucketAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; /** @@ -18,10 +20,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..fed6dd2f 100644 --- a/src/Aggregation/Pipeline/SumBucketAggregation.php +++ b/src/Aggregation/Pipeline/SumBucketAggregation.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; /** @@ -18,10 +20,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..9bae4acf 100644 --- a/src/Aggregation/Type/BucketingTrait.php +++ b/src/Aggregation/Type/BucketingTrait.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Type; /** @@ -16,12 +18,7 @@ */ 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..3c696b15 100644 --- a/src/Aggregation/Type/MetricTrait.php +++ b/src/Aggregation/Type/MetricTrait.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Aggregation\Type; /** @@ -16,12 +18,7 @@ */ 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..5990d4b2 100644 --- a/src/BuilderBag.php +++ b/src/BuilderBag.php @@ -9,41 +9,27 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL; -/** - * Container for named builders. - */ class BuilderBag { - /** - * @var BuilderInterface[] - */ - private $bag = []; - - /** - * @param BuilderInterface[] $builders - */ - public function __construct($builders = []) + private array $bag = []; + + public function __construct(array $builders = []) { foreach ($builders as $builder) { $this->add($builder); } } - /** - * Adds a builder. - * - * @param BuilderInterface $builder - * - * @return string - */ - public function add(BuilderInterface $builder) + public function add(BuilderInterface $builder): ?string { + $name = bin2hex(random_bytes(30)); + if (method_exists($builder, 'getName')) { $name = $builder->getName(); - } else { - $name = bin2hex(random_bytes(30)); } $this->bag[$name] = $builder; @@ -51,56 +37,27 @@ public function add(BuilderInterface $builder) return $name; } - /** - * Checks if builder exists by a specific name. - * - * @param string $name Builder name. - * - * @return bool - */ - public function has($name) + public function has(string $name): bool { return isset($this->bag[$name]); } - /** - * Removes a builder by name. - * - * @param string $name Builder name. - */ - public function remove($name) + public function remove(string $name): void { unset($this->bag[$name]); } - /** - * Clears contained builders. - */ - public function clear() + public function clear(): void { $this->bag = []; } - /** - * Returns a builder by name. - * - * @param string $name Builder name. - * - * @return BuilderInterface - */ - public function get($name) + public function get(string $name): BuilderInterface { return $this->bag[$name]; } - /** - * Returns all builders contained. - * - * @param string|null $type Builder type. - * - * @return BuilderInterface[] - */ - public function all($type = null) + public function all(?string $type = null): array { return array_filter( $this->bag, @@ -111,10 +68,7 @@ function (BuilderInterface $builder) use ($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..927c8ef7 100644 --- a/src/BuilderInterface.php +++ b/src/BuilderInterface.php @@ -9,24 +9,15 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL; -/** - * Interface BuilderInterface. - */ +use stdClass; + interface BuilderInterface { - /** - * Generates array which will be passed to elasticsearch-php client. - * - * @return array - */ - public function toArray(); + public function toArray(): array | stdClass; - /** - * Returns element type. - * - * @return string - */ - public function getType(); + public function getType(): string; } diff --git a/src/FieldAwareTrait.php b/src/FieldAwareTrait.php index 5cfaabc6..c9e00fee 100644 --- a/src/FieldAwareTrait.php +++ b/src/FieldAwareTrait.php @@ -9,29 +9,20 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL; trait FieldAwareTrait { - /** - * @var string - */ - private $field; + private ?string $field = null; - /** - * @return mixed - */ - 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; diff --git a/src/Highlight/Highlight.php b/src/Highlight/Highlight.php index 171e6a16..83f43b29 100644 --- a/src/Highlight/Highlight.php +++ b/src/Highlight/Highlight.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Highlight; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -21,38 +23,18 @@ class Highlight implements BuilderInterface { use ParametersTrait; - /** - * @var array Holds fields for highlight. - */ - private $fields = []; - - /** - * @var array - */ - private $tags; - - /** - * @param string $name Field name to highlight. - * @param array $params - * - * @return $this - */ - public function addField($name, array $params = []) + private array $fields = []; + + private ?array $tags = null; + + public function addField(string $name, array $params = []): static { $this->fields[$name] = $params; return $this; } - /** - * 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; @@ -60,18 +42,12 @@ public function setTags(array $preTags, array $postTags) return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'highlight'; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array { $output = []; diff --git a/src/InnerHit/NestedInnerHit.php b/src/InnerHit/NestedInnerHit.php index e52ca710..38f43b2a 100644 --- a/src/InnerHit/NestedInnerHit.php +++ b/src/InnerHit/NestedInnerHit.php @@ -9,12 +9,15 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\InnerHit; use ONGR\ElasticsearchDSL\NameAwareTrait; use ONGR\ElasticsearchDSL\NamedBuilderInterface; use ONGR\ElasticsearchDSL\ParametersTrait; use ONGR\ElasticsearchDSL\Search; +use stdClass; /** * Represents Elasticsearch top level nested inner hits. @@ -24,26 +27,10 @@ class NestedInnerHit implements NamedBuilderInterface { use ParametersTrait; + use NameAwareTrait; - /** - * @var string - */ - private $path; - - /** - * @var Search - */ - private $search; - - /** - * 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 = null, private ?string $path = null, private ?Search $search = null) { $this->setName($name); $this->setPath($path); @@ -52,87 +39,56 @@ public function __construct($name, $path, Search $search = null) } } - /** - * @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; return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'nested'; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array { - $out = $this->getSearch() ? $this->getSearch()->toArray() : new \stdClass(); + $out = new stdClass(); + + if (null !== $this->getSearch()) { + $out = $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..60fcd58b 100644 --- a/src/InnerHit/ParentInnerHit.php +++ b/src/InnerHit/ParentInnerHit.php @@ -1,13 +1,12 @@ 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..3712c68f 100644 --- a/src/NamedBuilderInterface.php +++ b/src/NamedBuilderInterface.php @@ -9,17 +9,11 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL; -/** - * Interface NamedBuilderInterface. - */ interface NamedBuilderInterface extends BuilderInterface { - /** - * Returns element name. - * - * @return string - */ - public function getName(); + public function getName(): ?string; } diff --git a/src/ParametersTrait.php b/src/ParametersTrait.php index 013b4b6d..bf5c47b2 100644 --- a/src/ParametersTrait.php +++ b/src/ParametersTrait.php @@ -9,93 +9,49 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL; -/** - * A trait which handles the behavior of parameters in queries, filters, etc. - */ 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]); } } - /** - * 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]; } - /** - * 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; return $this; } - /** - * 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..0d4caf33 100644 --- a/src/Query/Compound/BoolQuery.php +++ b/src/Query/Compound/BoolQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Compound; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,40 +25,27 @@ class BoolQuery implements BuilderInterface { use ParametersTrait; - const MUST = 'must'; - const MUST_NOT = 'must_not'; - const SHOULD = 'should'; - const FILTER = 'filter'; - - /** - * @var array - */ - private $container = []; - - /** - * Constructor to prepare container. - * - * @param array $container - */ - public function __construct(array $container = []) + public const MUST = 'must'; + + public const MUST_NOT = 'must_not'; + public const SHOULD = 'should'; + public const FILTER = 'filter'; + + public function __construct(private array $container = []) { foreach ($container as $type => $queries) { $queries = is_array($queries) ? $queries : [$queries]; - array_walk($queries, function ($query) use ($type) { - $this->add($query, $type); - }); + array_walk( + $queries, + function (BuilderInterface $query) use ($type): void { + $this->add($query, $type); + } + ); } } - /** - * Returns the query instances (by bool type). - * - * @param string|null $boolType - * - * @return array - */ - public function getQueries($boolType = null) + public function getQueries(?string $boolType = null): array { if ($boolType === null) { $queries = []; @@ -75,18 +64,7 @@ public function getQueries($boolType = null) return []; } - /** - * Add BuilderInterface object to bool operator. - * - * @param BuilderInterface $query Query add to the bool. - * @param string $type Bool type. Example: must, must_not, should. - * @param string $key Key that indicates a builder id. - * - * @return string Key of added builder. - * - * @throws \UnexpectedValueException - */ - public function add(BuilderInterface $query, $type = self::MUST, $key = null) + public function add(BuilderInterface $query, string $type = self::MUST, ?string $key = null): string { if (!in_array($type, [self::MUST, self::MUST_NOT, self::SHOULD, self::FILTER])) { throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type)); @@ -101,13 +79,10 @@ public function add(BuilderInterface $query, $type = self::MUST, $key = null) return $key; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array { if (count($this->container) === 1 && isset($this->container[self::MUST]) - && count($this->container[self::MUST]) === 1) { + && count($this->container[self::MUST]) === 1) { $query = reset($this->container[self::MUST]); return $query->toArray(); @@ -131,10 +106,7 @@ public function toArray() return [$this->getType() => $output]; } - /** - * {@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..03f37d54 100644 --- a/src/Query/Compound/BoostingQuery.php +++ b/src/Query/Compound/BoostingQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Compound; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -20,45 +22,20 @@ */ class BoostingQuery implements BuilderInterface { - /** - * @var BuilderInterface - */ - private $positive; - - /** - * @var BuilderInterface - */ - private $negative; + public function __construct( + private BuilderInterface $positive, + private BuilderInterface $negative, + private int|float $negativeBoost + ) { - /** - * @var int|float - */ - private $negativeBoost; - - /** - * @param BuilderInterface $positive - * @param BuilderInterface $negative - * @param int|float $negativeBoost - */ - public function __construct(BuilderInterface $positive, BuilderInterface $negative, $negativeBoost) - { - $this->positive = $positive; - $this->negative = $negative; - $this->negativeBoost = $negativeBoost; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'boosting'; } - /** - * {@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..9fcd624e 100644 --- a/src/Query/Compound/ConstantScoreQuery.php +++ b/src/Query/Compound/ConstantScoreQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Compound; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,33 +25,19 @@ class ConstantScoreQuery implements BuilderInterface { use ParametersTrait; - /** - * @var BuilderInterface - */ - private $query; - - /** - * @param BuilderInterface $query - * @param array $parameters - */ - public function __construct(BuilderInterface $query, array $parameters = []) - { - $this->query = $query; + public function __construct( + private BuilderInterface $query, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'constant_score'; } - /** - * {@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..e25f450e 100644 --- a/src/Query/Compound/DisMaxQuery.php +++ b/src/Query/Compound/DisMaxQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Compound; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,47 +25,26 @@ class DisMaxQuery implements BuilderInterface { use ParametersTrait; - /** - * @var BuilderInterface[] - */ - private $queries = []; + private array $queries = []; - /** - * Initializes Dis Max query. - * - * @param array $parameters - */ public function __construct(array $parameters = []) { $this->setParameters($parameters); } - /** - * Add query. - * - * @param BuilderInterface $query - * - * @return DisMaxQuery - */ - public function addQuery(BuilderInterface $query) + public function addQuery(BuilderInterface $query): static { $this->queries[] = $query; return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'dis_max'; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array { $query = []; foreach ($this->queries as $type) { diff --git a/src/Query/Compound/FunctionScoreQuery.php b/src/Query/Compound/FunctionScoreQuery.php index e7d96c1b..e131f853 100644 --- a/src/Query/Compound/FunctionScoreQuery.php +++ b/src/Query/Compound/FunctionScoreQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Compound; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,60 +25,34 @@ class FunctionScoreQuery implements BuilderInterface { use ParametersTrait; - /** - * @var BuilderInterface - */ - private $query; - - /** - * @var array[] - */ - private $functions; - - /** - * @param BuilderInterface $query - * @param array $parameters - */ - public function __construct(BuilderInterface $query, array $parameters = []) + private array $functions; + + public function __construct(private ?BuilderInterface $query = null, array $parameters = []) { - $this->query = $query; $this->setParameters($parameters); } - /** - * Returns the query instance. - * - * @return BuilderInterface object - */ - public function getQuery() + public function getQuery(): ?BuilderInterface { return $this->query; } - /** - * Creates field_value_factor function. - * - * @param string $field - * @param float $factor - * @param string $modifier - * @param BuilderInterface $query - * @param mixed $missing - * @return $this - */ public function addFieldValueFactorFunction( - $field, - $factor, - $modifier = 'none', + string $field, + float $factor, + string $modifier = 'none', BuilderInterface $query = null, - $missing = null - ) { + mixed $missing = null + ): static { $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); @@ -86,39 +62,21 @@ public function addFieldValueFactorFunction( return $this; } - /** - * 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) { $function['filter'] = $query->toArray(); } } - /** - * 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 - * - * @return $this - */ public function addDecayFunction( - $type, - $field, + string $type, + string $field, array $function, array $options = [], BuilderInterface $query = null, - $weight = null - ) { + int $weight = null + ): static { $function = array_filter( [ $type => array_merge( @@ -136,15 +94,7 @@ public function addDecayFunction( return $this; } - /** - * Adds function to function score without decay function. Influence search score only for specific query. - * - * @param float $weight - * @param BuilderInterface $query - * - * @return $this - */ - public function addWeightFunction($weight, BuilderInterface $query = null) + public function addWeightFunction(float $weight, BuilderInterface $query = null): static { $function = [ 'weight' => $weight, @@ -157,18 +107,10 @@ public function addWeightFunction($weight, BuilderInterface $query = null) return $this; } - /** - * 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): static { $function = [ - 'random_score' => $seed ? [ 'seed' => $seed ] : new \stdClass(), + 'random_score' => $seed ? ['seed' => $seed] : new \stdClass(), ]; $this->applyFilter($function, $query); @@ -178,22 +120,12 @@ public function addRandomFunction($seed = null, BuilderInterface $query = null) return $this; } - /** - * Adds script score function. - * - * @param string $inline - * @param array $params - * @param array $options - * @param BuilderInterface $query - * - * @return $this - */ public function addScriptScoreFunction( - $inline, + string $inline, array $params = [], array $options = [], BuilderInterface $query = null - ) { + ): static { $function = [ 'script_score' => [ 'script' => @@ -216,24 +148,14 @@ public function addScriptScoreFunction( return $this; } - /** - * Adds custom simple function. You can add to the array whatever you want. - * - * @param array $function - * - * @return $this - */ - public function addSimpleFunction(array $function) + public function addSimpleFunction(array $function): static { $this->functions[] = $function; return $this; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array { $query = [ 'query' => $this->query->toArray(), @@ -245,10 +167,7 @@ public function toArray() return [$this->getType() => $output]; } - /** - * {@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..2c16234e 100644 --- a/src/Query/FullText/CommonTermsQuery.php +++ b/src/Query/FullText/CommonTermsQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\FullText; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,40 +25,20 @@ 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 = []) - { - $this->field = $field; - $this->query = $query; + public function __construct( + private string $field, + private string $query, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'common'; } - /** - * {@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..4a4f312a 100644 --- a/src/Query/FullText/MatchPhrasePrefixQuery.php +++ b/src/Query/FullText/MatchPhrasePrefixQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\FullText; /** @@ -22,7 +24,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..7d89d288 100644 --- a/src/Query/FullText/MatchPhraseQuery.php +++ b/src/Query/FullText/MatchPhraseQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\FullText; /** @@ -22,7 +24,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..b2fc61a0 100644 --- a/src/Query/FullText/MatchQuery.php +++ b/src/Query/FullText/MatchQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\FullText; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,40 +25,20 @@ 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 = []) - { - $this->field = $field; - $this->query = $query; + public function __construct( + private string $field, + private string $query, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'match'; } - /** - * {@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..b6373f9f 100644 --- a/src/Query/FullText/MultiMatchQuery.php +++ b/src/Query/FullText/MultiMatchQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\FullText; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -29,40 +31,20 @@ 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 = []) - { - $this->fields = $fields; - $this->query = $query; + public function __construct( + private array $fields, + private string $query, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'multi_match'; } - /** - * {@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..2e16553d 100644 --- a/src/Query/FullText/QueryStringQuery.php +++ b/src/Query/FullText/QueryStringQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\FullText; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,33 +25,17 @@ 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 string $query, array $parameters = []) { - $this->query = $query; $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'query_string'; } - /** - * {@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..5df54a70 100644 --- a/src/Query/FullText/SimpleQueryStringQuery.php +++ b/src/Query/FullText/SimpleQueryStringQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\FullText; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,33 +25,17 @@ 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 string $query, array $parameters = []) { - $this->query = $query; $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'simple_query_string'; } - /** - * {@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..14b0bc82 100644 --- a/src/Query/Geo/GeoBoundingBoxQuery.php +++ b/src/Query/Geo/GeoBoundingBoxQuery.php @@ -23,52 +23,27 @@ 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 = []) - { - $this->field = $field; - $this->values = $values; + public function __construct( + private string $field, + private array $values, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'geo_bounding_box'; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array { return [ $this->getType() => $this->processArray([$this->field => $this->points()]) ]; } - /** - * @return array - * - * @throws \LogicException - */ - private function points() + private function points(): array { if (count($this->values) === 2) { return [ diff --git a/src/Query/Geo/GeoDistanceQuery.php b/src/Query/Geo/GeoDistanceQuery.php index 03cf2021..68bad2eb 100644 --- a/src/Query/Geo/GeoDistanceQuery.php +++ b/src/Query/Geo/GeoDistanceQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Geo; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,48 +25,21 @@ 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 = []) - { - $this->field = $field; - $this->distance = $distance; - $this->location = $location; - + public function __construct( + private string $field, + private string $distance, + private mixed $location, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'geo_distance'; } - /** - * {@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..753ad511 100644 --- a/src/Query/Geo/GeoPolygonQuery.php +++ b/src/Query/Geo/GeoPolygonQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Geo; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,40 +25,20 @@ 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 = []) - { - $this->field = $field; - $this->points = $points; + public function __construct( + private string $field, + private array $points = [], + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'geo_polygon'; } - /** - * {@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..d6a959e0 100644 --- a/src/Query/Geo/GeoShapeQuery.php +++ b/src/Query/Geo/GeoShapeQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Geo; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,50 +25,33 @@ class GeoShapeQuery implements BuilderInterface { use ParametersTrait; - const INTERSECTS = 'intersects'; - const DISJOINT = 'disjoint'; - const WITHIN = 'within'; - const CONTAINS = 'contains'; + public const INTERSECTS = 'intersects'; + + public const DISJOINT = 'disjoint'; + + public const WITHIN = 'within'; + + public const CONTAINS = 'contains'; - /** - * @var array - */ - private $fields = []; + private array $fields = []; - /** - * @param array $parameters - */ public function __construct(array $parameters = []) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'geo_shape'; } - /** - * Add geo-shape provided filter. - * - * @param string $field Field name. - * @param string $type Shape type. - * @param array $coordinates Shape coordinates. - * @param string $relation Spatial relation. - * @param array $parameters Additional parameters. - */ - public function addShape($field, $type, array $coordinates, $relation = self::INTERSECTS, array $parameters = []) - { - // TODO: remove this in the next major version - if (is_array($relation)) { - $parameters = $relation; - $relation = self::INTERSECTS; - trigger_error('$parameters as parameter 4 in addShape is deprecated', E_USER_DEPRECATED); - } - + public function addShape( + string $field, + string $type, + array $coordinates, + string $relation = self::INTERSECTS, + array $parameters = [] + ): void { $filter = array_merge( $parameters, [ @@ -81,33 +66,15 @@ public function addShape($field, $type, array $coordinates, $relation = self::IN ]; } - /** - * Add geo-shape pre-indexed filter. - * - * @param string $field Field name. - * @param string $id The ID of the document that containing the pre-indexed shape. - * @param string $type Name of the index where the pre-indexed shape is. - * @param string $index Index type where the pre-indexed shape is. - * @param string $relation Spatial relation. - * @param string $path The field specified as path containing the pre-indexed shape. - * @param array $parameters Additional parameters. - */ public function addPreIndexedShape( - $field, - $id, - $type, - $index, - $path, - $relation = self::INTERSECTS, + string $field, + string $id, + string $type, + string $index, + string $path, + string $relation = self::INTERSECTS, array $parameters = [] - ) { - // TODO: remove this in the next major version - if (is_array($relation)) { - $parameters = $relation; - $relation = self::INTERSECTS; - trigger_error('$parameters as parameter 6 in addShape is deprecated', E_USER_DEPRECATED); - } - + ): void { $filter = array_merge( $parameters, [ @@ -124,10 +91,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..68ee2da2 100644 --- a/src/Query/Joining/HasChildQuery.php +++ b/src/Query/Joining/HasChildQuery.php @@ -23,40 +23,20 @@ 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 = []) - { - $this->type = $type; - $this->query = $query; + public function __construct( + private string $type, + private BuilderInterface $query, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'has_child'; } - /** - * {@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..eaed53f8 100644 --- a/src/Query/Joining/HasParentQuery.php +++ b/src/Query/Joining/HasParentQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Joining; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,40 +25,20 @@ 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 = []) - { - $this->parentType = $parentType; - $this->query = $query; + public function __construct( + private string $parentType, + private BuilderInterface $query, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'has_parent'; } - /** - * {@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..c1b65c56 100644 --- a/src/Query/Joining/NestedQuery.php +++ b/src/Query/Joining/NestedQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Joining; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,40 +25,20 @@ 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 = []) - { - $this->path = $path; - $this->query = $query; + public function __construct( + private string $path, + private BuilderInterface $query, + array $parameters = [] + ) { $this->parameters = $parameters; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'nested'; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array { return [ $this->getType() => $this->processArray( @@ -68,22 +50,12 @@ public function toArray() ]; } - /** - * Returns nested query object. - * - * @return BuilderInterface - */ - public function getQuery() + public function getQuery(): BuilderInterface { return $this->query; } - /** - * Returns path this query is set for. - * - * @return string - */ - public function getPath() + public function getPath(): string { return $this->path; } diff --git a/src/Query/Joining/ParentIdQuery.php b/src/Query/Joining/ParentIdQuery.php index f6edd9b2..e2707a7c 100644 --- a/src/Query/Joining/ParentIdQuery.php +++ b/src/Query/Joining/ParentIdQuery.php @@ -1,5 +1,7 @@ childType = $childType; - $this->parentId = $parentId; + public function __construct( + private string $parentId, + private string $childType, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'parent_id'; } - /** - * {@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..9db9c5d2 100644 --- a/src/Query/MatchAllQuery.php +++ b/src/Query/MatchAllQuery.php @@ -9,10 +9,13 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query; use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\ParametersTrait; +use stdClass; /** * Represents Elasticsearch "match_all" query. @@ -23,28 +26,20 @@ class MatchAllQuery implements BuilderInterface { use ParametersTrait; - /** - * @param array $parameters Additional parameters. - */ public function __construct(array $parameters = []) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'match_all'; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array | stdClass { $params = $this->getParameters(); - return [$this->getType() => !empty($params) ? $params : new \stdClass()]; + + return [$this->getType() => $params ?: new stdClass()]; } } diff --git a/src/Query/Span/FieldMaskingSpanQuery.php b/src/Query/Span/FieldMaskingSpanQuery.php index f2cc4578..65e51846 100644 --- a/src/Query/Span/FieldMaskingSpanQuery.php +++ b/src/Query/Span/FieldMaskingSpanQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Span; use ONGR\ElasticsearchDSL\ParametersTrait; @@ -22,70 +24,39 @@ class FieldMaskingSpanQuery implements SpanQueryInterface { use ParametersTrait; - /** - * @var SpanQueryInterface - */ - private $query; - - /** - * @var string - */ - private $field; - - /** - * @param string $field - * @param SpanQueryInterface $query - */ - public function __construct($field, SpanQueryInterface $query) - { + public function __construct( + private string $field, + private SpanQueryInterface $query + ) { $this->setQuery($query); $this->setField($field); } - /** - * @return mixed - */ - public function getQuery() + public function getQuery(): SpanQueryInterface { return $this->query; } - /** - * @param mixed $query - * - * @return $this - */ - public function setQuery($query) + public function setQuery(SpanQueryInterface $query): static { $this->query = $query; 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; return $this; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array { $output = [ 'query' => $this->getQuery()->toArray(), @@ -97,10 +68,7 @@ public function toArray() return [$this->getType() => $output]; } - /** - * {@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..2599e9ad 100644 --- a/src/Query/Span/SpanContainingQuery.php +++ b/src/Query/Span/SpanContainingQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Span; use ONGR\ElasticsearchDSL\ParametersTrait; @@ -22,78 +24,42 @@ class SpanContainingQuery implements SpanQueryInterface { use ParametersTrait; - /** - * @param SpanQueryInterface - */ - private $little; - - /** - * @param SpanQueryInterface - */ - private $big; - - /** - * @param SpanQueryInterface $little - * @param SpanQueryInterface $big - */ - public function __construct(SpanQueryInterface $little, SpanQueryInterface $big) + public function __construct(private SpanQueryInterface $little, private 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) + public function setLittle(SpanQueryInterface $little): static { $this->little = $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) + public function setBig(SpanQueryInterface $big): static { $this->big = $big; return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'span_containing'; } - /** - * {@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..950bbac7 100644 --- a/src/Query/Span/SpanFirstQuery.php +++ b/src/Query/Span/SpanFirstQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Span; use ONGR\ElasticsearchDSL\ParametersTrait; @@ -22,42 +24,21 @@ 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 = []) - { - $this->query = $query; - $this->end = $end; + public function __construct( + private SpanQueryInterface $query, + private int $end, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + + public function getType(): string { return 'span_first'; } - /** - * {@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..493daf6c 100644 --- a/src/Query/Span/SpanMultiTermQuery.php +++ b/src/Query/Span/SpanMultiTermQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Span; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,37 +25,19 @@ 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 = []) - { - $this->query = $query; + public function __construct( + private BuilderInterface $query, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'span_multi'; } - /** - * {@inheritdoc} - * - * @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..7fdb87b5 100644 --- a/src/Query/Span/SpanNearQuery.php +++ b/src/Query/Span/SpanNearQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Span; /** @@ -18,43 +20,26 @@ */ class SpanNearQuery extends SpanOrQuery implements SpanQueryInterface { - /** - * @var int - */ - private $slop; + private int $slop; - /** - * @return int - */ - public function getSlop() + public function getSlop(): int { return $this->slop; } - /** - * @param int $slop - * - * @return $this - */ - public function setSlop($slop) + public function setSlop(int $slop): static { $this->slop = $slop; return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'span_near'; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array { $query = []; foreach ($this->getQueries() as $type) { diff --git a/src/Query/Span/SpanNotQuery.php b/src/Query/Span/SpanNotQuery.php index 1492d1a1..9fa1ce47 100644 --- a/src/Query/Span/SpanNotQuery.php +++ b/src/Query/Span/SpanNotQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Span; use ONGR\ElasticsearchDSL\ParametersTrait; @@ -22,40 +24,20 @@ 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 = []) - { - $this->include = $include; - $this->exclude = $exclude; + public function __construct( + private SpanQueryInterface $include, + private SpanQueryInterface $exclude, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'span_not'; } - /** - * {@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..75071cbc 100644 --- a/src/Query/Span/SpanOrQuery.php +++ b/src/Query/Span/SpanOrQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Span; use ONGR\ElasticsearchDSL\ParametersTrait; @@ -22,53 +24,31 @@ class SpanOrQuery implements SpanQueryInterface { use ParametersTrait; - /** - * @var SpanQueryInterface[] - */ - private $queries = []; + private array $queries = []; - /** - * @param array $parameters - */ public function __construct(array $parameters = []) { $this->setParameters($parameters); } - /** - * Add span query. - * - * @param SpanQueryInterface $query - * - * @return $this - */ - public function addQuery(SpanQueryInterface $query) + public function addQuery(SpanQueryInterface $query): static { $this->queries[] = $query; return $this; } - /** - * @return SpanQueryInterface[] - */ - public function getQueries() + public function getQueries(): array { return $this->queries; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'span_or'; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array { $query = []; foreach ($this->queries as $type) { diff --git a/src/Query/Span/SpanQueryInterface.php b/src/Query/Span/SpanQueryInterface.php index 6d0efdff..0727224b 100644 --- a/src/Query/Span/SpanQueryInterface.php +++ b/src/Query/Span/SpanQueryInterface.php @@ -9,13 +9,12 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Span; use ONGR\ElasticsearchDSL\BuilderInterface; -/** - * Interface SpanQueryInterface to recognise span queries. - */ interface SpanQueryInterface extends BuilderInterface { } diff --git a/src/Query/Span/SpanTermQuery.php b/src/Query/Span/SpanTermQuery.php index acfbf510..cfbce04f 100644 --- a/src/Query/Span/SpanTermQuery.php +++ b/src/Query/Span/SpanTermQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Span; use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; @@ -20,10 +22,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..fe4318a2 100644 --- a/src/Query/Span/SpanWithinQuery.php +++ b/src/Query/Span/SpanWithinQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Span; /** @@ -18,10 +20,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..b3434830 100644 --- a/src/Query/Specialized/MoreLikeThisQuery.php +++ b/src/Query/Specialized/MoreLikeThisQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Specialized; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,33 +25,19 @@ 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 = []) - { - $this->like = $like; + public function __construct( + private string $like, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'more_like_this'; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array { $query = []; diff --git a/src/Query/Specialized/ScriptQuery.php b/src/Query/Specialized/ScriptQuery.php index 07ef289e..f13ab30e 100644 --- a/src/Query/Specialized/ScriptQuery.php +++ b/src/Query/Specialized/ScriptQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\Specialized; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,33 +25,17 @@ 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 string $script, array $parameters = []) { - $this->script = $script; $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'script'; } - /** - * {@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..a470bf63 100644 --- a/src/Query/Specialized/TemplateQuery.php +++ b/src/Query/Specialized/TemplateQuery.php @@ -23,105 +23,58 @@ class TemplateQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $file; - - /** - * @var string - */ - private $inline; - - /** - * @var array - */ - private $params; - - /** - * @param string $file A template of the query - * @param string $inline A template of the query - * @param array $params Parameters to insert into template - */ - public function __construct($file = null, $inline = null, array $params = []) - { + public function __construct( + private ?string $file = null, + private ?string $inline = null, + private array $params = [] + ) { $this->setFile($file); $this->setInline($inline); $this->setParams($params); } - /** - * @return string - */ - public function getFile() + public function getFile(): ?string { return $this->file; } - /** - * @param string $file - * - * @return $this; - */ - public function setFile($file) + public function setFile(?string $file): static { $this->file = $file; return $this; } - /** - * @return string - */ - public function getInline() + public function getInline(): ?string { return $this->inline; } - /** - * @param string $inline - * - * @return $this - */ - public function setInline($inline) + public function setInline(?string $inline): static { $this->inline = $inline; return $this; } - /** - * @return array - */ - public function getParams() + public function getParams(): array { return $this->params; } - /** - * @param array $params - * - * @return $this - */ - public function setParams($params) + public function setParams(array $params): static { $this->params = $params; return $this; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'template'; } - /** - * {@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..96b60315 100644 --- a/src/Query/TermLevel/ExistsQuery.php +++ b/src/Query/TermLevel/ExistsQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\TermLevel; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -20,33 +22,16 @@ */ 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'; } - /** - * {@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..faca56eb 100644 --- a/src/Query/TermLevel/FuzzyQuery.php +++ b/src/Query/TermLevel/FuzzyQuery.php @@ -23,40 +23,20 @@ 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 = []) - { - $this->field = $field; - $this->value = $value; + public function __construct( + private string $field, + private string $value, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'fuzzy'; } - /** - * {@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..e86bcc7a 100644 --- a/src/Query/TermLevel/IdsQuery.php +++ b/src/Query/TermLevel/IdsQuery.php @@ -23,33 +23,17 @@ 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 array $values, array $parameters = []) { - $this->values = $values; $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'ids'; } - /** - * {@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..f96e41d6 100644 --- a/src/Query/TermLevel/PrefixQuery.php +++ b/src/Query/TermLevel/PrefixQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\TermLevel; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,40 +25,17 @@ 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 string $field, protected string $value, array $parameters = []) { - $this->field = $field; - $this->value = $value; $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'prefix'; } - /** - * {@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..f333211b 100644 --- a/src/Query/TermLevel/RangeQuery.php +++ b/src/Query/TermLevel/RangeQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\TermLevel; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,24 +25,15 @@ class RangeQuery implements BuilderInterface { use ParametersTrait; - /** - * Range control names. - */ - const LT = 'lt'; - const GT = 'gt'; - const LTE = 'lte'; - const GTE = 'gte'; + public const LT = 'lt'; + + public const GT = 'gt'; - /** - * @var string Field name. - */ - private $field; + public const LTE = 'lte'; - /** - * @param string $field - * @param array $parameters - */ - public function __construct($field, array $parameters = []) + public const GTE = 'gte'; + + public function __construct(private string $field, array $parameters = []) { $this->setParameters($parameters); @@ -51,22 +44,14 @@ 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'; } - /** - * {@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..5cba644f 100644 --- a/src/Query/TermLevel/RegexpQuery.php +++ b/src/Query/TermLevel/RegexpQuery.php @@ -23,40 +23,20 @@ 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 = []) - { - $this->field = $field; - $this->regexpValue = $regexpValue; + public function __construct( + private string $field, + private string $regexpValue, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'regexp'; } - /** - * {@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..378dde7a 100644 --- a/src/Query/TermLevel/TermQuery.php +++ b/src/Query/TermLevel/TermQuery.php @@ -23,40 +23,20 @@ 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 = []) - { - $this->field = $field; - $this->value = $value; + public function __construct( + private string $field, + private string $value, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'term'; } - /** - * {@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..18d7a11e 100644 --- a/src/Query/TermLevel/TermsQuery.php +++ b/src/Query/TermLevel/TermsQuery.php @@ -9,6 +9,9 @@ * file that was distributed with this source code. */ + +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\TermLevel; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,42 +26,20 @@ class TermsQuery implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $field; - - /** - * @var array - */ - private $terms; - - /** - * Constructor. - * - * @param string $field Field name - * @param array $terms An array of terms - * @param array $parameters Optional parameters - */ - public function __construct($field, $terms, array $parameters = []) - { - $this->field = $field; - $this->terms = $terms; + public function __construct( + private string $field, + private mixed $terms, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'terms'; } - /** - * {@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..793f9e18 100644 --- a/src/Query/TermLevel/TermsSetQuery.php +++ b/src/Query/TermLevel/TermsSetQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\TermLevel; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,57 +25,37 @@ 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'; - - /** - * @var string - */ - private $field; - - /** - * @var array - */ - private $terms; + public const MINIMUM_SHOULD_MATCH_TYPE_FIELD = 'minimum_should_match_field'; + public const MINIMUM_SHOULD_MATCH_TYPE_SCRIPT = 'minimum_should_match_script'; - /** - * Constructor. - * - * @param string $field Field name - * @param array $terms An array of terms - * @param array $parameters Parameters - */ - public function __construct($field, $terms, array $parameters) - { - $this->field = $field; - $this->terms = $terms; + public function __construct( + private string $field, + private array $terms, + array $parameters + ) { $this->validateParameters($parameters); $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'terms_set'; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array { $query = [ 'terms' => $this->terms, ]; - return [$this->getType() => [ - $this->field => $this->processArray($query), - ]]; + return [ + $this->getType() => [ + $this->field => $this->processArray($query), + ] + ]; } - private function validateParameters(array $parameters) + private function validateParameters(array $parameters): void { if (!isset($parameters[self::MINIMUM_SHOULD_MATCH_TYPE_FIELD]) && !isset($parameters[self::MINIMUM_SHOULD_MATCH_TYPE_SCRIPT]) diff --git a/src/Query/TermLevel/TypeQuery.php b/src/Query/TermLevel/TypeQuery.php index a5ae21c8..62f1eaad 100644 --- a/src/Query/TermLevel/TypeQuery.php +++ b/src/Query/TermLevel/TypeQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\TermLevel; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -20,33 +22,16 @@ */ class TypeQuery implements BuilderInterface { - /** - * @var string - */ - private $type; - - /** - * Constructor. - * - * @param string $type Type name - */ - public function __construct($type) + public function __construct(private string $type) { - $this->type = $type; } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'type'; } - /** - * {@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..3b9786f4 100644 --- a/src/Query/TermLevel/WildcardQuery.php +++ b/src/Query/TermLevel/WildcardQuery.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Query\TermLevel; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,40 +25,20 @@ 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 = []) - { - $this->field = $field; - $this->value = $value; + public function __construct( + private string $field, + private string $value, + array $parameters = [] + ) { $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'wildcard'; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array { $query = [ 'value' => $this->value, diff --git a/src/ScriptAwareTrait.php b/src/ScriptAwareTrait.php index 6eb2148e..e5cfd1cf 100644 --- a/src/ScriptAwareTrait.php +++ b/src/ScriptAwareTrait.php @@ -9,32 +9,20 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL; -/** - * A trait which handles elasticsearch aggregation script. - */ trait ScriptAwareTrait { - /** - * @var string - */ - private $script; + private ?string $script = null; - /** - * @return string - */ - public function getScript() + public function getScript(): ?string { return $this->script; } - /** - * @param string $script - * - * @return $this - */ - public function setScript($script) + public function setScript(?string $script): static { $this->script = $script; diff --git a/src/Search.php b/src/Search.php index 89726af4..3c3c45f9 100644 --- a/src/Search.php +++ b/src/Search.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -37,42 +39,33 @@ 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 * operations return the contents of the _source field unless you have used the * stored_fields parameter or if the _source field is disabled. - * - * @var bool */ - private $source; + private ?bool $source = null; /** * 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 +73,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 +82,30 @@ 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 +116,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 = []; /** * While a search request returns a single “page” of results, the scroll API can be used to retrieve @@ -155,41 +132,24 @@ 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 = []; - /** - * Constructor to initialize static properties - */ public function __construct() { $this->initializeSerializer(); } - /** - * Wakeup method to initialize static properties - */ public function __wakeup() { $this->initializeSerializer(); } - /** - * Initializes the serializer - */ - private function initializeSerializer() + private function initializeSerializer(): void { if (static::$serializer === null) { static::$serializer = new OrderedSerializer( @@ -201,26 +161,12 @@ private function initializeSerializer() } } - /** - * Destroys search endpoint. - * - * @param string $type Endpoint type. - */ - public function destroyEndpoint($type) + public function destroyEndpoint(string $type): void { unset($this->endpoints[$type]); } - /** - * Adds query to the search. - * - * @param 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, ?string $key = null): static { $endpoint = $this->getEndpoint(QueryEndpoint::NAME); $endpoint->addToBool($query, $boolType, $key); @@ -228,14 +174,7 @@ public function addQuery(BuilderInterface $query, $boolType = BoolQuery::MUST, $ return $this; } - /** - * Returns endpoint instance. - * - * @param string $type Endpoint type. - * - * @return SearchEndpointInterface - */ - private function getEndpoint($type) + private function getEndpoint(string $type): SearchEndpointInterface { if (!array_key_exists($type, $this->endpoints)) { $this->endpoints[$type] = SearchEndpointFactory::get($type); @@ -244,41 +183,21 @@ private function getEndpoint($type) return $this->endpoints[$type]; } - /** - * Returns queries inside BoolQuery instance. - * - * @return BoolQuery - */ - public function getQueries() + public function getQueries(): BoolQuery { $endpoint = $this->getEndpoint(QueryEndpoint::NAME); return $endpoint->getBool(); } - /** - * 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); return $this; } - /** - * Sets parameters to the endpoint. - * - * @param string $endpointName - * @param array $parameters - * - * @return $this - */ - public function setEndpointParameters($endpointName, array $parameters) + public function setEndpointParameters(string $endpointName, array $parameters): static { /** @var AbstractSearchEndpoint $endpoint */ $endpoint = $this->getEndpoint($endpointName); @@ -287,20 +206,11 @@ public function setEndpointParameters($endpointName, array $parameters) return $this; } - /** - * Adds a post filter to search. - * - * @param BuilderInterface $filter Filter. - * @param string $boolType Example boolType values: - * - must - * - must_not - * - should. - * @param string $key - * - * @return $this. - */ - public function addPostFilter(BuilderInterface $filter, $boolType = BoolQuery::MUST, $key = null) - { + public function addPostFilter( + BuilderInterface $filter, + string $boolType = BoolQuery::MUST, + ?string $key = null + ): static { $this ->getEndpoint(PostFilterEndpoint::NAME) ->addToBool($filter, $boolType, $key); @@ -308,124 +218,64 @@ public function addPostFilter(BuilderInterface $filter, $boolType = BoolQuery::M return $this; } - /** - * Returns queries inside BoolFilter instance. - * - * @return BoolQuery - */ - public function getPostFilters() + public function getPostFilters(): BoolQuery { $endpoint = $this->getEndpoint(PostFilterEndpoint::NAME); return $endpoint->getBool(); } - /** - * 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); return $this; } - /** - * 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()); return $this; } - /** - * Returns all aggregations. - * - * @return BuilderInterface[] - */ - public function getAggregations() + public function getAggregations(): array { return $this->getEndpoint(AggregationsEndpoint::NAME)->getAll(); } - /** - * 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()); return $this; } - /** - * Returns all inner hits. - * - * @return BuilderInterface[] - */ - public function getInnerHits() + public function getInnerHits(): array { return $this->getEndpoint(InnerHitsEndpoint::NAME)->getAll(); } - /** - * 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); return $this; } - /** - * Returns all set sorts. - * - * @return BuilderInterface[] - */ - public function getSorts() + public function getSorts(): array { return $this->getEndpoint(SortEndpoint::NAME)->getAll(); } - /** - * Allows to highlight search results on one or more fields. - * - * @param Highlight $highlight - * - * @return $this. - */ - public function addHighlight($highlight) + public function addHighlight(Highlight $highlight): static { $this->getEndpoint(HighlightEndpoint::NAME)->add($highlight); return $this; } - /** - * Returns highlight builder. - * - * @return BuilderInterface - */ - public function getHighlights() + public function getHighlights(): BuilderInterface { /** @var HighlightEndpoint $highlightEndpoint */ $highlightEndpoint = $this->getEndpoint(HighlightEndpoint::NAME); @@ -433,284 +283,168 @@ public function getHighlights() return $highlightEndpoint->getHighlight(); } - /** - * Adds suggest into search. - * - * @param BuilderInterface $suggest - * - * @return $this - */ - public function addSuggest(NamedBuilderInterface $suggest) + public function addSuggest(NamedBuilderInterface $suggest): static { $this->getEndpoint(SuggestEndpoint::NAME)->add($suggest, $suggest->getName()); return $this; } - /** - * Returns all suggests. - * - * @return BuilderInterface[] - */ - public function getSuggests() + public function getSuggests(): array { return $this->getEndpoint(SuggestEndpoint::NAME)->getAll(); } - /** - * @return null|int - */ - public function getFrom() + public function getFrom(): ?int { return $this->from; } - /** - * @param null|int $from - * - * @return $this - */ - public function setFrom($from) + public function setFrom(?int $from): 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) + public function setTrackTotalHits(bool $trackTotalHits): static { $this->trackTotalHits = $trackTotalHits; return $this; } - /** - * @return null|int - */ - public function getSize() + public function getSize(): ?int { return $this->size; } - /** - * @param null|int $size - * - * @return $this - */ - public function setSize($size) + public function setSize(?int $size): static { $this->size = $size; return $this; } - /** - * @return bool - */ - public function isSource() + public function isSource(): ?bool { return $this->source; } - /** - * @param bool $source - * - * @return $this - */ - public function setSource($source) + public function setSource(?bool $source): static { $this->source = $source; return $this; } - /** - * @return array - */ - public function getStoredFields() + public function getStoredFields(): ?array { return $this->storedFields; } - /** - * @param array $storedFields - * - * @return $this - */ - public function setStoredFields($storedFields) + public function setStoredFields(?array $storedFields): static { $this->storedFields = $storedFields; return $this; } - /** - * @return array - */ - public function getScriptFields() + public function getScriptFields(): ?array { return $this->scriptFields; } - /** - * @param array $scriptFields - * - * @return $this - */ - public function setScriptFields($scriptFields) + public function setScriptFields(?array $scriptFields): static { $this->scriptFields = $scriptFields; return $this; } - /** - * @return array - */ - public function getDocValueFields() + public function getDocValueFields(): ?array { return $this->docValueFields; } - /** - * @param array $docValueFields - * - * @return $this - */ - public function setDocValueFields($docValueFields) + public function setDocValueFields(?array $docValueFields): static { $this->docValueFields = $docValueFields; return $this; } - /** - * @return bool - */ - public function isExplain() + public function isExplain(): ?bool { return $this->explain; } - /** - * @param bool $explain - * - * @return $this - */ - public function setExplain($explain) + public function setExplain(?bool $explain): static { $this->explain = $explain; return $this; } - /** - * @return bool - */ - public function isVersion() + public function isVersion(): ?bool { return $this->version; } - /** - * @param bool $version - * - * @return $this - */ - public function setVersion($version) + public function setVersion(?bool $version): static { $this->version = $version; return $this; } - /** - * @return array - */ - public function getIndicesBoost() + public function getIndicesBoost(): ?array { return $this->indicesBoost; } - /** - * @param array $indicesBoost - * - * @return $this - */ - public function setIndicesBoost($indicesBoost) + public function setIndicesBoost(?array $indicesBoost): static { $this->indicesBoost = $indicesBoost; return $this; } - /** - * @return int - */ - public function getMinScore() + public function getMinScore(): ?int { return $this->minScore; } - /** - * @param int $minScore - * - * @return $this - */ - public function setMinScore($minScore) + public function setMinScore(?int $minScore): static { $this->minScore = $minScore; return $this; } - /** - * @return array - */ - public function getSearchAfter() + public function getSearchAfter(): ?array { return $this->searchAfter; } - /** - * @param array $searchAfter - * - * @return $this - */ - public function setSearchAfter($searchAfter) + public function setSearchAfter(?array $searchAfter): static { $this->searchAfter = $searchAfter; return $this; } - /** - * @return string - */ - public function getScroll() + public function getScroll(): ?string { return $this->scroll; } - /** - * @param string $scroll - * - * @return $this - */ - public function setScroll($scroll = '5m') + public function setScroll(?string $scroll = '5m'): static { $this->scroll = $scroll; @@ -719,40 +453,37 @@ public function setScroll($scroll = '5m') return $this; } - /** - * @param string $name - * @param string|array|bool $value - * - * @return $this - */ - public function addUriParam($name, $value) - { - if (in_array($name, [ - 'q', - 'df', - 'analyzer', - 'analyze_wildcard', - 'default_operator', - 'lenient', - 'explain', - '_source', - '_source_exclude', - '_source_include', - 'stored_fields', - 'sort', - 'track_scores', - 'timeout', - 'terminate_after', - 'from', - 'size', - 'search_type', - 'scroll', - 'allow_no_indices', - 'ignore_unavailable', - 'typed_keys', - 'pre_filter_shard_size', - 'ignore_unavailable', - ])) { + public function addUriParam(string $name, string|array|bool $value): static + { + if (in_array( + $name, + [ + 'q', + 'df', + 'analyzer', + 'analyze_wildcard', + 'default_operator', + 'lenient', + 'explain', + '_source', + '_source_exclude', + '_source_include', + 'stored_fields', + 'sort', + 'track_scores', + 'timeout', + 'terminate_after', + 'from', + 'size', + 'search_type', + 'scroll', + 'allow_no_indices', + 'ignore_unavailable', + 'typed_keys', + 'pre_filter_shard_size', + 'ignore_unavailable', + ] + )) { $this->uriParams[$name] = $value; } else { throw new \InvalidArgumentException(sprintf('Parameter %s is not supported.', $value)); @@ -761,20 +492,12 @@ public function addUriParam($name, $value) return $this; } - /** - * Returns query url parameters. - * - * @return array - */ - public function getUriParams() + public function getUriParams(): array { return $this->uriParams; } - /** - * {@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..50d04b21 100644 --- a/src/SearchEndpoint/AbstractSearchEndpoint.php +++ b/src/SearchEndpoint/AbstractSearchEndpoint.php @@ -9,10 +9,13 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\SearchEndpoint; use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\ParametersTrait; +use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery; use ONGR\ElasticsearchDSL\Serializer\Normalizer\AbstractNormalizable; /** @@ -22,15 +25,9 @@ abstract class AbstractSearchEndpoint extends AbstractNormalizable implements Se { use ParametersTrait; - /** - * @var BuilderInterface[] - */ - private $container = []; + private array $container = []; - /** - * {@inheritdoc} - */ - public function add(BuilderInterface $builder, $key = null) + public function add(BuilderInterface $builder, string $key = null): ?string { if (array_key_exists($key, $this->container)) { throw new \OverflowException(sprintf('Builder with %s name for endpoint has already been added!', $key)); @@ -45,18 +42,12 @@ public function add(BuilderInterface $builder, $key = null) return $key; } - /** - * {@inheritdoc} - */ - public function addToBool(BuilderInterface $builder, $boolType = null, $key = null) + public function addToBool(BuilderInterface $builder, ?string $boolType = null, ?string $key = null): string { throw new \BadFunctionCallException(sprintf("Endpoint %s doesn't support bool statements", static::NAME)); } - /** - * {@inheritdoc} - */ - public function remove($key) + public function remove(string $key): static { if ($this->has($key)) { unset($this->container[$key]); @@ -65,22 +56,12 @@ public function remove($key) return $this; } - /** - * Checks if builder with specific key exists. - * - * @param string $key Key to check if it exists in container. - * - * @return bool - */ - public function has($key) + public function has(string $key): bool { return array_key_exists($key, $this->container); } - /** - * {@inheritdoc} - */ - public function get($key) + public function get(string $key): ?BuilderInterface { if ($this->has($key)) { return $this->container[$key]; @@ -89,18 +70,12 @@ public function get($key) return null; } - /** - * {@inheritdoc} - */ - public function getAll($boolType = null) + public function getAll(?string $boolType = null): array { return $this->container; } - /** - * {@inheritdoc} - */ - public function getBool() + public function getBool(): ?BoolQuery { throw new \BadFunctionCallException(sprintf("Endpoint %s doesn't support bool statements", static::NAME)); } diff --git a/src/SearchEndpoint/AggregationsEndpoint.php b/src/SearchEndpoint/AggregationsEndpoint.php index 079a6a5d..7664274f 100644 --- a/src/SearchEndpoint/AggregationsEndpoint.php +++ b/src/SearchEndpoint/AggregationsEndpoint.php @@ -9,25 +9,18 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\SearchEndpoint; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -/** - * Search aggregations dsl endpoint. - */ class AggregationsEndpoint extends AbstractSearchEndpoint { - /** - * Endpoint name - */ - const NAME = 'aggregations'; + public const NAME = 'aggregations'; - /** - * {@inheritdoc} - */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) + public function normalize(NormalizerInterface $normalizer, string $format = null, array $context = []) { $output = []; if (count($this->getAll()) > 0) { diff --git a/src/SearchEndpoint/HighlightEndpoint.php b/src/SearchEndpoint/HighlightEndpoint.php index f2b8d8e9..1794ac56 100644 --- a/src/SearchEndpoint/HighlightEndpoint.php +++ b/src/SearchEndpoint/HighlightEndpoint.php @@ -9,35 +9,22 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\SearchEndpoint; use ONGR\ElasticsearchDSL\BuilderInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -/** - * Search highlight dsl endpoint. - */ class HighlightEndpoint extends AbstractSearchEndpoint { - /** - * Endpoint name - */ - const NAME = 'highlight'; + public const NAME = 'highlight'; - /** - * @var BuilderInterface - */ - private $highlight; + private ?BuilderInterface $highlight = null; - /** - * @var string Key for highlight storing. - */ - private $key; + private ?string $key = null; - /** - * {@inheritdoc} - */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) + public function normalize(NormalizerInterface $normalizer, string $format = null, array $context = []) { if ($this->highlight) { return $this->highlight->toArray(); @@ -46,10 +33,7 @@ public function normalize(NormalizerInterface $normalizer, $format = null, array return null; } - /** - * {@inheritdoc} - */ - public function add(BuilderInterface $builder, $key = null) + public function add(BuilderInterface $builder, ?string $key = null): ?string { if ($this->highlight) { throw new \OverflowException('Only one highlight can be set'); @@ -57,20 +41,16 @@ public function add(BuilderInterface $builder, $key = null) $this->key = $key; $this->highlight = $builder; + + return $key; } - /** - * {@inheritdoc} - */ - public function getAll($boolType = null) + public function getAll(?string $boolType = null): array { return [$this->key => $this->highlight]; } - /** - * @return BuilderInterface - */ - public function getHighlight() + public function getHighlight(): ?BuilderInterface { return $this->highlight; } diff --git a/src/SearchEndpoint/InnerHitsEndpoint.php b/src/SearchEndpoint/InnerHitsEndpoint.php index f1562fe6..34fc6b2d 100644 --- a/src/SearchEndpoint/InnerHitsEndpoint.php +++ b/src/SearchEndpoint/InnerHitsEndpoint.php @@ -14,20 +14,11 @@ use ONGR\ElasticsearchDSL\InnerHit\NestedInnerHit; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -/** - * Search inner hits dsl endpoint. - */ class InnerHitsEndpoint extends AbstractSearchEndpoint { - /** - * Endpoint name - */ - const NAME = 'inner_hits'; + public const NAME = 'inner_hits'; - /** - * {@inheritdoc} - */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) + public function normalize(NormalizerInterface $normalizer, string $format = null, array $context = []) { $output = []; if (count($this->getAll()) > 0) { diff --git a/src/SearchEndpoint/PostFilterEndpoint.php b/src/SearchEndpoint/PostFilterEndpoint.php index 6055b7ed..7b69574d 100644 --- a/src/SearchEndpoint/PostFilterEndpoint.php +++ b/src/SearchEndpoint/PostFilterEndpoint.php @@ -9,24 +9,17 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\SearchEndpoint; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -/** - * Search post filter dsl endpoint. - */ class PostFilterEndpoint extends QueryEndpoint { - /** - * Endpoint name - */ - const NAME = 'post_filter'; - - /** - * {@inheritdoc} - */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) + public const NAME = 'post_filter'; + + public function normalize(NormalizerInterface $normalizer, string $format = null, array $context = []) { if (!$this->getBool()) { return null; @@ -35,10 +28,7 @@ public function normalize(NormalizerInterface $normalizer, $format = null, array return $this->getBool()->toArray(); } - /** - * {@inheritdoc} - */ - public function getOrder() + public function getOrder(): int { return 1; } diff --git a/src/SearchEndpoint/QueryEndpoint.php b/src/SearchEndpoint/QueryEndpoint.php index 6212b05d..e929816f 100644 --- a/src/SearchEndpoint/QueryEndpoint.php +++ b/src/SearchEndpoint/QueryEndpoint.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\SearchEndpoint; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -16,30 +18,15 @@ use ONGR\ElasticsearchDSL\Serializer\Normalizer\OrderedNormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -/** - * Search query dsl endpoint. - */ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerInterface { - /** - * Endpoint name - */ - const NAME = 'query'; - - /** - * @var BoolQuery - */ - private $bool; - - /** - * @var bool - */ - private $filtersSet = false; - - /** - * {@inheritdoc} - */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) + public const NAME = 'query'; + + private ?BoolQuery $bool = null; + + private bool $filtersSet = false; + + public function normalize(NormalizerInterface $normalizer, string $format = null, array $context = []) { if (!$this->filtersSet && $this->hasReference('filter_query')) { /** @var BuilderInterface $filter */ @@ -55,18 +42,12 @@ public function normalize(NormalizerInterface $normalizer, $format = null, array return $this->bool->toArray(); } - /** - * {@inheritdoc} - */ - public function add(BuilderInterface $builder, $key = null) + public function add(BuilderInterface $builder, string $key = null): string { return $this->addToBool($builder, BoolQuery::MUST, $key); } - /** - * {@inheritdoc} - */ - public function addToBool(BuilderInterface $builder, $boolType = null, $key = null) + public function addToBool(BuilderInterface $builder, ?string $boolType = null, ?string $key = null): string { if (!$this->bool) { $this->bool = new BoolQuery(); @@ -75,26 +56,17 @@ public function addToBool(BuilderInterface $builder, $boolType = null, $key = nu return $this->bool->add($builder, $boolType, $key); } - /** - * {@inheritdoc} - */ - public function getOrder() + public function getOrder(): int { return 2; } - /** - * @return BoolQuery - */ - public function getBool() + public function getBool(): ?BoolQuery { return $this->bool; } - /** - * {@inheritdoc} - */ - public function getAll($boolType = null) + public function getAll($boolType = null): array { return $this->bool->getQueries($boolType); } diff --git a/src/SearchEndpoint/SearchEndpointFactory.php b/src/SearchEndpoint/SearchEndpointFactory.php index 17e6838b..1ccfae33 100644 --- a/src/SearchEndpoint/SearchEndpointFactory.php +++ b/src/SearchEndpoint/SearchEndpointFactory.php @@ -9,11 +9,10 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\SearchEndpoint; -/** - * Factory for search endpoints. - */ class SearchEndpointFactory { /** @@ -29,16 +28,7 @@ class SearchEndpointFactory 'inner_hits' => 'ONGR\ElasticsearchDSL\SearchEndpoint\InnerHitsEndpoint', ]; - /** - * Returns a search endpoint instance. - * - * @param string $type Type of endpoint. - * - * @return SearchEndpointInterface - * - * @throws \RuntimeException Endpoint does not exist. - */ - public static function get($type) + public static function get(string $type): SearchEndpointInterface { if (!array_key_exists($type, self::$endpoints)) { throw new \RuntimeException('Endpoint does not exist.'); diff --git a/src/SearchEndpoint/SearchEndpointInterface.php b/src/SearchEndpoint/SearchEndpointInterface.php index 17ec4abf..9cfd72ac 100644 --- a/src/SearchEndpoint/SearchEndpointInterface.php +++ b/src/SearchEndpoint/SearchEndpointInterface.php @@ -9,70 +9,25 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\SearchEndpoint; use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery; use Symfony\Component\Serializer\Normalizer\NormalizableInterface; -/** - * Interface used to define search endpoint. - */ interface SearchEndpointInterface extends NormalizableInterface { - /** - * Adds builder to search endpoint. - * - * @param BuilderInterface $builder Builder to add. - * @param array $key Additional parameters relevant to builder. - * - * @return string Key of added builder. - */ - public function add(BuilderInterface $builder, $key = null); + public function add(BuilderInterface $builder, ?string $key = null): ?string; - /** - * Adds builder to search endpoint's specific bool type container. - * - * @param BuilderInterface $builder Builder to add. - * @param array $boolType Bool type for query or filter. If bool type is left null - * it will be treated as MUST. - * @param array $key Additional parameters relevant to builder. - * - * @return string Key of added builder. - */ - public function addToBool(BuilderInterface $builder, $boolType = null, $key = null); + public function addToBool(BuilderInterface $builder, string $boolType = null, string $key = null): string; - /** - * Removes contained builder. - * - * @param int $key - * - * @return $this - */ - public function remove($key); + public function remove(string $key): static; - /** - * Returns contained builder or null if Builder is not found. - * - * @param int $key - * - * @return BuilderInterface|null - */ - public function get($key); + public function get(string $key): ?BuilderInterface; - /** - * Returns contained builder or null if Builder is not found. - * - * @param string|null $boolType If bool type is left null it will return all builders from container. - * - * @return array - */ - public function getAll($boolType = null); + public function getAll(?string $boolType = null): array; - /** - * Returns Bool filter or query instance with all builder objects inside. - * - * @return BoolQuery - */ - public function getBool(); + public function getBool(): ?BoolQuery; } diff --git a/src/SearchEndpoint/SortEndpoint.php b/src/SearchEndpoint/SortEndpoint.php index 2461c858..bc07c903 100644 --- a/src/SearchEndpoint/SortEndpoint.php +++ b/src/SearchEndpoint/SortEndpoint.php @@ -9,24 +9,17 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\SearchEndpoint; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -/** - * Search sort dsl endpoint. - */ class SortEndpoint extends AbstractSearchEndpoint { - /** - * Endpoint name - */ - const NAME = 'sort'; - - /** - * {@inheritdoc} - */ - public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) + public const NAME = 'sort'; + + public function normalize(NormalizerInterface $normalizer, string $format = null, array $context = []): array { $output = []; diff --git a/src/SearchEndpoint/SuggestEndpoint.php b/src/SearchEndpoint/SuggestEndpoint.php index 2e2e16ac..a9f4ab9b 100644 --- a/src/SearchEndpoint/SuggestEndpoint.php +++ b/src/SearchEndpoint/SuggestEndpoint.php @@ -9,29 +9,21 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\SearchEndpoint; use ONGR\ElasticsearchDSL\Suggest\TermSuggest; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -/** - * Search suggest dsl endpoint. - */ class SuggestEndpoint extends AbstractSearchEndpoint { - /** - * Endpoint name - */ const NAME = 'suggest'; - /** - * {@inheritdoc} - */ public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) { $output = []; if (count($this->getAll()) > 0) { - /** @var TermSuggest $suggest */ foreach ($this->getAll() as $suggest) { $output = array_merge($output, $suggest->toArray()); } diff --git a/src/Serializer/Normalizer/AbstractNormalizable.php b/src/Serializer/Normalizer/AbstractNormalizable.php index f3569d65..ea5a583b 100644 --- a/src/Serializer/Normalizer/AbstractNormalizable.php +++ b/src/Serializer/Normalizer/AbstractNormalizable.php @@ -9,14 +9,13 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Serializer\Normalizer; use ONGR\ElasticsearchDSL\ParametersTrait; use Symfony\Component\Serializer\Normalizer\NormalizableInterface; -/** - * Custom abstract normalizer which can save references for other objects. - */ abstract class AbstractNormalizable implements NormalizableInterface { use ParametersTrait { diff --git a/src/Serializer/Normalizer/CustomReferencedNormalizer.php b/src/Serializer/Normalizer/CustomReferencedNormalizer.php index 6cd24a8e..2ee0879b 100644 --- a/src/Serializer/Normalizer/CustomReferencedNormalizer.php +++ b/src/Serializer/Normalizer/CustomReferencedNormalizer.php @@ -9,24 +9,17 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Serializer\Normalizer; use Symfony\Component\Serializer\Normalizer\CustomNormalizer; -/** - * Normalizer used with referenced normalized objects. - */ class CustomReferencedNormalizer extends CustomNormalizer { - /** - * @var array - */ - private $references = []; - - /** - * {@inheritdoc} - */ - public function normalize($object, $format = null, array $context = []) + private array $references = []; + + public function normalize($object, $format = null, array $context = []): mixed { $object->setReferences($this->references); $data = parent::normalize($object, $format, $context); @@ -35,10 +28,7 @@ public function normalize($object, $format = null, array $context = []) return $data; } - /** - * {@inheritdoc} - */ - public function supportsNormalization($data, $format = null) + public function supportsNormalization(mixed $data, $format = null): bool { return $data instanceof AbstractNormalizable; } diff --git a/src/Serializer/Normalizer/OrderedNormalizerInterface.php b/src/Serializer/Normalizer/OrderedNormalizerInterface.php index bb6811b9..4542a70f 100644 --- a/src/Serializer/Normalizer/OrderedNormalizerInterface.php +++ b/src/Serializer/Normalizer/OrderedNormalizerInterface.php @@ -9,17 +9,11 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Serializer\Normalizer; -/** - * This should be implemented by normalizable object that required to be processed in specific order. - */ interface OrderedNormalizerInterface { - /** - * Returns normalization priority. - * - * @return int - */ - public function getOrder(); + public function getOrder(): int; } diff --git a/src/Serializer/OrderedSerializer.php b/src/Serializer/OrderedSerializer.php index bcbb9c3d..2e2325ef 100644 --- a/src/Serializer/OrderedSerializer.php +++ b/src/Serializer/OrderedSerializer.php @@ -9,20 +9,19 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Serializer; use ONGR\ElasticsearchDSL\Serializer\Normalizer\OrderedNormalizerInterface; use Symfony\Component\Serializer\Serializer; -/** - * Custom serializer which orders data before normalization. - */ class OrderedSerializer extends Serializer { /** * {@inheritdoc} */ - public function normalize($data, $format = null, array $context = []) + public function normalize(mixed $data, string $format = null, array $context = []): mixed { return parent::normalize( is_array($data) ? $this->order($data) : $data, @@ -31,10 +30,7 @@ public function normalize($data, $format = null, array $context = []) ); } - /** - * {@inheritdoc} - */ - public function denormalize($data, $type, $format = null, array $context = []) + public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed { return parent::denormalize( is_array($data) ? $this->order($data) : $data, @@ -44,14 +40,7 @@ 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); @@ -69,14 +58,7 @@ function (OrderedNormalizerInterface $a, OrderedNormalizerInterface $b) { return $data; } - /** - * 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, diff --git a/src/Sort/FieldSort.php b/src/Sort/FieldSort.php index 36cb8908..dff585cf 100644 --- a/src/Sort/FieldSort.php +++ b/src/Sort/FieldSort.php @@ -9,122 +9,71 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Sort; use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\ParametersTrait; +use stdClass; -/** - * Holds all the values required for basic sorting. - */ class FieldSort implements BuilderInterface { use ParametersTrait; - const ASC = 'asc'; - const DESC = 'desc'; - - /** - * @var string - */ - private $field; - - /** - * @var string - */ - private $order; - - /** - * @var BuilderInterface - */ - private $nestedFilter; - - /** - * @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 const ASC = 'asc'; + + public const DESC = 'desc'; + + private ?BuilderInterface $nestedFilter = null; + + public function __construct(private string $field, private mixed $order = null, $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(): mixed { return $this->order; } - /** - * @param string $order - * - * @return $this - */ - public function setOrder($order) + public function setOrder(mixed $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; return $this; } - /** - * Returns element type. - * - * @return string - */ - public function getType() + public function getType(): string { return 'sort'; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array { if ($this->order) { $this->addParameter('order', $this->order); @@ -135,7 +84,7 @@ public function toArray() } $output = [ - $this->field => !$this->getParameters() ? new \stdClass() : $this->getParameters(), + $this->field => $this->getParameters() ?: new stdClass(), ]; return $output; diff --git a/src/Sort/NestedSort.php b/src/Sort/NestedSort.php index 8954719f..a999cb5d 100644 --- a/src/Sort/NestedSort.php +++ b/src/Sort/NestedSort.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Sort; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -23,51 +25,25 @@ class NestedSort implements BuilderInterface { use ParametersTrait; - /** - * @var string - */ - private $path; - - /** - * @var BuilderInterface - */ - private $filter; - - /** - * @var BuilderInterface - */ - private $nestedFilter; - - /** - * @param string $path - * @param BuilderInterface $filter - * @param array $parameters - */ + private ?BuilderInterface $nestedFilter = null; + public function __construct( - $path, - BuilderInterface $filter = null, + private string $path, + private ?BuilderInterface $filter = null, array $parameters = [] ) { - $this->path = $path; - $this->filter = $filter; $this->setParameters($parameters); } - /** - * {@inheritdoc} - */ - public function getType() + public function getType(): string { return 'nested'; } - /** - * {@inheritdoc} - */ - public function toArray() + public function toArray(): array { $output = [ - 'path' => $this->path, + 'path' => $this->path, ]; if ($this->filter) { @@ -81,40 +57,22 @@ public function toArray() return $this->processArray($output); } - /** - * Returns nested filter object. - * - * @return BuilderInterface - */ - public function getFilter() + public function getFilter(): ?BuilderInterface { return $this->filter; } - /** - * Returns path this filter is set for. - * - * @return string - */ - public function getPath() + public function getPath(): string { 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) + public function setNestedFilter(?BuilderInterface $nestedFilter): static { $this->nestedFilter = $nestedFilter; diff --git a/src/Suggest/Suggest.php b/src/Suggest/Suggest.php index 1738d84d..d2454302 100644 --- a/src/Suggest/Suggest.php +++ b/src/Suggest/Suggest.php @@ -9,46 +9,24 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Suggest; 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; - - /** - * 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( + private string $name, + private string $type, + private string $text, + private string $field, + array $parameters = [] + ) { $this->setName($name); $this->setType($type); $this->setText($text); @@ -56,102 +34,62 @@ 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; return $this; } - /** - * {@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..4765f7c6 100644 --- a/tests/Functional/AbstractElasticsearchTestCase.php +++ b/tests/Functional/AbstractElasticsearchTestCase.php @@ -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(); diff --git a/tests/Unit/Aggregation/Bucketing/AdjacencyMatrixAggregationTest.php b/tests/Unit/Aggregation/Bucketing/AdjacencyMatrixAggregationTest.php index f73cc612..2e981968 100644 --- a/tests/Unit/Aggregation/Bucketing/AdjacencyMatrixAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/AdjacencyMatrixAggregationTest.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\AdjacencyMatrixAggregation; @@ -19,23 +21,7 @@ */ class AdjacencyMatrixAggregationTest extends \PHPUnit\Framework\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() -// { -// $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); -// $aggregation = new FiltersAggregation('test_agg'); -// $aggregation->addFilter($mock); -// } - - /** - * Test GetArray method. - */ - public function testFiltersAggregationGetArray() + public function testFiltersAggregationGetArray(): void { $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); $aggregation = new AdjacencyMatrixAggregation('test_agg'); @@ -44,20 +30,14 @@ public function testFiltersAggregationGetArray() $this->assertArrayHasKey('filters', $result); } - /** - * Tests getType method. - */ - public function testFiltersAggregationGetType() + public function testFiltersAggregationGetType(): void { $aggregation = new AdjacencyMatrixAggregation('foo'); $result = $aggregation->getType(); $this->assertEquals('adjacency_matrix', $result); } - /** - * Test for filter aggregation toArray() method. - */ - public function testToArray() + public function testToArray(): void { $aggregation = new AdjacencyMatrixAggregation('test_agg'); $filter = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface') @@ -90,10 +70,7 @@ public function testToArray() $this->assertEquals($expected, $results); } - /** - * 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'); diff --git a/tests/Unit/Aggregation/Bucketing/AudoDateHistogramAggregationTest.php b/tests/Unit/Aggregation/Bucketing/AudoDateHistogramAggregationTest.php index bece77d3..8c918517 100644 --- a/tests/Unit/Aggregation/Bucketing/AudoDateHistogramAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/AudoDateHistogramAggregationTest.php @@ -16,12 +16,8 @@ class AudoDateHistogramAggregationTest extends \PHPUnit\Framework\TestCase { - /** - * Tests agg. - */ - public function testAutoDateHistogramAggregationSetField() + public function testAutoDateHistogramAggregationSetField(): void { - // Case #0 terms aggregation. $aggregation = new AutoDateHistogramAggregation('test_agg', 'test_field'); $result = [ diff --git a/tests/Unit/Aggregation/Bucketing/ChildrenAggregationTest.php b/tests/Unit/Aggregation/Bucketing/ChildrenAggregationTest.php index f4cb6b3a..4b111c7e 100644 --- a/tests/Unit/Aggregation/Bucketing/ChildrenAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/ChildrenAggregationTest.php @@ -11,6 +11,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\ChildrenAggregation; /** @@ -18,13 +19,9 @@ */ class ChildrenAggregationTest extends \PHPUnit\Framework\TestCase { - /** - * Tests if ChildrenAggregation#getArray throws exception when expected. - * - * @expectedException \LogicException - */ public function testGetArrayException() { + $this->expectException(LogicException::class); $aggregation = new ChildrenAggregation('foo'); $aggregation->getArray(); } diff --git a/tests/Unit/Aggregation/Bucketing/DateHistogramAggregationTest.php b/tests/Unit/Aggregation/Bucketing/DateHistogramAggregationTest.php index 6f7544bb..3bb2f484 100644 --- a/tests/Unit/Aggregation/Bucketing/DateHistogramAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/DateHistogramAggregationTest.php @@ -9,8 +9,11 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\DateHistogramAggregation; /** @@ -18,31 +21,21 @@ */ class DateHistogramAggregationTest extends \PHPUnit\Framework\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(); } - /** - * Tests getType method. - */ - public function testDateHistogramAggregationGetType() + public function testDateHistogramAggregationGetType(): void { $aggregation = new DateHistogramAggregation('foo'); $result = $aggregation->getType(); $this->assertEquals('date_histogram', $result); } - /** - * Tests getArray method. - */ - public function testChildrenAggregationGetArray() + public function testChildrenAggregationGetArray(): void { $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation') ->disableOriginalConstructor() diff --git a/tests/Unit/Aggregation/Bucketing/DateRangeAggregationTest.php b/tests/Unit/Aggregation/Bucketing/DateRangeAggregationTest.php index 6a3c5dbe..9a515b4b 100644 --- a/tests/Unit/Aggregation/Bucketing/DateRangeAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/DateRangeAggregationTest.php @@ -11,30 +11,24 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\DateRangeAggregation; class DateRangeAggregationTest extends \PHPUnit\Framework\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() { + $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); } diff --git a/tests/Unit/Aggregation/Bucketing/FilterAggregationTest.php b/tests/Unit/Aggregation/Bucketing/FilterAggregationTest.php index b453e014..588065e7 100644 --- a/tests/Unit/Aggregation/Bucketing/FilterAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/FilterAggregationTest.php @@ -9,8 +9,11 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\FilterAggregation; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\HistogramAggregation; use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery; @@ -99,26 +102,20 @@ public function testToArray($aggregation, $expectedResult) $this->assertEquals($expectedResult, $aggregation->toArray()); } - /** - * Test for setField(). - * - * @expectedException \LogicException - * @expectedExceptionMessage doesn't support `field` parameter - */ - public function testSetField() + public function testSetField(): void { + $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 +123,7 @@ public function testToArrayNoFilter() /** * Test for toArray() with setting a filter. */ - public function testToArrayWithFilter() + public function testToArrayWithFilter(): void { $aggregation = new FilterAggregation('test_agg'); @@ -137,7 +134,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..b72c8324 100644 --- a/tests/Unit/Aggregation/Bucketing/FiltersAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/FiltersAggregationTest.php @@ -9,8 +9,11 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\FiltersAggregation; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -19,14 +22,11 @@ */ class FiltersAggregationTest extends \PHPUnit\Framework\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 { + $this->expectException(LogicException::class); + $this->expectExceptionMessage('In not anonymous filters filter name must be set.'); + $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); $aggregation = new FiltersAggregation('test_agg'); $aggregation->addFilter($mock); @@ -35,7 +35,7 @@ public function testIfExceptionIsThrown() /** * Test GetArray method. */ - public function testFiltersAggregationGetArray() + public function testFiltersAggregationGetArray(): void { $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); $aggregation = new FiltersAggregation('test_agg'); @@ -48,7 +48,7 @@ public function testFiltersAggregationGetArray() /** * Tests getType method. */ - public function testFiltersAggregationGetType() + public function testFiltersAggregationGetType(): void { $aggregation = new FiltersAggregation('foo'); $result = $aggregation->getType(); @@ -58,7 +58,7 @@ 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') @@ -93,7 +93,7 @@ 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'); diff --git a/tests/Unit/Aggregation/Bucketing/GeoDistanceAggregationTest.php b/tests/Unit/Aggregation/Bucketing/GeoDistanceAggregationTest.php index 89f2f5d5..a22f3ae3 100644 --- a/tests/Unit/Aggregation/Bucketing/GeoDistanceAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/GeoDistanceAggregationTest.php @@ -9,56 +9,45 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\GeoDistanceAggregation; class GeoDistanceAggregationTest extends \PHPUnit\Framework\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(); } - /** - * 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(); } - /** - * 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 function testGeoDistanceAggregationGetArrayDataProvider(): array { $out = []; $filterData = [ @@ -90,7 +79,7 @@ public function testGeoDistanceAggregationGetArrayDataProvider() * * @dataProvider testGeoDistanceAggregationGetArrayDataProvider */ - public function testGeoDistanceAggregationGetArray($filterData, $expected) + public function testGeoDistanceAggregationGetArray($filterData, $expected): void { $aggregation = new GeoDistanceAggregation('foo'); $aggregation->setOrigin($filterData['origin']); @@ -106,7 +95,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 +105,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..4822c358 100644 --- a/tests/Unit/Aggregation/Bucketing/GeoHashGridAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/GeoHashGridAggregationTest.php @@ -9,32 +9,24 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\GeoHashGridAggregation; -/** - * Unit test for geohash grid aggregation. - */ class GeoHashGridAggregationTest extends \PHPUnit\Framework\TestCase { - /** - * Test if exception is thrown. - * - * @expectedException \LogicException - */ public function testGeoHashGridAggregationException() { + $this->expectException(LogicException::class); + $agg = new GeoHashGridAggregation('test_agg'); $agg->getArray(); } - /** - * Data provider for testGeoHashGridAggregationGetArray(). - * - * @return array - */ - public function getArrayDataProvider() + public function getArrayDataProvider(): array { $out = []; @@ -65,7 +57,7 @@ public function getArrayDataProvider() * * @dataProvider getArrayDataProvider */ - public function testGeoHashGridAggregationGetArray($filterData, $expected) + public function testGeoHashGridAggregationGetArray($filterData, $expected): void { $aggregation = new GeoHashGridAggregation('foo'); $aggregation->setPrecision($filterData['precision']); @@ -77,10 +69,7 @@ public function testGeoHashGridAggregationGetArray($filterData, $expected) $this->assertEquals($result, $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..e8f40937 100644 --- a/tests/Unit/Aggregation/Bucketing/GlobalAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/GlobalAggregationTest.php @@ -9,18 +9,16 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\GlobalAggregation; class GlobalAggregationTest extends \PHPUnit\Framework\TestCase { - /** - * Data provider for testToArray(). - * - * @return array - */ - public function getToArrayData() + public function getToArrayData(): array { $out = []; @@ -64,7 +62,7 @@ public function getToArrayData() * * @dataProvider getToArrayData */ - public function testToArray($aggregation, $expectedResult) + public function testToArray($aggregation, $expectedResult): void { $this->assertEquals( json_encode($expectedResult), @@ -72,13 +70,10 @@ public function testToArray($aggregation, $expectedResult) ); } - /** - * Test for setField method on global aggregation. - * - * @expectedException \LogicException - */ - public function testSetField() + public function testSetField(): void { + $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..6481d0a2 100644 --- a/tests/Unit/Aggregation/Bucketing/Ipv4RangeAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/Ipv4RangeAggregationTest.php @@ -9,27 +9,24 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\Ipv4RangeAggregation; class Ipv4RangeAggregationTest extends \PHPUnit\Framework\TestCase { - /** - * Test exception when field and range are not set. - * - * @expectedException \LogicException - */ public function testIfExceptionIsThrownWhenFieldAndRangeAreNotSet() { + $this->expectException(LogicException::class); + $agg = new Ipv4RangeAggregation('foo'); $agg->toArray(); } - /** - * 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..48b4e520 100644 --- a/tests/Unit/Aggregation/Bucketing/MissingAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/MissingAggregationTest.php @@ -9,28 +9,25 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\MissingAggregation; class MissingAggregationTest extends \PHPUnit\Framework\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(); } - /** - * Test getArray method. - */ - public function testMissingAggregationGetArray() + public function testMissingAggregationGetArray(): void { $aggregation = new MissingAggregation('foo'); $aggregation->setField('bar'); @@ -38,10 +35,7 @@ public function testMissingAggregationGetArray() $this->assertEquals('bar', $result['field']); } - /** - * Test getType method. - */ - public function testMissingAggregationGetType() + public function testMissingAggregationGetType(): void { $aggregation = new MissingAggregation('bar'); $result = $aggregation->getType(); diff --git a/tests/Unit/Aggregation/Metric/CardinalityAggregationTest.php b/tests/Unit/Aggregation/Metric/CardinalityAggregationTest.php index 8ec9f2cf..6547323a 100644 --- a/tests/Unit/Aggregation/Metric/CardinalityAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/CardinalityAggregationTest.php @@ -9,8 +9,11 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Metric\Aggregation; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Metric\CardinalityAggregation; /** @@ -21,7 +24,7 @@ class CardinalityAggregationTest extends \PHPUnit\Framework\TestCase /** * Tests getArray method. */ - public function testGetArray() + public function testGetArray(): void { $aggregation = new CardinalityAggregation('bar'); @@ -50,22 +53,16 @@ public function testGetArray() $this->assertEquals(true, $result['rehash'], 'rehash=true when rehash is set to true'); } - /** - * 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(); } - /** - * 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..ffb7fcbf 100644 --- a/tests/Unit/Aggregation/Metric/GeoBoundsAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/GeoBoundsAggregationTest.php @@ -9,8 +9,11 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Metric\Aggregation; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Metric\GeoBoundsAggregation; /** @@ -18,31 +21,22 @@ */ class GeoBoundsAggregationTest extends \PHPUnit\Framework\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(); } - /** - * Tests getType method. - */ - public function testGeoBoundsAggregationGetType() + public function testGeoBoundsAggregationGetType(): void { $agg = new GeoBoundsAggregation('foo'); $result = $agg->getType(); $this->assertEquals('geo_bounds', $result); } - /** - * Tests getArray method. - */ - public function testGeoBoundsAggregationGetArray() + public function testGeoBoundsAggregationGetArray(): void { $agg = new GeoBoundsAggregation('foo'); $agg->setField('bar'); diff --git a/tests/Unit/Aggregation/Metric/GeoCentroidAggregationTest.php b/tests/Unit/Aggregation/Metric/GeoCentroidAggregationTest.php index dca14573..8d576f7e 100644 --- a/tests/Unit/Aggregation/Metric/GeoCentroidAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/GeoCentroidAggregationTest.php @@ -9,8 +9,11 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Metric\Aggregation; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Metric\GeoCentroidAggregation; /** @@ -18,30 +21,21 @@ */ class GeoCentroidAggregationTest extends \PHPUnit\Framework\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(); } - /** - * Tests getType method. - */ - public function testGeoCentroidAggregationGetType() + public function testGeoCentroidAggregationGetType(): void { $aggregation = new GeoCentroidAggregation('foo'); $this->assertEquals('geo_centroid', $aggregation->getType()); } - /** - * 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..6abcdf13 100644 --- a/tests/Unit/Aggregation/Metric/PercentileRanksAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/PercentileRanksAggregationTest.php @@ -11,6 +11,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Metric\Aggregation; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Metric\PercentileRanksAggregation; /** @@ -18,47 +19,32 @@ */ class PercentileRanksAggregationTest extends \PHPUnit\Framework\TestCase { - /** - * @var PercentileRanksAggregation - */ - public $agg; + public PercentileRanksAggregation $agg; - /** - * Phpunit setup. - */ - public function setUp() + public 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 +52,7 @@ public function testIfExceptionIsThrownWhenScriptSetAndValueNotSet() /** * Test getType method. */ - public function testGetType() + public function testGetType(): void { $this->assertEquals('percentile_ranks', $this->agg->getType()); } @@ -74,7 +60,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..3b29f310 100644 --- a/tests/Unit/Aggregation/Metric/PercentilesAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/PercentilesAggregationTest.php @@ -9,37 +9,31 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Metric\Aggregation; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Metric\PercentilesAggregation; class PercentilesAggregationTest extends \PHPUnit\Framework\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(); } - /** - * Test getType method. - */ - public function testGetType() + public function testGetType(): void { $aggregation = new PercentilesAggregation('bar'); $this->assertEquals('percentiles', $aggregation->getType()); } - /** - * Test getArray method. - */ - public function testGetArray() + public function testGetArray(): void { $aggregation = new PercentilesAggregation('bar', 'fieldValue', ['percentsValue']); $this->assertSame( diff --git a/tests/Unit/Aggregation/Pipeline/BucketScriptAggregationTest.php b/tests/Unit/Aggregation/Pipeline/BucketScriptAggregationTest.php index 16c85b62..ea18c365 100644 --- a/tests/Unit/Aggregation/Pipeline/BucketScriptAggregationTest.php +++ b/tests/Unit/Aggregation/Pipeline/BucketScriptAggregationTest.php @@ -9,8 +9,11 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\BucketScriptAggregation; /** @@ -18,10 +21,7 @@ */ class BucketScriptAggregationTest extends \PHPUnit\Framework\TestCase { - /** - * Tests toArray method. - */ - public function testToArray() + public function testToArray(): void { $aggregation = new BucketScriptAggregation( 'test', @@ -47,15 +47,11 @@ public function testToArray() $this->assertEquals($expected, $aggregation->toArray()); } - /** - * 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..32d9504d 100644 --- a/tests/Unit/Aggregation/Pipeline/BucketSelectorAggregationTest.php +++ b/tests/Unit/Aggregation/Pipeline/BucketSelectorAggregationTest.php @@ -9,8 +9,11 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline; +use LogicException; use ONGR\ElasticsearchDSL\Aggregation\Pipeline\BucketSelectorAggregation; /** @@ -18,10 +21,7 @@ */ class BucketSelectorAggregationTest extends \PHPUnit\Framework\TestCase { - /** - * Tests toArray method. - */ - public function testToArray() + public function testToArray(): void { $aggregation = new BucketSelectorAggregation( 'test', @@ -45,15 +45,11 @@ public function testToArray() $this->assertEquals($expected, $aggregation->toArray()); } - /** - * 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/ParametersTraitTest.php b/tests/Unit/ParametersTraitTest.php index d456e288..21bebe9f 100644 --- a/tests/Unit/ParametersTraitTest.php +++ b/tests/Unit/ParametersTraitTest.php @@ -26,7 +26,7 @@ class ParametersTraitTest extends \PHPUnit\Framework\TestCase /** * {@inheritdoc} */ - public function setUp() + public function setUp(): void { $this->parametersTraitMock = $this->getMockForTrait('ONGR\ElasticsearchDSL\ParametersTrait'); } diff --git a/tests/Unit/Query/Compound/BoolQueryTest.php b/tests/Unit/Query/Compound/BoolQueryTest.php index 93fe2db7..df59ed27 100644 --- a/tests/Unit/Query/Compound/BoolQueryTest.php +++ b/tests/Unit/Query/Compound/BoolQueryTest.php @@ -9,33 +9,30 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Compound; use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery; use ONGR\ElasticsearchDSL\Query\MatchAllQuery; use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; +use UnexpectedValueException; /** * Unit test for Bool. */ class BoolQueryTest extends \PHPUnit\Framework\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'); } - /** - * Tests constructor builds container - */ - public function testBoolConstructor() + public function testBoolConstructor(): void { $bool = new BoolQuery([ BoolQuery::SHOULD => [new TermQuery('key1', 'value1')], @@ -43,7 +40,7 @@ public function testBoolConstructor() new TermQuery('key2', 'value2'), new TermQuery('key3', 'value3'), ], - BoolQuery::MUST_NOT => new TermQuery('key4', 'value4') + BoolQuery::MUST_NOT => [new TermQuery('key4', 'value4')] ]); $expected = [ @@ -79,23 +76,17 @@ public function testBoolConstructor() $this->assertEquals($expected, $bool->toArray()); } - /** - * 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')], ]); } - /** - * Tests toArray() method. - */ - public function testBoolToArray() + public function testBoolToArray(): void { $bool = new BoolQuery(); $bool->add(new TermQuery('key1', 'value1'), BoolQuery::SHOULD); @@ -129,20 +120,14 @@ public function testBoolToArray() $this->assertEquals($expected, $bool->toArray()); } - /** - * 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()); } - /** - * Tests bool query in filter context. - */ - public function testBoolInFilterContext() + public function testBoolInFilterContext(): void { $bool = new BoolQuery(); $bool->add(new TermQuery('key1', 'value1'), BoolQuery::FILTER); @@ -168,10 +153,7 @@ public function testBoolInFilterContext() $this->assertEquals($expected, $bool->toArray()); } - /** - * 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); @@ -183,20 +165,14 @@ public function testSingleMust() $this->assertEquals($expected, $bool->toArray()); } - /** - * 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'); @@ -208,20 +184,14 @@ public function testGetQueries() $this->assertSame(array('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'); diff --git a/tests/Unit/Query/Compound/FunctionScoreQueryTest.php b/tests/Unit/Query/Compound/FunctionScoreQueryTest.php index 9997b0b7..07d658eb 100644 --- a/tests/Unit/Query/Compound/FunctionScoreQueryTest.php +++ b/tests/Unit/Query/Compound/FunctionScoreQueryTest.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Compound; use ONGR\ElasticsearchDSL\BuilderInterface; @@ -26,7 +28,7 @@ class FunctionScoreQueryTest extends \PHPUnit\Framework\TestCase * * @return array */ - public function addRandomFunctionProvider() + public function addRandomFunctionProvider(): array { return [ // Case #0. No seed. @@ -64,7 +66,7 @@ public function addRandomFunctionProvider() * * @dataProvider addRandomFunctionProvider */ - public function testAddRandomFunction($seed, $expectedArray) + public function testAddRandomFunction(mixed $seed, array $expectedArray): void { /** @var MatchAllQuery|MockObject $matchAllQuery */ $matchAllQuery = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\MatchAllQuery')->getMock(); @@ -74,11 +76,8 @@ public function testAddRandomFunction($seed, $expectedArray) $this->assertEquals(['function_score' => $expectedArray], $functionScoreQuery->toArray()); } - - /** - * Tests default argument values. - */ - public function testAddFieldValueFactorFunction() + + public function testAddFieldValueFactorFunction(): void { /** @var BuilderInterface|MockObject $builderInterface */ $builderInterface = $this->getMockForAbstractClass('ONGR\ElasticsearchDSL\BuilderInterface'); diff --git a/tests/Unit/Query/Geo/GeoBoundingBoxQueryTest.php b/tests/Unit/Query/Geo/GeoBoundingBoxQueryTest.php index bc9c01b1..7b00f1dd 100644 --- a/tests/Unit/Query/Geo/GeoBoundingBoxQueryTest.php +++ b/tests/Unit/Query/Geo/GeoBoundingBoxQueryTest.php @@ -9,19 +9,18 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Geo; +use LogicException; use ONGR\ElasticsearchDSL\Query\Geo\GeoBoundingBoxQuery; class GeoBoundingBoxQueryTest extends \PHPUnit\Framework\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(); } @@ -31,7 +30,7 @@ public function testGeoBoundBoxQueryException() * * @return array */ - public function getArrayDataProvider() + public function getArrayDataProvider(): array { return [ // Case #1 (2 values). @@ -108,15 +107,19 @@ public function getArrayDataProvider() /** * Tests toArray method. * - * @param string $field Field name. - * @param array $values Bounding box values. - * @param array $parameters Optional parameters. - * @param array $expected Expected result. + * @param string $field Field name. + * @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) - { + public function testToArray( + string $field, + array $values, + array $parameters, + array $expected + ): void { $query = new GeoBoundingBoxQuery($field, $values, $parameters); $result = $query->toArray(); $this->assertEquals(['geo_bounding_box' => $expected], $result); diff --git a/tests/Unit/Query/Span/SpanOrQueryTest.php b/tests/Unit/Query/Span/SpanOrQueryTest.php index 537caf44..6aa203d3 100644 --- a/tests/Unit/Query/Span/SpanOrQueryTest.php +++ b/tests/Unit/Query/Span/SpanOrQueryTest.php @@ -43,7 +43,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/Specialized/TemplateQueryTest.php b/tests/Unit/Query/Specialized/TemplateQueryTest.php index 80e497a5..09ba484c 100644 --- a/tests/Unit/Query/Specialized/TemplateQueryTest.php +++ b/tests/Unit/Query/Specialized/TemplateQueryTest.php @@ -9,8 +9,11 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Specialized; +use InvalidArgumentException; use ONGR\ElasticsearchDSL\Query\Specialized\TemplateQuery; /** @@ -18,10 +21,7 @@ */ class TemplateQueryTest extends \PHPUnit\Framework\TestCase { - /** - * Tests toArray() method with inline. - */ - public function testToArrayInline() + public function testToArrayInline(): void { $inline = '"term": {"field": "{{query_string}}"}'; $params = ['query_string' => 'all about search']; @@ -35,10 +35,7 @@ public function testToArrayInline() $this->assertEquals($expected, $query->toArray()); } - /** - * Tests toArray() method with file - */ - public function testToArrayFile() + public function testToArrayFile(): void { $file = 'my_template'; $params = ['query_string' => 'all about search']; @@ -54,13 +51,9 @@ public function testToArrayFile() $this->assertEquals($expected, $query->toArray()); } - /** - * 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/SearchEndpoint/SearchEndpointFactoryTest.php b/tests/Unit/SearchEndpoint/SearchEndpointFactoryTest.php index b33028bb..3ac4b19c 100644 --- a/tests/Unit/SearchEndpoint/SearchEndpointFactoryTest.php +++ b/tests/Unit/SearchEndpoint/SearchEndpointFactoryTest.php @@ -9,8 +9,11 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +use LogicException; use ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointFactory; @@ -19,20 +22,14 @@ */ class SearchEndpointFactoryTest extends \PHPUnit\Framework\TestCase { - /** - * Tests get method exception. - * - * @expectedException \RuntimeException - */ - public function testGet() + public function testGet(): void { + $this->expectException(LogicException::class); + SearchEndpointFactory::get('foo'); } - /** - * Tests if factory can create endpoint. - */ - public function testFactory() + public function testFactory(): void { SearchEndpointFactory::get(AggregationsEndpoint::NAME); } From ce2bbc7a4125a0366d70d7a4fc7ed8564062fa47 Mon Sep 17 00:00:00 2001 From: Khalimov Renato Date: Thu, 28 Jan 2021 19:00:37 +0300 Subject: [PATCH 2/3] refactored: issues of code-review --- composer.json | 9 ++++---- src/Aggregation/AbstractAggregation.php | 9 +++++--- .../AutoDateHistogramAggregation.php | 8 +++---- .../Bucketing/ChildrenAggregation.php | 2 -- .../Bucketing/CompositeAggregation.php | 4 +++- .../Bucketing/DateHistogramAggregation.php | 4 ---- .../Bucketing/DateRangeAggregation.php | 8 +++++-- .../DiversifiedSamplerAggregation.php | 16 +++---------- .../Bucketing/FilterAggregation.php | 4 ---- .../Bucketing/FiltersAggregation.php | 12 ++++++---- .../Bucketing/GeoDistanceAggregation.php | 23 ++++++++++--------- .../Bucketing/GeoHashGridAggregation.php | 5 ---- .../Bucketing/HistogramAggregation.php | 11 ++++----- .../Bucketing/Ipv4RangeAggregation.php | 4 +++- .../Bucketing/MissingAggregation.php | 2 -- .../Bucketing/NestedAggregation.php | 2 -- .../Bucketing/RangeAggregation.php | 13 +++++++---- .../Bucketing/ReverseNestedAggregation.php | 2 -- .../Bucketing/SamplerAggregation.php | 1 - .../Bucketing/TermsAggregation.php | 2 -- .../Matrix/MatrixStatsAggregation.php | 1 - .../Metric/ExtendedStatsAggregation.php | 11 --------- .../Metric/GeoBoundsAggregation.php | 1 - .../Metric/PercentileRanksAggregation.php | 6 +++-- .../Metric/PercentilesAggregation.php | 2 ++ src/Aggregation/Metric/TopHitsAggregation.php | 6 +++-- .../Pipeline/AbstractPipelineAggregation.php | 9 ++++++++ src/BuilderInterface.php | 2 +- src/InnerHit/ParentInnerHit.php | 9 ++++++++ src/Query/Compound/BoolQuery.php | 6 ++++- src/Query/Compound/FunctionScoreQuery.php | 2 +- src/Query/Geo/GeoBoundingBoxQuery.php | 9 -------- src/Query/Joining/HasChildQuery.php | 9 -------- src/Query/Joining/ParentIdQuery.php | 9 ++++++++ src/Query/Specialized/TemplateQuery.php | 9 -------- src/Query/TermLevel/FuzzyQuery.php | 9 -------- src/Query/TermLevel/IdsQuery.php | 9 -------- src/Query/TermLevel/RegexpQuery.php | 9 -------- src/Query/TermLevel/TermQuery.php | 9 -------- src/Query/TermLevel/TermsQuery.php | 2 +- src/SearchEndpoint/InnerHitsEndpoint.php | 9 -------- .../Bucketing/DateRangeAggregationTest.php | 4 ++-- .../Bucketing/FilterAggregationTest.php | 3 ++- .../Bucketing/GeoDistanceAggregationTest.php | 4 ++-- .../SearchEndpointFactoryTest.php | 8 ++++--- 45 files changed, 120 insertions(+), 178 deletions(-) diff --git a/composer.json b/composer.json index 1cc93d8a..15b7b50f 100644 --- a/composer.json +++ b/composer.json @@ -11,14 +11,15 @@ } ], "require": { - "php": "^7.0", - "symfony/serializer": "^3.0|^4.0", + "php": "^8.0", + "symfony/serializer": "5.*", "paragonie/random_compat": "*" }, "require-dev": { "elasticsearch/elasticsearch": "^7.0", - "phpunit/phpunit": "~6.0", - "squizlabs/php_codesniffer": "^3.0" + "phpunit/phpunit": "~9.0", + "squizlabs/php_codesniffer": "^3.0", + "friendsofphp/php-cs-fixer": "2.18.x-dev" }, "suggest": { "elasticsearch/elasticsearch": "This library is for elasticsearch/elasticsearch client to enhance it with DSL functionality." diff --git a/src/Aggregation/AbstractAggregation.php b/src/Aggregation/AbstractAggregation.php index eb86e707..81bfe2a6 100644 --- a/src/Aggregation/AbstractAggregation.php +++ b/src/Aggregation/AbstractAggregation.php @@ -9,8 +9,6 @@ * file that was distributed with this source code. */ -declare(strict_types=1); - namespace ONGR\ElasticsearchDSL\Aggregation; use ONGR\ElasticsearchDSL\BuilderBag; @@ -35,7 +33,7 @@ abstract class AbstractAggregation implements NamedBuilderInterface abstract protected function supportsNesting(): bool; - abstract protected function getArray(): array | stdClass; + abstract protected function getArray(): array|stdClass|null; public function __construct(string $name) { @@ -65,6 +63,11 @@ public function addAggregation(AbstractAggregation $abstractAggregation): static return $this; } + /** + * Returns all sub aggregations. + * + * @return BuilderBag[]|NamedBuilderInterface[] + */ public function getAggregations(): array { if ($this->aggregations) { diff --git a/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php b/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php index 233f0eaf..9a6a5caf 100644 --- a/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php +++ b/src/Aggregation/Bucketing/AutoDateHistogramAggregation.php @@ -27,10 +27,10 @@ class AutoDateHistogramAggregation extends AbstractAggregation use BucketingTrait; public function __construct( - private string $name, - private string $field, - private ?int $buckets = null, - private ?string $format = null + string $name, + string $field, + ?int $buckets = null, + ?string $format = null ) { parent::__construct($name); diff --git a/src/Aggregation/Bucketing/ChildrenAggregation.php b/src/Aggregation/Bucketing/ChildrenAggregation.php index 2e0a586b..52f0338f 100644 --- a/src/Aggregation/Bucketing/ChildrenAggregation.php +++ b/src/Aggregation/Bucketing/ChildrenAggregation.php @@ -28,8 +28,6 @@ class ChildrenAggregation extends AbstractAggregation public function __construct(private string $name, private ?string $children = null) { parent::__construct($name); - - $this->setChildren($children); } public function getChildren(): ?string diff --git a/src/Aggregation/Bucketing/CompositeAggregation.php b/src/Aggregation/Bucketing/CompositeAggregation.php index 820d8289..9910e1b7 100644 --- a/src/Aggregation/Bucketing/CompositeAggregation.php +++ b/src/Aggregation/Bucketing/CompositeAggregation.php @@ -30,7 +30,9 @@ class CompositeAggregation extends AbstractAggregation private array $after = []; - public function __construct(private string $name, private array $sources = []) + private array $sources = []; + + public function __construct(private string $name, array $sources = []) { parent::__construct($name); diff --git a/src/Aggregation/Bucketing/DateHistogramAggregation.php b/src/Aggregation/Bucketing/DateHistogramAggregation.php index f111d25a..bd75b73a 100644 --- a/src/Aggregation/Bucketing/DateHistogramAggregation.php +++ b/src/Aggregation/Bucketing/DateHistogramAggregation.php @@ -32,10 +32,6 @@ public function __construct( private $format = null ) { parent::__construct($name); - - $this->setField($field); - $this->setInterval($interval); - $this->setFormat($format); } public function getInterval(): mixed diff --git a/src/Aggregation/Bucketing/DateRangeAggregation.php b/src/Aggregation/Bucketing/DateRangeAggregation.php index ea5aea8a..dd1a82b4 100644 --- a/src/Aggregation/Bucketing/DateRangeAggregation.php +++ b/src/Aggregation/Bucketing/DateRangeAggregation.php @@ -25,12 +25,16 @@ class DateRangeAggregation extends AbstractAggregation { use BucketingTrait; + private array $ranges = []; + + private bool $keyed = false; + public function __construct( private string $name, private ?string $field = null, private ?string $format = null, - private array $ranges = [], - private bool $keyed = false + array $ranges = [], + bool $keyed = false ) { parent::__construct($name); diff --git a/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php b/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php index 3aae71ba..3aa172fe 100644 --- a/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php +++ b/src/Aggregation/Bucketing/DiversifiedSamplerAggregation.php @@ -25,22 +25,12 @@ class DiversifiedSamplerAggregation extends AbstractAggregation { use BucketingTrait; - /** - * DiversifiedSamplerAggregation constructor. - * - * @param string $name Aggregation name - * @param string $field Elasticsearch field name - * @param int $shardSize Shard size, by default it's 100 - */ public function __construct( - protected string $name, - protected ?string $field = null, - protected ?int $shardSize = null + private string $name, + private ?string $field = null, + private ?int $shardSize = null ) { parent::__construct($name); - - $this->setField($field); - $this->setShardSize($shardSize); } public function getShardSize(): int diff --git a/src/Aggregation/Bucketing/FilterAggregation.php b/src/Aggregation/Bucketing/FilterAggregation.php index 99d1525b..24f72fee 100644 --- a/src/Aggregation/Bucketing/FilterAggregation.php +++ b/src/Aggregation/Bucketing/FilterAggregation.php @@ -31,10 +31,6 @@ public function __construct( private ?BuilderInterface $filter = null ) { parent::__construct($name); - - if ($filter !== null) { - $this->setFilter($filter); - } } public function setFilter(BuilderInterface $filter): static diff --git a/src/Aggregation/Bucketing/FiltersAggregation.php b/src/Aggregation/Bucketing/FiltersAggregation.php index 3644884d..a5587393 100644 --- a/src/Aggregation/Bucketing/FiltersAggregation.php +++ b/src/Aggregation/Bucketing/FiltersAggregation.php @@ -26,10 +26,14 @@ class FiltersAggregation extends AbstractAggregation { use BucketingTrait; + private array $filters = []; + + private bool $anonymous = false; + public function __construct( - private string $name, - private array $filters = [], - private bool $anonymous = false + string $name, + ?array $filters = [], + bool $anonymous = false ) { parent::__construct($name); @@ -63,7 +67,7 @@ public function addFilter(?BuilderInterface $filter = null, string $name = ''): return $this; } - public function getArray(): array + public function getArray(): ?array { return $this->filters; } diff --git a/src/Aggregation/Bucketing/GeoDistanceAggregation.php b/src/Aggregation/Bucketing/GeoDistanceAggregation.php index 1c3484fb..cea11dd0 100644 --- a/src/Aggregation/Bucketing/GeoDistanceAggregation.php +++ b/src/Aggregation/Bucketing/GeoDistanceAggregation.php @@ -25,16 +25,17 @@ class GeoDistanceAggregation extends AbstractAggregation { use BucketingTrait; - /** - * Inner aggregations container init. - */ + private mixed $origin; + + private $ranges = []; + public function __construct( - protected string $name, - protected ?string $field = null, - protected ?string $origin = null, - protected array $ranges = [], - protected ?string $unit = null, - protected ?string $distanceType = null + private string $name, + ?string $field = null, + mixed $origin = null, + array $ranges = [], + ?string $unit = null, + ?string $distanceType = null ) { parent::__construct($name); @@ -49,12 +50,12 @@ public function __construct( $this->setDistanceType($distanceType); } - public function getOrigin(): ?string + public function getOrigin(): mixed { return $this->origin; } - public function setOrigin(?string $origin): static + public function setOrigin(mixed $origin): static { $this->origin = $origin; diff --git a/src/Aggregation/Bucketing/GeoHashGridAggregation.php b/src/Aggregation/Bucketing/GeoHashGridAggregation.php index bc242840..17e43f03 100644 --- a/src/Aggregation/Bucketing/GeoHashGridAggregation.php +++ b/src/Aggregation/Bucketing/GeoHashGridAggregation.php @@ -33,11 +33,6 @@ public function __construct( private ?int $shardSize = null ) { parent::__construct($name); - - $this->setField($field); - $this->setPrecision($precision); - $this->setSize($size); - $this->setShardSize($shardSize); } public function getPrecision(): ?int diff --git a/src/Aggregation/Bucketing/HistogramAggregation.php b/src/Aggregation/Bucketing/HistogramAggregation.php index 9d79562f..0e8d5b45 100644 --- a/src/Aggregation/Bucketing/HistogramAggregation.php +++ b/src/Aggregation/Bucketing/HistogramAggregation.php @@ -31,6 +31,8 @@ class HistogramAggregation extends AbstractAggregation protected array $extendedBounds = []; + protected ?bool $keyed = null; + public function __construct( private string $name, private ?string $field = null, @@ -38,16 +40,13 @@ public function __construct( private ?int $minDocCount = null, private ?string $orderMode = null, private string $orderDirection = self::DIRECTION_ASC, - private ?int $extendedBoundsMin = null, - private ?int $extendedBoundsMax = null, - private ?bool $keyed = null + ?int $extendedBoundsMin = null, + ?int $extendedBoundsMax = null, + ?bool $keyed = null ) { parent::__construct($name); $this->setField($field); - $this->setInterval($interval); - $this->setMinDocCount($minDocCount); - $this->setOrder($orderMode, $orderDirection); $this->setExtendedBounds($extendedBoundsMin, $extendedBoundsMax); $this->setKeyed($keyed); } diff --git a/src/Aggregation/Bucketing/Ipv4RangeAggregation.php b/src/Aggregation/Bucketing/Ipv4RangeAggregation.php index 60b8783f..99bb6786 100644 --- a/src/Aggregation/Bucketing/Ipv4RangeAggregation.php +++ b/src/Aggregation/Bucketing/Ipv4RangeAggregation.php @@ -25,10 +25,12 @@ class Ipv4RangeAggregation extends AbstractAggregation { use BucketingTrait; + private array $ranges = []; + public function __construct( private string $name, private ?string $field = null, - private array $ranges = [] + array $ranges = [] ) { parent::__construct($name); diff --git a/src/Aggregation/Bucketing/MissingAggregation.php b/src/Aggregation/Bucketing/MissingAggregation.php index e7867b13..de99907a 100644 --- a/src/Aggregation/Bucketing/MissingAggregation.php +++ b/src/Aggregation/Bucketing/MissingAggregation.php @@ -28,8 +28,6 @@ class MissingAggregation extends AbstractAggregation public function __construct(private string $name, private ?string $field = null) { parent::__construct($name); - - $this->setField($field); } public function getArray(): array diff --git a/src/Aggregation/Bucketing/NestedAggregation.php b/src/Aggregation/Bucketing/NestedAggregation.php index 0d188fc6..858c7485 100644 --- a/src/Aggregation/Bucketing/NestedAggregation.php +++ b/src/Aggregation/Bucketing/NestedAggregation.php @@ -28,8 +28,6 @@ class NestedAggregation extends AbstractAggregation public function __construct(private string $name, private ?string $path = null) { parent::__construct($name); - - $this->setPath($path); } public function getPath(): ?string diff --git a/src/Aggregation/Bucketing/RangeAggregation.php b/src/Aggregation/Bucketing/RangeAggregation.php index 2c82c2ce..0b14bbb4 100644 --- a/src/Aggregation/Bucketing/RangeAggregation.php +++ b/src/Aggregation/Bucketing/RangeAggregation.php @@ -1,7 +1,5 @@ setField($field); $this->setKeyed($keyed); + foreach ($ranges as $range) { $from = isset($range['from']) ? $range['from'] : null; $to = isset($range['to']) ? $range['to'] : null; diff --git a/src/Aggregation/Bucketing/ReverseNestedAggregation.php b/src/Aggregation/Bucketing/ReverseNestedAggregation.php index cc537c38..5d163aeb 100644 --- a/src/Aggregation/Bucketing/ReverseNestedAggregation.php +++ b/src/Aggregation/Bucketing/ReverseNestedAggregation.php @@ -29,8 +29,6 @@ class ReverseNestedAggregation extends AbstractAggregation public function __construct(private string $name, private ?string $path = null) { parent::__construct($name); - - $this->setPath($path); } public function getPath(): ?string diff --git a/src/Aggregation/Bucketing/SamplerAggregation.php b/src/Aggregation/Bucketing/SamplerAggregation.php index 5b43657e..2b5458ee 100644 --- a/src/Aggregation/Bucketing/SamplerAggregation.php +++ b/src/Aggregation/Bucketing/SamplerAggregation.php @@ -33,7 +33,6 @@ public function __construct( parent::__construct($name); $this->setField($field); - $this->setShardSize($shardSize); } public function getShardSize(): ?int diff --git a/src/Aggregation/Bucketing/TermsAggregation.php b/src/Aggregation/Bucketing/TermsAggregation.php index 5b92dd74..e112162d 100644 --- a/src/Aggregation/Bucketing/TermsAggregation.php +++ b/src/Aggregation/Bucketing/TermsAggregation.php @@ -9,8 +9,6 @@ * file that was distributed with this source code. */ -declare(strict_types=1); - namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; diff --git a/src/Aggregation/Matrix/MatrixStatsAggregation.php b/src/Aggregation/Matrix/MatrixStatsAggregation.php index 38205081..6692210a 100644 --- a/src/Aggregation/Matrix/MatrixStatsAggregation.php +++ b/src/Aggregation/Matrix/MatrixStatsAggregation.php @@ -34,7 +34,6 @@ public function __construct( parent::__construct($name); $this->setField($field); - $this->setMode($mode); } public function getMode(): string diff --git a/src/Aggregation/Metric/ExtendedStatsAggregation.php b/src/Aggregation/Metric/ExtendedStatsAggregation.php index cf6d320e..f30500d6 100644 --- a/src/Aggregation/Metric/ExtendedStatsAggregation.php +++ b/src/Aggregation/Metric/ExtendedStatsAggregation.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace ONGR\ElasticsearchDSL\Aggregation\Metric; use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; @@ -35,8 +26,6 @@ public function __construct( parent::__construct($name); $this->setField($field); - $this->setSigma($sigma); - $this->setScript($script); } public function getSigma(): ?int diff --git a/src/Aggregation/Metric/GeoBoundsAggregation.php b/src/Aggregation/Metric/GeoBoundsAggregation.php index f1a2d323..b3f10250 100644 --- a/src/Aggregation/Metric/GeoBoundsAggregation.php +++ b/src/Aggregation/Metric/GeoBoundsAggregation.php @@ -30,7 +30,6 @@ public function __construct(private string $name, private ?string $field = null, parent::__construct($name); $this->setField($field); - $this->setWrapLongitude($wrapLongitude); } public function isWrapLongitude(): bool diff --git a/src/Aggregation/Metric/PercentileRanksAggregation.php b/src/Aggregation/Metric/PercentileRanksAggregation.php index 004e6641..f18b03ac 100644 --- a/src/Aggregation/Metric/PercentileRanksAggregation.php +++ b/src/Aggregation/Metric/PercentileRanksAggregation.php @@ -28,6 +28,8 @@ class PercentileRanksAggregation extends AbstractAggregation use ScriptAwareTrait; + private ?int $compression = null; + public function __construct( private string $name, private ?string $field = null, @@ -89,12 +91,12 @@ public function getArray(): array return $out; } - private function isRequiredParametersSet(array $a): void + private function isRequiredParametersSet(array $a): bool { if (array_key_exists('field', $a) && array_key_exists('values', $a) || (array_key_exists('script', $a) && array_key_exists('values', $a)) ) { - return; + 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 f9ddfc28..bc62bafa 100644 --- a/src/Aggregation/Metric/PercentilesAggregation.php +++ b/src/Aggregation/Metric/PercentilesAggregation.php @@ -28,6 +28,8 @@ class PercentilesAggregation extends AbstractAggregation use ScriptAwareTrait; + private ?int $compression = null; + public function __construct( private string $name, private ?string $field = null, diff --git a/src/Aggregation/Metric/TopHitsAggregation.php b/src/Aggregation/Metric/TopHitsAggregation.php index b0eeae2e..9860c434 100644 --- a/src/Aggregation/Metric/TopHitsAggregation.php +++ b/src/Aggregation/Metric/TopHitsAggregation.php @@ -89,9 +89,11 @@ public function getType(): string public function getArray(): array|stdClass { - $sortsOutput = []; + $sortsOutput = null; $addedSorts = array_filter($this->getSorts()); + if ($addedSorts) { + $sortsOutput = []; foreach ($addedSorts as $sort) { $sortsOutput[] = $sort->toArray(); } @@ -106,7 +108,7 @@ public function getArray(): array|stdClass fn(mixed $val): bool => (($val || is_array($val) || ($val || is_numeric($val)))) ); - return $output ?: new stdClass(); + return $output ?? new stdClass(); } public function getSort(): ?BuilderInterface diff --git a/src/Aggregation/Pipeline/AbstractPipelineAggregation.php b/src/Aggregation/Pipeline/AbstractPipelineAggregation.php index cf7e3e95..a1981458 100644 --- a/src/Aggregation/Pipeline/AbstractPipelineAggregation.php +++ b/src/Aggregation/Pipeline/AbstractPipelineAggregation.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + declare(strict_types=1); namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; diff --git a/src/BuilderInterface.php b/src/BuilderInterface.php index 927c8ef7..c328b578 100644 --- a/src/BuilderInterface.php +++ b/src/BuilderInterface.php @@ -17,7 +17,7 @@ interface BuilderInterface { - public function toArray(): array | stdClass; + public function toArray(): array | stdClass | null; public function getType(): string; } diff --git a/src/InnerHit/ParentInnerHit.php b/src/InnerHit/ParentInnerHit.php index 60fcd58b..9d9ba68f 100644 --- a/src/InnerHit/ParentInnerHit.php +++ b/src/InnerHit/ParentInnerHit.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + declare(strict_types=1); namespace ONGR\ElasticsearchDSL\InnerHit; diff --git a/src/Query/Compound/BoolQuery.php b/src/Query/Compound/BoolQuery.php index 0d4caf33..e6f01cc0 100644 --- a/src/Query/Compound/BoolQuery.php +++ b/src/Query/Compound/BoolQuery.php @@ -28,10 +28,14 @@ class BoolQuery implements BuilderInterface public const MUST = 'must'; public const MUST_NOT = 'must_not'; + public const SHOULD = 'should'; + public const FILTER = 'filter'; - public function __construct(private array $container = []) + private array $container = []; + + public function __construct(array $container = []) { foreach ($container as $type => $queries) { $queries = is_array($queries) ? $queries : [$queries]; diff --git a/src/Query/Compound/FunctionScoreQuery.php b/src/Query/Compound/FunctionScoreQuery.php index e131f853..bec5946d 100644 --- a/src/Query/Compound/FunctionScoreQuery.php +++ b/src/Query/Compound/FunctionScoreQuery.php @@ -158,7 +158,7 @@ public function addSimpleFunction(array $function): static public function toArray(): array { $query = [ - 'query' => $this->query->toArray(), + 'query' => $this->query->toArray() ?: null, 'functions' => $this->functions, ]; diff --git a/src/Query/Geo/GeoBoundingBoxQuery.php b/src/Query/Geo/GeoBoundingBoxQuery.php index 14b0bc82..bfcac788 100644 --- a/src/Query/Geo/GeoBoundingBoxQuery.php +++ b/src/Query/Geo/GeoBoundingBoxQuery.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace ONGR\ElasticsearchDSL\Query\Geo; use ONGR\ElasticsearchDSL\BuilderInterface; diff --git a/src/Query/Joining/HasChildQuery.php b/src/Query/Joining/HasChildQuery.php index 68ee2da2..90c6956f 100644 --- a/src/Query/Joining/HasChildQuery.php +++ b/src/Query/Joining/HasChildQuery.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace ONGR\ElasticsearchDSL\Query\Joining; use ONGR\ElasticsearchDSL\BuilderInterface; diff --git a/src/Query/Joining/ParentIdQuery.php b/src/Query/Joining/ParentIdQuery.php index e2707a7c..2703144b 100644 --- a/src/Query/Joining/ParentIdQuery.php +++ b/src/Query/Joining/ParentIdQuery.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + declare(strict_types=1); namespace ONGR\ElasticsearchDSL\Query\Joining; diff --git a/src/Query/Specialized/TemplateQuery.php b/src/Query/Specialized/TemplateQuery.php index a470bf63..1ff0140a 100644 --- a/src/Query/Specialized/TemplateQuery.php +++ b/src/Query/Specialized/TemplateQuery.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace ONGR\ElasticsearchDSL\Query\Specialized; use ONGR\ElasticsearchDSL\BuilderInterface; diff --git a/src/Query/TermLevel/FuzzyQuery.php b/src/Query/TermLevel/FuzzyQuery.php index faca56eb..b1c90f13 100644 --- a/src/Query/TermLevel/FuzzyQuery.php +++ b/src/Query/TermLevel/FuzzyQuery.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace ONGR\ElasticsearchDSL\Query\TermLevel; use ONGR\ElasticsearchDSL\BuilderInterface; diff --git a/src/Query/TermLevel/IdsQuery.php b/src/Query/TermLevel/IdsQuery.php index e86bcc7a..070ba55f 100644 --- a/src/Query/TermLevel/IdsQuery.php +++ b/src/Query/TermLevel/IdsQuery.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace ONGR\ElasticsearchDSL\Query\TermLevel; use ONGR\ElasticsearchDSL\BuilderInterface; diff --git a/src/Query/TermLevel/RegexpQuery.php b/src/Query/TermLevel/RegexpQuery.php index 5cba644f..7ebb9e4a 100644 --- a/src/Query/TermLevel/RegexpQuery.php +++ b/src/Query/TermLevel/RegexpQuery.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace ONGR\ElasticsearchDSL\Query\TermLevel; use ONGR\ElasticsearchDSL\BuilderInterface; diff --git a/src/Query/TermLevel/TermQuery.php b/src/Query/TermLevel/TermQuery.php index 378dde7a..c1743aff 100644 --- a/src/Query/TermLevel/TermQuery.php +++ b/src/Query/TermLevel/TermQuery.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace ONGR\ElasticsearchDSL\Query\TermLevel; use ONGR\ElasticsearchDSL\BuilderInterface; diff --git a/src/Query/TermLevel/TermsQuery.php b/src/Query/TermLevel/TermsQuery.php index 18d7a11e..7a1a8763 100644 --- a/src/Query/TermLevel/TermsQuery.php +++ b/src/Query/TermLevel/TermsQuery.php @@ -1,5 +1,6 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace ONGR\ElasticsearchDSL\SearchEndpoint; use ONGR\ElasticsearchDSL\InnerHit\NestedInnerHit; diff --git a/tests/Unit/Aggregation/Bucketing/DateRangeAggregationTest.php b/tests/Unit/Aggregation/Bucketing/DateRangeAggregationTest.php index 9a515b4b..55e3c418 100644 --- a/tests/Unit/Aggregation/Bucketing/DateRangeAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/DateRangeAggregationTest.php @@ -67,7 +67,7 @@ public function testDateRangeAggregationGetType() * * @return array */ - public function testDateRangeAggregationConstructorProvider() + public function dateRangeAggregationConstructorProvider() { return [ // Case #0. Minimum arguments. @@ -106,7 +106,7 @@ public function testDateRangeAggregationConstructorProvider() * @param string $format * @param array $ranges * - * @dataProvider testDateRangeAggregationConstructorProvider + * @dataProvider dateRangeAggregationConstructorProvider */ public function testDateRangeAggregationConstructor($field = null, $format = null, array $ranges = null) { diff --git a/tests/Unit/Aggregation/Bucketing/FilterAggregationTest.php b/tests/Unit/Aggregation/Bucketing/FilterAggregationTest.php index 588065e7..6c117445 100644 --- a/tests/Unit/Aggregation/Bucketing/FilterAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/FilterAggregationTest.php @@ -128,7 +128,8 @@ public function testToArrayWithFilter(): void $aggregation = new FilterAggregation('test_agg'); $aggregation->setFilter(new ExistsQuery('test')); - $aggregation->toArray(); + + $this->assertIsArray($aggregation->toArray()); } /** diff --git a/tests/Unit/Aggregation/Bucketing/GeoDistanceAggregationTest.php b/tests/Unit/Aggregation/Bucketing/GeoDistanceAggregationTest.php index a22f3ae3..e76a749e 100644 --- a/tests/Unit/Aggregation/Bucketing/GeoDistanceAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/GeoDistanceAggregationTest.php @@ -47,7 +47,7 @@ public function testGeoDistanceAggregationAddRangeException(): void $agg->addRange(); } - public function testGeoDistanceAggregationGetArrayDataProvider(): array + public function geoDistanceAggregationGetArrayDataProvider(): array { $out = []; $filterData = [ @@ -77,7 +77,7 @@ public function testGeoDistanceAggregationGetArrayDataProvider(): array * @param array $filterData * @param array $expected * - * @dataProvider testGeoDistanceAggregationGetArrayDataProvider + * @dataProvider geoDistanceAggregationGetArrayDataProvider */ public function testGeoDistanceAggregationGetArray($filterData, $expected): void { diff --git a/tests/Unit/SearchEndpoint/SearchEndpointFactoryTest.php b/tests/Unit/SearchEndpoint/SearchEndpointFactoryTest.php index 3ac4b19c..fee294cd 100644 --- a/tests/Unit/SearchEndpoint/SearchEndpointFactoryTest.php +++ b/tests/Unit/SearchEndpoint/SearchEndpointFactoryTest.php @@ -13,9 +13,9 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; -use LogicException; use ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointFactory; +use RuntimeException; /** * Unit test class for search endpoint factory. @@ -24,13 +24,15 @@ class SearchEndpointFactoryTest extends \PHPUnit\Framework\TestCase { public function testGet(): void { - $this->expectException(LogicException::class); + $this->expectException(RuntimeException::class); SearchEndpointFactory::get('foo'); } public function testFactory(): void { - SearchEndpointFactory::get(AggregationsEndpoint::NAME); + $searchEndpoint = SearchEndpointFactory::get(AggregationsEndpoint::NAME); + + $this->assertInstanceOf(AggregationsEndpoint::class, $searchEndpoint); } } From a1700f1ba2a6ab9119600060d623e60a80810442 Mon Sep 17 00:00:00 2001 From: Khalimov Renato Date: Thu, 28 Jan 2021 19:02:43 +0300 Subject: [PATCH 3/3] fixed: types --- src/Aggregation/Bucketing/GeoDistanceAggregation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Aggregation/Bucketing/GeoDistanceAggregation.php b/src/Aggregation/Bucketing/GeoDistanceAggregation.php index cea11dd0..8e4344a2 100644 --- a/src/Aggregation/Bucketing/GeoDistanceAggregation.php +++ b/src/Aggregation/Bucketing/GeoDistanceAggregation.php @@ -27,7 +27,7 @@ class GeoDistanceAggregation extends AbstractAggregation private mixed $origin; - private $ranges = []; + private array $ranges = []; public function __construct( private string $name,