Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support intersection types in MutatingScope::getTypeToInstantiateForNew() #1388

Merged
merged 1 commit into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 16 additions & 26 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5009,39 +5009,29 @@ private function exactInstantiation(New_ $node, string $className): ?Type

private function getTypeToInstantiateForNew(Type $type): Type
{
$decideType = static function (Type $type): ?Type {
if ($type instanceof ConstantStringType) {
return new ObjectType($type->getValue());
}
if ($type instanceof GenericClassStringType) {
return $type->getGenericType();
}
if ((new ObjectWithoutClassType())->isSuperTypeOf($type)->yes()) {
return $type;
}
return null;
};

if ($type instanceof UnionType) {
$types = [];
foreach ($type->getTypes() as $innerType) {
$decidedType = $decideType($innerType);
if ($decidedType === null) {
return new ObjectWithoutClassType();
}
$types = array_map(fn (Type $type) => $this->getTypeToInstantiateForNew($type), $type->getTypes());
return TypeCombinator::union(...$types);
}

$types[] = $decidedType;
}
if ($type instanceof IntersectionType) {
$types = array_map(fn (Type $type) => $this->getTypeToInstantiateForNew($type), $type->getTypes());
return TypeCombinator::intersect(...$types);
}

return TypeCombinator::union(...$types);
if ($type instanceof ConstantStringType) {
return new ObjectType($type->getValue());
}

if ($type instanceof GenericClassStringType) {
return $type->getGenericType();
}

$decidedType = $decideType($type);
if ($decidedType === null) {
return new ObjectWithoutClassType();
if ((new ObjectWithoutClassType())->isSuperTypeOf($type)->yes()) {
return $type;
}

return $decidedType;
return new ObjectWithoutClassType();
}

/** @api */
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/non-empty-string-strrchr-specifying.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/non-empty-string-strcasing-specifying.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/conditional-complex-templates.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7374.php');
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7374.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types = 1);

namespace Bug7374;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/** @return class-string<self>&literal-string */
public static function getClass(): string {
return self::class;
}

public function build(): void {
$class = self::getClass();
assertType(self::class, new $class());
}
}