Skip to content

Commit

Permalink
Add support for assertObjectHasAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroennoten authored and ondrejmirtes committed Aug 12, 2021
1 parent 2968a62 commit 7c01ef9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ private static function getExpressionResolvers(): array
'ArrayHasKey' => function (Scope $scope, Arg $key, Arg $array): FuncCall {
return new \PhpParser\Node\Expr\FuncCall(new Name('array_key_exists'), [$key, $array]);
},
'ObjectHasAttribute' => function (Scope $scope, Arg $property, Arg $object): FuncCall {
return new \PhpParser\Node\Expr\FuncCall(new Name('property_exists'), [$object, $property]);
},
];
}

Expand Down
8 changes: 7 additions & 1 deletion tests/Type/PHPUnit/data/assert-function.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use function PHPStan\Testing\assertType;
use function PHPUnit\Framework\assertArrayHasKey;
use function PHPUnit\Framework\assertArrayNotHasKey;
use function PHPUnit\Framework\assertInstanceOf;
use function PHPUnit\Framework\assertObjectHasAttribute;

class Foo
{
Expand All @@ -30,4 +30,10 @@ public function arrayHasStringKey(array $a): void
assertType("array&hasOffset('key')", $a);
}

public function objectHasAttribute(object $a): void
{
assertObjectHasAttribute('property', $a);
assertType("object&hasProperty(property)", $a);
}

}

0 comments on commit 7c01ef9

Please sign in to comment.