Skip to content

Commit

Permalink
Prevent checking assertions on $this-> types, always accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Apr 12, 2020
1 parent 9c129f3 commit 5988149
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,8 @@ public static function analyze(
$class_storage_for_method = $codebase->methods->getClassLikeStorageForMethod($method_id);
$plain_getter_property = null;

if ((isset($class_storage_for_method->methods[$method_name_lc]))
if ($lhs_var_id !== '$this'
&& (isset($class_storage_for_method->methods[$method_name_lc]))
&& !$class_storage_for_method->methods[$method_name_lc]->overridden_somewhere
&& !$class_storage_for_method->methods[$method_name_lc]->overridden_downstream
&& ($plain_getter_property = $class_storage_for_method->methods[$method_name_lc]->plain_getter)
Expand Down
61 changes: 39 additions & 22 deletions tests/MethodCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,23 +580,23 @@ function takesWithoutArguments(A $a) : void {
class A {
/** @var int|string|null */
public $a;
/** @return int|string|null */
function getA() {
return $this->a;
return $this->a;
}
function takesNullOrA(?A $a) : void {}
}
$a = new A();
$a->a = 1;
echo $a->getA() + 2;
$a->a = "string";
echo strlen($a->getA());
$a->a = null;
$a->takesNullOrA($a->getA());
'
Expand All @@ -606,19 +606,36 @@ function takesNullOrA(?A $a) : void {}
class A {
/** @var string|null */
public $a;
/** @return string|null */
function getA() {
return $this->a;
return $this->a;
}
}
$a = new A();
if ($a->getA()) {
echo strlen($a->getA());
}
'
}'
],
'ignorePossiblyNull' => [
'<?php
class Foo {
protected ?string $type = null;
public function prepend(array $arr) : string {
return $this->getType();
}
/**
* @psalm-ignore-nullable-return
*/
public function getType() : ?string
{
return $this->type;
}
}'
],
];
}
Expand Down Expand Up @@ -1009,25 +1026,25 @@ public function fooFoo(int $a): void {}
class A {
/** @var string|null */
public $a;
/** @return string|null */
function getA() {
return $this->a;
return $this->a;
}
}
class AChild extends A {
function getA() {
return rand(0, 1) ? $this->a : null;
}
}
function foo(A $a) : void {
if ($a->getA()) {
echo strlen($a->getA());
}
}
foo(new AChild());',
'error_message' => 'PossiblyNullArgument'
],
Expand All @@ -1036,30 +1053,30 @@ function foo(A $a) : void {
class A {
/** @var string|null */
public $a;
/** @psalm-assert-if-true string $this->a */
function hasA() {
return is_string($this->a);
return is_string($this->a);
}
/** @return string|null */
function getA() {
return $this->a;
return $this->a;
}
}
class AChild extends A {
function getA() {
return rand(0, 1) ? $this->a : null;
}
}
function foo(A $a) : void {
if ($a->hasA()) {
echo strlen($a->getA());
}
}
foo(new AChild());',
'error_message' => 'PossiblyNullArgument'
]
Expand Down

0 comments on commit 5988149

Please sign in to comment.