Skip to content

Commit

Permalink
Fix side effect for method annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Dec 30, 2022
1 parent 97efd4d commit acd1c61
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Reflection/Annotations/AnnotationMethodReflection.php
Expand Up @@ -109,6 +109,13 @@ public function getThrowType(): ?Type

public function hasSideEffects(): TrinaryLogic
{
if (
strtolower($this->getName()) !== '__construct'
&& $this->returnType->isVoid()->yes()
) {
return TrinaryLogic::createYes();
}

return TrinaryLogic::createMaybe();
}

Expand Down
Expand Up @@ -613,4 +613,10 @@ public function testPhpUnitIntegration(): void
$this->analyse([__DIR__ . '/../../Analyser/data/phpunit-integration.php'], []);
}

public function testBug8586(): void
{
$this->checkAlwaysTrueStrictComparison = true;
$this->analyse([__DIR__ . '/data/bug-8586.php'], []);
}

}
38 changes: 38 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-8586.php
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);

namespace Bug8586;

class Foo
{
public function getString(): ?string
{
return '';
}
}

/**
* @method void refreshFromAnnotation(object $object)
*/
class EntityManager
{
public function refresh(object $object): void
{
}
}

class HelloWorld
{
public function sayHello(Foo $foo, EntityManager $em): void
{
\assert($foo->getString() === null);
$em->refreshFromAnnotation($foo);
\assert($foo->getString() !== null);
}

public function sayHello2(Foo $foo, EntityManager $em): void
{
\assert($foo->getString() === null);
$em->refresh($foo);
\assert($foo->getString() !== null);
}
}

0 comments on commit acd1c61

Please sign in to comment.