Page MenuHomePhorge

Evaluate PHP Rector to quick refactor Phorge to support PHP 8.1 / 8.2 or do other things
Closed, ResolvedPublic

Description

Rector is a FLOSS tool that instantly upgrades and refactors the PHP code of any PHP 5.3+ code. Let's try it.

Source code:

https://github.com/rectorphp/rector

This Task contains some attempts, some configurations to be tested. Feel free to update as you test more.

Configuration to add support up to PHP 8.2

1<?php
2/**
3 * Rector.php configuration to remove very legacy PHP < 5.5 stuff from
4 * Phorge/Phabricator and Arcanist.
5 *
6 * Author: (2023) Valerio Bozzolan and contributors
7 * License: this specific configuration file is not elegible for copyright
8 * since it's just a configuration
9 * so I release it under CC0 public domain.
10 * Task: https://we.phorge.it/T15145
11 * Source: https://we.phorge.it/P6
12 */
13declare(strict_types=1);
14
15use Rector\Config\RectorConfig;
16use Rector\Set\ValueObject\LevelSetList;
17use Rector\Core\ValueObject\PhpVersion;
18use Rector\Set\ValueObject\DowngradeLevelSetList;
19use Rector\Set\ValueObject\SetList;
20
21return static function (RectorConfig $rectorConfig): void {
22
23 // shared directories to be touched by Rector
24 // these paths exist in both Arcanist and Phorge
25 $paths = [
26 __DIR__ . '/bin',
27 __DIR__ . '/externals',
28 __DIR__ . '/resources',
29 __DIR__ . '/scripts',
30 __DIR__ . '/src',
31 __DIR__ . '/support',
32 ];
33
34 // rare directories to be touched by Rector
35 // these paths are probably defined in only Arcanist, or only Phorge
36 $phaths_rare = [
37 __DIR__ . '/conf',
38 __DIR__ . '/webroot',
39 ];
40
41 // ignore unexisting paths, add existing paths
42 foreach( $phaths_rare as $path ) {
43 if( file_exists( $path ) ) {
44 $paths[] = $path;
45 }
46 }
47
48 // process just these paths
49 $rectorConfig->paths( $paths );
50
51 // ignore various test cases at the moment
52 $rectorConfig->skip( [
53 __DIR__ . '/src/__tests__/' . '**',
54 __DIR__ . '/src/*/__tests__/' . '**',
55 ] );
56
57 // trying to add support to this PHP version and below (PHP 8.2)
58 $rectorConfig->sets( [
59 Rector\Set\ValueObject\LevelSetList::UP_TO_PHP_82,
60 ] );
61
62 // trying to respect obsolete Phorge's code styles previous to PHP 5.5
63 $rectorConfig->skip([
64 // for some reasons we want long "array()" instead of "[]"...
65 // https://we.phorge.it/book/contrib/article/php_coding_standards/
66 Rector\Php54\Rector\Array_\LongArrayToShortArrayRector::class,
67
68 // avoid to add the PHPDOC "* @return never" since it can be confusing
69 Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector::class,
70 ]);
71
72 // tried to drop the () function that just returns the first argument
73 // so I tried just renaming this function to an empty space
74 // so it should do
75 // - id( new Asd() )
76 // + ( new Asd() )
77 // but this is not supported in Rector since it throws an exception
78// $rectorConfig->ruleWithConfiguration(RenameFunctionRector::class, [
79// 'id' => '',
80// ]);
81// So it was implemented with a sed
82
83 // trying to set the current (and minimum) PHP version to be adopted as base
84 // https://getrector.com/documentation/php-version-features
85 // https://we.phorge.it/book/phorge/article/installation_guide/
86 $rectorConfig->phpVersion(PhpVersion::PHP_55);
87
88 // trying to don't destroy my computer that does not have 16 cores but 12
89 // defaults:
90 // time: 120
91 // cores: 16
92 // jobSize: 20
93 // https://getrector.com/documentation/troubleshooting-parallel
94 $rectorConfig->parallel( 120, 12, 20 );
95};

Configuration to remove very old features < PHP 5.5

