Skip to content

Commit

Permalink
Fix NativeMethodReflection::hasSideEffects()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 2, 2021
1 parent f5e6704 commit 98fb540
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Reflection/Native/NativeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;
use PHPStan\Type\VoidType;

class NativeMethodReflection implements MethodReflection
{
Expand Down Expand Up @@ -48,6 +49,9 @@ public function __construct(
?Type $throwType
)
{
if ($reflection->getName() === 'setValue') {
var_dump('here');

This comment has been minimized.

Copy link
@staabm

staabm Apr 2, 2021

Contributor

Forgotten debug out?

}
$this->reflectionProvider = $reflectionProvider;
$this->declaringClass = $declaringClass;
$this->reflection = $reflection;
Expand Down Expand Up @@ -143,9 +147,29 @@ public function getThrowType(): ?Type

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

return $this->hasSideEffects;
}

private function isVoid(): bool
{
foreach ($this->variants as $variant) {
if (!$variant->getReturnType() instanceof VoidType) {
return false;
}
}

return true;
}

public function getDocComment(): ?string
{
if ($this->stubPhpDocString !== null) {
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10334,6 +10334,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3302.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-1511.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4434.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4231.php');
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4231.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Bug4231;

use function PHPStan\Analyser\assertType;

function (): void {
$property = new \ReflectionProperty($this, 'data');

if (is_null($property->getValue($this))) {
assertType('null', $property->getValue($this));
$property->setValue($this, 'Some value which is not null');
assertType('mixed', $property->getValue($this));
}
};

function (): void {
$property = new \ReflectionProperty($this, 'data');

if (is_null($property->getValue($this))) {
assertType('null', $property->getValue($this));
$property->setValue($this, 'Some value which is not null');

$value = $property->getValue($this);
assertType('mixed', $value);
}
};

0 comments on commit 98fb540

Please sign in to comment.