Skip to content

Commit 6dd9d24

Browse files
minor #33236 Add return-types with help from DebugClassLoader in the CI (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- Add return-types with help from DebugClassLoader in the CI | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #33228 | License | MIT | Doc PR | - I've spent a great deal of time on this PR, experimenting with adding return types to the codebase. TL;DR: my conclusion is that we cannot make it for 5.0. There are two reasons for this: 1. The burden this will put on the community is immense, especially when considering that third party libs must also be updated for any apps to work at all on a return-typed 5.0. Symfony must add them last, not first. 2. We need return type covariance, yet this won't be available before PHP 7.4, while 5.0 supports 7.2. What's attached? - ~a draft patching logic in `DebugClassLoader` to add return-type where it discovers this should be done~ - return types added automatically thanks to #33283 - ~manual fixes for situations not handled (yet, if possible at all) by that logic in `DebugClassLoader`~ #33332 What's achieved? Tests are green \o/ At this stage, I think we have to acknowledge we won't add return-types in 5.0 but prepare a serious plan to add them in 6.0. This plan could be: - [x] make DebugClassLoader able to automate adding return types. - [x] in 4.4: add all possible return types that don't break BC, e.g. in `Tests` and in generated code - [x] spot and fix places where annotations aren't accurate, add more annotations where possible. - [x] ensure `DebugClassLoader` triggers the best possible deprecations that encourage ppl to add return-types in their libs/apps. This means we could decide to disable the current ones (see #33235) and to re-enable them in 5.1. This will also give us the time to fine-tune the tooling (item 1. on this list) Ideally, we could reach a point where we could test branch 4.4 *with* return-types: we'd use the tooling to add them automatically in the CI job, then we'd run tests and they should be green. Let's do this? Help Wanted, here is how: *With PHP 7.4*, run `php .github/patch-types.php`. This will add return types everywhere possible. Then run tests, e.g. `./phpunit src/Symfony/Component/HttpFoundation --exclude-group legacy,issue-32995` Here are the components that fail with return types added, please help me check them all with a PR on [my fork](https://github.com/nicolas-grekas/symfony/tree/eh-return-types): - [x] src/Symfony/Bridge/Doctrine - [x] src/Symfony/Bridge/Monolog - [x] src/Symfony/Bridge/PhpUnit - [x] src/Symfony/Bridge/ProxyManager - [x] src/Symfony/Bridge/Twig - [x] src/Symfony/Bundle/DebugBundle - [x] src/Symfony/Bundle/FrameworkBundle - [x] src/Symfony/Bundle/SecurityBundle - [x] src/Symfony/Bundle/TwigBundle - [x] src/Symfony/Bundle/WebProfilerBundle - [x] src/Symfony/Bundle/WebServerBundle - [x] src/Symfony/Component/Asset - [x] src/Symfony/Component/BrowserKit - [x] src/Symfony/Component/Cache - [x] nicolas-grekas#28 src/Symfony/Component/Config - [x] src/Symfony/Component/Console - [x] src/Symfony/Component/CssSelector - [x] src/Symfony/Component/Debug - [x] nicolas-grekas#28 src/Symfony/Component/DependencyInjection - [x] src/Symfony/Component/DomCrawler - [x] src/Symfony/Component/Dotenv - [x] src/Symfony/Component/ErrorHandler - [x] src/Symfony/Component/ErrorRenderer - [x] nicolas-grekas#24 src/Symfony/Component/EventDispatcher - [x] src/Symfony/Component/ExpressionLanguage - [x] src/Symfony/Component/Filesystem - [x] src/Symfony/Component/Finder - [x] src/Symfony/Component/Form - [x] src/Symfony/Component/HttpClient - [x] src/Symfony/Component/HttpFoundation - [x] src/Symfony/Component/HttpKernel - [x] src/Symfony/Component/Inflector - [x] src/Symfony/Component/Intl - [x] src/Symfony/Component/Ldap - [x] src/Symfony/Component/Lock - [x] src/Symfony/Component/Mailer - [x] src/Symfony/Component/Messenger - [x] src/Symfony/Component/Mime - [x] src/Symfony/Component/OptionsResolver - [x] src/Symfony/Component/Process - [x] src/Symfony/Component/PropertyAccess - [x] src/Symfony/Component/PropertyInfo - [x] nicolas-grekas#25 src/Symfony/Component/Routing - [x] nicolas-grekas#26 src/Symfony/Component/Security - [x] src/Symfony/Component/Security/Core - [x] src/Symfony/Component/Security/Guard - [x] src/Symfony/Component/Security/Http - [x] nicolas-grekas#29 src/Symfony/Component/Serializer - [x] src/Symfony/Component/Security/Csrf - [x] src/Symfony/Component/Stopwatch - [x] src/Symfony/Component/Templating - [x] nicolas-grekas#27 src/Symfony/Component/Translation - [x] src/Symfony/Component/Validator - [x] src/Symfony/Component/VarDumper - [x] src/Symfony/Component/VarExporter - [x] src/Symfony/Component/WebLink - [x] src/Symfony/Component/Workflow - [x] src/Symfony/Component/Yaml - [x] src/Symfony/Contracts Commits ------- 11149a1 Add return-types with help from DebugClassLoader in the CI
2 parents db9549d + 11149a1 commit 6dd9d24

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

