From 470f2a43f8cd2d0dd7d7caab10c803117f2b6134 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 24 May 2025 14:56:39 +0200 Subject: [PATCH 1/2] Improve array_search inference --- src/Type/Accessory/AccessoryArrayListType.php | 2 +- src/Type/Accessory/HasOffsetValueType.php | 8 ++++-- src/Type/Accessory/NonEmptyArrayType.php | 2 +- src/Type/Accessory/OversizedArrayType.php | 2 +- src/Type/ArrayType.php | 6 ++++- src/Type/Constant/ConstantArrayType.php | 27 ++++++++++++------- src/Type/IntersectionType.php | 4 +-- src/Type/MixedType.php | 2 +- src/Type/NeverType.php | 2 +- ...archFunctionDynamicReturnTypeExtension.php | 15 +++-------- src/Type/StaticType.php | 4 +-- src/Type/Traits/LateResolvableTypeTrait.php | 4 +-- src/Type/Traits/MaybeArrayTypeTrait.php | 2 +- src/Type/Traits/NonArrayTypeTrait.php | 2 +- src/Type/Type.php | 2 +- src/Type/UnionType.php | 4 +-- tests/PHPStan/Analyser/nsrt/array-search.php | 16 ++++++++--- tests/PHPStan/Analyser/nsrt/bug-3789.php | 2 +- tests/PHPStan/Analyser/nsrt/bug-7809.php | 4 +-- 19 files changed, 64 insertions(+), 46 deletions(-) diff --git a/src/Type/Accessory/AccessoryArrayListType.php b/src/Type/Accessory/AccessoryArrayListType.php index 3e9c952933..8b367dfe79 100644 --- a/src/Type/Accessory/AccessoryArrayListType.php +++ b/src/Type/Accessory/AccessoryArrayListType.php @@ -221,7 +221,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return new MixedType(); } - public function searchArray(Type $needleType): Type + public function searchArray(Type $needleType, TrinaryLogic $strict): Type { return new MixedType(); } diff --git a/src/Type/Accessory/HasOffsetValueType.php b/src/Type/Accessory/HasOffsetValueType.php index b1f2f9eb8f..a95c1757d4 100644 --- a/src/Type/Accessory/HasOffsetValueType.php +++ b/src/Type/Accessory/HasOffsetValueType.php @@ -254,11 +254,15 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return new NonEmptyArrayType(); } - public function searchArray(Type $needleType): Type + public function searchArray(Type $needleType, TrinaryLogic $strict): Type { if ( $needleType instanceof ConstantScalarType && $this->valueType instanceof ConstantScalarType - && $needleType->getValue() === $this->valueType->getValue() + && ( + $needleType->getValue() === $this->valueType->getValue() + // @phpstan-ignore equal.notAllowed + || ($strict->no() && $needleType->getValue() == $this->valueType->getValue()) // phpcs:ignore + ) ) { return new UnionType([ new IntegerType(), diff --git a/src/Type/Accessory/NonEmptyArrayType.php b/src/Type/Accessory/NonEmptyArrayType.php index ae4db44bec..45b0934bb6 100644 --- a/src/Type/Accessory/NonEmptyArrayType.php +++ b/src/Type/Accessory/NonEmptyArrayType.php @@ -204,7 +204,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return $this; } - public function searchArray(Type $needleType): Type + public function searchArray(Type $needleType, TrinaryLogic $strict): Type { return new MixedType(); } diff --git a/src/Type/Accessory/OversizedArrayType.php b/src/Type/Accessory/OversizedArrayType.php index 3b56b813e0..f5ce0c38b2 100644 --- a/src/Type/Accessory/OversizedArrayType.php +++ b/src/Type/Accessory/OversizedArrayType.php @@ -199,7 +199,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return $this; } - public function searchArray(Type $needleType): Type + public function searchArray(Type $needleType, TrinaryLogic $strict): Type { return new MixedType(); } diff --git a/src/Type/ArrayType.php b/src/Type/ArrayType.php index 10447c2383..279fb97ffe 100644 --- a/src/Type/ArrayType.php +++ b/src/Type/ArrayType.php @@ -434,8 +434,12 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return $this; } - public function searchArray(Type $needleType): Type + public function searchArray(Type $needleType, TrinaryLogic $strict): Type { + if ($strict->yes() && $this->getIterableValueType()->isSuperTypeOf($needleType)->no()) { + return new ConstantBooleanType(false); + } + return TypeCombinator::union($this->getIterableKeyType(), new ConstantBooleanType(false)); } diff --git a/src/Type/Constant/ConstantArrayType.php b/src/Type/Constant/ConstantArrayType.php index ba30e4f5fe..c9a7c667c4 100644 --- a/src/Type/Constant/ConstantArrayType.php +++ b/src/Type/Constant/ConstantArrayType.php @@ -874,22 +874,31 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return $builder->getArray(); } - public function searchArray(Type $needleType): Type + public function searchArray(Type $needleType, TrinaryLogic $strict): Type { $matches = []; $hasIdenticalValue = false; foreach ($this->valueTypes as $index => $valueType) { - $isNeedleSuperType = $valueType->isSuperTypeOf($needleType); - if ($isNeedleSuperType->no()) { - continue; + if ($strict->yes()) { + $isNeedleSuperType = $valueType->isSuperTypeOf($needleType); + if ($isNeedleSuperType->no()) { + continue; + } } - if ($needleType instanceof ConstantScalarType && $valueType instanceof ConstantScalarType - && $needleType->getValue() === $valueType->getValue() - && !$this->isOptionalKey($index) - ) { - $hasIdenticalValue = true; + if ($needleType instanceof ConstantScalarType && $valueType instanceof ConstantScalarType) { + // @phpstan-ignore equal.notAllowed + $isLooseEqual = $needleType->getValue() == $valueType->getValue(); // phpcs:ignore + if (!$isLooseEqual) { + continue; + } + if ( + ($strict->no() || $needleType->getValue() === $valueType->getValue()) + && !$this->isOptionalKey($index) + ) { + $hasIdenticalValue = true; + } } $matches[] = $this->keyTypes[$index]; diff --git a/src/Type/IntersectionType.php b/src/Type/IntersectionType.php index 22b6c7b0e3..06606ccf95 100644 --- a/src/Type/IntersectionType.php +++ b/src/Type/IntersectionType.php @@ -891,9 +891,9 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return $this->intersectTypes(static fn (Type $type): Type => $type->reverseArray($preserveKeys)); } - public function searchArray(Type $needleType): Type + public function searchArray(Type $needleType, TrinaryLogic $strict): Type { - return $this->intersectTypes(static fn (Type $type): Type => $type->searchArray($needleType)); + return $this->intersectTypes(static fn (Type $type): Type => $type->searchArray($needleType, $strict)); } public function shiftArray(): Type diff --git a/src/Type/MixedType.php b/src/Type/MixedType.php index 4a2fe6bf2c..a120342e02 100644 --- a/src/Type/MixedType.php +++ b/src/Type/MixedType.php @@ -257,7 +257,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return new ArrayType(new MixedType($this->isExplicitMixed), new MixedType($this->isExplicitMixed)); } - public function searchArray(Type $needleType): Type + public function searchArray(Type $needleType, TrinaryLogic $strict): Type { if ($this->isArray()->no()) { return new ErrorType(); diff --git a/src/Type/NeverType.php b/src/Type/NeverType.php index 77a07d300c..299d61c56f 100644 --- a/src/Type/NeverType.php +++ b/src/Type/NeverType.php @@ -318,7 +318,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return new NeverType(); } - public function searchArray(Type $needleType): Type + public function searchArray(Type $needleType, TrinaryLogic $strict): Type { return new NeverType(); } diff --git a/src/Type/Php/ArraySearchFunctionDynamicReturnTypeExtension.php b/src/Type/Php/ArraySearchFunctionDynamicReturnTypeExtension.php index e1662dbe5f..b392e91340 100644 --- a/src/Type/Php/ArraySearchFunctionDynamicReturnTypeExtension.php +++ b/src/Type/Php/ArraySearchFunctionDynamicReturnTypeExtension.php @@ -12,7 +12,6 @@ use PHPStan\Type\NeverType; use PHPStan\Type\NullType; use PHPStan\Type\Type; -use PHPStan\Type\TypeCombinator; use function count; #[AutowiredService] @@ -41,20 +40,14 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, } if ($argsCount < 3) { - return TypeCombinator::union($haystackArgType->getIterableKeyType(), new ConstantBooleanType(false)); - } - - $strictArgType = $scope->getType($functionCall->getArgs()[2]->value); - if (!$strictArgType->isTrue()->yes()) { - return TypeCombinator::union($haystackArgType->getIterableKeyType(), new ConstantBooleanType(false)); + $strictArgType = new ConstantBooleanType(false); + } else { + $strictArgType = $scope->getType($functionCall->getArgs()[2]->value); } $needleArgType = $scope->getType($functionCall->getArgs()[0]->value); - if ($haystackArgType->getIterableValueType()->isSuperTypeOf($needleArgType)->no()) { - return new ConstantBooleanType(false); - } - return $haystackArgType->searchArray($needleArgType); + return $haystackArgType->searchArray($needleArgType, $strictArgType->isTrue()); } } diff --git a/src/Type/StaticType.php b/src/Type/StaticType.php index e9b06e82c8..181dfbab04 100644 --- a/src/Type/StaticType.php +++ b/src/Type/StaticType.php @@ -440,9 +440,9 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return $this->getStaticObjectType()->reverseArray($preserveKeys); } - public function searchArray(Type $needleType): Type + public function searchArray(Type $needleType, TrinaryLogic $strict): Type { - return $this->getStaticObjectType()->searchArray($needleType); + return $this->getStaticObjectType()->searchArray($needleType, $strict); } public function shiftArray(): Type diff --git a/src/Type/Traits/LateResolvableTypeTrait.php b/src/Type/Traits/LateResolvableTypeTrait.php index 11f8f3048b..ef2e137fd5 100644 --- a/src/Type/Traits/LateResolvableTypeTrait.php +++ b/src/Type/Traits/LateResolvableTypeTrait.php @@ -293,9 +293,9 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return $this->resolve()->reverseArray($preserveKeys); } - public function searchArray(Type $needleType): Type + public function searchArray(Type $needleType, TrinaryLogic $strict): Type { - return $this->resolve()->searchArray($needleType); + return $this->resolve()->searchArray($needleType, $strict); } public function shiftArray(): Type diff --git a/src/Type/Traits/MaybeArrayTypeTrait.php b/src/Type/Traits/MaybeArrayTypeTrait.php index 654429b3c6..4c08acb695 100644 --- a/src/Type/Traits/MaybeArrayTypeTrait.php +++ b/src/Type/Traits/MaybeArrayTypeTrait.php @@ -84,7 +84,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return new ErrorType(); } - public function searchArray(Type $needleType): Type + public function searchArray(Type $needleType, TrinaryLogic $strict): Type { return new ErrorType(); } diff --git a/src/Type/Traits/NonArrayTypeTrait.php b/src/Type/Traits/NonArrayTypeTrait.php index 6ce17430db..aaa9357d3a 100644 --- a/src/Type/Traits/NonArrayTypeTrait.php +++ b/src/Type/Traits/NonArrayTypeTrait.php @@ -84,7 +84,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return new ErrorType(); } - public function searchArray(Type $needleType): Type + public function searchArray(Type $needleType, TrinaryLogic $strict): Type { return new ErrorType(); } diff --git a/src/Type/Type.php b/src/Type/Type.php index 7732a77639..b788fb2a03 100644 --- a/src/Type/Type.php +++ b/src/Type/Type.php @@ -152,7 +152,7 @@ public function popArray(): Type; public function reverseArray(TrinaryLogic $preserveKeys): Type; - public function searchArray(Type $needleType): Type; + public function searchArray(Type $needleType, TrinaryLogic $strict): Type; public function shiftArray(): Type; diff --git a/src/Type/UnionType.php b/src/Type/UnionType.php index 717c812cd9..620510131d 100644 --- a/src/Type/UnionType.php +++ b/src/Type/UnionType.php @@ -776,9 +776,9 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return $this->unionTypes(static fn (Type $type): Type => $type->reverseArray($preserveKeys)); } - public function searchArray(Type $needleType): Type + public function searchArray(Type $needleType, TrinaryLogic $strict): Type { - return $this->unionTypes(static fn (Type $type): Type => $type->searchArray($needleType)); + return $this->unionTypes(static fn (Type $type): Type => $type->searchArray($needleType, $strict)); } public function shiftArray(): Type diff --git a/tests/PHPStan/Analyser/nsrt/array-search.php b/tests/PHPStan/Analyser/nsrt/array-search.php index f11d02cb24..680c56844a 100644 --- a/tests/PHPStan/Analyser/nsrt/array-search.php +++ b/tests/PHPStan/Analyser/nsrt/array-search.php @@ -30,7 +30,7 @@ public function normalArrays(array $arr, string $string): void if (array_key_exists(17, $arr) && $arr[17] === 'foo') { assertType('int', array_search('foo', $arr, true)); - assertType('int|false', array_search('foo', $arr)); + assertType('int', array_search('foo', $arr)); assertType('int|false', array_search($string, $arr, true)); } } @@ -39,25 +39,33 @@ public function constantArrays(array $arr, string $string): void { /** @var array{'a', 'b', 'c'} $arr */ assertType('1', array_search('b', $arr, true)); - assertType('0|1|2|false', array_search('b', $arr)); + assertType('1', array_search('b', $arr)); assertType('0|1|2|false', array_search($string, $arr, true)); + assertType('0|1|2|false', array_search($string, $arr, false)); /** @var array{} $arr */ assertType('false', array_search('b', $arr, true)); assertType('false', array_search('b', $arr)); assertType('false', array_search($string, $arr, true)); + assertType('false', array_search($string, $arr, false)); + + /** @var array{1, '1', '2'} $arr */ + assertType('1', array_search('1', $arr, true)); + assertType('0|1', array_search('1', $arr)); + assertType('1|2|false', array_search($string, $arr, true)); + assertType('0|1|2|false', array_search($string, $arr, false)); } public function constantArraysWithOptionalKeys(array $arr, string $string): void { /** @var array{0: 'a', 1?: 'b', 2: 'c'} $arr */ assertType('1|false', array_search('b', $arr, true)); - assertType('0|1|2|false', array_search('b', $arr)); + assertType('1|false', array_search('b', $arr)); assertType('0|1|2|false', array_search($string, $arr, true)); /** @var array{0: 'a', 1?: 'b', 2: 'b'} $arr */ assertType('1|2', array_search('b', $arr, true)); - assertType('0|1|2|false', array_search('b', $arr)); + assertType('1|2', array_search('b', $arr)); assertType('0|1|2|false', array_search($string, $arr, true)); } diff --git a/tests/PHPStan/Analyser/nsrt/bug-3789.php b/tests/PHPStan/Analyser/nsrt/bug-3789.php index 133ce49b50..95934512e3 100644 --- a/tests/PHPStan/Analyser/nsrt/bug-3789.php +++ b/tests/PHPStan/Analyser/nsrt/bug-3789.php @@ -19,5 +19,5 @@ function doFoo(string $needle, array $haystack): void { assertType('0|1|2', array_search('foo', $haystack, true)); assertType('0|1|2|false', array_search($needle, $haystack)); - assertType('0|1|2|false', array_search('foo', $haystack)); + assertType('0|1|2', array_search('foo', $haystack)); } diff --git a/tests/PHPStan/Analyser/nsrt/bug-7809.php b/tests/PHPStan/Analyser/nsrt/bug-7809.php index 2769152110..a6c0c139cb 100644 --- a/tests/PHPStan/Analyser/nsrt/bug-7809.php +++ b/tests/PHPStan/Analyser/nsrt/bug-7809.php @@ -6,7 +6,7 @@ use function PHPStan\Testing\assertType; function foo(bool $strict = false): void { - assertType('0|1|2|false', array_search('c', ['a', 'b', 'c'], $strict)); + assertType('2', array_search('c', ['a', 'b', 'c'], $strict)); } function bar(): void{ @@ -14,5 +14,5 @@ function bar(): void{ } function baz(): void{ - assertType('0|1|2|false', array_search('c', ['a', 'b', 'c'], false)); + assertType('2', array_search('c', ['a', 'b', 'c'], false)); } From de8d35fbbfb45a71adc0e2ff1d39fb0932ca80ea Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 17 Jul 2025 17:10:08 +0200 Subject: [PATCH 2/2] Avoid BC break --- src/Type/Accessory/AccessoryArrayListType.php | 2 +- src/Type/Accessory/HasOffsetValueType.php | 3 ++- src/Type/Accessory/NonEmptyArrayType.php | 2 +- src/Type/Accessory/OversizedArrayType.php | 2 +- src/Type/ArrayType.php | 3 ++- src/Type/Constant/ConstantArrayType.php | 3 ++- src/Type/IntersectionType.php | 2 +- src/Type/MixedType.php | 2 +- src/Type/NeverType.php | 2 +- src/Type/StaticType.php | 2 +- src/Type/Traits/LateResolvableTypeTrait.php | 2 +- src/Type/Traits/MaybeArrayTypeTrait.php | 2 +- src/Type/Traits/NonArrayTypeTrait.php | 2 +- src/Type/Type.php | 2 +- src/Type/UnionType.php | 2 +- 15 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Type/Accessory/AccessoryArrayListType.php b/src/Type/Accessory/AccessoryArrayListType.php index 8b367dfe79..6ed6baeced 100644 --- a/src/Type/Accessory/AccessoryArrayListType.php +++ b/src/Type/Accessory/AccessoryArrayListType.php @@ -221,7 +221,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return new MixedType(); } - public function searchArray(Type $needleType, TrinaryLogic $strict): Type + public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type { return new MixedType(); } diff --git a/src/Type/Accessory/HasOffsetValueType.php b/src/Type/Accessory/HasOffsetValueType.php index a95c1757d4..5e7c834942 100644 --- a/src/Type/Accessory/HasOffsetValueType.php +++ b/src/Type/Accessory/HasOffsetValueType.php @@ -254,8 +254,9 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return new NonEmptyArrayType(); } - public function searchArray(Type $needleType, TrinaryLogic $strict): Type + public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type { + $strict ??= TrinaryLogic::createMaybe(); if ( $needleType instanceof ConstantScalarType && $this->valueType instanceof ConstantScalarType && ( diff --git a/src/Type/Accessory/NonEmptyArrayType.php b/src/Type/Accessory/NonEmptyArrayType.php index 45b0934bb6..d360d85172 100644 --- a/src/Type/Accessory/NonEmptyArrayType.php +++ b/src/Type/Accessory/NonEmptyArrayType.php @@ -204,7 +204,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return $this; } - public function searchArray(Type $needleType, TrinaryLogic $strict): Type + public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type { return new MixedType(); } diff --git a/src/Type/Accessory/OversizedArrayType.php b/src/Type/Accessory/OversizedArrayType.php index f5ce0c38b2..77fdec48fb 100644 --- a/src/Type/Accessory/OversizedArrayType.php +++ b/src/Type/Accessory/OversizedArrayType.php @@ -199,7 +199,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return $this; } - public function searchArray(Type $needleType, TrinaryLogic $strict): Type + public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type { return new MixedType(); } diff --git a/src/Type/ArrayType.php b/src/Type/ArrayType.php index 279fb97ffe..80cfc6b09f 100644 --- a/src/Type/ArrayType.php +++ b/src/Type/ArrayType.php @@ -434,8 +434,9 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return $this; } - public function searchArray(Type $needleType, TrinaryLogic $strict): Type + public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type { + $strict ??= TrinaryLogic::createMaybe(); if ($strict->yes() && $this->getIterableValueType()->isSuperTypeOf($needleType)->no()) { return new ConstantBooleanType(false); } diff --git a/src/Type/Constant/ConstantArrayType.php b/src/Type/Constant/ConstantArrayType.php index c9a7c667c4..cc1e394694 100644 --- a/src/Type/Constant/ConstantArrayType.php +++ b/src/Type/Constant/ConstantArrayType.php @@ -874,8 +874,9 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return $builder->getArray(); } - public function searchArray(Type $needleType, TrinaryLogic $strict): Type + public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type { + $strict ??= TrinaryLogic::createMaybe(); $matches = []; $hasIdenticalValue = false; diff --git a/src/Type/IntersectionType.php b/src/Type/IntersectionType.php index 06606ccf95..d1f9f85b9f 100644 --- a/src/Type/IntersectionType.php +++ b/src/Type/IntersectionType.php @@ -891,7 +891,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return $this->intersectTypes(static fn (Type $type): Type => $type->reverseArray($preserveKeys)); } - public function searchArray(Type $needleType, TrinaryLogic $strict): Type + public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type { return $this->intersectTypes(static fn (Type $type): Type => $type->searchArray($needleType, $strict)); } diff --git a/src/Type/MixedType.php b/src/Type/MixedType.php index a120342e02..1245743947 100644 --- a/src/Type/MixedType.php +++ b/src/Type/MixedType.php @@ -257,7 +257,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return new ArrayType(new MixedType($this->isExplicitMixed), new MixedType($this->isExplicitMixed)); } - public function searchArray(Type $needleType, TrinaryLogic $strict): Type + public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type { if ($this->isArray()->no()) { return new ErrorType(); diff --git a/src/Type/NeverType.php b/src/Type/NeverType.php index 299d61c56f..659368d6da 100644 --- a/src/Type/NeverType.php +++ b/src/Type/NeverType.php @@ -318,7 +318,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return new NeverType(); } - public function searchArray(Type $needleType, TrinaryLogic $strict): Type + public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type { return new NeverType(); } diff --git a/src/Type/StaticType.php b/src/Type/StaticType.php index 181dfbab04..2d5a7c3ad8 100644 --- a/src/Type/StaticType.php +++ b/src/Type/StaticType.php @@ -440,7 +440,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return $this->getStaticObjectType()->reverseArray($preserveKeys); } - public function searchArray(Type $needleType, TrinaryLogic $strict): Type + public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type { return $this->getStaticObjectType()->searchArray($needleType, $strict); } diff --git a/src/Type/Traits/LateResolvableTypeTrait.php b/src/Type/Traits/LateResolvableTypeTrait.php index ef2e137fd5..0e1dee9904 100644 --- a/src/Type/Traits/LateResolvableTypeTrait.php +++ b/src/Type/Traits/LateResolvableTypeTrait.php @@ -293,7 +293,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return $this->resolve()->reverseArray($preserveKeys); } - public function searchArray(Type $needleType, TrinaryLogic $strict): Type + public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type { return $this->resolve()->searchArray($needleType, $strict); } diff --git a/src/Type/Traits/MaybeArrayTypeTrait.php b/src/Type/Traits/MaybeArrayTypeTrait.php index 4c08acb695..a4080f0aa1 100644 --- a/src/Type/Traits/MaybeArrayTypeTrait.php +++ b/src/Type/Traits/MaybeArrayTypeTrait.php @@ -84,7 +84,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return new ErrorType(); } - public function searchArray(Type $needleType, TrinaryLogic $strict): Type + public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type { return new ErrorType(); } diff --git a/src/Type/Traits/NonArrayTypeTrait.php b/src/Type/Traits/NonArrayTypeTrait.php index aaa9357d3a..5f586ad1a6 100644 --- a/src/Type/Traits/NonArrayTypeTrait.php +++ b/src/Type/Traits/NonArrayTypeTrait.php @@ -84,7 +84,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return new ErrorType(); } - public function searchArray(Type $needleType, TrinaryLogic $strict): Type + public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type { return new ErrorType(); } diff --git a/src/Type/Type.php b/src/Type/Type.php index b788fb2a03..b9c88f51e6 100644 --- a/src/Type/Type.php +++ b/src/Type/Type.php @@ -152,7 +152,7 @@ public function popArray(): Type; public function reverseArray(TrinaryLogic $preserveKeys): Type; - public function searchArray(Type $needleType, TrinaryLogic $strict): Type; + public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type; public function shiftArray(): Type; diff --git a/src/Type/UnionType.php b/src/Type/UnionType.php index 620510131d..33069e9377 100644 --- a/src/Type/UnionType.php +++ b/src/Type/UnionType.php @@ -776,7 +776,7 @@ public function reverseArray(TrinaryLogic $preserveKeys): Type return $this->unionTypes(static fn (Type $type): Type => $type->reverseArray($preserveKeys)); } - public function searchArray(Type $needleType, TrinaryLogic $strict): Type + public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type { return $this->unionTypes(static fn (Type $type): Type => $type->searchArray($needleType, $strict)); }