Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 16 additions & 12 deletions src/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,18 +466,22 @@ private function createFromGeneric(GenericTypeNode $type, Context $context): Typ

private function createFromCallable(CallableTypeNode $type, Context $context): Callable_
{
return new Callable_(array_map(
function (CallableTypeParameterNode $param) use ($context): CallableParameter {
return new CallableParameter(
$this->createType($param->type, $context),
$param->parameterName !== '' ? trim($param->parameterName, '$') : null,
$param->isReference,
$param->isVariadic,
$param->isOptional
);
},
$type->parameters
), $this->createType($type->returnType, $context));
return new Callable_(
(string) $type->identifier,
array_map(
function (CallableTypeParameterNode $param) use ($context): CallableParameter {
return new CallableParameter(
$this->createType($param->type, $context),
$param->parameterName !== '' ? trim($param->parameterName, '$') : null,
$param->isReference,
$param->isVariadic,
$param->isOptional
);
},
$type->parameters
),
$this->createType($type->returnType, $context)
);
}

private function createFromConst(ConstTypeNode $type, Context $context): Type
Expand Down
17 changes: 14 additions & 3 deletions src/Types/Callable_.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
final class Callable_ implements Type
{
/** @var string */
private $identifier;
/** @var Type|null */
private $returnType;
/** @var CallableParameter[] */
Expand All @@ -30,12 +32,21 @@ final class Callable_ implements Type
/**
* @param CallableParameter[] $parameters
*/
public function __construct(array $parameters = [], ?Type $returnType = null)
{
public function __construct(
string $identifier = 'callable',
array $parameters = [],
?Type $returnType = null
) {
$this->identifier = $identifier;
$this->parameters = $parameters;
$this->returnType = $returnType;
}

public function getIdentifier(): string
{
return $this->identifier;
}

/** @return CallableParameter[] */
public function getParameters(): array
{
Expand All @@ -52,6 +63,6 @@ public function getReturnType(): ?Type
*/
public function __toString(): string
{
return 'callable';
return $this->identifier;
}
}
12 changes: 11 additions & 1 deletion tests/unit/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1135,11 +1135,20 @@ public function callableProvider(): array
],
[
'callable(): Foo',
new Callable_([], new Object_(new Fqsen('\\phpDocumentor\\Foo'))),
new Callable_('callable', [], new Object_(new Fqsen('\\phpDocumentor\\Foo'))),
],
[
'Closure(): Foo',
new Callable_('Closure', [], new Object_(new Fqsen('\\phpDocumentor\\Foo'))),
],
[
'\Closure(): Foo',
new Callable_('\Closure', [], new Object_(new Fqsen('\\phpDocumentor\\Foo'))),
],
[
'callable(): (Foo&Bar)',
new Callable_(
'callable',
[],
new Intersection(
[
Expand All @@ -1152,6 +1161,7 @@ public function callableProvider(): array
[
'callable(A&...$a=, B&...=, C): Foo',
new Callable_(
'callable',
[
new CallableParameter(
new Object_(new Fqsen('\\phpDocumentor\\A')),
Expand Down
81 changes: 81 additions & 0 deletions tests/unit/Types/CallableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/

namespace phpDocumentor\Reflection\Types;

use phpDocumentor\Reflection\Fqsen;
use PHPUnit\Framework\TestCase;

final class CallableTest extends TestCase
{
public function testCreate(): void
{
$identifier = 'callable';
$parameters = [
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 CallableParameter(
new Object_(new Fqsen('\\phpDocumentor\\C')),
null,
false,
false,
false
),
];

$returnType = new Object_(new Fqsen('\\phpDocumentor\\Foo'));

$type = new Callable_($identifier, $parameters, $returnType);

$this->assertSame($identifier, $type->getIdentifier());
$this->assertSame($parameters, $type->getParameters());
$this->assertSame($returnType, $type->getReturnType());
}

/**
* @dataProvider provideToStringData
*/
public function testToString(string $expectedResult, Callable_ $type): void
{
$this->assertSame($expectedResult, (string) $type);
}

/**
* @return array<string, array{string, Callable_}>
*/
public static function provideToStringData(): array
{
return [
'basic' => [
'callable',
new Callable_(),
],
'closure' => [
'\Closure',
new Callable_('\Closure'),
],
];
}
}