Skip to content

Commit 5b7a115

Browse files
committed
IssetCheck - respect treatPhpDocTypesAsCertain for variables
1 parent 719831d commit 5b7a115

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎src/Rules/IssetCheck.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function check(Expr $expr, Scope $scope, string $operatorDescription, cal
4545
}
4646

4747
return $this->generateError(
48-
$scope->getVariableType($expr->name),
48+
$this->treatPhpDocTypesAsCertain ? $scope->getType($expr) : $scope->getNativeType($expr),
4949
sprintf('Variable $%s %s always exists and', $expr->name, $operatorDescription),
5050
$typeMessageCallback,
5151
);

‎tests/PHPStan/Rules/Variables/EmptyRuleTest.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ public function testBug6974(): void
9191
{
9292
$this->treatPhpDocTypesAsCertain = false;
9393
$this->strictUnnecessaryNullsafePropertyFetch = false;
94+
$this->analyse([__DIR__ . '/data/bug-6974.php'], [
95+
[
96+
'Variable $a in empty() always exists and is always falsy.',
97+
12,
98+
],
99+
[
100+
'Variable $a in empty() always exists and is always falsy.',
101+
21,
102+
],
103+
[
104+
'Variable $a in empty() always exists and is always falsy.',
105+
30,
106+
],
107+
]);
108+
}
109+
110+
public function testBug6974TreatPhpDocTypesAsCertain(): void
111+
{
112+
$this->treatPhpDocTypesAsCertain = true;
113+
$this->strictUnnecessaryNullsafePropertyFetch = false;
94114
$this->analyse([__DIR__ . '/data/bug-6974.php'], [
95115
[
96116
'Variable $a in empty() always exists and is always falsy.',
@@ -193,4 +213,12 @@ public function testBug7199(): void
193213
$this->analyse([__DIR__ . '/data/bug-7199.php'], []);
194214
}
195215

216+
public function testBug9126(): void
217+
{
218+
$this->treatPhpDocTypesAsCertain = false;
219+
$this->strictUnnecessaryNullsafePropertyFetch = false;
220+
221+
$this->analyse([__DIR__ . '/data/bug-9126.php'], []);
222+
}
223+
196224
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Bug9126;
4+
5+
class User {}
6+
7+
class Resume {
8+
9+
/**
10+
* @return User
11+
*/
12+
public function getOwner() {
13+
return $this->owner;
14+
}
15+
}
16+
17+
function (): void {
18+
$resume = new Resume();
19+
$owner = $resume->getOwner();
20+
if (!empty($owner)) {
21+
echo "not empty";
22+
}
23+
};

0 commit comments

Comments
 (0)