Skip to content
Permalink
Browse files

[ErrorHandler] fix return-type patching logic

  • Loading branch information...
nicolas-grekas committed Sep 25, 2019
1 parent 745248f commit 302a9219769b824165fe62c4dc121759b52bed8d
@@ -22,7 +22,6 @@ env:
- MESSENGER_AMQP_DSN=amqp://localhost/%2f/messages
- MESSENGER_REDIS_DSN=redis://127.0.0.1:7001/messages
- SYMFONY_PHPUNIT_DISABLE_RESULT_CACHE=1
- SYMFONY_PATCH_TYPE_DECLARATIONS=force=0

matrix:
include:
@@ -299,9 +298,8 @@ install:
ln -sd $(realpath src/Symfony/Contracts) vendor/symfony/contracts
sed -i 's/"\*\*\/Tests\/"//' composer.json
composer install --optimize-autoloader
export SYMFONY_PATCH_TYPE_DECLARATIONS=force=object
php .github/patch-types.php
php .github/patch-types.php # ensure the script is idempotent
SYMFONY_PATCH_TYPE_DECLARATIONS=force=object php .github/patch-types.php
SYMFONY_PATCH_TYPE_DECLARATIONS=force=object php .github/patch-types.php # ensure the script is idempotent
PHPUNIT_X="$PHPUNIT_X,legacy"
fi
@@ -14,5 +14,8 @@ if (!getenv('SYMFONY_PHPUNIT_VERSION')) {
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');
require __DIR__.'/vendor/symfony/phpunit-bridge/bin/simple-phpunit';
@@ -27,11 +27,14 @@
* It can also patch classes to turn docblocks into actual return types.
* This behavior is controlled by the SYMFONY_PATCH_TYPE_DECLARATIONS env var,
* 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
* - "object" to turn union types to the "object" type when possible (not recommended)
* - "1"/"0" to enable/disable patching of return types
* - php: the target version of PHP - e.g. "7.1" doesn't generate "object" types
* - "1" to add all possible return types including magic methods
* - "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
* php-cs-fixer after, with rules "phpdoc_trim_consecutive_blank_line_separation"
@@ -177,6 +180,7 @@ public function __construct(callable $classLoader)
$this->patchTypes += [
'force' => null,
'php' => null,
'deprecations' => false,
];
if (!isset(self::$caseCheck)) {
@@ -571,7 +575,7 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array
$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')) {
$this->patchTypes['force'] = $forcePatchTypes ?: 'docblock';
}
@@ -600,7 +604,7 @@ public function checkAnnotations(\ReflectionClass $refl, string $class): array
if (strncmp($ns, $declaringClass, $len)) {
if ($canAddReturnType && 'docblock' === $this->patchTypes['force'] && false === strpos($method->getFileName(), \DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR)) {
$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);
}
}
@@ -24,7 +24,7 @@ protected function setUp(): void
{
$this->patchTypes = getenv('SYMFONY_PATCH_TYPE_DECLARATIONS');
$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'];
spl_autoload_register($this->loader, true, true);
}

0 comments on commit 302a921

Please sign in to comment.
You can’t perform that action at this time.