Skip to content

Commit 3920d37

Browse files
greg0ireondrejmirtes
authored andcommitted
Add support for keyExists()
Closes #25
1 parent 714e7a6 commit 3920d37

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,12 @@ private static function getExpressionResolvers(): array
320320
new \PhpParser\Node\Name($classType->getValue())
321321
);
322322
},
323+
'keyExists' => function (Scope $scope, Arg $array, Arg $key): \PhpParser\Node\Expr {
324+
return new \PhpParser\Node\Expr\FuncCall(
325+
new \PhpParser\Node\Name('array_key_exists'),
326+
[$key, $array]
327+
);
328+
},
323329
'true' => function (Scope $scope, Arg $expr): \PhpParser\Node\Expr {
324330
return new \PhpParser\Node\Expr\BinaryOp\Identical(
325331
$expr->value,

tests/Type/WebMozartAssert/AssertTypeSpecifyingExtensionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ public function testExtension(): void
164164
'Variable $af is: int',
165165
126,
166166
],
167+
[
168+
'Variable $things is: array(\'foo\' => string, \'bar\' => string)',
169+
131,
170+
],
167171
]);
168172
}
169173

tests/Type/WebMozartAssert/data/data.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
124124
/** @var int|false $af */
125125
Assert::notFalse($af);
126126
$af;
127+
128+
/** @var array{foo?: string, bar?: string} $things */
129+
$things = doFoo();
130+
Assert::keyExists($things, 'foo');
131+
$things;
127132
}
128133

129134
}

0 commit comments

Comments
 (0)