-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathAssertArrayHasKeyCallFactory.php
More file actions
63 lines (51 loc) · 1.93 KB
/
AssertArrayHasKeyCallFactory.php
File metadata and controls
63 lines (51 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\CodeQuality\NodeFactory;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Analyser\Scope;
use Rector\PHPUnit\Enum\PHPUnitClassName;
final class AssertArrayHasKeyCallFactory
{
public function create(Variable $dimFetchVariable, Expr $dimExpr, Scope $scope): Expression
{
$args = $this->createArgs($dimFetchVariable, $dimExpr);
if ($this->isInsideTestCase($scope)) {
$call = new MethodCall(new Variable('this'), 'assertArrayHasKey', $args);
return new Expression($call);
}
$call = new StaticCall(new FullyQualified(PHPUnitClassName::ASSERT), 'assertArrayHasKey', $args);
return new Expression($call);
}
private function isInsideTestCase(Scope $scope): bool
{
if (! $scope->isInClass()) {
return false;
}
$classReflection = $scope->getClassReflection();
return $classReflection->is(PHPUnitClassName::TEST_CASE);
}
/**
* @return Arg[]
*/
private function createArgs(Variable $dimFetchVariable, Expr $dimExpr): array
{
// add detailed error: 'Existing keys are: ' . implode(', ', array_keys($data))
$arrayKeysFuncCall = new FuncCall(new Name('array_keys'), [new Arg($dimFetchVariable)]);
$implodeFuncCall = new FuncCall(new Name('implode'), [
new Arg(new String_(', ')),
new Arg($arrayKeysFuncCall),
]);
$concat = new Concat(new String_('Existing keys are: '), $implodeFuncCall);
return [new Arg($dimExpr), new Arg($dimFetchVariable), new Arg($concat)];
}
}