1<?php
2/**
3 * Rector.php configuration to remove very legacy PHP < 5.5 stuff from
4 * Phorge/Phabricator and Arcanist.
5 *
6 * Author: (2023) Valerio Bozzolan and contributors
7 * License: this specific configuration file is not elegible for copyright
8 * since it's just a configuration
9 * so I release it under CC0 public domain.
10 * Task: https://we.phorge.it/T15145
11 * Source: https://we.phorge.it/P6
12 */
13declare(strict_types=1);
14
15use Rector\Config\RectorConfig;
16use Rector\Set\ValueObject\LevelSetList;
17use Rector\Core\ValueObject\PhpVersion;
18use Rector\Set\ValueObject\DowngradeLevelSetList;
19use Rector\Set\ValueObject\SetList;
20
21return static function (RectorConfig $rectorConfig): void {
22
23 // shared directories to be touched by Rector
24 // these paths exist in both Arcanist and Phorge
25 $paths = [
26 __DIR__ . '/bin',
27 __DIR__ . '/externals',
28 __DIR__ . '/resources',
29 __DIR__ . '/scripts',
30 __DIR__ . '/src',
31 __DIR__ . '/support',
32 ];
33
34 // rare directories to be touched by Rector
35 // these paths are probably defined in only Arcanist, or only Phorge
36 $phaths_rare = [
37 __DIR__ . '/conf',
38 __DIR__ . '/webroot',
39 ];
40
41 // ignore unexisting paths, add existing paths
42 foreach( $phaths_rare as $path ) {
43 if( file_exists( $path ) ) {
44 $paths[] = $path;
45 }
46 }
47
48 // process just these paths
49 $rectorConfig->paths( $paths );
50
51 // ignore various test cases
52 // for some reasons it does not work very well...
53 $rectorConfig->skip( [
54 '*' . '/__tests__/' . '*',
55 ] );
56
57 // trying to set the current (and minimum) PHP version to be adopted as base
58 // https://getrector.com/documentation/php-version-features
59 // https://we.phorge.it/book/phorge/article/installation_guide/
60 $rectorConfig->phpVersion(PhpVersion::PHP_55);
61
62// // trying to add support to this PHP version and below (PHP 8.2)
63// $rectorConfig->sets( [
64// Rector\Set\ValueObject\LevelSetList::UP_TO_PHP_82,
65// ] );
66
67 // trying to remove very old features from old PHP versions (older than PHP 5.5)
68 // NOTE: I've found these rules by try-catch using UP_TO_PHP_82 first and then
69 // looking to the proposed changes. So these are probably the minimal changes.
70 $rectorConfig->rule(Rector\Php53\Rector\FuncCall\DirNameFileConstantToDirConstantRector::class);
71 $rectorConfig->rule(Rector\Php53\Rector\Ternary\TernaryToElvisRector::class);
72
73// // This rule is confusing to me
74// https://stackoverflow.com/q/75648554/3451846
75// $rectorConfig->rule(Rector\Php55\Rector\Class_\ClassConstantToSelfClassRector::class);
76
77
78// // trying to respect obsolete Phorge's code styles previous to PHP 5.5
79// $rectorConfig->skip([
80// // for some reasons we want long "array()" instead of "[]"...
81// // https://we.phorge.it/book/contrib/article/php_coding_standards/
82// Rector\Php54\Rector\Array_\LongArrayToShortArrayRector::class,
83//
84// // avoid to add the PHPDOC "* @return never" since it can be confusing
85// Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector::class,
86// ]);
87
88 // tried to drop the () function that just returns the first argument
89 // so I tried just renaming this function to an empty space
90 // so it should do
91 // - id( new Asd() )
92 // + ( new Asd() )
93 // but this is not supported in Rector since it throws an exception
94// $rectorConfig->ruleWithConfiguration(RenameFunctionRector::class, [
95// 'id' => '',
96// ]);
97// So it was implemented in this way:
98// find . -name "*.php" ! -name rector.php -exec sed -E -i 's/([ =.+,-\)])id\(/\1(/g' {} +
99
100 // trying to don't destroy my computer that does not have 16 cores but 12
101 // defaults:
102 // time: 120
103 // cores: 16
104 // jobSize: 20
105 // https://getrector.com/documentation/troubleshooting-parallel
106 $rectorConfig->parallel( 120, 12, 20 );
107};

Execute locally

Create the rector.php file in the root of your repository (copy from the above Pastes).

Then install Rector:

composer require rector/rector --dev

Then run it locally:

vendor/bin/rector process --debug
NOTE: To try on a single file, or a single directory, express its path as a command-line argument.
WARNING: The future of our children is important. Don't try to randomly execute on Phorge/Phabricator! This could melt your CPU; nuke your storage; kill polar bears; implode the poles. The planet's temperature will increase by half a degree. Don't think that Greta Thunberg will be happy about this. Try only on >= 20GB of RAM, on an SSD, and with a CPU with more than 12 cores. If you don't have enough resources, steal someone else's computer (for example - steal "the cloud").
IMPORTANT: If your computer was stolen it is not our fault. But, look for it in the garbage can and inspect your storage. You may not be able to recover the CPU, but on the disk maybe some person will have left a shiny copy of Phorge that supports PHP 8.2. In that situation it's surely not our fault.

Online demo:

https://getrector.com/demo

Documentation:

https://getrector.com/documentation

Event Timeline

valerio.bozzolan created this task.
valerio.bozzolan created this object in space S1 Public.

I'm not assigned to this since I'm not really active. I was just curious. Feel free to give it a try and add more notes.

valerio.bozzolan renamed this task from Evaluate PHP Rector to quick refactor Phorge to support PHP 8.1 to Evaluate PHP Rector to quick refactor Phorge to support PHP 8.1 or do other things.Mar 6 2023, 06:49
valerio.bozzolan claimed this task.
valerio.bozzolan updated the task description. (Show Details)

I'm pretty impressed by the power of Rector. I almost figured out how to use the 2% of it: Rector put my whole house in order, including Phorge and Arcanist that were on the bedside table.

I've attached a couple of configurations in the task description.

There is only one little thing that I think it's not supported by Rector: the removal of a function. For example to drop id( new Foo() ) and replace with ( new Foo() ). To do that I created just a find+sed line like this:

find . -name "*.php" ! -name rector.php -exec sed -E -i 's/([ =.+,-\)])id\(/\1(/g' {} +

It's a nightmare to read but it just replaces id( with ( - skipping asd_id( and other false-positives.

I just want to mark this as resolved since Rector.php is quite nice and simple and we can surely use this tool to do nice things, even without adding it in our Composer but just locally, when we need it.

valerio.bozzolan renamed this task from Evaluate PHP Rector to quick refactor Phorge to support PHP 8.1 or do other things to Evaluate PHP Rector to quick refactor Phorge to support PHP 8.1 / 8.2 or do other things.Mar 15 2023, 07:36