Skip to content

Commit f92aaba

Browse files
committed
Merge branch 2.1.x into 2.2.x
2 parents a9ab57b + de99d53 commit f92aaba

51 files changed

Lines changed: 912 additions & 257 deletions

Some content is hidden

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

src/Analyser/ExprHandler/InstanceofHandler.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use PHPStan\Analyser\ExprHandler;
1313
use PHPStan\Analyser\MutatingScope;
1414
use PHPStan\Analyser\NodeScopeResolver;
15-
use PHPStan\Analyser\Traverser\InstanceOfClassTypeTraverser;
1615
use PHPStan\DependencyInjection\AutowiredService;
1716
use PHPStan\Type\BooleanType;
1817
use PHPStan\Type\Constant\ConstantBooleanType;
@@ -21,7 +20,6 @@
2120
use PHPStan\Type\ObjectType;
2221
use PHPStan\Type\StaticType;
2322
use PHPStan\Type\Type;
24-
use PHPStan\Type\TypeTraverser;
2523
use PHPStan\Type\TypeUtils;
2624
use function array_merge;
2725
use function strtolower;
@@ -94,9 +92,9 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type
9492
}
9593
} else {
9694
$classType = $scope->getType($expr->class);
97-
$traverser = new InstanceOfClassTypeTraverser();
98-
$classType = TypeTraverser::map($classType, $traverser);
99-
$uncertainty = $traverser->getUncertainty();
95+
$result = $classType->toObjectTypeForInstanceofCheck();
96+
$classType = $result->type;
97+
$uncertainty = $result->uncertainty;
10098
}
10199

