Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Types/CallableParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@

use phpDocumentor\Reflection\Type;

use function trim;

/**
* Value Object representing a Callable parameters.
*
* @psalm-immutable
*/
final class CallableParameter
final class CallableParameter implements Type
{
/** @var Type */
private $type;
Expand Down Expand Up @@ -74,4 +76,14 @@ public function isOptional(): bool
{
return $this->isOptional;
}

public function __toString(): string
{
$reference = $this->isReference ? '&' : '';
$variadic = $this->isVariadic ? '...' : '';
$optional = $this->isOptional ? '=' : '';
$name = $this->name !== null ? '$' . $this->name : '';

return trim($this->type . ' ' . $reference . $variadic . $name . $optional);
}
}
14 changes: 13 additions & 1 deletion src/Types/Callable_.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use phpDocumentor\Reflection\Type;

use function implode;

/**
* Value Object representing a Callable type.
*
Expand Down Expand Up @@ -63,6 +65,16 @@ public function getReturnType(): ?Type
*/
public function __toString(): string
{
return $this->identifier;
if (!$this->parameters && $this->returnType === null) {
return $this->identifier;
}

if ($this->returnType instanceof self) {
$returnType = '(' . (string) $this->returnType . ')';
} else {
$returnType = (string) $this->returnType;
}

return $this->identifier . '(' . implode(', ', $this->parameters) . '): ' . $returnType;
}
}
28 changes: 28 additions & 0 deletions tests/unit/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,34 @@ public function callableProvider(): array
new Object_(new Fqsen('\\phpDocumentor\\Foo'))
),
],
[
'Closure(mixed): (callable(mixed): mixed)',
new Callable_(
'Closure',
[
new CallableParameter(
new Mixed_(),
null,
false,
false,
false
),
],
new Callable_(
'callable',
[
new CallableParameter(
new Mixed_(),
null,
false,
false,
false
),
],
new Mixed_()
)
),
],
];
}

Expand Down
59 changes: 59 additions & 0 deletions tests/unit/Types/CallableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,65 @@ public static function provideToStringData(): array
'\Closure',
new Callable_('\Closure'),
],
'with different types' => [
'callable(\\phpDocumentor\\C, \\phpDocumentor\\A &...$a=, \\phpDocumentor\\B &...=): '
. '\\phpDocumentor\\Foo',
new Callable_(
'callable',
[
new CallableParameter(
new Object_(new Fqsen('\\phpDocumentor\\C')),
null,
false,
false,
false
),
new CallableParameter(
new Object_(new Fqsen('\\phpDocumentor\\A')),
'a',
true,
true,
true
),
new CallableParameter(
new Object_(new Fqsen('\\phpDocumentor\\B')),
null,
true,
true,
true
),
],
new Object_(new Fqsen('\\phpDocumentor\\Foo'))
),
],
'return callable' => [
'Closure(mixed): (callable(mixed): mixed)',
new Callable_(
'Closure',
[
new CallableParameter(
new Mixed_(),
null,
false,
false,
false
),
],
new Callable_(
'callable',
[
new CallableParameter(
new Mixed_(),
null,
false,
false,
false
),
],
new Mixed_()
)
),
],
];
}
}