.github/patch-types.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
if (false === getenv('SYMFONY_PATCH_TYPE_DECLARATIONS')) {
4+
putenv('SYMFONY_PATCH_TYPE_DECLARATIONS=force=1&php71-compat=0');
5+
}
6+
7+
require __DIR__.'/../.phpunit/phpunit-8.3-0/vendor/autoload.php';
8+
9+
file_put_contents(__DIR__.'/../vendor/autoload.php', preg_replace('/^return (Composer.*);/m', <<<'EOTXT'
10+
$loader = \1;
11+
$loader->addClassMap(['Symfony\Component\Debug\Exception\FlattenException' => \dirname(__DIR__).'/src/Symfony/Component/Debug/Exception/FlattenException.php']);
12+
13+
return $loader;
14+
15+
EOTXT
16+
, file_get_contents(__DIR__.'/../vendor/autoload.php')));
17+
18+
$loader = require __DIR__.'/../vendor/autoload.php';
19+
20+
Symfony\Component\ErrorHandler\DebugClassLoader::enable();
21+
22+
foreach ($loader->getClassMap() as $class => $file) {
23+
switch (true) {
24+
case false !== strpos(realpath($file), '/vendor/'):
25+
case false !== strpos($file, '/src/Symfony/Bridge/PhpUnit/'):
26+
case false !== strpos($file, '/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Validation/Article.php'):
27+
case false !== strpos($file, '/src/Symfony/Component/Config/Tests/Fixtures/BadParent.php'):
28+
case false !== strpos($file, '/src/Symfony/Component/Debug/Tests/Fixtures/'):
29+
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Compiler/OptionalServiceClass.php'):
30+
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/ParentNotExists.php'):
31+
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Prototype/BadClasses/MissingParent.php'):
32+
case false !== strpos($file, '/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/'):
33+
case false !== strpos($file, '/src/Symfony/Component/ErrorHandler/Tests/Fixtures/'):
34+
case false !== strpos($file, '/src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php'):
35+
case false !== strpos($file, '/src/Symfony/Component/PropertyInfo/Tests/Fixtures/ParentDummy.php'):
36+
case false !== strpos($file, '/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectOuter.php'):
37+
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/NotLoadableClass.php'):
38+
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/Php74.php') && \PHP_VERSION_ID < 70400:
39+
continue 2;
40+
}
41+
42+
class_exists($class);
43+
}
44+
45+
Symfony\Component\ErrorHandler\DebugClassLoader::disable();

