Skip to content

Commit 78eb4cb

Browse files
committed
Add identifier to the Callable
1 parent baf8970 commit 78eb4cb

File tree

4 files changed

+122
-16
lines changed

4 files changed

+122
-16
lines changed

src/TypeResolver.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -466,18 +466,22 @@ private function createFromGeneric(GenericTypeNode $type, Context $context): Typ
466466

467467
private function createFromCallable(CallableTypeNode $type, Context $context): Callable_
468468
{
469-
return new Callable_(array_map(
470-
function (CallableTypeParameterNode $param) use ($context): CallableParameter {
471-
return new CallableParameter(
472-
$this->createType($param->type, $context),
473-
$param->parameterName !== '' ? trim($param->parameterName, '$') : null,
474-
$param->isReference,
475-
$param->isVariadic,
476-
$param->isOptional
477-
);
478-
},
479-
$type->parameters
480-
), $this->createType($type->returnType, $context));
469+
return new Callable_(
470+
(string) $type->identifier,
471+
array_map(
472+
function (CallableTypeParameterNode $param) use ($context): CallableParameter {
473+
return new CallableParameter(
474+
$this->createType($param->type, $context),
475+
$param->parameterName !== '' ? trim($param->parameterName, '$') : null,
476+
$param->isReference,
477+
$param->isVariadic,
478+
$param->isOptional
479+
);
480+
},
481+
$type->parameters
482+
),
483+
$this->createType($type->returnType, $context)
484+
);
481485
}
482486

483487
private function createFromConst(ConstTypeNode $type, Context $context): Type

src/Types/Callable_.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
final class Callable_ implements Type
2424
{
25+
/** @var string */
26+
private $identifier;
2527
/** @var Type|null */
2628
private $returnType;
2729
/** @var CallableParameter[] */
@@ -30,12 +32,21 @@ final class Callable_ implements Type
3032
/**
3133
* @param CallableParameter[] $parameters
3234
*/
33-
public function __construct(array $parameters = [], ?Type $returnType = null)
34-
{
35+
public function __construct(
36+
string $identifier = 'callable',
37+
array $parameters = [],
38+
?Type $returnType = null
39+
) {
40+
$this->identifier = $identifier;
3541
$this->parameters = $parameters;
3642
$this->returnType = $returnType;
3743
}
3844

45+
public function getIdentifier(): string
46+
{
47+
return $this->identifier;
48+
}
49+
3950
/** @return CallableParameter[] */
4051
public function getParameters(): array
4152
{
@@ -52,6 +63,6 @@ public function getReturnType(): ?Type
5263
*/
5364
public function __toString(): string
5465
{
55-
return 'callable';
66+
return $this->identifier;
5667
}
5768
}

tests/unit/TypeResolverTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,11 +1135,20 @@ public function callableProvider(): array
11351135
],
11361136
[
11371137
'callable(): Foo',
1138-
new Callable_([], new Object_(new Fqsen('\\phpDocumentor\\Foo'))),
1138+
new Callable_('callable', [], new Object_(new Fqsen('\\phpDocumentor\\Foo'))),
1139+
],
1140+
[
1141+
'Closure(): Foo',
1142+
new Callable_('Closure', [], new Object_(new Fqsen('\\phpDocumentor\\Foo'))),
1143+
],
1144+
[
1145+
'\Closure(): Foo',
1146+
new Callable_('\Closure', [], new Object_(new Fqsen('\\phpDocumentor\\Foo'))),
11391147
],
11401148
[
11411149
'callable(): (Foo&Bar)',
11421150
new Callable_(
1151+
'callable',
11431152
[],
11441153
new Intersection(
11451154
[
@@ -1152,6 +1161,7 @@ public function callableProvider(): array
11521161
[
11531162
'callable(A&...$a=, B&...=, C): Foo',
11541163
new Callable_(
1164+
'callable',
11551165
[
11561166
new CallableParameter(
11571167
new Object_(new Fqsen('\\phpDocumentor\\A')),

tests/unit/Types/CallableTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Reflection\Types;
15+
16+
use phpDocumentor\Reflection\Fqsen;
17+
use PHPUnit\Framework\TestCase;
18+
19+
final class CallableTest extends TestCase
20+
{
21+
public function testCreate(): void
22+
{
23+
$identifier = 'callable';
24+
$parameters = [
25+
new CallableParameter(
26+
new Object_(new Fqsen('\\phpDocumentor\\A')),
27+
'a',
28+
true,
29+
true,
30+
true
31+
),
32+
new CallableParameter(
33+
new Object_(new Fqsen('\\phpDocumentor\\B')),
34+
null,
35+
true,
36+
true,
37+
true
38+
),
39+
new CallableParameter(
40+
new Object_(new Fqsen('\\phpDocumentor\\C')),
41+
null,
42+
false,
43+
false,
44+
false
45+
),
46+
];
47+
48+
$returnType = new Object_(new Fqsen('\\phpDocumentor\\Foo'));
49+
50+
$type = new Callable_($identifier, $parameters, $returnType);
51+
52+
$this->assertSame($identifier, $type->getIdentifier());
53+
$this->assertSame($parameters, $type->getParameters());
54+
$this->assertSame($returnType, $type->getReturnType());
55+
}
56+
57+
/**
58+
* @dataProvider provideToStringData
59+
*/
60+
public function testToString(string $expectedResult, Callable_ $type): void
61+
{
62+
$this->assertSame($expectedResult, (string) $type);
63+
}
64+
65+
/**
66+
* @return array<string, array{string, Callable_}>
67+
*/
68+
public static function provideToStringData(): array
69+
{
70+
return [
71+
'basic' => [
72+
'callable',
73+
new Callable_(),
74+
],
75+
'closure' => [
76+
'\Closure',
77+
new Callable_('\Closure'),
78+
],
79+
];
80+
}
81+
}

0 commit comments

Comments
 (0)