Skip to content

Commit

Permalink
IssetCheck - respect treatPhpDocTypesAsCertain for variables
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 1, 2023
1 parent 719831d commit 5b7a115
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Rules/IssetCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function check(Expr $expr, Scope $scope, string $operatorDescription, cal
}

return $this->generateError(
$scope->getVariableType($expr->name),
$this->treatPhpDocTypesAsCertain ? $scope->getType($expr) : $scope->getNativeType($expr),
sprintf('Variable $%s %s always exists and', $expr->name, $operatorDescription),
$typeMessageCallback,
);
Expand Down
28 changes: 28 additions & 0 deletions tests/PHPStan/Rules/Variables/EmptyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ public function testBug6974(): void
{
$this->treatPhpDocTypesAsCertain = false;
$this->strictUnnecessaryNullsafePropertyFetch = false;
$this->analyse([__DIR__ . '/data/bug-6974.php'], [
[
'Variable $a in empty() always exists and is always falsy.',
12,
],
[
'Variable $a in empty() always exists and is always falsy.',
21,
],
[
'Variable $a in empty() always exists and is always falsy.',
30,
],
]);
}

public function testBug6974TreatPhpDocTypesAsCertain(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->strictUnnecessaryNullsafePropertyFetch = false;
$this->analyse([__DIR__ . '/data/bug-6974.php'], [
[
'Variable $a in empty() always exists and is always falsy.',
Expand Down Expand Up @@ -193,4 +213,12 @@ public function testBug7199(): void
$this->analyse([__DIR__ . '/data/bug-7199.php'], []);
}

public function testBug9126(): void
{
$this->treatPhpDocTypesAsCertain = false;
$this->strictUnnecessaryNullsafePropertyFetch = false;

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

}
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-9126.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Bug9126;

class User {}

class Resume {

/**
* @return User
*/
public function getOwner() {
return $this->owner;
}
}

function (): void {
$resume = new Resume();
$owner = $resume->getOwner();
if (!empty($owner)) {
echo "not empty";
}
};

0 comments on commit 5b7a115

Please sign in to comment.