Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/Analyser/ExprHandler/NewHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,13 @@
$classTemplateTypes = $traverser->getClassTemplateTypes();

if (count($classTemplateTypes) === count($originalClassTemplateTypes)) {
$propertyType = TypeCombinator::removeNull($scope->getType($assignedToProperty));
$nonFinalObjectType = $isStatic ? new StaticType($nonFinalClassReflection) : new ObjectType($resolvedClassName, classReflection: $nonFinalClassReflection);
if ($nonFinalObjectType->isSuperTypeOf($propertyType)->yes()) {
return $propertyType;
$propertyType = $this->resolveAssignedPropertyDeclaredType($assignedToProperty, $scope);
if ($propertyType !== null) {
$propertyType = TypeCombinator::removeNull($propertyType);
$nonFinalObjectType = $isStatic ? new StaticType($nonFinalClassReflection) : new ObjectType($resolvedClassName, classReflection: $nonFinalClassReflection);
if ($nonFinalObjectType->isSuperTypeOf($propertyType)->yes()) {

Check warning on line 423 in src/Analyser/ExprHandler/NewHandler.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ($propertyType !== null) { $propertyType = TypeCombinator::removeNull($propertyType); $nonFinalObjectType = $isStatic ? new StaticType($nonFinalClassReflection) : new ObjectType($resolvedClassName, classReflection: $nonFinalClassReflection); - if ($nonFinalObjectType->isSuperTypeOf($propertyType)->yes()) { + if (!$nonFinalObjectType->isSuperTypeOf($propertyType)->no()) { return $propertyType; } }

Check warning on line 423 in src/Analyser/ExprHandler/NewHandler.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ($propertyType !== null) { $propertyType = TypeCombinator::removeNull($propertyType); $nonFinalObjectType = $isStatic ? new StaticType($nonFinalClassReflection) : new ObjectType($resolvedClassName, classReflection: $nonFinalClassReflection); - if ($nonFinalObjectType->isSuperTypeOf($propertyType)->yes()) { + if (!$nonFinalObjectType->isSuperTypeOf($propertyType)->no()) { return $propertyType; } }
return $propertyType;
}
}
}
}
Expand Down Expand Up @@ -590,4 +593,25 @@
return TypeTraverser::map($newGenericType, new GenericTypeTemplateTraverser($resolvedTemplateTypeMap));
}

private function resolveAssignedPropertyDeclaredType(Node\Expr $propertyFetch, MutatingScope $scope): ?Type
{
if ($propertyFetch instanceof Node\Expr\PropertyFetch && $propertyFetch->name instanceof Node\Identifier) {
$holderType = $scope->getType($propertyFetch->var);
$propertyName = $propertyFetch->name->name;
if ($holderType->hasInstanceProperty($propertyName)->yes()) {

Check warning on line 601 in src/Analyser/ExprHandler/NewHandler.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ($propertyFetch instanceof Node\Expr\PropertyFetch && $propertyFetch->name instanceof Node\Identifier) { $holderType = $scope->getType($propertyFetch->var); $propertyName = $propertyFetch->name->name; - if ($holderType->hasInstanceProperty($propertyName)->yes()) { + if (!$holderType->hasInstanceProperty($propertyName)->no()) { return $holderType->getInstanceProperty($propertyName, $scope)->getReadableType(); } } elseif ($propertyFetch instanceof Node\Expr\StaticPropertyFetch && $propertyFetch->name instanceof Node\VarLikeIdentifier && $propertyFetch->class instanceof Name) {

Check warning on line 601 in src/Analyser/ExprHandler/NewHandler.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ($propertyFetch instanceof Node\Expr\PropertyFetch && $propertyFetch->name instanceof Node\Identifier) { $holderType = $scope->getType($propertyFetch->var); $propertyName = $propertyFetch->name->name; - if ($holderType->hasInstanceProperty($propertyName)->yes()) { + if (!$holderType->hasInstanceProperty($propertyName)->no()) { return $holderType->getInstanceProperty($propertyName, $scope)->getReadableType(); } } elseif ($propertyFetch instanceof Node\Expr\StaticPropertyFetch && $propertyFetch->name instanceof Node\VarLikeIdentifier && $propertyFetch->class instanceof Name) {
return $holderType->getInstanceProperty($propertyName, $scope)->getReadableType();
}
} elseif ($propertyFetch instanceof Node\Expr\StaticPropertyFetch && $propertyFetch->name instanceof Node\VarLikeIdentifier && $propertyFetch->class instanceof Name) {
$className = $scope->resolveName($propertyFetch->class);
if ($this->reflectionProvider->hasClass($className)) {
$classRefl = $this->reflectionProvider->getClass($className);
$propertyName = $propertyFetch->name->name;
if ($classRefl->hasStaticProperty($propertyName)) {
return $classRefl->getStaticProperty($propertyName)->getReadableType();
}
}
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,10 @@ public function testBug13624(): void
$this->analyse([__DIR__ . '/data/bug-13624.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug11844(): void
{
$this->analyse([__DIR__ . '/data/bug-11844.php'], []);
}

}
48 changes: 48 additions & 0 deletions tests/PHPStan/Rules/TooWideTypehints/data/bug-11844.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug11844;

class StaticPropertyCase
{
/**
* @var \WeakMap<object, string>|null
*/
private static ?\WeakMap $map = null;

public static function init(): void
{
if (self::$map === null) {
self::$map = new \WeakMap();
}
}
}

class InstancePropertyCase
{
/**
* @var \WeakMap<object, string>|null
*/
private ?\WeakMap $map = null;

public function init(): void
{
if ($this->map === null) {
$this->map = new \WeakMap();
}
}
}

class DirectAssignCase
{
/**
* @var \WeakMap<object, string>|null
*/
private ?\WeakMap $map = null;

public function initAlways(): void
{
$this->map = new \WeakMap();
}
}
Loading