rectorphp / rector

Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
https://getrector.com
MIT License
8.77k stars 687 forks source link

[Meta] [PHP 7.1 do 5.6] Plan to downgrade PHP #6079

Closed TomasVotruba closed 3 years ago

TomasVotruba commented 3 years ago

After Rector 0.10 with PHP 7.1 downgraded code, we want to go even deeper. Next stop is PHP 5.6, as that's where most legacy projects are stuck. Right before the huge wall of PHP 7.0 :)

This is meta issue to reference downgrade rules. To kick of with few.

From PHP 7.1 to PHP 7.0 -

RFC links: https://wiki.php.net/rfc#php_71

From PHP 7.0 to PHP 5.6

RFC link: https://wiki.php.net/rfc#php_70

TomasVotruba commented 3 years ago

@samsonasik Could you complete list with most wide-spread features in PHP world? Feel free to also to start with adding those rules.

samsonasik commented 3 years ago

I will try

samsonasik commented 3 years ago

✔️ Remove void type seems implemented at https://github.com/rectorphp/rector/blob/main/rules/DowngradePhp71/Rector/FunctionLike/DowngradeVoidTypeDeclarationRector.php

samsonasik commented 3 years ago

✔️ Remove scalar type declarations seems implemented at https://github.com/rectorphp/rector/blob/main/rules/DowngradePhp70/Rector/FunctionLike/DowngradeTypeDeclarationRector.php

TomasVotruba commented 3 years ago

@samsonasik Could you update the issue description with links to these rules? So we have it one place

samsonasik commented 3 years ago

@TomasVotruba updated.

samsonasik commented 3 years ago

@TomasVotruba for Remove anonymous classes, do you think it need to create a new dedicated class and create the instance of it before remove the anonymous class?

TomasVotruba commented 3 years ago

I think so :+1:

lulco commented 3 years ago

From PHP 7.1 to PHP 7.0 - remove nullable types

samsonasik commented 3 years ago

@lulco that already in DowngradeNullableTypeDeclarationRector https://github.com/rectorphp/rector/blob/main/rules/DowngradePhp71/Rector/FunctionLike/DowngradeNullableTypeDeclarationRector.php

I will update the list.

samsonasik commented 3 years ago

@lulco list updated.

TomasVotruba commented 3 years ago

There was a tool that downgraded most common features of PHP 7 to PHP 5 - https://github.com/spatie/7to5 for inspiration. :tada: It also has implemented NodeVisitors - https://github.com/spatie/7to5/tree/master/src/NodeVisitors

samsonasik commented 3 years ago

@TomasVotruba scoped to 7.0 process got error on adding phpstan-src

composer require phpstan/phpstan-src --ignore-platform-reqs

Using version ^0.12.85 for phpstan/phpstan-src

./composer.json has been updated

PHP Fatal error:  Cannot declare class RectorPrefix20210505\Composer\InstalledVersions, because the name is already in use in /home/runner/work/rector/rector/remote-repository-php70/vendor/composer/InstalledVersions.php on line 7

Fatal error: Cannot declare class RectorPrefix20210505\Composer\InstalledVersions, because the name is already in use in /home/runner/work/rector/rector/remote-repository-php70/vendor/composer/InstalledVersions.php on line 7

Error: Process completed with exit code 255.

ref https://github.com/rectorphp/rector/runs/2506182264?check_suite_focus=true#step:19:9

Do you have idea how to solve it?

leoloso commented 3 years ago

I think you can avoid scoping InstalledVersions.php without any harm, and avoiding this issue.

samsonasik commented 3 years ago

@leoloso ok, let's try on https://github.com/rectorphp/rector/pull/6335, thank you.

samsonasik commented 3 years ago

@leoloso it seems still same error https://github.com/rectorphp/rector/runs/2507401040#step:19:10 , while it actually already changed in vendor/composer/package-versions-deprecated/src/PackageVersions/Versions.php :

https://github.com/rectorphp/rector-prefixed-php70/blob/c3740d9d7639e4d74950cd1235c41c51b7ab2b84/vendor/composer/package-versions-deprecated/src/PackageVersions/Versions.php#L6

leoloso commented 3 years ago

But you're handling it through a patcher, which operates after doing the scoping. I was suggesting to completely skip scoping that file.

That's what I did my project when I had the same issue as you have now, and now it works well (source):

'files-whitelist' => [
    convertRelativeToFullPath('vendor/composer/InstalledVersions.php'),
],
samsonasik commented 3 years ago

@leoloso thank you. @TomasVotruba the phpstan-src versioning php requirement seems different with phpstan's. It can't be solved with ignore platform reqs as it has different packages requirement, do you have idea how to solve it? Thank you.

TomasVotruba commented 3 years ago

The phpstan-src must be downgraded to PHP 7.1

samsonasik commented 3 years ago

Yes, but getting phpstan-src from php 7.3 is not possible, even with ignore platform reqs due different packages requirements, ref:

