Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hasSideEffects for AnnotationMethodReflection #2155

Merged
merged 2 commits into from Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Reflection/Annotations/AnnotationMethodReflection.php
Expand Up @@ -11,6 +11,7 @@
use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Type;
use function strtolower;

class AnnotationMethodReflection implements ExtendedMethodReflection
{
Expand Down Expand Up @@ -109,6 +110,13 @@ public function getThrowType(): ?Type

public function hasSideEffects(): TrinaryLogic
{
if (
VincentLanglet marked this conversation as resolved.
Show resolved Hide resolved
strtolower($this->getName()) !== '__construct'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can delete the thing about __construct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

&& $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);
}
}