degraciamathieu/php-smelly-code-detector

PHP代码异味检测器

v2.1.0 2023-07-12 06:31 UTC

README

testing phpstan Packagist Version Packagist PHP Version

php-smelly-code-detector

 "代码异味通常是系统更深层次问题的表面迹象。" ~ Martin Fowler

代码异味是具有以下公式的潜在问题代码指示器:($ccn + $arg) * $loc

高异味值通常会揭示方法过于复杂。

这种复杂性可能会损害方法的可维护性,有利于未来出现错误。

此指示器不能替代开发者的专业知识,必须首先将其视为检测“异味”代码的警报。

安装

Requires >= PHP 8.1

Phar

此工具以 PHP存档(PHAR) 的形式分发

wget https://github.com/DeGraciaMathieu/php-smelly-code-detector/raw/master/builds/smelly-code-detector
php smelly-code-detector --version

Composer

或者,您可以直接使用 composer

composer require degraciamathieu/php-smelly-code-detector --dev

分析方法

php smelly-code-detector inspect-method {path}

选项

示例

默认情况下显示20行,您可以使用 --limit= 选项配置此设置。

$ php smelly-code-detector inspect-method app --limit=5
❀ PHP Smelly Code Detector ❀
   84 [============================]
+-----------------------------------------------------+--------+------------+-----+-----+-----+-------+
| file                                                | method | visibility | loc | arg | ccn | Smell |
+-----------------------------------------------------+--------+------------+-----+-----+-----+-------+
| app/Http/Controllers/Blog/AdminPostController.php   | update | Public     | 25  | 2   | 3   | 125   |
| app/Http/Controllers/Auth/NewPasswordController.php | store  | Public     | 29  | 1   | 2   | 87    |
| app/Http/Controllers/Blog/AdminPostController.php   | store  | Public     | 26  | 1   | 2   | 78    |
| app/Console/Commands/FetchGoogleFonts.php           | store  | Private    | 26  | 1   | 2   | 78    |
| app/Http/Middleware/RedirectIfAuthenticated.php     | handle | Public     | 11  | 3   | 4   | 77    |
+-----------------------------------------------------+--------+------------+-----+-----+-----+-------+
5/183 methods displayed

您可以使用 --only=--ignore= 选项选择扫描的文件。

$ php smelly-code-detector inspect-method app --only=Controller.php --limit=10
❀ PHP Smelly Code Detector ❀
   24 [============================]
+-----------------------------------------------------------+--------------------+------------+-----+-----+-----+-------+
| file                                                      | method             | visibility | loc | arg | ccn | Smell |
+-----------------------------------------------------------+--------------------+------------+-----+-----+-----+-------+
| app/Http/Controllers/Blog/AdminPostController.php         | update             | Public     | 25  | 2   | 3   | 125   |
| app/Http/Controllers/Auth/NewPasswordController.php       | store              | Public     | 29  | 1   | 2   | 87    |
| app/Http/Controllers/Blog/AdminPostController.php         | store              | Public     | 26  | 1   | 2   | 78    |
| app/Http/Controllers/User/ProfileController.php           | updateAvatar       | Public     | 21  | 1   | 2   | 63    |
| app/Http/Controllers/Blog/GalleryController.php           | uploadPicture      | Public     | 21  | 1   | 2   | 63    |
| app/Http/Controllers/Auth/PasswordResetLinkController.php | store              | Public     | 17  | 1   | 2   | 51    |
| app/Http/Controllers/User/ProfileController.php           | updateInformations | Public     | 17  | 1   | 2   | 51    |
| app/Http/Controllers/Auth/RegisteredUserController.php    | store              | Public     | 25  | 1   | 1   | 50    |
| app/Http/Controllers/Blog/ShowPostController.php          | __invoke           | Public     | 12  | 2   | 2   | 48    |
| app/Http/Controllers/Forum/CommentController.php          | store              | Public     | 16  | 2   | 1   | 48    |
+-----------------------------------------------------------+--------------------+------------+-----+-----+-----+-------+
10/50 methods displayed

您可以使用 --public--protected--private 选项选择要保留的可见性。

$ php smelly-code-detector inspect-method app --private
❀ PHP Smelly Code Detector ❀
   84 [============================]
