diff --git a/.travis.yml b/.travis.yml index 19f2186295..e1490ca203 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/phpunit b/phpunit index b885570d65..a0f1af86d7 100755 --- a/phpunit +++ b/phpunit @@ -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'; diff --git a/src/Symfony/Component/ErrorHandler/DebugClassLoader.php b/src/Symfony/Component/ErrorHandler/DebugClassLoader.php index 6d9239c5ac..a09d8d57c6 100644 --- a/src/Symfony/Component/ErrorHandler/DebugClassLoader.php +++ b/src/Symfony/Component/ErrorHandler/DebugClassLoader.php @@ -27,11 +27,14 @@ use ProxyManager\Proxy\ProxyInterface; * 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 @@ class DebugClassLoader $this->patchTypes += [ 'force' => null, 'php' => null, + 'deprecations' => false, ]; if (!isset(self::$caseCheck)) { @@ -571,7 +575,7 @@ class DebugClassLoader $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 @@ class DebugClassLoader 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); } } diff --git a/src/Symfony/Component/ErrorHandler/Tests/DebugClassLoaderTest.php b/src/Symfony/Component/ErrorHandler/Tests/DebugClassLoaderTest.php index 99bb28cd56..4731f2a2db 100644 --- a/src/Symfony/Component/ErrorHandler/Tests/DebugClassLoaderTest.php +++ b/src/Symfony/Component/ErrorHandler/Tests/DebugClassLoaderTest.php @@ -24,7 +24,7 @@ class DebugClassLoaderTest extends TestCase { $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); }