Skip to content

Commit b0818b2

Browse files
JasperS2307Jasper Smulders
andauthored
Add support for mixed and ReflectionUnionType (#73)
Co-authored-by: Jasper Smulders <jsmulders@hostnet.nl>
1 parent 3e82036 commit b0818b2

12 files changed

Lines changed: 66 additions & 30 deletions

.github/workflows/main.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111

1212
strategy:
1313
matrix:
14-
php-versions: ['7.3', '7.4', '8.0']
14+
php-versions: ['8.0', '8.1']
1515
name: PHP ${{ matrix.php-versions }}
1616
steps:
1717
- uses: actions/checkout@v2

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Installer for all the Hostnet Doctrine entity libraries",
55
"license": "MIT",
66
"require": {
7-
"php": ">=7.3",
7+
"php": "^8.0",
88
"composer-plugin-api": "^2.0.0",
99
"doctrine/annotations": "^1.13.2",
1010
"phpdocumentor/type-resolver": "^1.4.0",

src/ReflectionMethod.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,10 @@ public function getReturnType(): ?ReflectionType
6767
}
6868

6969
if ($type = $this->method->getReturnType()) {
70-
// Self is not valid when used in different places.
71-
if ('self' === $type->getName()) {
72-
return null;
73-
}
70+
$reflection_type = new ReflectionType($type);
7471

75-
return new ReflectionType($type);
72+
// Self is not valid when used in different places.
73+
return false === \strpos($reflection_type->getName(), 'self') ? $reflection_type : null;
7674
}
7775

7876
return null;

src/ReflectionParameter.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,7 @@ public function isPassedByReference(): bool
8888
return $this->parameter->isPassedByReference();
8989
}
9090

91-
/**
92-
* @return mixed
93-
*
94-
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint
95-
*/
96-
public function getDefaultValue()
91+
public function getDefaultValue(): mixed
9792
{
9893
return $this->parameter->getDefaultValue();
9994
}

src/ReflectionType.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,30 @@ public function __construct(\ReflectionType $type)
2020

2121
public function getName(): string
2222
{
23-
$name = $this->type->getName();
23+
if ($this->type instanceof \ReflectionUnionType) {
24+
$names = [];
25+
foreach ($this->type->getTypes() as $type) {
26+
$names[] = $this->resolveName($type->getName());
27+
}
2428

25-
// Some types can not be qualified
26-
if (in_array($name, self::NON_QUALIFIED_TYPES, true)) {
27-
return $name;
29+
return implode('|', $names);
2830
}
2931

30-
return '\\' . $name;
32+
return $this->resolveName($this->type->getName());
3133
}
3234

3335
public function allowsNull(): bool
3436
{
3537
return $this->type->allowsNull();
3638
}
39+
40+
private function resolveName(string $name): string
41+
{
42+
// Some types can not be qualified
43+
if (in_array($name, self::NON_QUALIFIED_TYPES, true)) {
44+
return $name;
45+
}
46+
47+
return '\\' . $name;
48+
}
3749
}

src/ReflectionTypeInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface ReflectionTypeInterface
1919
'float',
2020
'int',
2121
'string',
22+
'mixed',
2223
];
2324

2425
public function getName(): string;

src/Resources/templates/interface.php.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface {{ class_name }}Interface{% if parent %} extends {{ parent }}Interface
1414
{{ method.docComment | raw }}
1515
public function {{ method.name }}({% include 'parameters.php.twig' with { parameters: method.parameters } %})
1616
{%- if method.returnType -%}
17-
: {% if method.returnType.allowsNull %}?{% endif %}{{ method.returnType.name }}
17+
: {% if method.returnType.allowsNull and method.returnType.name != "mixed" %}?{% endif %}{{ method.returnType.name }}
1818
{%- endif -%}
1919
;
2020
{% endif %}

src/Resources/templates/parameters.php.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{% if parameter.allowsNull() -%}
44
{% set is_obligatory = not parameter.isOptional() -%}
55
{% set has_actual_default_value = parameter.isDefaultValueAvailable() and parameter.defaultValue is not null -%}
6-
{% if is_obligatory or has_actual_default_value -%}
6+
{% if (is_obligatory or has_actual_default_value) and parameter.type.name != "mixed" -%}
77
?
88
{%- endif -%}
99
{% endif -%}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Hostnet\Component\EntityPlugin\Fixtures;
3+
4+
class ReflectionReturnSelf
5+
{
6+
public function docBlock(): self
7+
{
8+
return new self();
9+
}
10+
}

test/ReflectionMethodTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Hostnet\Component\EntityPlugin\Fixtures\Reflection;
1010
use Hostnet\Component\EntityPlugin\Fixtures\ReflectionReturn;
11+
use Hostnet\Component\EntityPlugin\Fixtures\ReflectionReturnSelf;
1112
use PHPUnit\Framework\TestCase;
1213

1314
/**
@@ -62,9 +63,15 @@ public function testGetReturnType(): void
6263
{
6364
$this->assertEquals(null, $this->method->getReturnType());
6465

65-
if (PHP_MAJOR_VERSION >= 7) {
66-
$php7_method = new ReflectionMethod(new \ReflectionMethod(ReflectionReturn::class, 'docBlock'));
67-
$this->assertEquals('array', $php7_method->getReturnType()->getName());
68-
}
66+
$php7_method = new ReflectionMethod(new \ReflectionMethod(ReflectionReturn::class, 'docBlock'));
67+
$this->assertEquals('array', $php7_method->getReturnType()->getName());
68+
}
69+
70+
public function testGetReturnTypeSelf(): void
71+
{
72+
$this->assertEquals(null, $this->method->getReturnType());
73+
74+
$php7_method = new ReflectionMethod(new \ReflectionMethod(ReflectionReturnSelf::class, 'docBlock'));
75+
$this->assertEquals(null, $php7_method->getReturnType());
6976
}
7077
}

0 commit comments

Comments
 (0)