minor #33711 [ErrorHandler] fix return-type patching logic (nicolas-grekas)
This PR was merged into the 4.4 branch.
Discussion
----------
[ErrorHandler] fix return-type patching logic
| Q | A
| ------------- | ---
| Branch? | 4.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | -
| License | MIT
| Doc PR | -
My bad.
Commits
-------
302a921976
[ErrorHandler] fix return-type patching logic
This commit is contained in:
commit
b56a4b4f3a
@ -22,7 +22,6 @@ env:
|
|||||||
- MESSENGER_AMQP_DSN=amqp://localhost/%2f/messages
|
- MESSENGER_AMQP_DSN=amqp://localhost/%2f/messages
|
||||||
- MESSENGER_REDIS_DSN=redis://127.0.0.1:7001/messages
|
- MESSENGER_REDIS_DSN=redis://127.0.0.1:7001/messages
|
||||||
- SYMFONY_PHPUNIT_DISABLE_RESULT_CACHE=1
|
- SYMFONY_PHPUNIT_DISABLE_RESULT_CACHE=1
|
||||||
- SYMFONY_PATCH_TYPE_DECLARATIONS=force=0
|
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
@ -299,9 +298,8 @@ install:
|
|||||||
ln -sd $(realpath src/Symfony/Contracts) vendor/symfony/contracts
|
ln -sd $(realpath src/Symfony/Contracts) vendor/symfony/contracts
|
||||||
sed -i 's/"\*\*\/Tests\/"//' composer.json
|
sed -i 's/"\*\*\/Tests\/"//' composer.json
|
||||||
composer install --optimize-autoloader
|
composer install --optimize-autoloader
|
||||||
export SYMFONY_PATCH_TYPE_DECLARATIONS=force=object
|
SYMFONY_PATCH_TYPE_DECLARATIONS=force=object php .github/patch-types.php
|
||||||
php .github/patch-types.php
|
SYMFONY_PATCH_TYPE_DECLARATIONS=force=object php .github/patch-types.php # ensure the script is idempotent
|
||||||
php .github/patch-types.php # ensure the script is idempotent
|
|
||||||
PHPUNIT_X="$PHPUNIT_X,legacy"
|
PHPUNIT_X="$PHPUNIT_X,legacy"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
3
phpunit
3
phpunit
@ -14,5 +14,8 @@ if (!getenv('SYMFONY_PHPUNIT_VERSION')) {
|
|||||||
putenv('SYMFONY_PHPUNIT_VERSION=6.5');
|
putenv('SYMFONY_PHPUNIT_VERSION=6.5');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!getenv('SYMFONY_PATCH_TYPE_DECLARATIONS')) {
|
||||||
|
putenv('SYMFONY_PATCH_TYPE_DECLARATIONS=deprecations=1');
|
||||||
|
}
|
||||||
putenv('SYMFONY_PHPUNIT_DIR='.__DIR__.'/.phpunit');
|
putenv('SYMFONY_PHPUNIT_DIR='.__DIR__.'/.phpunit');
|
||||||
require __DIR__.'/vendor/symfony/phpunit-bridge/bin/simple-phpunit';
|
require __DIR__.'/vendor/symfony/phpunit-bridge/bin/simple-phpunit';
|
||||||
|
@ -27,11 +27,14 @@ use ProxyManager\Proxy\ProxyInterface;
|
|||||||
* It can also patch classes to turn docblocks into actual return types.
|
* It can also patch classes to turn docblocks into actual return types.
|
||||||
* This behavior is controlled by the SYMFONY_PATCH_TYPE_DECLARATIONS env var,
|
* This behavior is controlled by the SYMFONY_PATCH_TYPE_DECLARATIONS env var,
|
||||||
* which is a url-encoded array with the follow parameters:
|
* which is a url-encoded array with the follow parameters:
|
||||||
* - force: any value enables deprecation notices - can be any of:
|
* - "force": any value enables deprecation notices - can be any of:
|
||||||
* - "docblock" to patch only docblock annotations
|
* - "docblock" to patch only docblock annotations
|
||||||
* - "object" to turn union types to the "object" type when possible (not recommended)
|
* - "object" to turn union types to the "object" type when possible (not recommended)
|
||||||
* - "1"/"0" to enable/disable patching of return types
|
* - "1" to add all possible return types including magic methods
|
||||||
* - php: the target version of PHP - e.g. "7.1" doesn't generate "object" types
|
* - "0" to add possible return types excluding magic 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
|
||||||
*
|
*
|
||||||
* Note that patching doesn't care about any coding style so you'd better to run
|
* Note that patching doesn't care about any coding style so you'd better to run
|
||||||
* php-cs-fixer after, with rules "phpdoc_trim_consecutive_blank_line_separation"
|
* php-cs-fixer after, with rules "phpdoc_trim_consecutive_blank_line_separation"
|
||||||
@ -177,6 +180,7 @@ class DebugClassLoader
|
|||||||
$this->patchTypes += [
|
$this->patchTypes += [
|
||||||
'force' => null,
|
'force' => null,
|
||||||
'php' => null,
|
'php' => null,
|
||||||
|
'deprecations' => false,
|
||||||
];
|
];
|
||||||
|
|
||||||
if (!isset(self::$caseCheck)) {
|
if (!isset(self::$caseCheck)) {
|
||||||
@ -571,7 +575,7 @@ class DebugClassLoader
|
|||||||
|
|
||||||
$forcePatchTypes = $this->patchTypes['force'];
|
$forcePatchTypes = $this->patchTypes['force'];
|
||||||
|
|
||||||
if ($canAddReturnType = $forcePatchTypes && false === strpos($method->getFileName(), \DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR)) {
|
if ($canAddReturnType = null !== $forcePatchTypes && false === strpos($method->getFileName(), \DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR)) {
|
||||||
if ('void' !== (self::MAGIC_METHODS[$method->name] ?? 'void')) {
|
if ('void' !== (self::MAGIC_METHODS[$method->name] ?? 'void')) {
|
||||||
$this->patchTypes['force'] = $forcePatchTypes ?: 'docblock';
|
$this->patchTypes['force'] = $forcePatchTypes ?: 'docblock';
|
||||||
}
|
}
|
||||||
@ -600,7 +604,7 @@ class DebugClassLoader
|
|||||||
if (strncmp($ns, $declaringClass, $len)) {
|
if (strncmp($ns, $declaringClass, $len)) {
|
||||||
if ($canAddReturnType && 'docblock' === $this->patchTypes['force'] && false === strpos($method->getFileName(), \DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR)) {
|
if ($canAddReturnType && 'docblock' === $this->patchTypes['force'] && false === strpos($method->getFileName(), \DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR)) {
|
||||||
$this->patchMethod($method, $returnType, $declaringFile, $normalizedType);
|
$this->patchMethod($method, $returnType, $declaringFile, $normalizedType);
|
||||||
} elseif ('' !== $declaringClass && null !== $this->patchTypes['force']) {
|
} elseif ('' !== $declaringClass && $this->patchTypes['deprecations']) {
|
||||||
$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);
|
$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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ class DebugClassLoaderTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->patchTypes = getenv('SYMFONY_PATCH_TYPE_DECLARATIONS');
|
$this->patchTypes = getenv('SYMFONY_PATCH_TYPE_DECLARATIONS');
|
||||||
$this->errorReporting = error_reporting(E_ALL);
|
$this->errorReporting = error_reporting(E_ALL);
|
||||||
putenv('SYMFONY_PATCH_TYPE_DECLARATIONS=force=0');
|
putenv('SYMFONY_PATCH_TYPE_DECLARATIONS=deprecations=1');
|
||||||
$this->loader = [new DebugClassLoader([new ClassLoader(), 'loadClass']), 'loadClass'];
|
$this->loader = [new DebugClassLoader([new ClassLoader(), 'loadClass']), 'loadClass'];
|
||||||
spl_autoload_register($this->loader, true, true);
|
spl_autoload_register($this->loader, true, true);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user