Skip to content

Commit

Permalink
Support intersection types in MutatingScope::getTypeToInstantiateForN…
Browse files Browse the repository at this point in the history
…ew()
  • Loading branch information
Richard van Velzen authored and ondrejmirtes committed Jun 2, 2022
1 parent d0a9d08 commit 72cd455
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
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
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,12 @@ public function testBug7351(): void
$this->assertNoErrors($errors);
}

public function testBug7374(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-7374.php');
$this->assertNoErrors($errors);
}

/**
* @param string[]|null $allAnalysedFiles
* @return Error[]
Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7374.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace Bug7374;

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

public function build(): self {
$class = self::getClass();
return new $class();
}
}

0 comments on commit 72cd455

Please sign in to comment.