8000 [ErrorHandler] fix return-type patching logic · symfony/symfony@302a921 · GitHub
[go: up one dir, main page]

Skip to content

Commit 302a921

Browse files
[ErrorHandler] fix return-type patching logic
1 parent 745248f commit 302a921

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ env:
2222
- MESSENGER_AMQP_DSN=amqp://localhost/%2f/messages
2323
- MESSENGER_REDIS_DSN=redis://127.0.0.1:7001/messages
2424
- SYMFONY_PHPUNIT_DISABLE_RESULT_CACHE=1
25-
- SYMFONY_PATCH_TYPE_DECLARATIONS=force=0
2625

2726
matrix:
2827
include:
@@ -299,9 +298,8 @@ install:
299298
ln -sd $(realpath src/Symfony/Contracts) vendor/symfony/contracts
300299
sed -i 's/"\*\*\/Tests\/"//' composer.json
301300
composer install --optimize-autoloader
302-
export SYMFONY_PATCH_TYPE_DECLARATIONS=force=object
303-
php .github/patch-types.php
304-
php .github/patch-types.php # ensure the script is idempotent
301+
SYMFONY_PATCH_TYPE_DECLARATIONS=force=object php .github/patch-types.php
302+
SYMFONY_PATCH_TYPE_DECLARATIONS=force=object php .github/patch-types.php # ensure the script is idempotent
305303
PHPUNIT_X="$PHPUNIT_X,legacy"
306304
fi
307305

phpunit

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ if (!getenv('SYMFONY_PHPUNIT_VERSION')) {
1414
putenv('SYMFONY_PHPUNIT_VERSION=6.5');
1515
}
1616
}
17+
if (!getenv('SYMFONY_PATCH_TYPE_DECLARATIONS')) {
18+
putenv('SYMFONY_PATCH_TYPE_DECLARATIONS=deprecations=1');
19+
}
1720
putenv('SYMFONY_PHPUNIT_DIR='.__DIR__.'/.phpunit');
1821
require __DIR__.'/vendor/symfony/phpunit-bridge/bin/simple-phpunit';

src/Symfony/Component/ErrorHandler/DebugClassLoader.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@
2727
* It can also patch classes to turn docblocks into actual return types.
2828
* This behavior is controlled by the SYMFONY_PATCH_TYPE_DECLARATIONS env var,
2929
* which is a url-encoded array with the follow parameters:
30-
* - force: any value enables deprecation notices - can be any of:
30+
* - "force": any value enables deprecation notices - can be any of:
3131
* - "docblock" to patch only docblock annotations
3232
* - "object" to turn union types to the "object" type when possible (not recommended)
33-
* - "1"/"0" to enable/disable patching of return types
34-
* - php: the target version of PHP - e.g. "7.1" doesn't generate "object" types
33+
* - "1" to add all possible return types including magic methods
34+
* - "0" to add possible return types excluding magic methods
35+
* - "php": the target version of PHP - e.g. "7.1" doesn't generate "object" types
36+
* - "deprecations": "1" to trigger a deprecation notice when a child class misses a
37+
* return type while the parent declares an "@return" annotation
3538
*
3639
* Note that patching doesn't care about any coding style so you'd better to run
3740
* php-cs-fixer after, with rules "phpdoc_trim_consecutive_blank_line_separation"
@@ -177,6 +180,7 @@ public function __construct(callable $classLoader)
177180
$this->patchTypes += [
178181
'force' => null,
179182
'php' => null,
183+
'deprecations' => false,
180184
];
181185

182186
if (!isset(self::$caseCheck)) {
@@ -571,7 +575,7 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array
571575

572576
$forcePatchTypes = $this->patchTypes['force'];
573577

574-
if ($canAddReturnType = $forcePatchTypes && false === strpos($method->getFileName(), \DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR)) {
578+
if ($canAddReturnType = null !== $forcePatchTypes && false === strpos($method->getFileName(), \DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR)) {
575579
if ('void' !== (self::MAGIC_METHODS[$method->name] ?? 'void')) {
576580
$this->patchTypes['force'] = $forcePatchTypes ?: 'docblock';
577581
}
@@ -600,7 +604,7 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array
600604
if (strncmp($ns, $declaringClass, $len)) {
601605
if ($canAddReturnType && 'docblock' === $this->patchTypes['force'] && false === strpos($method->getFileName(), \DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR)) {
602606
$this->patchMethod($method, $returnType, $declaringFile, $normalizedType);
603-
} elseif ('' !== $declaringClass && null !== $this->patchTypes['force']) {
607+
} elseif ('' !== $declaringClass && $this->patchTypes['deprecations']) {
604608
$deprecations[] = sprintf('Method "%s::%s()" will return "%s" as of its next major version. Doing the same in child class "%s" will be required when upgrading.', $declaringClass, $method->name, $normalizedType, $className);
605609
}
606610
}

src/Symfony/Component/ErrorHandler/Tests/DebugClassLoaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected function setUp(): void
2424
{
2525
$this->patchTypes = getenv('SYMFONY_PATCH_TYPE_DECLARATIONS');
2626
$this->errorReporting = error_reporting(E_ALL);
27-
putenv('SYMFONY_PATCH_TYPE_DECLARATIONS=force=0');
27+
putenv('SYMFONY_PATCH_TYPE_DECLARATIONS=deprecations=1');
2828
$this->loader = [new DebugClassLoader([new ClassLoader(), 'loadClass']), 'loadClass'];
2929
spl_autoload_register($this->loader, true, true);
3030
}

0 commit comments

Comments
 (0)
0