Skip to content

Commit 4db4c63

Browse files
committed
Support for self in phpdoc
This is typically useful in traits that would accept a parameter of their own type.
1 parent 40a1d92 commit 4db4c63

File tree

6 files changed

+57
-0
lines changed

6 files changed

+57
-0
lines changed

src/Factory/AbstractFieldsConfigurationFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ final protected function getTypeFromPhpDeclaration(ReflectionMethod $method, ?st
160160
*/
161161
private function adjustNamespace(ReflectionMethod $method, string $type): string
162162
{
163+
if ($type === 'self') {
164+
$type = $method->getDeclaringClass()->getName();
165+
}
166+
163167
$namespace = $method->getDeclaringClass()->getNamespaceName();
164168
if ($namespace) {
165169
$namespace = $namespace . '\\';
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GraphQLTests\Doctrine\Blog\Model\Special;
6+
7+
use Doctrine\ORM\Mapping as ORM;
8+
use GraphQLTests\Doctrine\Blog\Model\AbstractModel;
9+
10+
/**
11+
* @ORM\Entity
12+
*/
13+
final class SelfSupport extends AbstractModel
14+
{
15+
private $sibling;
16+
17+
/**
18+
* @return null|self
19+
*/
20+
public function getSibling(): ?self
21+
{
22+
return $this->sibling;
23+
}
24+
25+
/**
26+
* @param self $sibling
27+
*/
28+
public function setSibling(self $sibling): void
29+
{
30+
$this->sibling = $sibling;
31+
}
32+
}

tests/InputTypesTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public function testDefaultValuesPartialInput(): void
3434
$this->assertType('tests/data/DefaultValuePartialInput.graphqls', $actual);
3535
}
3636

37+
public function testSelfSupportInput(): void
38+
{
39+
$actual = $this->types->getInput(Blog\Model\Special\SelfSupport::class);
40+
$this->assertType('tests/data/SelfSupportInput.graphqls', $actual);
41+
}
42+
3743
public function testInputWithoutTypeMustThrow(): void
3844
{
3945
$this->expectExceptionMessage('Could not find type for parameter `$bar` for method `GraphQLTests\Doctrine\Blog\Model\Special\NoTypeInput::setFoo()`. Either type hint the parameter, or specify the type with `@API\Input` annotation.');

tests/OutputTypesTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public function testDefaultValuesOutput(): void
4747
$this->assertType('tests/data/DefaultValue.graphqls', $actual);
4848
}
4949

50+
public function testSelfSupportOutput(): void
51+
{
52+
$actual = $this->types->getOutput(Blog\Model\Special\SelfSupport::class);
53+
$this->assertType('tests/data/SelfSupport.graphqls', $actual);
54+
}
55+
5056
public function testFieldWithoutTypeMustThrow(): void
5157
{
5258
$this->expectExceptionMessage('Could not find type for method `GraphQLTests\Doctrine\Blog\Model\Special\NoType::getWithoutTypeHint()`. Either type hint the return value, or specify the type with `@API\Field` annotation.');

tests/data/SelfSupport.graphqls

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type SelfSupport {
2+
sibling: SelfSupport
3+
id: ID!
4+
creationDate: DateTime!
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
input SelfSupportInput {
2+
sibling: SelfSupportID!
3+
creationDate: DateTime!
4+
}

0 commit comments

Comments
 (0)