8000 feature #42623 [ErrorHandler] Turn return-type annotations into depre… · symfony/symfony@0cb07c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0cb07c7

Browse files
feature #42623 [ErrorHandler] Turn return-type annotations into deprecations by default + add mode to turn them into native types (nicolas-grekas)
This PR was merged into the 5.4 branch. Discussion ---------- [ErrorHandler] Turn return-type annotations into deprecations by default + add mode to turn them into native types | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Leverages #42149 We need extensive doc on the topic for sure, a whole new chapter. DebugClassLoader allows patching an app or a lib by going through these steps: - require `symfony/error-handler` if not already there - run `composer install -q --optimize-autoloader` - copy/paste the below script to the root of the app/lib, in `patch-types.php` - run the script with `SYMFONY_PATCH_TYPE_DECLARATIONS='force=phpdoc' php patch-types.php` `SYMFONY_PATCH_TYPE_DECLARATIONS` can be set to: - `'force=phpdoc'` to copy ``@return`` annotations from parent classes, to express that the next major version of that lib is going to add a native return types; - `'force=1'` to turn ``@return`` annotations into native return types, but only on tests/private/final/internal methods; - `'force=2'` to turn ``@return`` annotations into native return types, for all possible methods. ```php <?php if (false === getenv('SYMFONY_PATCH_TYPE_DECLARATIONS')) { echo "Please define the SYMFONY_PATCH_TYPE_DECLARATIONS env var when running this script.\n"; exit(1); } $loader = require __DIR__.'/vendor/autoload.php'; Symfony\Component\ErrorHandler\DebugClassLoader::enable(); foreach ($loader->getClassMap() as $class => $file) { switch (true) { case false !== strpos($file = realpath($file), '/vendor/'): case false !== strpos($file, '/src/path-to-exclude/'): // add as many as required continue; } class_exists($class); } Symfony\Component\ErrorHandler\DebugClassLoader::checkClasses(); ``` Commits ------- bb11e62 [ErrorHandler] Turn return-type annotations into deprecations by default, add mode to turn them into native types
2 parents 81c2007 + bb11e62 commit 0cb07c7

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

.github/patch-types.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
switch (true) {
1616
case false !== strpos($file = realpath($file), '/vendor/'):
1717
case false !== strpos($file, '/src/Symfony/Bridge/PhpUnit/'):
18-
case false !== strpos($file, '/Attribute/'):
1918
case false !== strpos($file, '/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Validation/Article.php'):
2019
case false !== strpos($file, '/src/Symfony/Component/Cache/Tests/Fixtures/DriverWrapper.php'):
2120
case false !== strpos($file, '/src/Symfony/Component/Config/Tests/Fixtures/BadFileName.php'):

.github/workflows/unit-tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,11 @@ jobs:
139139
if: "${{ matrix.php == '8.0' && ! matrix.mode }}"
140140
run: |
141141
sed -i 's/"\*\*\/Tests\/"//' composer.json
142+
git add .
142143
composer install -q --optimize-autoloader
143144
SYMFONY_PATCH_TYPE_DECLARATIONS='force=1&php=7.2' php .github/patch-types.php
144145
SYMFONY_PATCH_TYPE_DECLARATIONS='force=1&php=7.2' php .github/patch-types.php # ensure the script is idempotent
146+
git diff --exit-code
145147
echo PHPUNIT="$PHPUNIT,legacy" >> $GITHUB_ENV
146148
147149
- name: Run tests

src/Symfony/Component/ErrorHandler/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
7+
* Make `DebugClassLoader` trigger deprecation notices on missing return types
8+
* Add `SYMFONY_PATCH_TYPE_DECLARATIONS='force=2'` mode to `DebugClassLoader` to turn annotations into native return types
9+
410
5.2.0
511
-----
612

src/Symfony/Component/ErrorHandler/DebugClassLoader.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434
* which is a url-encoded array with the follow parameters:
3535
* - "force": any value enables deprecation notices - can be any of:
3636
* - "phpdoc" to patch only docblock annotations
37-
* - "object" to turn union types to the "object" type when possible (not recommended)
38-
* - "1" to add all possible return types including magic methods
39-
* - "0" to add possible return types excluding magic methods
37+
* - "2" to add all possible return types
38+
* - "1" to add return types but only to tests/final/internal/private methods
4039
* - "php": the target version of PHP - e.g. "7.1" doesn't generate "object" types
4140
* - "deprecations": "1" to trigger a deprecation notice when a child class misses a
4241
* return type while the parent declares an "@return" annotation
@@ -73,6 +72,7 @@ class DebugClassLoader
7372
'mixed' => 'mixed',
7473
'static' => 'static',
7574
'$this' => 'static',
75+
'list' => 'array',
7676
];
7777

7878
private const BUILTIN_RETURN_TYPES = [
@@ -127,7 +127,7 @@ public function __construct(callable $classLoader)
127127
$this->patchTypes += [
128128
'force' => null,
129129
'php' => \PHP_MAJOR_VERSION.'.'.\PHP_MINOR_VERSION,
130-
'deprecations' => false,
130+
'deprecations' => \PHP_VERSION_ID >= 70400,
131131
];
132132

133133
if ('phpdoc' === $this->patchTypes['force']) {
@@ -534,7 +534,8 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array
534534
$this->patchTypes['force'] = $forcePatchTypes ?: 'docblock';
535535
}
536536

537-
$canAddReturnType = false !== stripos($method->getFileName(), \DIRECTORY_SEPARATOR.'Tests'.\DIRECTORY_SEPARATOR)
537+
$canAddReturnType = 2 === (int) $forcePatchTypes
538+
|| false !== stripos($method->getFileName(), \DIRECTORY_SEPARATOR.'Tests'.\DIRECTORY_SEPARATOR)
538539
|| $refl->isFinal()
539540
|| $method->isFinal()
540541
|| $method->isPrivate()

0 commit comments

Comments
 (0)
0