Skip to content

Commit b4d53cb

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 b4d53cb

File tree

4 files changed

+77
-12
lines changed

4 files changed

+77
-12
lines changed

Diff for: composer.json

+2-1
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": "*",

Diff for: composer.lock

+44-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/TypeResolver.php

+13-8
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;
@@ -103,11 +104,8 @@
103104
use function sprintf;
104105
use function strpos;
105106
use function strtolower;
106-
use function trigger_error;
107107
use function trim;
108108

109-
use const E_USER_DEPRECATED;
110-
111109
final class TypeResolver
112110
{
113111
/** @var string Definition of the NAMESPACE operator in PHP */
@@ -579,11 +577,18 @@ private function parse(TokenIterator $tokenIterator): TypeNode
579577
*/
580578
private function tryParseRemainingCompoundTypes(TokenIterator $tokenIterator, Context $context, Type $type): Type
581579
{
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-
);
580+
if (
581+
$tokenIterator->isCurrentTokenType(Lexer::TOKEN_UNION) ||
582+
$tokenIterator->isCurrentTokenType(Lexer::TOKEN_INTERSECTION)
583+
) {
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;

Diff for: tests/unit/TypeResolverTest.php

+18-2
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)