Skip to content

Commit 30fd664

Browse files
committed
Merge branch 'fix'
2 parents 2399dfc + 6ce78ab commit 30fd664

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

src/Toolkit/Collection/ReadOnlyCollectionTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public function __construct($items = [])
3535
$this->Items = $this->getItemsArray($items);
3636
}
3737

38+
/**
39+
* @inheritDoc
40+
*/
41+
public function isEmpty(): bool
42+
{
43+
return !$this->Items;
44+
}
45+
3846
/**
3947
* @inheritDoc
4048
*/

src/Toolkit/Contract/Collection/CollectionInterface.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
use OutOfRangeException;
1212

1313
/**
14-
* An array-like collection of items
15-
*
1614
* @api
1715
*
1816
* @template TKey of array-key
@@ -60,6 +58,15 @@ interface CollectionInterface extends
6058
*/
6159
public function __construct($items = []);
6260

61+
/**
62+
* Check if the collection is empty
63+
*
64+
* @phpstan-assert-if-false non-empty-array<TKey,TValue> $this->all()
65+
* @phpstan-assert-if-false !null $this->first()
66+
* @phpstan-assert-if-false !null $this->last()
67+
*/
68+
public function isEmpty(): bool;
69+
6370
/**
6471
* Add or replace an item with a given key
6572
*
@@ -73,6 +80,9 @@ public function set($key, $value);
7380
* Check if an item with a given key exists
7481
*
7582
* @param TKey $key
83+
* @phpstan-assert-if-true non-empty-array<TKey,TValue> $this->all()
84+
* @phpstan-assert-if-true !null $this->first()
85+
* @phpstan-assert-if-true !null $this->last()
7686
*/
7787
public function has($key): bool;
7888

@@ -246,6 +256,9 @@ public function slice(int $offset, ?int $length = null);
246256
* Check if a value is in the collection
247257
*
248258
* @param TValue $value
259+
* @phpstan-assert-if-true non-empty-array<TKey,TValue> $this->all()
260+
* @phpstan-assert-if-true !null $this->first()
261+
* @phpstan-assert-if-true !null $this->last()
249262
*/
250263
public function hasValue($value, bool $strict = false): bool;
251264

src/Toolkit/Utility/Get.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,11 @@ public static function code(
553553
}
554554
$classes = Arr::toIndex($classes);
555555
$constRegex = [];
556-
foreach (array_keys($constants) as $string) {
557-
$constRegex[] = preg_quote($string, '/');
556+
if ($constants) {
557+
uksort($constants, fn($a, $b) => strlen($b) <=> strlen($a));
558+
foreach (array_keys($constants) as $string) {
559+
$constRegex[] = preg_quote($string, '/');
560+
}
558561
}
559562
switch (count($constRegex)) {
560563
case 0:

src/Toolkit/Utility/Reflect.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use ReflectionAttribute;
99
use ReflectionClass;
1010
use ReflectionClassConstant;
11+
use ReflectionConstant;
1112
use ReflectionException;
1213
use ReflectionExtension;
1314
use ReflectionFunction;
@@ -36,7 +37,7 @@ final class Reflect extends AbstractUtility
3637
/**
3738
* Get a list of names from a list of reflectors
3839
*
39-
* @param array<ReflectionAttribute<object>|ReflectionClass<object>|ReflectionClassConstant|ReflectionExtension|ReflectionFunctionAbstract|ReflectionNamedType|ReflectionParameter|ReflectionProperty|ReflectionZendExtension> $reflectors
40+
* @param array<ReflectionAttribute<object>|ReflectionClass<object>|ReflectionClassConstant|ReflectionConstant|ReflectionExtension|ReflectionFunctionAbstract|ReflectionNamedType|ReflectionParameter|ReflectionProperty|ReflectionZendExtension> $reflectors
4041
* @return string[]
4142
*/
4243
public static function getNames(array $reflectors): array
@@ -248,7 +249,7 @@ public static function getAcceptedTypes(
248249
$union[] = Arr::unwrap($intersection);
249250
}
250251

251-
/** @var array<class-string[]|class-string> */
252+
/** @var array<class-string[]|class-string>|array<string[]|string> */
252253
return $union ?? [];
253254
}
254255

@@ -314,8 +315,8 @@ private static function doGetTypes(?ReflectionType $type, bool $names): array
314315
return [];
315316
}
316317

318+
/** @var ReflectionNamedType $type */
317319
foreach (Arr::flatten(self::doNormaliseType($type)) as $type) {
318-
/** @var ReflectionNamedType $type */
319320
$name = $type->getName();
320321
if (isset($seen[$name])) {
321322
continue;

tests/phpstan-conditional.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@
1717

1818
$dir = dirname(__DIR__);
1919

20+
$ignoreErrors = \PHP_VERSION_ID >= 80400 ? [] : [
21+
[
22+
'message' => '#^Call to method getName\(\) on an unknown class ReflectionConstant\.$#',
23+
'identifier' => 'class.notFound',
24+
],
25+
[
26+
'message' => '#^Parameter \$reflectors of method Salient\\\\Utility\\\\Reflect\:\:getNames\(\) has invalid type ReflectionConstant\.$#',
27+
'identifier' => 'class.notFound',
28+
'count' => 1,
29+
'path' => "$dir/src/Toolkit/Utility/Reflect.php",
30+
],
31+
];
32+
2033
if (\PHP_VERSION_ID < 80000) {
2134
return [
2235
'includes' => $includes,
@@ -32,7 +45,7 @@
3245
"$dir/tests/unit/Toolkit/Core/EventDispatcher/listenerWithIntersectionType.php",
3346
],
3447
],
35-
'ignoreErrors' => [
48+
'ignoreErrors' => array_merge([
3649
[
3750
'message' => '#^Parameter \#1 \$ch of function curl_(?:errno|exec|getinfo|reset|setopt(?:_array)?) expects resource, CurlHandle\|resource(\|null)? given\.$#',
3851
],
@@ -66,15 +79,15 @@
6679
'count' => 1,
6780
'path' => "$dir/src/Toolkit/Utility/Str.php",
6881
],
69-
],
82+
], $ignoreErrors),
7083
] + $parameters,
7184
];
7285
}
7386

7487
return [
7588
'includes' => $includes,
7689
'parameters' => [
77-
'ignoreErrors' => [
90+
'ignoreErrors' => array_merge([
7891
[
7992
'message' => '#^Parameter \#1 \$handle of function curl_(?:errno|exec|getinfo|reset|setopt(?:_array)?) expects CurlHandle, CurlHandle\|resource(\|null)? given\.$#',
8093
],
@@ -94,6 +107,6 @@
94107
'count' => 1,
95108
'path' => "$dir/src/Toolkit/Utility/Arr.php",
96109
],
97-
],
110+
], $ignoreErrors),
98111
] + $parameters,
99112
];

0 commit comments

Comments
 (0)