https://github.com/phpstan/phpstan-src/blob/40d93a147556aa696d1aa6c0e1dad9c7530820e3/composer.json#L8-L12

Do we need php 7.4 ?

TomasVotruba commented 3 years ago

Do we need php 7.4 ?

I think so :+1: the version Rector runs downgrade in is not really relevant. It can be PHP 8 too

samsonasik commented 3 years ago

@TomasVotruba the phpstan-src is now included, but on downgrading process, it got error:

Run sh build/downgrade-rector-php70.sh rector-build-php70

[NOTE] Running downgrade in 'rector-build-php70' directory

PHP Fatal error:  Uncaught Nette\MemberAccessException: Cannot read an undeclared property PHPStan\DependencyInjection\Configurator::$parameters. in /home/runner/work/rector/rector/vendor/nette/utils/src/Utils/ObjectHelpers.php:31

Stack trace:
#0 /home/runner/work/rector/rector/vendor/nette/utils/src/SmartObject.php(76): Nette\Utils\ObjectHelpers::strictGet()
#1 /home/runner/work/rector/rector/vendor/phpstan/phpstan-src/src/DependencyInjection/Configurator.php(42): Nette\Bootstrap\Configurator->__get()
#2 /home/runner/work/rector/rector/vendor/nette/bootstrap/src/Bootstrap/Configurator.php(252): PHPStan\DependencyInjection\Configurator->loadContainer()
#3 /home/runner/work/rector/rector/vendor/phpstan/phpstan-src/src/DependencyInjection/ContainerFactory.php(102): Nette\Bootstrap\Configurator->createContainer()
#4 /home/runner/work/rector/rector/packages/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php(43): PHPStan\DependencyInjection\ContainerFactory->create()
#5 /tmp/rector/cache/Container7O4zmPC/HttpKernelRect in /home/runner/work/rector/rector/vendor/nette/utils/src/Utils/ObjectHelpers.php on line 31

Fatal error: Uncaught Nette\MemberAccessException: Cannot read an undeclared property PHPStan\DependencyInjection\Configurator::$parameters. in /home/runner/work/rector/rector/vendor/nette/utils/src/Utils/ObjectHelpers.php:31
Stack trace:
#0 /home/runner/work/rector/rector/vendor/nette/utils/src/SmartObject.php(76): Nette\Utils\ObjectHelpers::strictGet()
#1 /home/runner/work/rector/rector/vendor/phpstan/phpstan-src/src/DependencyInjection/Configurator.php(42): Nette\Bootstrap\Configurator->__get()
#2 /home/runner/work/rector/rector/vendor/nette/bootstrap/src/Bootstrap/Configurator.php(252): PHPStan\DependencyInjection\Configurator->loadContainer()
#3 /home/runner/work/rector/rector/vendor/phpstan/phpstan-src/src/DependencyInjection/ContainerFactory.php(102): Nette\Bootstrap\Configurator->createContainer()
#4 /home/runner/work/rector/rector/packages/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php(43): PHPStan\DependencyInjection\ContainerFactory->create()
#5 /tmp/rector/cache/Container7O4zmPC/HttpKernelRect in /home/runner/work/rector/rector/vendor/nette/utils/src/Utils/ObjectHelpers.php on line 31
Error: Process completed with exit code 255.

in https://github.com/rectorphp/rector/runs/2511018616#step:7:8 .

It seems some code are different between in phpstan/phpstan and phpstan/phpstan-src, another example is current error:

PHP Fatal error:  Interface 'PHPStan\BetterReflection\SourceLocator\Type\SourceLocator' not found

in https://github.com/rectorphp/rector-prefixed-php70/runs/2500555629#step:4:4 which not exists in https://github.com/phpstan/phpstan-src

TomasVotruba commented 3 years ago

Btw, just a tip. Instead of pushing to main and waiting for CI, you can use local script to get feedback much faster.

I did the same for building rector-prefixed in full_build.sh

TomasVotruba commented 3 years ago

It might be easier to downgrade phpstan/phpstan-src first as standalone project. And when that is ok add it to Rector. Scoping PHPStan has own scoper.php that might be needed 1:1 https://github.com/phpstan/phpstan-src/blob/master/compiler/build/scoper.inc.php

samsonasik commented 3 years ago

@TomasVotruba thank you, I may need to create full_build_php70.sh then for excluding the dependencies that cause conflicts, ref:

https://github.com/rectorphp/rector/blob/19be81179615bd074e9cc7c304746a57a4ea32a9/.github/workflows/build_scoped_rector_php70.yaml#L37-L48

That removal need to be executing first as composer/xdebug-handler and phpstan/phpstan are in require.

I will check the phpstan's scoper.inc.php if that works to scope phpstan-src first.

TomasVotruba commented 3 years ago

We've just launched Sponsorware initiative to make this happen with your help :muscle:

TomasVotruba commented 3 years ago

Untill the sponsorware treshold #1 is met, this issue is not a priority. Closing to keep focus on active ones.