102100
if ($classType->isSuperTypeOf(new MixedType())->yes()) {

src/Analyser/Traverser/InstanceOfClassTypeTraverser.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/Analyser/TypeSpecifier.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -165,24 +165,9 @@ public function specifyTypesInCondition(
165165
}
166166

167167
$classType = $scope->getType($expr->class);
168-
$uncertainty = false;
169-
$type = TypeTraverser::map($classType, static function (Type $type, callable $traverse) use (&$uncertainty): Type {
170-
if ($type instanceof UnionType || $type instanceof IntersectionType) {
171-
return $traverse($type);
172-
}
173-
if ($type->getObjectClassNames() !== []) {
174-
$uncertainty = true;
175-
return $type;
176-
}
177-
if ($type instanceof GenericClassStringType) {
178-
$uncertainty = true;
179-
return $type->getGenericType();
180-
}
181-
if ($type instanceof ConstantStringType) {
182-
return new ObjectType($type->getValue());
183-
}
184-
return new MixedType();
185-
});
168+
$result = $classType->toObjectTypeForInstanceofCheck();
169+
$type = $result->type;
170+
$uncertainty = $result->uncertainty;
186171

187172
if (!$type->isSuperTypeOf(new MixedType())->yes()) {
188173
if ($context->true()) {

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
use PHPStan\Type\TypeCombinator;
9494
use PHPStan\Type\TypehintHelper;
9595
use PHPStan\Type\TypeResult;
96-
use PHPStan\Type\TypeTraverser;
9796
use PHPStan\Type\TypeUtils;
9897
use PHPStan\Type\TypeWithClassName;
9998
use PHPStan\Type\UnionType;
@@ -2451,54 +2450,7 @@ public function getClassConstFetchTypeByReflection(Name|Expr $class, string $con
24512450
}
24522451

24532452
if (strtolower($constantName) === 'class') {
2454-
return TypeTraverser::map(
2455-
$constantClassType,
2456-
function (Type $type, callable $traverse): Type {
2457-
if ($type instanceof UnionType || $type instanceof IntersectionType) {
2458-
return $traverse($type);
2459-
}
2460-
2461-
if ($type instanceof NullType) {
2462-
return $type;
2463-
}
2464-
2465-
if ($type instanceof EnumCaseObjectType) {
2466-
return new IntersectionType([
2467-
new GenericClassStringType(new ObjectType($type->getClassName())),
2468-
new AccessoryLiteralStringType(),
2469-
]);
2470-
}
2471-
2472-
$objectClassNames = $type->getObjectClassNames();
2473-
if (count($objectClassNames) > 1) {
2474-
throw new ShouldNotHappenException();
2475-
}
2476-
2477-
if ($type instanceof TemplateType && $objectClassNames === []) {
2478-
return new IntersectionType([
2479-
new GenericClassStringType($type),
2480-
new AccessoryLiteralStringType(),
2481-
]);
2482-
} elseif ($objectClassNames !== [] && $this->getReflectionProvider()->hasClass($objectClassNames[0])) {
2483-
$reflection = $this->getReflectionProvider()->getClass($objectClassNames[0]);
2484-
if ($reflection->isFinalByKeyword()) {
2485-
return new ConstantStringType($reflection->getName(), true);
2486-
}
2487-
2488-
return new IntersectionType([
2489-
new GenericClassStringType($type),
2490-
new AccessoryLiteralStringType(),
2491-
]);
2492-
} elseif ($type->isObject()->yes()) {
2493-
return new IntersectionType([
2494-
new ClassStringType(),
2495-
new AccessoryLiteralStringType(),
2496-
]);
2497-
}
2498-
2499-
return new ErrorType();
2500-
},
2501-
);
2453+
return $constantClassType->toClassConstantType($this->getReflectionProvider());
25022454
}
25032455

25042456
if ($constantClassType->isClassString()->yes()) {
@@ -2699,33 +2651,7 @@ public function getBitwiseNotType(Expr $expr, callable $getTypeCallback): Type
26992651

27002652
public function getBitwiseNotTypeFromType(Type $exprType): Type
27012653
{
2702-
return TypeTraverser::map($exprType, static function (Type $type, callable $traverse): Type {
2703-
if ($type instanceof UnionType || $type instanceof IntersectionType) {
2704-
return $traverse($type);
2705-
}
2706-
if ($type instanceof ConstantStringType) {
2707-
return new ConstantStringType(~$type->getValue());
2708-
}
2709-
if ($type->isString()->yes()) {
2710-
$accessories = [];
2711-
if (!$type->isNonEmptyString()->yes()) {
2712-
return new StringType();
2713-
}
2714-
2715-
$accessories[] = new AccessoryNonEmptyStringType();
2716-
// it is not useful to apply numeric and literal strings here.
2717-
// numeric string isn't certainly kept numeric: 3v4l.org/JERDB
2718-
2719-
return new IntersectionType([new StringType(), ...$accessories]);
2720-
}
2721-
if ($type instanceof ConstantIntegerType || $type instanceof ConstantFloatType) {
2722-
return new ConstantIntegerType(~ (int) $type->getValue());
2723-
}
2724-
if ($type->isInteger()->yes() || $type->isFloat()->yes()) {
2725-
return new IntegerType();
2726-
}
2727-
return new ErrorType();
2728-
});
2654+
return $exprType->toBitwiseNotType();
27292655
}
27302656

27312657
private function resolveName(Name $name, ?ClassReflection $classReflection): string

src/Type/Accessory/AccessoryArrayListType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,11 @@ public function toNumber(): Type
488488
return new ErrorType();
489489
}
490490

491+
public function toBitwiseNotType(): Type
492+
{
493+
return new ErrorType();
494+
}
495+
491496
public function toAbsoluteNumber(): Type
492497
{
493498
return new ErrorType();

src/Type/Accessory/AccessoryLiteralStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ public function toNumber(): Type
181181
return new ErrorType();
182182
}
183183

184+
public function toBitwiseNotType(): Type
185+
{
186+
return new StringType();
187+
}
188+
184189
public function toAbsoluteNumber(): Type
185190
{
186191
return new ErrorType();

src/Type/Accessory/AccessoryLowercaseStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ public function toNumber(): Type
178178
return new ErrorType();
179179
}
180180

181+
public function toBitwiseNotType(): Type
182+
{
183+
return new StringType();
184+
}
185+
181186
public function toAbsoluteNumber(): Type
182187
{
183188
return new ErrorType();

src/Type/Accessory/AccessoryNonEmptyStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ public function toNumber(): Type
183183
return new ErrorType();
184184
}
185185

186+
public function toBitwiseNotType(): Type
187+
{
188+
return new IntersectionType([new StringType(), new self()]);
189+
}
190+
186191
public function toAbsoluteNumber(): Type
187192
{
188193
return new ErrorType();

src/Type/Accessory/AccessoryNonFalsyStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ public function toNumber(): Type
185185
return new ErrorType();
186186
}
187187

188+
public function toBitwiseNotType(): Type
189+
{
190+
return new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]);
191+
}
192+
188193
public function toAbsoluteNumber(): Type
189194
{
190195
return new ErrorType();

src/Type/Accessory/AccessoryNumericStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ public function toNumber(): Type
178178
]);
179179
}
180180

181+
public function toBitwiseNotType(): Type
182+
{
183+
return new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]);
184+
}
185+
181186
public function toAbsoluteNumber(): Type
182187
{
183188
return $this->toNumber()->toAbsoluteNumber();

0 commit comments

Comments
 (0)