Skip to content

Commit c500936

Browse files
ntzmondrejmirtes
authored andcommitted
add support for isArrayAccessible
1 parent 8925bd3 commit c500936

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ This extension specifies types of values passed to:
5959
* `Assert::oneOf`
6060
* `Assert::methodExists`
6161
* `Assert::propertyExists`
62+
* `Assert::isArrayAccessible`
6263
* `nullOr*` and `all*` variants of the above methods
6364

6465

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

+12
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,18 @@ private static function getExpressionResolvers(): array
418418
[$object, $property]
419419
);
420420
},
421+
'isArrayAccessible' => function (Scope $scope, Arg $expr): \PhpParser\Node\Expr {
422+
return new \PhpParser\Node\Expr\BinaryOp\BooleanOr(
423+
new \PhpParser\Node\Expr\FuncCall(
424+
new \PhpParser\Node\Name('is_array'),
425+
[$expr]
426+
),
427+
new \PhpParser\Node\Expr\Instanceof_(
428+
$expr->value,
429+
new \PhpParser\Node\Name(\ArrayAccess::class)
430+
)
431+
);
432+
},
421433
];
422434
}
423435

tests/Type/WebMozartAssert/data/data.php

+4-1
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, $ao, $ap)
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, $aq)
1111
{
1212
\PHPStan\Testing\assertType('mixed', $a);
1313

@@ -168,6 +168,9 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
168168
/** @var object $ap */
169169
Assert::propertyExists($ap, 'foo');
170170
\PHPStan\Testing\assertType('object&hasProperty(foo)', $ap);
171+
172+
Assert::isArrayAccessible($aq);
173+
\PHPStan\Testing\assertType('array|ArrayAccess', $aq);
171174
}
172175

173176
}

0 commit comments

Comments
 (0)