Skip to content

Commit

Permalink
add support for isArrayAccessible
Browse files Browse the repository at this point in the history
  • Loading branch information
ntzm authored and ondrejmirtes committed Jul 4, 2021
1 parent 8925bd3 commit c500936
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -59,6 +59,7 @@ This extension specifies types of values passed to:
* `Assert::oneOf`
* `Assert::methodExists`
* `Assert::propertyExists`
* `Assert::isArrayAccessible`
* `nullOr*` and `all*` variants of the above methods


Expand Down
12 changes: 12 additions & 0 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Expand Up @@ -418,6 +418,18 @@ private static function getExpressionResolvers(): array
[$object, $property]
);
},
'isArrayAccessible' => function (Scope $scope, Arg $expr): \PhpParser\Node\Expr {
return new \PhpParser\Node\Expr\BinaryOp\BooleanOr(
new \PhpParser\Node\Expr\FuncCall(
new \PhpParser\Node\Name('is_array'),
[$expr]
),
new \PhpParser\Node\Expr\Instanceof_(
$expr->value,
new \PhpParser\Node\Name(\ArrayAccess::class)
)
);
},
];
}

Expand Down
5 changes: 4 additions & 1 deletion tests/Type/WebMozartAssert/data/data.php
Expand Up @@ -7,7 +7,7 @@
class Foo
{

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)
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)
{
\PHPStan\Testing\assertType('mixed', $a);

Expand Down Expand Up @@ -168,6 +168,9 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
/** @var object $ap */
Assert::propertyExists($ap, 'foo');
\PHPStan\Testing\assertType('object&hasProperty(foo)', $ap);

Assert::isArrayAccessible($aq);
\PHPStan\Testing\assertType('array|ArrayAccess', $aq);
}

}
Expand Down

0 comments on commit c500936

Please sign in to comment.