.travis.yml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ env:
2626
matrix:
2727
include:
2828
- php: 7.1
29-
env: php_extra="7.2"
29+
env: php_extra="7.2 7.4snapshot"
3030
- php: 7.3
3131
env: deps=high
3232
- php: 7.4snapshot
@@ -79,10 +79,8 @@ before_install:
7979
export COMPONENTS=$(find src/Symfony -mindepth 2 -type f -name phpunit.xml.dist -printf '%h\n')
8080
find ~/.phpenv -name xdebug.ini -delete
8181
82-
if [[ $TRAVIS_PHP_VERSION = 7.4* && $deps ]]; then
82+
if [[ $TRAVIS_PHP_VERSION = 7.4* ]]; then
8383
export PHPUNIT_X="$PHPUNIT_X,issue-32995"
84-
elif [[ $TRAVIS_PHP_VERSION = 7.4* ]]; then
85-
export PHPUNIT_X="$PHPUNIT --group issue-32995"
8684
fi
8785
8886
nanoseconds () {
@@ -150,7 +148,7 @@ before_install:
150148
- |
151149
# php.ini configuration
152150
for PHP in $TRAVIS_PHP_VERSION $php_extra; do
153-
phpenv global $PHP 2>/dev/null || (cd / && wget https://s3.amazonaws.com/travis-php-archives/binaries/ubuntu/14.04/x86_64/php-$PHP.tar.bz2 -O - | tar -xj)
151+
phpenv global $PHP 2>/dev/null || (cd / && wget https://storage.googleapis.com/travis-ci-language-archives/php/binaries/ubuntu/16.04/x86_64/php-$PHP.tar.bz2 -O - | tar -xj)
154152
INI=~/.phpenv/versions/$PHP/etc/conf.d/travis.ini
155153
echo date.timezone = Europe/Paris >> $INI
156154
echo memory_limit = -1 >> $INI
@@ -262,7 +260,7 @@ install:
262260
run_tests () {
263261
set -e
264262
export PHP=$1
265-
if [[ $PHP != $TRAVIS_PHP_VERSION && $TRAVIS_PULL_REQUEST != false ]]; then
263+
if [[ $PHP != 7.4* && $PHP != $TRAVIS_PHP_VERSION && $TRAVIS_PULL_REQUEST != false ]]; then
266264
echo -e "\\n\\e[1;34mIntermediate PHP version $PHP is skipped for pull requests.\\e[0m"
267265
break
268266
fi
@@ -279,6 +277,17 @@ install:
279277
echo "$COMPONENTS" | parallel --gnu "tfold {} 'cd {} && ([ -e composer.lock ] && ${COMPOSER_UP/update/install} || $COMPOSER_UP --prefer-lowest --prefer-stable) && $PHPUNIT_X'"
280278
echo "$COMPONENTS" | xargs -n1 -I{} tar --append -f ~/php-ext/composer-lowest.lock.tar {}/composer.lock
281279
else
280+
if [[ $PHP = 7.4* ]]; then
281+
# add return types before running the test suite
282+
rm vendor/symfony/contracts -Rf
283+
ln -sd $(realpath src/Symfony/Contracts) vendor/symfony/contracts
284+
sed -i 's/"\*\*\/Tests\/"//' composer.json
285+
composer install --optimize-autoloader
286+
php .github/patch-types.php
287+
php .github/patch-types.php # ensure the script is idempotent
288+
export PHPUNIT_X="$PHPUNIT_X,issue-32995,legacy"
289+
fi
290+
282291
echo "$COMPONENTS" | parallel --gnu "tfold {} $PHPUNIT_X {}"
283292
tfold src/Symfony/Component/Console.tty $PHPUNIT src/Symfony/Component/Console --group tty
284293
if [[ $PHP = ${MIN_PHP%.*} ]]; then

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MongoDbSessionHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected function doDestroy($sessionId)
100100
}
101101

102102
/**
103-
* @return int
103+
* @return bool
104104
*/
105105
public function gc($maxlifetime)
106106
{

0 commit comments

Comments
 (0)