Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Reflection/Dummy/ChangedTypeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function __construct(
private ?array $namedArgumentsVariants,
private ?Type $selfOutType,
private ?Type $throwType,
private Assertions $assertions,
)
{
}
Expand Down Expand Up @@ -133,7 +134,7 @@ public function hasSideEffects(): TrinaryLogic

public function getAsserts(): Assertions
{
return $this->reflection->getAsserts();
return $this->assertions;
}

public function acceptsNamedArguments(): TrinaryLogic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private function transformMethodWithStaticType(ClassReflection $declaringClass,
$namedArgumentVariants,
$selfOutType,
$throwType,
$method->getAsserts()->mapTypes($this->transformStaticTypeCallback),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ private function transformMethodWithStaticType(ClassReflection $declaringClass,
$namedArgumentsVariants,
$selfOutType,
$throwType,
$method->getAsserts()->mapTypes(fn (Type $type): Type => $this->transformStaticType($type)),
);
}

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

namespace Bug12376;

use function PHPStan\Testing\assertType;

/**
* workaround https://github.com/phpstan/phpstan-src/pull/3853
*
* @template T of object
* @param class-string<T> $class
* @return T
*/
function newNonFinalInstance(string $class): object
{
return new $class();
}

class Base {}

class A extends Base
{
/**
* @template T of object
* @param T $object
* @return (T is static ? T : static)
*
* @phpstan-assert static $object
*/
public static function assertInstanceOf(object $object)
{
if (!$object instanceof static) {
throw new \Exception();
}

return $object;
}
}

class B extends A {}
class C extends Base {}

$o = newNonFinalInstance(\DateTime::class);
$r = A::assertInstanceOf($o);
assertType('*NEVER*', $o);
assertType('Bug12376\A', $r);

$o = newNonFinalInstance(A::class);
$r = A::assertInstanceOf($o);
assertType('Bug12376\A', $o);
assertType('Bug12376\A', $r);

$o = newNonFinalInstance(B::class);
$r = A::assertInstanceOf($o);
assertType('Bug12376\B', $o);
assertType('Bug12376\B', $r);

$o = newNonFinalInstance(C::class);
$r = A::assertInstanceOf($o);
assertType('*NEVER*', $o);
assertType('Bug12376\A', $r);

$o = newNonFinalInstance(A::class);
$r = B::assertInstanceOf($o);
assertType('Bug12376\B', $o);
assertType('Bug12376\B', $r);
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3608,6 +3608,16 @@ public function testBug9141(): void
$this->analyse([__DIR__ . '/data/bug-9141.php'], []);
}

public function testBug12548(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->checkExplicitMixed = true;

$this->analyse([__DIR__ . '/data/bug-12548.php'], []);
}

public function testBug3589(): void
{
$this->checkThisOnly = false;
Expand Down
86 changes: 86 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-12548.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php declare(strict_types = 1);

namespace Bug12548;

class BaseSat extends \stdClass
{
/**
* @template T of object
*
* @param T $object
*
* @phpstan-assert-if-true =static $object
*/
public static function assertInstanceOf(object $object): bool
{
return $object instanceof static;
}

/**
* @template T of object
*
* @param T $object
*
* @return (T is static ? T : static)
*
* @phpstan-assert static $object
*/
public static function assertInstanceOf2(object $object)
{
if (!$object instanceof static) {
throw new \Error('Object is not an instance of static class');
}

return $object;
}
}

class StdSat extends BaseSat
{
public function foo(): void {}

/**
* @template T of object
*
* @param T $object
*
* @return (T is static ? T : static)
*
* @phpstan-assert static $object
*/
public static function assertInstanceOf3(object $object)
{
if (!$object instanceof static) {
throw new \Error('Object is not an instance of static class');
}

return $object;
}
}

class TestCase
{
private function createStdSat(): \stdClass
{
return new StdSat();
}

public function testAssertInstanceOf(): void
{
$o = $this->createStdSat();
$o->foo(); // @phpstan-ignore method.nonObject (EXPECTED)

$o = $this->createStdSat();
if (StdSat::assertInstanceOf($o)) {
$o->foo();
}

$o = $this->createStdSat();
StdSat::assertInstanceOf2($o);
$o->foo();

$o = $this->createStdSat();
StdSat::assertInstanceOf3($o);
$o->foo();
}
}
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,14 @@ public function testBug12645(): void
]);
}

public function testBug11289(): void
{
$this->checkThisOnly = false;
$this->checkUnionTypes = true;
$this->checkDynamicProperties = false;
$this->analyse([__DIR__ . '/data/bug-11289.php'], []);
}

public function testBug8668(): void
{
$this->checkThisOnly = false;
Expand Down
37 changes: 37 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-11289.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug11289;

abstract class SomeAbstractClass
{
private bool $someValue = true;

/**
* @phpstan-assert-if-true =static $other
*/
public function equals(?self $other): bool
{
return $other instanceof static
&& $this->someValue === $other->someValue;
}
}

class SomeConcreteClass extends SomeAbstractClass
{
public function __construct(
private bool $someOtherValue,
) {}

public function equals(?SomeAbstractClass $other): bool
{
return parent::equals($other)
&& $this->someOtherValue === $other->someOtherValue;
}
}

$a = new SomeConcreteClass(true);
$b = new SomeConcreteClass(false);

var_dump($a->equals($b), $b->equals($b));
Loading