Skip to content

[ErrorHandler] Turn return-type annotations into deprecations by default + add mode to turn them into native types #42623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/patch-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/ErrorHandler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
-----

Expand Down
11 changes: 6 additions & 5 deletions src/Symfony/Component/ErrorHandler/DebugClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,6 +72,7 @@ class DebugClassLoader
'mixed' => 'mixed',
'static' => 'static',
'$this' => 'static',
'list' => 'array',
];

private const BUILTIN_RETURN_TYPES = [
Expand Down Expand Up @@ -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']) {
Expand Down Expand Up @@ -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()
Expand Down