Skip to content

Commit e4fb16e

Browse files
authored
Improve the conversion of Callable to a string (#259)
* Improve the conversion of `Callable` to a string * Fix CS * Refactor `CallableParameter` * Revert "Refactor `CallableParameter`" This reverts commit dfed23f.
1 parent e2ac359 commit e4fb16e

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

src/Types/CallableParameter.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
use phpDocumentor\Reflection\Type;
1616

17+
use function trim;
18+
1719
/**
1820
* Value Object representing a Callable parameters.
1921
*
@@ -74,4 +76,14 @@ public function isOptional(): bool
7476
{
7577
return $this->isOptional;
7678
}
79+
80+
public function __toString(): string
81+
{
82+
$reference = $this->isReference ? '&' : '';
83+
$variadic = $this->isVariadic ? '...' : '';
84+
$optional = $this->isOptional ? '=' : '';
85+
$name = $this->name !== null ? '$' . $this->name : '';
86+
87+
return trim($this->type . ' ' . $reference . $variadic . $name . $optional);
88+
}
7789
}

src/Types/Callable_.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
use phpDocumentor\Reflection\Type;
1717

18+
use function implode;
19+
1820
/**
1921
* Value Object representing a Callable type.
2022
*
@@ -63,6 +65,16 @@ public function getReturnType(): ?Type
6365
*/
6466
public function __toString(): string
6567
{
66-
return $this->identifier;
68+
if (!$this->parameters && $this->returnType === null) {
69+
return $this->identifier;
70+
}
71+
72+
if ($this->returnType instanceof self) {
73+
$returnType = '(' . (string) $this->returnType . ')';
74+
} else {
75+
$returnType = (string) $this->returnType;
76+
}
77+
78+
return $this->identifier . '(' . implode(', ', $this->parameters) . '): ' . $returnType;
6779
}
6880
}

tests/unit/TypeResolverTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,34 @@ public function callableProvider(): array
11921192
new Object_(new Fqsen('\\phpDocumentor\\Foo'))
11931193
),
11941194
],
1195+
[
1196+
'Closure(mixed): (callable(mixed): mixed)',
1197+
new Callable_(
1198+
'Closure',
1199+
[
1200+
new CallableParameter(
1201+
new Mixed_(),
1202+
null,
1203+
false,
1204+
false,
1205+
false
1206+
),
1207+
],
1208+
new Callable_(
1209+
'callable',
1210+
[
1211+
new CallableParameter(
1212+
new Mixed_(),
1213+
null,
1214+
false,
1215+
false,
1216+
false
1217+
),
1218+
],
1219+
new Mixed_()
1220+
)
1221+
),
1222+
],
11951223
];
11961224
}
11971225

tests/unit/Types/CallableTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,65 @@ public static function provideToStringData(): array
7676
'\Closure',
7777
new Callable_('\Closure'),
7878
],
79+
'with different types' => [
80+
'callable(\\phpDocumentor\\C, \\phpDocumentor\\A &...$a=, \\phpDocumentor\\B &...=): '
81+
. '\\phpDocumentor\\Foo',
82+
new Callable_(
83+
'callable',
84+
[
85+
new CallableParameter(
86+
new Object_(new Fqsen('\\phpDocumentor\\C')),
87+
null,
88+
false,
89+
false,
90+
false
91+
),
92+
new CallableParameter(
93+
new Object_(new Fqsen('\\phpDocumentor\\A')),
94+
'a',
95+
true,
96+
true,
97+
true
98+
),
99+
new CallableParameter(
100+
new Object_(new Fqsen('\\phpDocumentor\\B')),
101+
null,
102+
true,
103+
true,
104+
true
105+
),
106+
],
107+
new Object_(new Fqsen('\\phpDocumentor\\Foo'))
108+
),
109+
],
110+
'return callable' => [
111+
'Closure(mixed): (callable(mixed): mixed)',
112+
new Callable_(
113+
'Closure',
114+
[
115+
new CallableParameter(
116+
new Mixed_(),
117+
null,
118+
false,
119+
false,
120+
false
121+
),
122+
],
123+
new Callable_(
124+
'callable',
125+
[
126+
new CallableParameter(
127+
new Mixed_(),
128+
null,
129+
false,
130+
false,
131+
false
132+
),
133+
],
134+
new Mixed_()
135+
)
136+
),
137+
],
79138
];
80139
}
81140
}

0 commit comments

Comments
 (0)