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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
<?php declare(strict_types=1);
namespace PhpParser\Node\Expr;
use PhpParser\Node\Arg;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\VariadicPlaceholder;
use PHPUnit\Framework\Attributes\DataProvider;
class CallableLikeTest extends \PHPUnit\Framework\TestCase {
#[DataProvider('provideTestIsFirstClassCallable')]
public function testIsFirstClassCallable(CallLike $node, bool $isFirstClassCallable): void {
$this->assertSame($isFirstClassCallable, $node->isFirstClassCallable());
if (!$isFirstClassCallable) {
$this->assertSame($node->getRawArgs(), $node->getArgs());
}
}
/**
* @dataProvider provideTestGetArg
*/
public function testGetArg(CallLike $node, ?Arg $expected): void {
$this->assertSame($expected, $node->getArg('bar', 1));
}
public static function provideTestIsFirstClassCallable() {
$normalArgs = [new Arg(new Int_(1))];
$callableArgs = [new VariadicPlaceholder()];
return [
[new FuncCall(new Name('test'), $normalArgs), false],
[new FuncCall(new Name('test'), $callableArgs), true],
[new MethodCall(new Variable('this'), 'test', $normalArgs), false],
[new MethodCall(new Variable('this'), 'test', $callableArgs), true],
[new StaticCall(new Name('Test'), 'test', $normalArgs), false],
[new StaticCall(new Name('Test'), 'test', $callableArgs), true],
[new New_(new Name('Test'), $normalArgs), false],
[new NullsafeMethodCall(new Variable('this'), 'test', $normalArgs), false],
// This is not legal code, but accepted by the parser.
[new New_(new Name('Test'), $callableArgs), true],
[new NullsafeMethodCall(new Variable('this'), 'test', $callableArgs), true],
];
}
public static function provideTestGetArg() {
$foo = new Arg(new Int_(1));
$namedFoo = new Arg(new Int_(1), false, false, [], new Identifier('foo'));
$bar = new Arg(new Int_(2));
$namedBar = new Arg(new Int_(2), false, false, [], new Identifier('bar'));
$unpack = new Arg(new Int_(3), false, true);
$callableArgs = [new VariadicPlaceholder()];
return [
[new FuncCall(new Name('test'), [$foo]), null],
[new FuncCall(new Name('test'), [$namedFoo]), null],
[new FuncCall(new Name('test'), [$foo, $bar]), $bar],
[new FuncCall(new Name('test'), [$namedBar]), $namedBar],
[new FuncCall(new Name('test'), [$namedFoo, $namedBar]), $namedBar],
[new FuncCall(new Name('test'), [$namedBar, $namedFoo]), $namedBar],
[new FuncCall(new Name('test'), [$namedFoo, $unpack]), null],
[new FuncCall(new Name('test'), $callableArgs), null],
[new MethodCall(new Variable('this'), 'test', [$foo]), null],
[new MethodCall(new Variable('this'), 'test', [$namedFoo]), null],
[new MethodCall(new Variable('this'), 'test', [$foo, $bar]), $bar],
[new MethodCall(new Variable('this'), 'test', [$namedBar]), $namedBar],
[new MethodCall(new Variable('this'), 'test', [$namedFoo, $namedBar]), $namedBar],
[new MethodCall(new Variable('this'), 'test', [$namedBar, $namedFoo]), $namedBar],
[new MethodCall(new Variable('this'), 'test', [$namedFoo, $unpack]), null],
[new MethodCall(new Variable('this'), 'test', $callableArgs), null],
[new StaticCall(new Name('Test'), 'test', [$foo]), null],
[new StaticCall(new Name('Test'), 'test', [$namedFoo]), null],
[new StaticCall(new Name('Test'), 'test', [$foo, $bar]), $bar],
[new StaticCall(new Name('Test'), 'test', [$namedBar]), $namedBar],
[new StaticCall(new Name('Test'), 'test', [$namedFoo, $namedBar]), $namedBar],
[new StaticCall(new Name('Test'), 'test', [$namedBar, $namedFoo]), $namedBar],
[new StaticCall(new Name('Test'), 'test', [$namedFoo, $unpack]), null],
[new StaticCall(new Name('Test'), 'test', $callableArgs), null],
[new New_(new Name('test'), [$foo]), null],
[new New_(new Name('test'), [$namedFoo]), null],
[new New_(new Name('test'), [$foo, $bar]), $bar],
[new New_(new Name('test'), [$namedBar]), $namedBar],
[new New_(new Name('test'), [$namedFoo, $namedBar]), $namedBar],
[new New_(new Name('test'), [$namedBar, $namedFoo]), $namedBar],
[new New_(new Name('test'), [$namedFoo, $unpack]), null],
[new NullsafeMethodCall(new Variable('this'), 'test', [$foo]), null],
[new NullsafeMethodCall(new Variable('this'), 'test', [$namedFoo]), null],
[new NullsafeMethodCall(new Variable('this'), 'test', [$foo, $bar]), $bar],
[new NullsafeMethodCall(new Variable('this'), 'test', [$namedBar]), $namedBar],
[new NullsafeMethodCall(new Variable('this'), 'test', [$namedFoo, $namedBar]), $namedBar],
[new NullsafeMethodCall(new Variable('this'), 'test', [$namedBar, $namedFoo]), $namedBar],
[new NullsafeMethodCall(new Variable('this'), 'test', [$namedFoo, $unpack]), null],
// This is not legal code, but accepted by the parser.
[new New_(new Name('Test'), $callableArgs), null],
[new NullsafeMethodCall(new Variable('this'), 'test', $callableArgs), null],
];
}
}
|