+--------------------------------------------------+---------------------------------+------------+-----+-----+-----+-------+
| file                                             | method                          | visibility | loc | arg | ccn | Smell |
+--------------------------------------------------+---------------------------------+------------+-----+-----+-----+-------+
| app/Console/Commands/FetchGoogleFonts.php        | store                           | Private    | 26  | 1   | 2   | 78    |
| app/Services/Community/CreatorRepository.php     | instantiateCreatorsFromResponse | Private    | 24  | 1   | 2   | 72    |
| app/Notifications/VerifyEmail.php                | verificationUrl                 | Private    | 10  | 1   | 1   | 20    |
| app/Http/Controllers/Blog/ShowPostController.php | postAreNotDisplayable           | Private    | 3   | 2   | 2   | 12    |
+--------------------------------------------------+---------------------------------+------------+-----+-----+-----+-------+
4/4 methods displayed

默认情况下,行将按异味值排序,您可以使用 --sort-by= 选项更改排序顺序,以下值可用:loc、arg、ccl、smell。

$ php smelly-code-detector inspect-method app --sort-by=ccl --limit=3
❀ PHP Smelly Code Detector ❀
   84 [============================]
+---------------------------------------------------+-----------------------+------------+-----+-----+-----+-------+
| file                                              | method                | visibility | loc | arg | ccn | Smell |
+---------------------------------------------------+-----------------------+------------+-----+-----+-----+-------+
| app/Http/Middleware/RedirectIfAuthenticated.php   | handle                | Public     | 11  | 3   | 4   | 77    |
| app/Http/Controllers/Blog/AdminPostController.php | update                | Public     | 25  | 2   | 3   | 125   |
| app/Providers/RouteServiceProvider.php            | configureRateLimiting | Protected  | 5   | 0   | 2   | 10    |
+---------------------------------------------------+-----------------------+------------+-----+-----+-----+-------+
3/183 methods displayed

分析类

此命令给出类的总异味值,以及按方法平均和按可见性分解。

php smelly-code-detector inspect-class {path}

选项

示例

$ php smelly-code-detector inspect-class app
❀ PHP Smelly Code Detector ❀
+-----------------------------------------------------+-------+-------+-----+--------+-------+-------+
| class                                               | count | smell | avg | public | prot. | priv. |
+-----------------------------------------------------+-------+-------+-----+--------+-------+-------+
| app/Http/Controllers/Blog/AdminPostController.php   | 8     | 244   | 30  | 100 %  | 0 %   | 0 %   |
| app/Http/Controllers/User/ProfileController.php     | 5     | 150   | 30  | 100 %  | 0 %   | 0 %   |
| app/Services/Community/CreatorRepository.php        | 3     | 118   | 39  | 38 %   | 61 %  | 0 %   |
| app/Console/Commands/FetchGoogleFonts.php           | 4     | 117   | 29  | 9 %    | 66 %  | 23 %  |
| app/Http/Controllers/Forum/CommentController.php    | 4     | 116   | 29  | 100 %  | 0 %   | 0 %   |
| app/Http/Controllers/Auth/NewPasswordController.php | 2     | 93    | 46  | 100 %  | 0 %   | 0 %   |
| app/Http/Controllers/Forum/TopicController.php      | 7     | 81    | 11  | 100 %  | 0 %   | 0 %   |
| app/Policies/UserPolicy.php                         | 8     | 77    | 9   | 100 %  | 0 %   | 0 %   |
| app/Policies/TopicPolicy.php                        | 8     | 77    | 9   | 100 %  | 0 %   | 0 %   |
| app/Policies/CommentPolicy.php                      | 8     | 77    | 9   | 100 %  | 0 %   | 0 %   |
| app/Policies/SubscriberPolicy.php                   | 8     | 77    | 9   | 100 %  | 0 %   | 0 %   |
| app/Policies/PostPolicy.php                         | 8     | 77    | 9   | 100 %  | 0 %   | 0 %   |
| app/Http/Middleware/RedirectIfAuthenticated.php     | 1     | 77    | 77  | 100 %  | 0 %   | 0 %   |
| app/Http/Controllers/Blog/GalleryController.php     | 3     | 76    | 25  | 100 %  | 0 %   | 0 %   |
| app/Services/Markdown/MarkdownProvider.php          | 1     | 75    | 75  | 100 %  | 0 %   | 0 %   |
| app/Http/Controllers/Blog/ShowPostController.php    | 3     | 74    | 24  | 64 %   | 16 %  | 18 %  |
| app/Http/Requests/LoginRequest.php                  | 5     | 70    | 14  | 100 %  | 0 %   | 0 %   |
| app/Models/Post.php                                 | 6     | 68    | 11  | 58 %   | 0 %   | 41 %  |
| app/Notifications/VerifyEmail.php                   | 5     | 61    | 12  | 67 %   | 32 %  | 0 %   |
| app/Channels/DiscordWebhookChannel.php              | 3     | 61    | 20  | 31 %   | 0 %   | 68 %  |
+-----------------------------------------------------+-------+-------+-----+--------+-------+-------+
20/84 class displayed