Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f2af15b

Browse files
committed
Prevent issue with accessory-only IntersectionType
1 parent fa4e7d2 commit f2af15b

19 files changed

Lines changed: 110 additions & 9 deletions

src/Analyser/MutatingScope.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
use PHPStan\ShouldNotHappenException;
6464
use PHPStan\TrinaryLogic;
6565
use PHPStan\Type\Accessory\AccessoryArrayListType;
66-
use PHPStan\Type\Accessory\AccessoryType;
6766
use PHPStan\Type\Accessory\HasOffsetValueType;
6867
use PHPStan\Type\Accessory\NonEmptyArrayType;
6968
use PHPStan\Type\Accessory\OversizedArrayType;
@@ -3164,15 +3163,10 @@ public function addTypeToExpression(Expr $expr, Type $type): self
31643163
return $this->specifyExpressionType($expr, $newType, $newType, TrinaryLogic::createYes());
31653164
}
31663165

3167-
$newNativeType = TypeCombinator::intersect($type, $nativeType);
3168-
if ($newNativeType instanceof AccessoryType) {
3169-
$newNativeType = $nativeType;
3170-
}
3171-
31723166
return $this->specifyExpressionType(
31733167
$expr,
31743168
TypeCombinator::intersect($type, $originalExprType),
3175-
$newNativeType,
3169+
TypeCombinator::intersect($type, $nativeType),
31763170
TrinaryLogic::createYes(),
31773171
);
31783172
}

src/Type/Accessory/AccessoryArrayListType.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
88
use PHPStan\TrinaryLogic;
99
use PHPStan\Type\AcceptsResult;
10+
use PHPStan\Type\ArrayType;
1011
use PHPStan\Type\BooleanType;
1112
use PHPStan\Type\CompoundType;
1213
use PHPStan\Type\Constant\ConstantFloatType;
@@ -549,6 +550,11 @@ public function getFiniteTypes(): array
549550
return [];
550551
}
551552

553+
public function getDefaultBaseType(): Type
554+
{
555+
return new ArrayType(new MixedType(), new MixedType());
556+
}
557+
552558
public function toPhpDocNode(): TypeNode
553559
{
554560
return new IdentifierTypeNode('list');

src/Type/Accessory/AccessoryLiteralStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,11 @@ public function getFiniteTypes(): array
383383
return [];
384384
}
385385

386+
public function getDefaultBaseType(): Type
387+
{
388+
return new StringType();
389+
}
390+
386391
public function toPhpDocNode(): TypeNode
387392
{
388393
return new IdentifierTypeNode('literal-string');

src/Type/Accessory/AccessoryLowercaseStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ public function getFiniteTypes(): array
388388
return [];
389389
}
390390

391+
public function getDefaultBaseType(): Type
392+
{
393+
return new StringType();
394+
}
395+
391396
public function toPhpDocNode(): TypeNode
392397
{
393398
return new IdentifierTypeNode('lowercase-string');

src/Type/Accessory/AccessoryNonEmptyStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,11 @@ public function getFiniteTypes(): array
392392
return [];
393393
}
394394

395+
public function getDefaultBaseType(): Type
396+
{
397+
return new StringType();
398+
}
399+
395400
public function toPhpDocNode(): TypeNode
396401
{
397402
return new IdentifierTypeNode('non-empty-string');

src/Type/Accessory/AccessoryNonFalsyStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,11 @@ public function getFiniteTypes(): array
389389
return [];
390390
}
391391

392+
public function getDefaultBaseType(): Type
393+
{
394+
return new StringType();
395+
}
396+
392397
public function toPhpDocNode(): TypeNode
393398
{
394399
return new IdentifierTypeNode('non-falsy-string');

src/Type/Accessory/AccessoryNumericStringType.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ public function generalize(GeneralizePrecision $precision): Type
373373
public function tryRemove(Type $typeToRemove): ?Type
374374
{
375375
if ($typeToRemove instanceof ConstantStringType && $typeToRemove->getValue() === '0') {
376-
return new IntersectionType([$this, new AccessoryNonFalsyStringType()]);
376+
return new IntersectionType([new StringType(), $this, new AccessoryNonFalsyStringType()]);
377377
}
378378

379379
return null;
@@ -392,6 +392,11 @@ public function getFiniteTypes(): array
392392
return [];
393393
}
394394

395+
public function getDefaultBaseType(): Type
396+
{
397+
return new StringType();
398+
}
399+
395400
public function toPhpDocNode(): TypeNode
396401
{
397402
return new IdentifierTypeNode('numeric-string');

src/Type/Accessory/AccessoryType.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,14 @@
5555
interface AccessoryType extends Type
5656
{
5757

58+
/**
59+
* Returns the base type this accessory refines.
60+
*
61+
* Used when an accessory would otherwise end up in an `IntersectionType`
62+
* without an explicit base type — the returned type provides that base.
63+
* For example `AccessoryNonEmptyStringType` returns `string`, array accessories
64+
* return `array`, and offset accessories return `array|ArrayAccess`.
65+
*/
66+
public function getDefaultBaseType(): Type;
67+
5868
}

src/Type/Accessory/AccessoryUppercaseStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ public function getFiniteTypes(): array
388388
return [];
389389
}
390390

391+
public function getDefaultBaseType(): Type
392+
{
393+
return new StringType();
394+
}
395+
391396
public function toPhpDocNode(): TypeNode
392397
{
393398
return new IdentifierTypeNode('uppercase-string');

src/Type/Accessory/HasMethodType.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use PHPStan\Type\IsSuperTypeOfResult;
2323
use PHPStan\Type\MixedType;
2424
use PHPStan\Type\ObjectType;
25+
use PHPStan\Type\ObjectWithoutClassType;
2526
use PHPStan\Type\StringType;
2627
use PHPStan\Type\Traits\MaybeCallableTypeTrait;
2728
use PHPStan\Type\Traits\MaybeIterableTypeTrait;
@@ -330,6 +331,11 @@ public function getFiniteTypes(): array
330331
return [];
331332
}
332333

334+
public function getDefaultBaseType(): Type
335+
{
336+
return new ObjectWithoutClassType();
337+
}
338+
333339
public function toPhpDocNode(): TypeNode
334340
{
335341
return new IdentifierTypeNode(''); // no PHPDoc representation

0 commit comments

Comments
 (0)