Skip to content

Commit 3497c5f

Browse files
committed
Be strict about deprecations
The original implementation was causing issues with deprecations triggered in the wrong conditions. By introducing controlled deprecations using doctrine/deprecation we are able to handle this more properly. The tests are updated to test the correct behavior, a new deprecation issue is created to track the deprecations.
1 parent ac4f96a commit 3497c5f

File tree

4 files changed

+74
-9
lines changed

4 files changed

+74
-9
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"require": {
1313
"php": "^7.4 || ^8.0",
1414
"phpdocumentor/reflection-common": "^2.0",
15-
"phpstan/phpdoc-parser": "^1.13"
15+
"phpstan/phpdoc-parser": "^1.13",
16+
"doctrine/deprecations": "^1.0"
1617
},
1718
"require-dev": {
1819
"ext-tokenizer": "*",

composer.lock

Lines changed: 44 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/TypeResolver.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace phpDocumentor\Reflection;
1515

16+
use Doctrine\Deprecations\Deprecation;
1617
use InvalidArgumentException;
1718
use phpDocumentor\Reflection\PseudoTypes\ArrayShape;
1819
use phpDocumentor\Reflection\PseudoTypes\ArrayShapeItem;
@@ -579,11 +580,15 @@ private function parse(TokenIterator $tokenIterator): TypeNode
579580
*/
580581
private function tryParseRemainingCompoundTypes(TokenIterator $tokenIterator, Context $context, Type $type): Type
581582
{
582-
trigger_error(
583-
'Legacy nullable type detected, please update your code as
584-
you are using nullable types in a docblock. support will be removed in v2.0.0',
585-
E_USER_DEPRECATED
586-
);
583+
if ($tokenIterator->isCurrentTokenType(Lexer::TOKEN_UNION) || $tokenIterator->isCurrentTokenType(Lexer::TOKEN_INTERSECTION)) {
584+
Deprecation::trigger(
585+
'phpdocumentor/type-resolver',
586+
'https://github.com/phpDocumentor/TypeResolver/issues/184',
587+
'Legacy nullable type detected, please update your code as
588+
you are using nullable types in a docblock. support will be removed in v2.0.0',
589+
);
590+
}
591+
587592
$continue = true;
588593
while ($continue) {
589594
$continue = false;

tests/unit/TypeResolverTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace phpDocumentor\Reflection;
1515

16+
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
1617
use InvalidArgumentException;
1718
use phpDocumentor\Reflection\PseudoTypes\CallableString;
1819
use phpDocumentor\Reflection\PseudoTypes\ConstExpression;
@@ -73,6 +74,8 @@
7374
*/
7475
class TypeResolverTest extends TestCase
7576
{
77+
use VerifyDeprecations;
78+
7679
/**
7780
* @uses \phpDocumentor\Reflection\Types\Context
7881
* @uses \phpDocumentor\Reflection\Types\Array_
@@ -855,8 +858,14 @@ public function testArrayKeyValueSpecification(): void
855858
* @dataProvider illegalLegacyFormatProvider
856859
* @testdox create type from $type
857860
*/
858-
public function testTypeBuilding(string $type, Type $expected): void
861+
public function testTypeBuilding(string $type, Type $expected, bool $deprecation = false): void
859862
{
863+
if ($deprecation) {
864+
$this->expectDeprecationWithIdentifier('https://github.com/phpDocumentor/TypeResolver/issues/184');
865+
} else {
866+
$this->expectNoDeprecationWithIdentifier('https://github.com/phpDocumentor/TypeResolver/issues/184');
867+
}
868+
860869
$fixture = new TypeResolver();
861870
$actual = $fixture->resolve($type, new Context('phpDocumentor'));
862871

@@ -1090,16 +1099,19 @@ public function illegalLegacyFormatProvider(): array
10901099
{
10911100
return [
10921101
[
1093-
'?string|bool',
1102+
'?string |bool',
10941103
new Compound([new Nullable(new String_()), new Boolean()]),
1104+
true,
10951105
],
10961106
[
10971107
'?string|?bool',
10981108
new Compound([new Nullable(new String_()), new Nullable(new Boolean())]),
1109+
true,
10991110
],
11001111
[
11011112
'?string|?bool|null',
11021113
new Compound([new Nullable(new String_()), new Nullable(new Boolean()), new Null_()]),
1114+
true,
11031115
],
11041116
[
11051117
'?string|bool|Foo',
@@ -1108,10 +1120,12 @@ public function illegalLegacyFormatProvider(): array
11081120
new Boolean(),
11091121
new Object_(new Fqsen('\\phpDocumentor\\Foo')),
11101122
]),
1123+
true,
11111124
],
11121125
[
11131126
'?string&bool',
11141127
new Intersection([new Nullable(new String_()), new Boolean()]),
1128+
true,
11151129
],
11161130
[
11171131
'?string&bool|Foo',
@@ -1121,6 +1135,7 @@ public function illegalLegacyFormatProvider(): array
11211135
new Compound([new Boolean(), new Object_(new Fqsen('\\phpDocumentor\\Foo'))]),
11221136
]
11231137
),
1138+
true,
11241139
],
11251140
[
11261141
'?string&?bool|null',
@@ -1130,6 +1145,7 @@ public function illegalLegacyFormatProvider(): array
11301145
new Null_(),
11311146
]
11321147
),
1148+
true,
11331149
],
11341150
];
11351151
}

0 commit comments

Comments
 (0)