Skip to content

Commit 9ee5945

Browse files
authored
Merge pull request #62 from ppavlovic/master
PHP8.2 related fixes
2 parents 1214145 + 1e21504 commit 9ee5945

9 files changed

Lines changed: 28 additions & 23 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"g4/code-coverage" : "1.*"
4343
},
4444
"require": {
45-
"php" : ">=7.3",
45+
"php" : ">=8.2",
4646
"ext-curl" : "*",
4747
"ext-json" : "*",
4848
"g4/factory" : "1.*",

src/Collection/CollectionAbstract.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ abstract class CollectionAbstract implements \Iterator, \Countable
1414

1515
protected $_total = 0;
1616

17-
public function count()
17+
public function count(): int
1818
{
1919
return $this->_total;
2020
}
2121

22-
public function current()
22+
public function current(): mixed
2323
{
2424
return $this->_getObject();
2525
}
@@ -29,29 +29,26 @@ public function getRawData()
2929
return $this->_rawData;
3030
}
3131

32-
public function key()
32+
public function key(): mixed
3333
{
3434
return $this->_pointer;
3535
}
3636

37-
public function next()
37+
public function next(): void
3838
{
3939
$row = $this->_getObject();
4040

4141
if (!empty($row)) {
4242
$this->_incrementPointer();
4343
}
44-
45-
return $row;
4644
}
4745

48-
public function rewind()
46+
public function rewind(): void
4947
{
5048
$this->_pointer = 0;
51-
return $this;
5249
}
5350

54-
public function valid()
51+
public function valid(): bool
5552
{
5653
return !is_null($this->current());
5754
}

src/Common/Bulk.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getData()
7070
/**
7171
* @return int
7272
*/
73-
public function count()
73+
public function count(): int
7474
{
7575
return count($this->data);
7676
}

src/Common/RawData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(array $data, $total)
3131
$this->total = $total;
3232
}
3333

34-
public function count()
34+
public function count(): int
3535
{
3636
if ($this->count === null) {
3737
$this->count = count($this->data);

src/Common/SimpleRawData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct(array $data)
2121
$this->data = $data;
2222
}
2323

24-
public function count()
24+
public function count(): int
2525
{
2626
if ($this->count === null) {
2727
$this->count = count($this->data);

src/Engine/Elasticsearch/ElasticsearchComparisonFormatter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class ElasticsearchComparisonFormatter implements ComparisonFormatterInterface
2929
const RANGE = 'range';
3030
const TERMS = 'terms';
3131

32+
private ElasticsearchIdentity $identity;
33+
3234
public function __construct(ElasticsearchIdentity $identity)
3335
{
3436
$this->identity = $identity;

src/Engine/Elasticsearch/ElasticsearchResponse.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,17 @@ public function getHits()
5050
*/
5151
public function getTotal()
5252
{
53+
if (!is_array($this->decodedResponse)) {
54+
return 0;
55+
}
5356
switch (true) {
54-
case $this->hasError():
55-
return 0;
5657
case array_key_exists(self::KEY_RESPONSES_M_SEARCH, $this->decodedResponse):
5758
return $this->getTotalFromMultiSearch();
5859
case array_key_exists(self::KEY_HITS, $this->decodedResponse):
5960
return (new TotalCount($this->decodedResponse[self::KEY_HITS]))->getValue();
6061
case array_key_exists(self::KEY_COUNT, $this->decodedResponse):
6162
return (new TotalCount($this->decodedResponse[self::KEY_COUNT]))->getValue();
63+
case $this->hasError():
6264
default:
6365
return 0;
6466
}
@@ -67,10 +69,10 @@ public function getTotal()
6769
/**
6870
* @return array
6971
*/
70-
private function getDecodedResponse()
72+
private function getDecodedResponse(): ?array
7173
{
7274
if ($this->decodedResponse === null) {
73-
$this->decodedResponse = json_decode($this->response, true);
75+
$this->decodedResponse = is_string($this->response) ? json_decode($this->response, true) : null;
7476
}
7577
return $this->decodedResponse;
7678
}
@@ -80,7 +82,7 @@ public function hasError()
8082
return $this->decodedResponse === null || array_key_exists(self::KEY_ERROR, $this->decodedResponse);
8183
}
8284

83-
public function getErrorMessage()
85+
public function getErrorMessage(): string
8486
{
8587
if ($this->decodedResponse === null) {
8688
return json_encode(['Error decoding response', $this->response]);
@@ -110,10 +112,7 @@ private function getHitsFromSearch()
110112
: [];
111113
}
112114

113-
/**
114-
* @return int
115-
*/
116-
private function getTotalFromMultiSearch()
115+
private function getTotalFromMultiSearch(): int
117116
{
118117
$multiSearchTotal = 0;
119118
foreach ($this->decodedResponse[self::KEY_RESPONSES_M_SEARCH] as $singleHits) {

src/Engine/Elasticsearch/ElasticsearchSelectionFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ public function where()
8484

8585
foreach ($this->identity->getComparisons() as $oneComparison) {
8686
if ($oneComparison instanceof Comparison && !$oneComparison->getValue()->isEmpty()) {
87-
if (preg_match("/^-/", $oneComparison->getName())) {
87+
$comparisonName = $oneComparison->getName() ?: '';
88+
if (str_starts_with($comparisonName, '-')) {
8889
$comparisons['must_not'][]= $oneComparison->getComparison($this->makeComparisonFormatter());
8990
} else {
9091
$comparisons['must'][]= $oneComparison->getComparison($this->makeComparisonFormatter());

tests/unit/src/Engine/Elasticsearch/ElasticsearchResponseTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,16 @@ public function testGarbageResponse()
7878

7979
protected function setUp(): void
8080
{
81+
/**
82+
* @codingStandardsIgnoreStart
83+
*/
8184
$this->dataWithHits = '{"took":16,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":null,"hits":[1]}}';
8285
$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}';
8386

8487
$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}]]';
88+
/**
89+
* @codingStandardsIgnoreEnd
90+
*/
8591
}
8692

8793
protected function tearDown(): void

0 commit comments

Comments
 (0)