From bb11e62aa2285dfb71cb08d918122b38754363e0 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 18 Aug 2021 11:24:26 +0200 Subject: [PATCH] [ErrorHandler] Turn return-type annotations into deprecations by default, add mode to turn them into native types --- .github/patch-types.php | 1 - .github/workflows/unit-tests.yml | 2 ++ src/Symfony/Component/ErrorHandler/CHANGELOG.md | 6 ++++++ .../Component/ErrorHandler/DebugClassLoader.php | 11 ++++++----- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/.github/patch-types.php b/.github/patch-types.php index fa806ac42ae14..733790ce6c6a3 100644 --- a/.github/patch-types.php +++ b/.github/patch-types.php @@ -15,7 +15,6 @@ switch (true) { case false !== strpos($file = realpath($file), '/vendor/'): case false !== strpos($file, '/src/Symfony/Bridge/PhpUnit/'): - case false !== strpos($file, '/Attribute/'): case false !== strpos($file, '/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Validation/Article.php'): case false !== strpos($file, '/src/Symfony/Component/Cache/Tests/Fixtures/DriverWrapper.php'): case false !== strpos($file, '/src/Symfony/Component/Config/Tests/Fixtures/BadFileName.php'): diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index b2fabdd9c8dbb..030e55abbef96 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -139,9 +139,11 @@ jobs: if: "${{ matrix.php == '8.0' && ! matrix.mode }}" run: | sed -i 's/"\*\*\/Tests\/"//' composer.json + git add . composer install -q --optimize-autoloader SYMFONY_PATCH_TYPE_DECLARATIONS='force=1&php=7.2' php .github/patch-types.php SYMFONY_PATCH_TYPE_DECLARATIONS='force=1&php=7.2' php .github/patch-types.php # ensure the script is idempotent + git diff --exit-code echo PHPUNIT="$PHPUNIT,legacy" >> $GITHUB_ENV - name: Run tests diff --git a/src/Symfony/Component/ErrorHandler/CHANGELOG.md b/src/Symfony/Component/ErrorHandler/CHANGELOG.md index 870933ab8db8f..2976566a1f025 100644 --- a/src/Symfony/Component/ErrorHandler/CHANGELOG.md +++ b/src/Symfony/Component/ErrorHandler/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +5.4 +--- + + * Make `DebugClassLoader` trigger deprecation notices on missing return types + * Add `SYMFONY_PATCH_TYPE_DECLARATIONS='force=2'` mode to `DebugClassLoader` to turn annotations into native return types + 5.2.0 ----- diff --git a/src/Symfony/Component/ErrorHandler/DebugClassLoader.php b/src/Symfony/Component/ErrorHandler/DebugClassLoader.php index 159e0daf5ed78..2f2978cb29cf3 100644 --- a/src/Symfony/Component/ErrorHandler/DebugClassLoader.php +++ b/src/Symfony/Component/ErrorHandler/DebugClassLoader.php @@ -34,9 +34,8 @@ * which is a url-encoded array with the follow parameters: * - "force": any value enables deprecation notices - can be any of: * - "phpdoc" to patch only docblock annotations - * - "object" to turn union types to the "object" type when possible (not recommended) - * - "1" to add all possible return types including magic methods - * - "0" to add possible return types excluding magic methods + * - "2" to add all possible return types + * - "1" to add return types but only to tests/final/internal/private methods * - "php": the target version of PHP - e.g. "7.1" doesn't generate "object" types * - "deprecations": "1" to trigger a deprecation notice when a child class misses a * return type while the parent declares an "@return" annotation @@ -73,6 +72,7 @@ class DebugClassLoader 'mixed' => 'mixed', 'static' => 'static', '$this' => 'static', + 'list' => 'array', ]; private const BUILTIN_RETURN_TYPES = [ @@ -127,7 +127,7 @@ public function __construct(callable $classLoader) $this->patchTypes += [ 'force' => null, 'php' => \PHP_MAJOR_VERSION.'.'.\PHP_MINOR_VERSION, - 'deprecations' => false, + 'deprecations' => \PHP_VERSION_ID >= 70400, ]; if ('phpdoc' === $this->patchTypes['force']) { @@ -534,7 +534,8 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array $this->patchTypes['force'] = $forcePatchTypes ?: 'docblock'; } - $canAddReturnType = false !== stripos($method->getFileName(), \DIRECTORY_SEPARATOR.'Tests'.\DIRECTORY_SEPARATOR) + $canAddReturnType = 2 === (int) $forcePatchTypes + || false !== stripos($method->getFileName(), \DIRECTORY_SEPARATOR.'Tests'.\DIRECTORY_SEPARATOR) || $refl->isFinal() || $method->isFinal() || $method->isPrivate()