Skip to content

Commit 8925bd3

Browse files
ntzmondrejmirtes
authored andcommitted
add support for methodExists and propertyExists
1 parent 9b093e3 commit 8925bd3

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ This extension specifies types of values passed to:
5757
* `Assert::minCount`
5858
* `Assert::inArray`
5959
* `Assert::oneOf`
60+
* `Assert::methodExists`
61+
* `Assert::propertyExists`
6062
* `nullOr*` and `all*` variants of the above methods
6163

6264

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,18 @@ private static function getExpressionResolvers(): array
406406
]
407407
);
408408
},
409+
'methodExists' => function (Scope $scope, Arg $object, Arg $method): \PhpParser\Node\Expr {
410+
return new \PhpParser\Node\Expr\FuncCall(
411+
new \PhpParser\Node\Name('method_exists'),
412+
[$object, $method]
413+
);
414+
},
415+
'propertyExists' => function (Scope $scope, Arg $object, Arg $property): \PhpParser\Node\Expr {
416+
return new \PhpParser\Node\Expr\FuncCall(
417+
new \PhpParser\Node\Name('property_exists'),
418+
[$object, $property]
419+
);
420+
},
409421
];
410422
}
411423

tests/Type/WebMozartAssert/data/data.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class Foo
88
{
99

10-
public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $r, $s, ?int $t, ?int $u, $x, $aa, array $ab, $ac, $ad, $ae, $af, $ag, array $ah, $ai, $al, $am, $an)
10+
public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $r, $s, ?int $t, ?int $u, $x, $aa, array $ab, $ac, $ad, $ae, $af, $ag, array $ah, $ai, $al, $am, $an, $ao, $ap)
1111
{
1212
\PHPStan\Testing\assertType('mixed', $a);
1313

@@ -160,6 +160,14 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
160160

161161
Assert::oneOf($an, [1, 2]);
162162
\PHPStan\Testing\assertType('1|2', $an);
163+
164+
/** @var object $ao */
165+
Assert::methodExists($ao, 'foo');
166+
\PHPStan\Testing\assertType('object&hasMethod(foo)', $ao);
167+
168+
/** @var object $ap */
169+
Assert::propertyExists($ap, 'foo');
170+
\PHPStan\Testing\assertType('object&hasProperty(foo)', $ap);
163171
}
164172

165173
}

0 commit comments

Comments
 (0)