Skip to content

Commit e614085

Browse files
committed
Fix phpstan warning for offsetAccess.invalidOffset
1 parent e180056 commit e614085

1 file changed

Lines changed: 49 additions & 34 deletions

File tree

src/Traits/QueryableBlock.php

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ trait QueryableBlock
1111
{
1212
public static function like(mixed $value1, mixed $value2): bool
1313
{
14-
1514
$strValue1 = strval($value1);
1615
$strValue2 = strval($value2);
1716
return str_contains($strValue1, $strValue2);
@@ -28,7 +27,6 @@ public function where(
2827
mixed $value = null,
2928
bool $preseveKeys = true,
3029
): self {
31-
3230
if (func_num_args() === 1) {
3331
$value = true;
3432
$operator = Operator::EQUAL;
@@ -43,57 +41,64 @@ public function where(
4341
foreach ($this as $key => $element) {
4442
$elementToCheck = $element;
4543
if (is_array($element)) {
46-
$elementToCheck = Block::make($element, $this->iteratorReturnsBlock);
44+
$elementToCheck = Block::make(
45+
$element,
46+
$this->iteratorReturnsBlock,
47+
);
4748
}
48-
if (! $elementToCheck instanceof Block) {
49+
if (!$elementToCheck instanceof Block) {
4950
return Block::make([], $this->iteratorReturnsBlock);
5051
}
5152

5253
$found = match ($operator) {
53-
Operator::EQUAL => ($elementToCheck->get($field) == $value),
54+
Operator::EQUAL => $elementToCheck->get($field) == $value,
5455
Operator::GREATER_THAN => $elementToCheck->get($field) > $value,
5556
Operator::LESS_THAN => $elementToCheck->get($field) < $value,
56-
Operator::GREATER_THAN_OR_EQUAL => $elementToCheck->get($field) >= $value,
57-
Operator::LESS_THAN_OR_EQUAL => $elementToCheck->get($field) <= $value,
57+
Operator::GREATER_THAN_OR_EQUAL => $elementToCheck->get(
58+
$field,
59+
) >= $value,
60+
Operator::LESS_THAN_OR_EQUAL => $elementToCheck->get($field)
61+
<= $value,
5862
Operator::NOT_EQUAL => $elementToCheck->get($field) != $value,
59-
Operator::STRICT_NOT_EQUAL => $elementToCheck->get($field) !== $value,
63+
Operator::STRICT_NOT_EQUAL => $elementToCheck->get($field)
64+
!== $value,
6065
Operator::IN => in_array($elementToCheck->get($field), $value),
6166
Operator::HAS => in_array($value, $elementToCheck->get($field)),
62-
Operator::LIKE => str_contains($elementToCheck->get($field), (string) $value),
67+
Operator::LIKE => str_contains(
68+
$elementToCheck->get($field),
69+
(string) $value,
70+
),
6371
default => $elementToCheck->get($field) === $value,
6472
};
6573
if ($found) {
66-
6774
if ($preseveKeys) {
68-
$returnData[$key] = $element instanceof Block ? $element->toArray() : $element;
75+
$returnData[$key]
76+
= $element instanceof Block
77+
? $element->toArray()
78+
: $element;
6979
} else {
70-
$returnData[] = $element instanceof Block ? $element->toArray() : $element;
71-
;
80+
$returnData[]
81+
= $element instanceof Block
82+
? $element->toArray()
83+
: $element;
7284
}
73-
7485
}
7586
}
7687
return self::make($returnData, $this->iteratorReturnsBlock);
7788
}
7889

79-
80-
public function orderBy(string|int $field, string $order = 'asc'): self
90+
public function orderBy(string|int $field, string $order = "asc"): self
8191
{
8292
$map = [];
8393
$array = $this->data;
8494

8595
foreach ($this as $key => $item) {
8696
$map[$key] = $item->get($field);
8797
}
88-
if ($order === 'desc') {
89-
array_multisort(
90-
$map,
91-
SORT_DESC,
92-
$array,
93-
);
94-
98+
if ($order === "desc") {
99+
array_multisort($map, SORT_DESC, $array);
95100
} else {
96-
array_multisort($map, $array) ;
101+
array_multisort($map, $array);
97102
}
98103

99104
return self::make($array, $this->iteratorReturnsBlock);
@@ -139,11 +144,11 @@ public function groupBy(string|int $field): self
139144

140145
foreach ($this as $value) {
141146
$property = $value->get($field);
142-
$property = self::castVariableForStrval($property);
147+
$property = self::castForArrayKey($property);
143148
if (!$property) {
144149
continue;
145150
}
146-
if (! array_key_exists(strval($property), $result)) {
151+
if (!array_key_exists(strval($property), $result)) {
147152
$result[$property] = [];
148153
}
149154
$result[$property][] = $value->toArray();
@@ -170,16 +175,26 @@ public function groupByFunction(callable $groupFunction): self
170175
return self::make($result);
171176
}
172177

173-
174-
private static function castVariableForStrval(mixed $property): bool|float|int|string|null
175-
{
178+
private static function castVariableForStrval(
179+
mixed $property,
180+
): bool|float|int|string|null {
176181
return match (gettype($property)) {
177-
'boolean' => $property,
178-
'double' => $property,
179-
'integer' => $property,
180-
'string' => $property,
182+
"boolean" => $property,
183+
"double" => $property,
184+
"integer" => $property,
185+
"string" => $property,
186+
default => null,
187+
};
188+
}
189+
private static function castForArrayKey(
190+
mixed $property,
191+
): bool|int|string|null {
192+
return match (gettype($property)) {
193+
"boolean" => $property,
194+
"double" => strval($property),
195+
"integer" => $property,
196+
"string" => $property,
181197
default => null,
182198
};
183199
}
184-
185200
}

0 commit comments

Comments
 (0)