diff --git a/Makefile b/Makefile index 1bf0661..1f8e47b 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ TITLE = [data-mapper] unit-tests: @/bin/echo "${TITLE} unit test suite started..." \ - && XDEBUG_MODE=off php5.6 ./vendor/bin/phpunit -c tests/unit/phpunit.xml --coverage-html tests/unit/coverage + && XDEBUG_MODE=off php ./vendor/bin/phpunit -c tests/unit/phpunit.xml --coverage-html tests/unit/coverage integration-tests: @/bin/echo "${TITLE} starting virtual machine ..." \ diff --git a/composer.json b/composer.json index 4488bd6..c4c5869 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,7 @@ "g4/code-coverage" : "1.*" }, "require": { - "php" : ">=7.3", + "php" : ">=8.2", "ext-curl" : "*", "ext-json" : "*", "g4/factory" : "1.*", diff --git a/src/Collection/CollectionAbstract.php b/src/Collection/CollectionAbstract.php index 0a66da8..4f8b727 100644 --- a/src/Collection/CollectionAbstract.php +++ b/src/Collection/CollectionAbstract.php @@ -14,12 +14,12 @@ abstract class CollectionAbstract implements \Iterator, \Countable protected $_total = 0; - public function count() + public function count(): int { return $this->_total; } - public function current() + public function current(): mixed { return $this->_getObject(); } @@ -29,29 +29,26 @@ public function getRawData() return $this->_rawData; } - public function key() + public function key(): mixed { return $this->_pointer; } - public function next() + public function next(): void { $row = $this->_getObject(); if (!empty($row)) { $this->_incrementPointer(); } - - return $row; } - public function rewind() + public function rewind(): void { $this->_pointer = 0; - return $this; } - public function valid() + public function valid(): bool { return !is_null($this->current()); } diff --git a/src/Common/Bulk.php b/src/Common/Bulk.php index d95f4b8..a8efc98 100644 --- a/src/Common/Bulk.php +++ b/src/Common/Bulk.php @@ -70,7 +70,7 @@ public function getData() /** * @return int */ - public function count() + public function count(): int { return count($this->data); } diff --git a/src/Common/RawData.php b/src/Common/RawData.php index e83540d..8ebf282 100644 --- a/src/Common/RawData.php +++ b/src/Common/RawData.php @@ -31,7 +31,7 @@ public function __construct(array $data, $total) $this->total = $total; } - public function count() + public function count(): int { if ($this->count === null) { $this->count = count($this->data); diff --git a/src/Common/SimpleRawData.php b/src/Common/SimpleRawData.php index 26f13f3..48545d3 100644 --- a/src/Common/SimpleRawData.php +++ b/src/Common/SimpleRawData.php @@ -21,7 +21,7 @@ public function __construct(array $data) $this->data = $data; } - public function count() + public function count(): int { if ($this->count === null) { $this->count = count($this->data); diff --git a/src/Engine/Elasticsearch/ElasticsearchComparisonFormatter.php b/src/Engine/Elasticsearch/ElasticsearchComparisonFormatter.php index c04545a..13f55ea 100644 --- a/src/Engine/Elasticsearch/ElasticsearchComparisonFormatter.php +++ b/src/Engine/Elasticsearch/ElasticsearchComparisonFormatter.php @@ -29,6 +29,8 @@ class ElasticsearchComparisonFormatter implements ComparisonFormatterInterface const RANGE = 'range'; const TERMS = 'terms'; + private ElasticsearchIdentity $identity; + public function __construct(ElasticsearchIdentity $identity) { $this->identity = $identity; diff --git a/src/Engine/Elasticsearch/ElasticsearchResponse.php b/src/Engine/Elasticsearch/ElasticsearchResponse.php index 503ec76..b2399c2 100644 --- a/src/Engine/Elasticsearch/ElasticsearchResponse.php +++ b/src/Engine/Elasticsearch/ElasticsearchResponse.php @@ -50,15 +50,17 @@ public function getHits() */ public function getTotal() { + if (!is_array($this->decodedResponse)) { + return 0; + } switch (true) { - case $this->hasError(): - return 0; case array_key_exists(self::KEY_RESPONSES_M_SEARCH, $this->decodedResponse): return $this->getTotalFromMultiSearch(); case array_key_exists(self::KEY_HITS, $this->decodedResponse): return (new TotalCount($this->decodedResponse[self::KEY_HITS]))->getValue(); case array_key_exists(self::KEY_COUNT, $this->decodedResponse): return (new TotalCount($this->decodedResponse[self::KEY_COUNT]))->getValue(); + case $this->hasError(): default: return 0; } @@ -67,10 +69,10 @@ public function getTotal() /** * @return array */ - private function getDecodedResponse() + private function getDecodedResponse(): ?array { if ($this->decodedResponse === null) { - $this->decodedResponse = json_decode($this->response, true); + $this->decodedResponse = is_string($this->response) ? json_decode($this->response, true) : null; } return $this->decodedResponse; } @@ -80,7 +82,7 @@ public function hasError() return $this->decodedResponse === null || array_key_exists(self::KEY_ERROR, $this->decodedResponse); } - public function getErrorMessage() + public function getErrorMessage(): string { if ($this->decodedResponse === null) { return json_encode(['Error decoding response', $this->response]); @@ -110,10 +112,7 @@ private function getHitsFromSearch() : []; } - /** - * @return int - */ - private function getTotalFromMultiSearch() + private function getTotalFromMultiSearch(): int { $multiSearchTotal = 0; foreach ($this->decodedResponse[self::KEY_RESPONSES_M_SEARCH] as $singleHits) { diff --git a/src/Engine/Elasticsearch/ElasticsearchSelectionFactory.php b/src/Engine/Elasticsearch/ElasticsearchSelectionFactory.php index c201714..9efff77 100644 --- a/src/Engine/Elasticsearch/ElasticsearchSelectionFactory.php +++ b/src/Engine/Elasticsearch/ElasticsearchSelectionFactory.php @@ -84,7 +84,8 @@ public function where() foreach ($this->identity->getComparisons() as $oneComparison) { if ($oneComparison instanceof Comparison && !$oneComparison->getValue()->isEmpty()) { - if (preg_match("/^-/", $oneComparison->getName())) { + $comparisonName = $oneComparison->getName() ?: ''; + if (str_starts_with($comparisonName, '-')) { $comparisons['must_not'][]= $oneComparison->getComparison($this->makeComparisonFormatter()); } else { $comparisons['must'][]= $oneComparison->getComparison($this->makeComparisonFormatter()); diff --git a/src/Engine/Elasticsearch/Operators/QueryStringOperator.php b/src/Engine/Elasticsearch/Operators/QueryStringOperator.php index c66e4f6..eaa5171 100644 --- a/src/Engine/Elasticsearch/Operators/QueryStringOperator.php +++ b/src/Engine/Elasticsearch/Operators/QueryStringOperator.php @@ -18,13 +18,10 @@ public function __construct($name, SingleValue $value) public function format() { return [ - QueryConnector::NAME_QUERY_STRING_QUERY => + QueryConnector::NAME_QUERY_STRING => [ - QueryConnector::NAME_QUERY_STRING => - [ - QueryConnector::NAME_QUERY_STRING_QUERY => $this->value->getValue(), - QueryConnector::ANALYZE_WILDCARD => true, - ], + QueryConnector::NAME_QUERY_STRING_QUERY => $this->value->getValue(), + QueryConnector::ANALYZE_WILDCARD => true, ], ]; } diff --git a/tests/unit/src/Engine/Elasticsearch/ElasticsearchComparisonFormatterTest.php b/tests/unit/src/Engine/Elasticsearch/ElasticsearchComparisonFormatterTest.php index 6062678..8badb35 100644 --- a/tests/unit/src/Engine/Elasticsearch/ElasticsearchComparisonFormatterTest.php +++ b/tests/unit/src/Engine/Elasticsearch/ElasticsearchComparisonFormatterTest.php @@ -29,7 +29,7 @@ protected function setUp(): void public function tearDown(): void { $this->comparisonFormatter = null; - $this->operatorMock = null; + $this->operatorMock = null; } public function testEqual() @@ -51,8 +51,8 @@ public function testEqualCI() ['match' => [ 'email' => [ 'query' => 'text@example.com', - 'type' => 'phrase' - ] + 'type' => 'phrase', + ], ]], $this->comparisonFormatter->format('email', $this->operatorMock, new SingleValue('text@example.com')) ); @@ -69,8 +69,8 @@ public function testEqualCIEsVersion7() $this->assertEquals( ['match' => [ 'email' => [ - 'query' => 'text@example.com' - ] + 'query' => 'text@example.com', + ], ]], $comparisonFormatter->format('email', $this->operatorMock, new SingleValue('text@example.com')) ); @@ -157,10 +157,10 @@ public function testTimeFromInMinutes() $this->assertEquals([ 'range' => [ 'online' => [ - 'gt' => strtotime("-15 minute", time()), + 'gt' => strtotime("-15 minute", time()), 'format' => 'epoch_second', - ] - ] + ], + ], ], $this->comparisonFormatter->format('online', $this->operatorMock, new SingleValue(15))); } @@ -214,12 +214,10 @@ public function testQueryString() $this->assertEquals( [ - 'query' => [ - 'query_string' => [ - 'query' => 'username: *test* OR email: *test*', - 'analyze_wildcard' => true, - ], - ] + 'query_string' => [ + 'query' => 'username: *test* OR email: *test*', + 'analyze_wildcard' => true, + ], ], $this->comparisonFormatter ->format('', $this->operatorMock, new SingleValue('username: *test* OR email: *test*')) @@ -237,8 +235,8 @@ public function testQueryStringVersion7() $this->assertEquals( [ 'query_string' => [ - 'query' => 'username: *test* OR email: *test*' - ] + 'query' => 'username: *test* OR email: *test*', + ], ], $comparisonFormatter->format('', $this->operatorMock, new SingleValue('username: *test* OR email: *test*')) ); diff --git a/tests/unit/src/Engine/Elasticsearch/ElasticsearchResponseTest.php b/tests/unit/src/Engine/Elasticsearch/ElasticsearchResponseTest.php index 1c00f02..a84366d 100644 --- a/tests/unit/src/Engine/Elasticsearch/ElasticsearchResponseTest.php +++ b/tests/unit/src/Engine/Elasticsearch/ElasticsearchResponseTest.php @@ -78,10 +78,16 @@ public function testGarbageResponse() protected function setUp(): void { + /** + * @codingStandardsIgnoreStart + */ $this->dataWithHits = '{"took":16,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":null,"hits":[1]}}'; $this->dataWithError = '{"error":{"root_cause":[{"type":"query_parsing_exception","reason":"field [location] is not a geo_point field","index":"profiles","line":1,"col":270}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query_fetch","grouped":true,"failed_shards":[{"shard":0,"index":"profiles","node":"75N9T595S-eqHYV8_o08ng","reason":{"type":"query_parsing_exception","reason":"field [location] is not a geo_point field","index":"profiles","line":1,"col":270}}]},"status":400}'; $this->errorMessage = '["search_phase_execution_exception",[{"type":"query_parsing_exception","reason":"field [location] is not a geo_point field","index":"profiles","line":1,"col":270}]]'; + /** + * @codingStandardsIgnoreEnd + */ } protected function tearDown(): void