Skip to content

Commit 6aaff11

Browse files
simonhammesondrejmirtes
authored andcommitted
Add support for assertArrayHasKey (#100)
1 parent efc0099 commit 6aaff11

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php

+3
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ private static function getExpressionResolvers(): array
252252
]
253253
);
254254
},
255+
'ArrayHasKey' => function (Scope $scope, Arg $key, Arg $array): FuncCall {
256+
return new \PhpParser\Node\Expr\FuncCall(new Name('array_key_exists'), [$key, $array]);
257+
},
255258
];
256259
}
257260

tests/Type/PHPUnit/data/assert-function.php

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace AssertFunction;
44

55
use function PHPStan\Testing\assertType;
6+
use function PHPUnit\Framework\assertArrayHasKey;
7+
use function PHPUnit\Framework\assertArrayNotHasKey;
68
use function PHPUnit\Framework\assertInstanceOf;
79

810
class Foo
@@ -17,4 +19,15 @@ public function doFoo($o): void
1719
assertType(self::class, $o);
1820
}
1921

22+
public function arrayHasNumericKey(array $a): void {
23+
assertArrayHasKey(0, $a);
24+
assertType('array&hasOffset(0)', $a);
25+
}
26+
27+
public function arrayHasStringKey(array $a): void
28+
{
29+
assertArrayHasKey('key', $a);
30+
assertType("array&hasOffset('key')", $a);
31+
}
32+
2033
}

0 commit comments

Comments
 (0)