Skip to content

Commit 8699be8

Browse files
authored
Support remaining *Exists assertions
1 parent ed8c49f commit 8699be8

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Type/BeberleiAssert/AssertHelper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,22 @@ private static function getExpressionResolvers(): array
393393
[$key, $array],
394394
),
395395
),
396+
'propertyExists' => static fn (Scope $scope, Arg $object, Arg $property): Expr => new FuncCall(
397+
new Name('property_exists'),
398+
[$object, $property],
399+
),
400+
'methodExists' => static fn (Scope $scope, Arg $object, Arg $method): Expr => new FuncCall(
401+
new Name('method_exists'),
402+
[$object, $method],
403+
),
404+
'classExists' => static fn (Scope $scope, Arg $value): Expr => new FuncCall(
405+
new Name('class_exists'),
406+
[$value],
407+
),
408+
'interfaceExists' => static fn (Scope $scope, Arg $value): Expr => new FuncCall(
409+
new Name('interface_exists'),
410+
[$value],
411+
),
396412
'notBlank' => static fn (Scope $scope, Arg $value): Expr => new BooleanAnd(
397413
new BooleanAnd(
398414
new NotIdentical(

tests/Type/BeberleiAssert/data/data.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
4141
Assertion::objectOrClass($j);
4242
\PHPStan\Testing\assertType('object', $j);
4343

44+
Assertion::propertyExists($j, 'foo');
45+
\PHPStan\Testing\assertType('object&hasProperty(foo)', $j);
46+
47+
Assertion::methodExists($j, 'doBar');
48+
\PHPStan\Testing\assertType('object&hasMethod(doBar)&hasProperty(foo)', $j);
49+
50+
/** @var string $classString */
51+
$classString = doFoo();
52+
Assertion::classExists($classString);
53+
\PHPStan\Testing\assertType('class-string', $classString);
54+
55+
/** @var string $interfaceString */
56+
$interfaceString = doFoo();
57+
Assertion::interfaceExists($interfaceString);
58+
\PHPStan\Testing\assertType('class-string', $interfaceString);
59+
4460
Assertion::isResource($k);
4561
\PHPStan\Testing\assertType('resource', $k);
4662

0 commit comments

Comments
 (0)