Skip to content

Add rector/rector checks to improve code quality #20114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Jul 1, 2025

Conversation

cedric-anne
Copy link
Member

Checklist before requesting a review

  • I have read the CONTRIBUTING document.
  • I have performed a self-review of my code.

Description

Rector is a powerfull tool that is able to fasten refactoring by automatically patching the code. It has many rules (700+) for many different cases.

For the moment, I applied only some of the "Code Quality" rule set (https://getrector.com/find-rule?rectorSet=core-code-quality&activeRectorSetGroup=core). I tried to apply only rules that result in a small diff, to ease the validation of the PR.

Later, this tool may be usefull to:

We may also, in the future, provide rector rules sets to ease the migration from a GLPI version to another, in an extension dedicated to GLPI plugins.

This PR should be consider as an initial step. Once it will be validated and merged, everyone will be able to quickly add additional rules.

@cedric-anne cedric-anne self-assigned this Jun 25, 2025
foreach ($parents_members as $parent) {
$members[] = $parent;
}
$members = $parents_members;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$members = self::getParentsMembers($item, '');

src/Stat.php Outdated
@@ -435,7 +433,7 @@ public static function showTable($itemtype, $type, $date1, $date2, $start, array
// Set display type for export if define
$output_type = $_GET["display_type"] ?? Search::HTML_OUTPUT;
$output = SearchEngine::getOutputForLegacyKey($output_type);
$is_html_output = is_a($output, HTMLSearchOutput::class);
$is_html_output = $output instanceof \Glpi\Search\Output\HTMLSearchOutput;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removing the use?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the command executed by @cedric-anne did the change automatically.

Maybe it is considered a good practice because it makes the code more readable in some cases ?
Indeed sometimes the class name is not unique and the developers will need to check the full namespace to be sure of what is being used.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just uses the FQCN because the ->withImportNames() config line is commented. I will fix it manually.

src/Stat.php Outdated
@@ -727,9 +725,9 @@ public static function showTable($itemtype, $type, $date1, $date2, $start, array

$timedisplay = $nb_solved > 0 ? array_sum($data) / $nb_solved : 0;

if ($is_html_output || is_a($output, Pdf::class)) {
if ($is_html_output || $output instanceof \Glpi\Search\Output\Pdf) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removing the use?

src/Stat.php Outdated
$timedisplay = Html::timestampToString($timedisplay, 0, false);
} elseif (is_a($output, Csv::class)) {
} elseif ($output instanceof \Glpi\Search\Output\Csv) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still present in use, no need for full name.

@SebSept
Copy link
Contributor

SebSept commented Jun 26, 2025

That's a must have. Fine to have rector. I just wonder if it's the right time to integrate it.

@trasher
Copy link
Contributor

trasher commented Jun 26, 2025

I just wonder if it's the right time to integrate it.

Seems good for met: there not so much changes, and impact is low.

Copy link
Contributor

@AdrienClairembault AdrienClairembault left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok for me once tests are fixed

@@ -257,14 +257,14 @@ private static function doGenerateKeys(): void
try {
$key = openssl_pkey_new($config);
} catch (OpensslException $e) {
throw new RuntimeException("Unable to generate keys: " . $e->getMessage(), $e->getCode(), $e);
throw new RuntimeException("Unable to generate keys: " . $e->getMessage(), 0, previous: $e);
Copy link
Contributor

@AdrienClairembault AdrienClairembault Jun 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small note but is there a reason to manually set the code to 0 here ? Since you use named parameters you could avoid setting the code if you don't need to.

Copy link
Member Author

@cedric-anne cedric-anne Jun 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rector will says that the code should be set from the previous exception if it is not explicitely set. For me, it does not make sense.
We could remove this specific rule if we consider it too annoying.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It make sense to set it from the previous exception, why shouldn't we ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the latest commit to keep the previous exception code. It does not change much, but I had to change some tests.

Copy link
Contributor

@trasher trasher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK for me. Cannot be merged due to a conflict on composer.lock file

@cedric-anne cedric-anne merged commit d2e91af into glpi-project:main Jul 1, 2025
7 checks passed
@cedric-anne cedric-anne deleted the 11.0/rector branch July 1, 2025 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants