Skip to content
Closed
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
13 changes: 12 additions & 1 deletion src/Type/Generic/GenericObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPStan\Type\IsSuperTypeOfResult;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
Expand Down Expand Up @@ -186,7 +187,17 @@
if (!$thisVariance->invariant()) {
$results[] = $thisVariance->isValidVariance($templateType, $this->types[$i], $ancestor->types[$i]);
} else {
$results[] = $templateType->isValidVariance($this->types[$i], $ancestor->types[$i]);
$result = $templateType->isValidVariance($this->types[$i], $ancestor->types[$i]);
if ($acceptsContext && !$result->yes()) {

Check warning on line 191 in src/Type/Generic/GenericObjectType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $results[] = $thisVariance->isValidVariance($templateType, $this->types[$i], $ancestor->types[$i]); } else { $result = $templateType->isValidVariance($this->types[$i], $ancestor->types[$i]); - if ($acceptsContext && !$result->yes()) { + if ($acceptsContext && $result->no()) { $thisWithoutNull = TypeCombinator::removeNull($this->types[$i]); if ( $this->types[$i]->isSuperTypeOf($ancestor->types[$i])->yes()

Check warning on line 191 in src/Type/Generic/GenericObjectType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $results[] = $thisVariance->isValidVariance($templateType, $this->types[$i], $ancestor->types[$i]); } else { $result = $templateType->isValidVariance($this->types[$i], $ancestor->types[$i]); - if ($acceptsContext && !$result->yes()) { + if ($acceptsContext && $result->no()) { $thisWithoutNull = TypeCombinator::removeNull($this->types[$i]); if ( $this->types[$i]->isSuperTypeOf($ancestor->types[$i])->yes()
$thisWithoutNull = TypeCombinator::removeNull($this->types[$i]);
if (
$this->types[$i]->isSuperTypeOf($ancestor->types[$i])->yes()
&& $ancestor->types[$i]->isSuperTypeOf($thisWithoutNull)->yes()
) {
$result = IsSuperTypeOfResult::createYes();
}
}
$results[] = $result;
}

$results[] = IsSuperTypeOfResult::createFromBoolean($thisVariance->validPosition($ancestorVariance));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
class TypesAssignedToPropertiesRuleTest extends RuleTestCase
{

private bool $checkNullables = true;

private bool $checkExplicitMixed = false;

private bool $checkImplicitMixed = false;

protected function getRule(): Rule
{
return new TypesAssignedToPropertiesRule(new RuleLevelHelper(self::createReflectionProvider(), true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false, true), new PropertyReflectionFinder());
return new TypesAssignedToPropertiesRule(new RuleLevelHelper(self::createReflectionProvider(), $this->checkNullables, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false, true), new PropertyReflectionFinder());
}

public function testTypesAssignedToProperties(): void
Expand Down Expand Up @@ -1038,4 +1040,10 @@ public function testBug4525(): void
$this->analyse([__DIR__ . '/data/bug-4525.php'], []);
}

public function testBug13876(): void
{
$this->checkNullables = false;
$this->analyse([__DIR__ . '/data/bug-13876.php'], []);
}

}
68 changes: 68 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-13876.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php declare(strict_types = 1);

namespace Bug13876;

/**
* @template BAIT
* @template PROMISED
*/
class Trap
{

/**
* @var BAIT
*/
private $bait;

/**
* @var \Closure(BAIT):PROMISED
*/
private $switch;

/**
* @param BAIT $bait
* @param \Closure(BAIT):PROMISED $switch
*/
public function __construct($bait, \Closure $switch)
{
$this->bait = $bait;
$this->switch = $switch;
}

/**
* @return PROMISED
*/
public function fall()
{
return ($this->switch)($this->bait);
}
}

class A {}

class B {

/**
* @var Trap<int|null, A|null>
*/
private Trap $b;

public function __construct() {

/**
* @var Trap<int|null, A|null>
*/
$nullPerson = new Trap(null, function (): ?A {
return null;
});

$this->b = $nullPerson;
}

/**
* @return ?A
*/
public function getB() {
return $this->b->fall();
}
}
Loading