Skip to content
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

Clean-up unusedcode #125

Merged
merged 4 commits into from
Feb 18, 2024
Merged

Conversation

llaville
Copy link
Contributor

All Submissions:

New Feature Submissions:

  1. Does your submission pass tests?
  2. Have you lint your code locally prior to submission?

Goal 1

By running PHP Mess Detector on this repository, I've recently opened a report about unused private method.

Here are original reporting with PHPMD 2.15 (PHAR version)

php -d error_reporting=8191 phpmd.phar src ansi unusedcode

Expected results :

Found 0 violations and 0 errors in 359ms

No mess detected

Current results :

FILE: /shared/backups/forks/symbol-parser/src/Symbol/Loader/FileSymbolLoader.php
--------------------------------------------------------------------------------
 139 | VIOLATION | Avoid unused private methods such as 'filterExistingDir'.


FILE: /shared/backups/forks/symbol-parser/src/Symbol/Loader/PsrSymbolLoader.php
-------------------------------------------------------------------------------
 22  | VIOLATION | Avoid unused local variables such as '$dir'.

Found 2 violations and 0 errors in 691ms

This PR fixed the two violations by upgrading code to compatible PHP 7.4 syntax !

Goal 2

Remove unused imports

This PR add a PHP CS Fixer config file (.php-cs-fixer.dist.php) to detect at least unused imports

With such file content, and by running command

php php-cs-fixer.phar check -v --diff

I got this report

PHP CS Fixer 3.35.1 (ec1ccc2) Freezy Vrooom by Fabien Potencier and Dariusz Ruminski.
PHP runtime: 8.2.16
Loaded config default from "/shared/backups/forks/symbol-parser/.php-cs-fixer.dist.php".
..............................F.F..................................................................                               99 / 99 (100%)
Legend: .-no changes, F-fixed, S-skipped (cached or empty file), I-invalid file syntax (file ignored), E-error
   1) src/Parser/PHP/Strategy/FullQualifiedParameterStrategy.php (no_unused_imports)
      ---------- begin diff ----------
--- /shared/backups/forks/symbol-parser/src/Parser/PHP/Strategy/FullQualifiedParameterStrategy.php
+++ /shared/backups/forks/symbol-parser/src/Parser/PHP/Strategy/FullQualifiedParameterStrategy.php
@@ -5,7 +5,6 @@
 namespace ComposerUnused\SymbolParser\Parser\PHP\Strategy;

 use PhpParser\Node;
-use PhpParser\Node\Expr\New_;
 use PhpParser\Node\Name\FullyQualified;

 final class FullQualifiedParameterStrategy implements StrategyInterface

      ----------- end diff -----------

   2) src/Parser/PHP/SymbolCollectorInterface.php (no_unused_imports)
      ---------- begin diff ----------
--- /shared/backups/forks/symbol-parser/src/Parser/PHP/SymbolCollectorInterface.php
+++ /shared/backups/forks/symbol-parser/src/Parser/PHP/SymbolCollectorInterface.php
@@ -5,7 +5,6 @@
 namespace ComposerUnused\SymbolParser\Parser\PHP;

 use Closure;
-use ComposerUnused\SymbolParser\Symbol\SymbolName;
 use PhpParser\NodeVisitor;

 interface SymbolCollectorInterface extends NodeVisitor

      ----------- end diff -----------


Found 2 of 99 files that can be fixed in 0.319 seconds, 18.633 MB memory used

@llaville
Copy link
Contributor Author

@icanhazstring I think we should also add the global_namespace_import rule and configure like that :

diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php
index 0d5bd29..5b84188 100644
--- a/.php-cs-fixer.dist.php
+++ b/.php-cs-fixer.dist.php
@@ -4,6 +4,7 @@ declare(strict_types=1);

 return (new PhpCsFixer\Config())
     ->setRules([
+        'global_namespace_import' => ['import_classes' => true, 'import_constants' => true, 'import_functions' => true],
         'no_unused_imports' => true,
     ])
     ->setFinder(

That will allows to have code homogeneous with global imports. WDYT about it ?

Copy link
Member

@icanhazstring icanhazstring left a comment

Choose a reason for hiding this comment

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

Looks good. Why not. LGTM

@icanhazstring icanhazstring merged commit 031a2de into composer-unused:main Feb 18, 2024
10 checks passed
@llaville llaville deleted the unusedcode branch February 18, 2024 21:13
@llaville llaville mentioned this pull request Feb 20, 2024
5 tasks
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.

2 participants