Skip to content

Commit efaa82a

Browse files
ondrejmirtesclaude
andcommitted
Merge the turbo-ext review fixes (#6550)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
2 parents 4c63e37 + 1d6bd94 commit efaa82a

152 files changed

Lines changed: 4386 additions & 1212 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎build/PHPStan/Build/TurboDeclarationGenerator.php‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@
5656
* Shared by turbo-ext/bin/generate-declarations.php and the drift check in
5757
* turbo-ext/bin/side-by-side.php.
5858
*
59-
* A property whose declaration reg::Class cannot express (a string or
60-
* non-empty array default, an intersection type, a defaulted union of
61-
* classes) leaves its class without declareProperties(); the class keeps
62-
* declaring its properties by hand.
59+
* A property whose declaration reg::Class cannot express (a non-empty
60+
* array default, a string default of a nullable or union type, an
61+
* intersection type, a defaulted union of classes) leaves its class without
62+
* declareProperties(); the class keeps declaring its properties by hand.
6363
*/
6464
final class TurboDeclarationGenerator
6565
{
@@ -722,6 +722,9 @@ private function renderProperty(ReflectionProperty $property): string
722722
if ($default === false) {
723723
return sprintf('cls.property(%s, %s, reg::PropertyKind::TypedFalse, %s%s);', $name, $flags, $mask, $classArg);
724724
}
725+
if (is_string($default) && $classes === [] && $masks === ['MAY_BE_STRING']) {
726+
return sprintf('cls.typedStringProperty(%s, %s, %s);', $name, $flags, $this->cString($default));
727+
}
725728

726729
throw new RuntimeException('a typed default reg::Class cannot declare');
727730
}

‎phpstan-baseline.neon‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,12 +1392,6 @@ parameters:
13921392
count: 1
13931393
path: src/Type/IntersectionType.php
13941394

1395-
-
1396-
rawMessage: 'Doing instanceof PHPStan\Type\Enum\EnumCaseObjectType is error-prone and deprecated. Use Type::getEnumCaseObject() or Type::getEnumCases() instead.'
1397-
identifier: phpstanApi.instanceofType
1398-
count: 1
1399-
path: src/Type/IntersectionType.php
1400-
14011395
-
14021396
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
14031397
identifier: phpstanApi.instanceofType

‎src/Turbo/TurboExtensionEnabler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
final class TurboExtensionEnabler
3434
{
3535

36-
public const EXPECTED_EXTENSION_VERSION = '7dbb0bd';
36+
public const EXPECTED_EXTENSION_VERSION = '9f55e69';
3737

3838
private static bool $active = false;
3939

‎src/Type/CallableTypeHelper.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static function isParametersAcceptorSuperTypeOf(
9191
$parameterDescription,
9292
$ourParameterType->describe($verbosity),
9393
),
94-
]));
94+
]), $isSuperType->lazyReasons);
9595
}
9696

9797
$result = $result->and($isSuperType);

‎src/Type/IntersectionType.php‎

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
use PHPStan\Type\Traits\NonRemoveableTypeTrait;
5757
use function array_filter;
5858
use function array_intersect_key;
59+
use function array_key_exists;
5960
use function array_map;
6061
use function array_shift;
6162
use function array_unique;
@@ -64,13 +65,15 @@
6465
use function implode;
6566
use function in_array;
6667
use function is_int;
68+
use function is_string;
6769
use function ksort;
6870
use function sprintf;
6971
use function str_starts_with;
7072
use function strcasecmp;
7173
use function strlen;
7274
use function substr;
7375
use function usort;
76+
use const PHP_INT_MAX;
7477

7578
/** @api */
7679
#[InstanceofDeprecated]
@@ -1169,7 +1172,9 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
11691172
if (!is_int($constantScalarValue)) {
11701173
continue;
11711174
}
1172-
if (IntegerRangeType::fromInterval(0, $constantScalarValue + 1)->isSuperTypeOf($offsetType)->yes()) {
1175+
// no offset exists past PHP_INT_MAX: the list may grow up to it
1176+
$maxListOffset = $constantScalarValue < PHP_INT_MAX ? $constantScalarValue + 1 : null;
1177+
if (IntegerRangeType::fromInterval(0, $maxListOffset)->isSuperTypeOf($offsetType)->yes()) {
11731178
$result = TypeCombinator::intersect($result, new AccessoryArrayListType());
11741179
break 2;
11751180
}
@@ -1782,20 +1787,32 @@ public function exponentiate(Type $exponent): Type
17821787

17831788
public function getFiniteTypes(): array
17841789
{
1785-
$compare = [];
1790+
$first = null;
1791+
$others = [];
17861792
foreach ($this->types as $type) {
1787-
$oneType = [];
1788-
foreach ($type->getFiniteTypes() as $finiteType) {
1789-
if ($finiteType instanceof EnumCaseObjectType) {
1790-
$oneType[$finiteType->getClassName() . '::' . $finiteType->getEnumCaseName()] = $finiteType;
1791-
continue;
1792-
}
1793-
$oneType[$finiteType->describe(VerbosityLevel::typeOnly())] = $finiteType;
1793+
$indexed = self::indexFiniteTypes($type->getFiniteTypes());
1794+
if ($first === null) {
1795+
$first = $indexed;
1796+
continue;
17941797
}
1795-
$compare[] = $oneType;
1798+
1799+
$others[] = $indexed;
1800+
}
1801+
1802+
if ($first === null) {
1803+
return [];
17961804
}
17971805

1798-
$result = array_values(array_intersect_key(...$compare));
1806+
$result = [];
1807+
foreach ($first as $key => $finiteType) {
1808+
foreach ($others as $other) {
1809+
if (!self::hasFiniteType($other, is_string($key) ? $key : null, $finiteType)) {
1810+
continue 2;
1811+
}
1812+
}
1813+
1814+
$result[] = $finiteType;
1815+
}
17991816

18001817
if (count($result) > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) {
18011818
return [];
@@ -1804,6 +1821,57 @@ public function getFiniteTypes(): array
18041821
return $result;
18051822
}
18061823

1824+
/**
1825+
* The finite types of one member, one per value, in their order: keyed by
1826+
* FiniteTypeSet::key(), which never keys by a number, and the values it
1827+
* does not key (floats, constant arrays) by a list position, deduplicated
1828+
* by equals().
1829+
*
1830+
* @param list<Type> $finiteTypes
1831+
* @return array<int|string, Type>
1832+
*/
1833+
private static function indexFiniteTypes(array $finiteTypes): array
1834+
{
1835+
$indexed = [];
1836+
foreach ($finiteTypes as $finiteType) {
1837+
$key = FiniteTypeSet::key($finiteType);
1838+
if ($key !== null) {
1839+
$indexed[$key] = $finiteType;
1840+
continue;
1841+
}
1842+
1843+
foreach ($indexed as $existingKey => $existing) {
1844+
if (is_int($existingKey) && $existing->equals($finiteType)) {
1845+
$indexed[$existingKey] = $finiteType;
1846+
continue 2;
1847+
}
1848+
}
1849+
1850+
$indexed[] = $finiteType;
1851+
}
1852+
1853+
return $indexed;
1854+
}
1855+
1856+
/**
1857+
* @param array<int|string, Type> $indexed
1858+
* @param string|null $key the FiniteTypeSet::key() of $finiteType
1859+
*/
1860+
private static function hasFiniteType(array $indexed, ?string $key, Type $finiteType): bool
1861+
{
1862+
if ($key !== null) {
1863+
return array_key_exists($key, $indexed);
1864+
}
1865+
1866+
foreach ($indexed as $otherKey => $other) {
1867+
if (is_int($otherKey) && $other->equals($finiteType)) {
1868+
return true;
1869+
}
1870+
}
1871+
1872+
return false;
1873+
}
1874+
18071875
/**
18081876
* @param callable(Type $type): TrinaryLogic $getResult
18091877
* @param (callable(Type $type): bool)|null $filter

‎tests/PHPStan/Type/CallableTypeTest.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,28 @@ public function testAccepts(
421421
);
422422
}
423423

424+
public function testIsSuperTypeOfKeepsLazyReasonsOfMaybeParameter(): void
425+
{
426+
$sealed = [new NeverType(true), new NeverType(true)];
427+
$acceptingParameterType = new ConstantArrayType([new ConstantStringType('a'), new ConstantStringType('b')], [new IntegerType(), new IntegerType()], optionalKeys: [1], unsealed: $sealed);
428+
$passedParameterType = new UnionType([
429+
new ConstantArrayType([new ConstantStringType('a')], [new IntegerType()], unsealed: $sealed),
430+
new ConstantArrayType([new ConstantStringType('b')], [new IntegerType()], unsealed: $sealed),
431+
]);
432+
$accepting = new CallableType([new NativeParameterReflection('x', false, $acceptingParameterType, PassedByReference::createNo(), false, null)], new VoidType());
433+
$passed = new CallableType([new NativeParameterReflection('x', false, $passedParameterType, PassedByReference::createNo(), false, null)], new VoidType());
434+
435+
// the passed parameter's isSuperTypeOf() is maybe, with a lazy reason from its array{b: int} member
436+
$parameterResult = $passedParameterType->isSuperTypeOf($acceptingParameterType);
437+
$this->assertTrue($parameterResult->maybe());
438+
$this->assertNotSame([], $parameterResult->lazyReasons);
439+
440+
$result = $accepting->isSuperTypeOf($passed);
441+
$this->assertTrue($result->maybe());
442+
$reasons = $result->getReasons();
443+
$this->assertCount(2, $reasons);
444+
$this->assertSame('Type array{a: int}|array{b: int} of parameter #1 $x of passed callable needs to be same or wider than parameter type array{a: int, b?: int} of accepting callable.', $reasons[0]);
445+
$this->assertStringStartsWith('Sealed array shapes array{b: int} and array{a: int, b?: int} cannot be intersected.', $reasons[1]);
446+
}
447+
424448
}

‎tests/PHPStan/Type/IntersectionTypeTest.php‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use PHPStan\Type\Accessory\NonEmptyArrayType;
2121
use PHPStan\Type\Accessory\OversizedArrayType;
2222
use PHPStan\Type\Constant\ConstantArrayType;
23+
use PHPStan\Type\Constant\ConstantBooleanType;
24+
use PHPStan\Type\Constant\ConstantFloatType;
2325
use PHPStan\Type\Constant\ConstantIntegerType;
2426
use PHPStan\Type\Constant\ConstantStringType;
2527
use PHPStan\Type\Enum\EnumCaseObjectType;
@@ -30,8 +32,10 @@
3032
use stdClass;
3133
use Test\ClassWithToString;
3234
use Traversable;
35+
use function array_map;
3336
use function count;
3437
use function sprintf;
38+
use const PHP_INT_MAX;
3539
use const PHP_VERSION_ID;
3640

3741
class IntersectionTypeTest extends PHPStanTestCase
@@ -1016,4 +1020,68 @@ public function testConstantOfIntersectionWithTemplateMixed(IntersectionType $ty
10161020
$this->assertSame(DateTimeInterface::class, $constant->getDeclaringClass()->getName());
10171021
}
10181022

1023+
/**
1024+
* @return Iterator<string, array{IntersectionType, list<string>}>
1025+
*/
1026+
public static function dataGetFiniteTypes(): Iterator
1027+
{
1028+
yield 'integers' => [
1029+
new IntersectionType([
1030+
new UnionType([new ConstantIntegerType(1), new ConstantIntegerType(2)]),
1031+
new UnionType([new ConstantIntegerType(1), new ConstantIntegerType(2), new ConstantIntegerType(3)]),
1032+
]),
1033+
['1', '2'],
1034+
];
1035+
yield 'strings' => [
1036+
new IntersectionType([
1037+
new UnionType([new ConstantStringType('a'), new ConstantStringType('b'), new ConstantStringType('c')]),
1038+
new UnionType([new ConstantStringType('c'), new ConstantStringType('a')]),
1039+
]),
1040+
["'a'", "'c'"],
1041+
];
1042+
yield 'floats' => [
1043+
new IntersectionType([
1044+
new UnionType([new ConstantFloatType(1.5), new ConstantFloatType(2.5)]),
1045+
new UnionType([new ConstantFloatType(2.5), new ConstantFloatType(3.5)]),
1046+
]),
1047+
['2.5'],
1048+
];
1049+
yield 'mixed kinds' => [
1050+
new IntersectionType([
1051+
new UnionType([new ConstantIntegerType(1), new ConstantStringType('1'), new ConstantBooleanType(true), new NullType()]),
1052+
new UnionType([new ConstantStringType('1'), new NullType(), new ConstantIntegerType(2)]),
1053+
]),
1054+
["'1'", 'null'],
1055+
];
1056+
yield 'disjoint' => [
1057+
new IntersectionType([
1058+
new UnionType([new ConstantIntegerType(1), new ConstantIntegerType(2)]),
1059+
new UnionType([new ConstantIntegerType(3), new ConstantIntegerType(4)]),
1060+
]),
1061+
[],
1062+
];
1063+
}
1064+
1065+
/**
1066+
* @param list<string> $expected
1067+
*/
1068+
#[DataProvider('dataGetFiniteTypes')]
1069+
public function testGetFiniteTypes(IntersectionType $type, array $expected): void
1070+
{
1071+
$this->assertSame($expected, array_map(static fn (Type $finiteType): string => $finiteType->describe(VerbosityLevel::precise()), $type->getFiniteTypes()));
1072+
}
1073+
1074+
public function testSetOffsetValueTypeOnListWithOffsetAtIntMax(): void
1075+
{
1076+
$type = new IntersectionType([
1077+
new ArrayType(new IntegerType(), new MixedType()),
1078+
new AccessoryArrayListType(),
1079+
new HasOffsetType(new ConstantIntegerType(PHP_INT_MAX)),
1080+
]);
1081+
1082+
$result = $type->setOffsetValueType(new ConstantIntegerType(3), new ConstantIntegerType(1));
1083+
$this->assertTrue($result->isList()->yes());
1084+
$this->assertSame('non-empty-list&hasOffset(9223372036854775807)&hasOffsetValue(3, 1)', $result->describe(VerbosityLevel::precise()));
1085+
}
1086+
10191087
}

‎turbo-ext/src/AbsorbedTemplateArgumentType.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void pt_register_absorbed_template_argument_type()
4848

4949
cls.method(sigs::equals, [](INTERNAL_FUNCTION_PARAMETERS) {
5050
zval *type;
51-
if (!zp::parse<zp::Obj>(execute_data, type)) RETURN_THROWS();
51+
if (!zp::parse<zp::TypeObj>(execute_data, type)) RETURN_THROWS();
5252
RETURN_BOOL(AbsorbedTemplateArgumentType::equals(type));
5353
});
5454

0 commit comments

Comments
 (0)