PHP Insights 介紹

PHP Insights 可以用來分析PHP專案的程式品質,可以很簡單的方式從 terminal 直接進行分析。

支援 PHP 7.2+ 以上版本。 並且適用於 Laravel, Symfony, Yii, WordPress, Magento2, 等架構。

這裏主要針對 安裝及 Laravel 使用方式進行說明。

安裝方式:

composer require nunomaduro/phpinsights --dev

Docker 使用者可以透過以下方式

docker run -it --rm -v $(pwd):/app nunomaduro/phpinsights

Laravel 架構中使用

在 Laravel 架構使用 phpinsights ,可以透過 publish 將 insights 加入 artisan command

php artisan vendor:publish --provider="NunoMaduro\PhpInsights\Application\Adapters\Laravel\InsightsServiceProvider"

執行完畢之後,相關 inights 設定會在 config/insights.php

執行檢測

直接透過 phpinsights 執行

./vendor/bin/phpinsights

或者有將 insights 加入 artisan ,則可以執行下方指令

php artisan insights

下方是,針對 Laravel (5.8) 安裝後直接進行檢測,跑出結果:

[MISC] 91.3 pts on coding style and 0 security issues encountered

• [Code] Useless overriding method:
  app/Exceptions/Handler.php:47: Possible useless method overriding detected
  app/Providers/EventServiceProvider.php:28: Possible useless method overriding detected
  app/Providers/RouteServiceProvider.php:24: Possible useless method overriding detected
  +1 issues omitted

• [Code] Unused parameter:
  app/Console/Kernel.php:25: Unused parameter $schedule.

• [Code] Declare strict types:
  routes/channels.php:1: Missing declare(strict_types = 1).
  routes/console.php:1: Missing declare(strict_types = 1).
  routes/web.php:1: Missing declare(strict_types = 1).
  +23 issues omitted

• [Code] Empty comment:
  app/Providers/EventServiceProvider.php:32: Empty comment
  app/Providers/RouteServiceProvider.php:26: Empty comment
  app/Providers/RouteServiceProvider.php:42: Empty comment
  +8 issues omitted

• [Code] Disallow mixed type hint:
  app/Http/Middleware/RedirectIfAuthenticated.php:16: Usage of "mixed" type hint is disallowed.

• [Code] Type hint declaration:
  app/User.php:25: @var annotation of property \App\User::$hidden does not specify type hint for its items.
  app/User.php:34: @var annotation of property \App\User::$casts does not specify type hint for its items.
  routes/console.php:16: Closure does not have void return type hint.
  +42 issues omitted

• [Code] Static closure:
  routes/api.php:16: Closure not using "$this" should be declared static.
  routes/channels.php:14: Closure not using "$this" should be declared static.
  routes/web.php:14: Closure not using "$this" should be declared static.

• [Architecture] Normal classes are forbidden. Classes must be final or abstract:
  app/Http/Controllers/Auth/VerificationController.php
  app/Http/Kernel.php
  app/Console/Kernel.php
  +19 issues omitted

• [Architecture] The name property in the `composer.json` contains the default value

• [Style] Object operator indent:
  app/Providers/RouteServiceProvider.php:69: Object operator not indented correctly; expected 12 spaces but found 13
  app/Providers/RouteServiceProvider.php:70: Object operator not indented correctly; expected 12 spaces but found 13
  app/Providers/RouteServiceProvider.php:71: Object operator not indented correctly; expected 12 spaces but found 13
  +2 issues omitted

• [Style] Line length:
  app/Providers/AuthServiceProvider.php:6: Line exceeds 80 characters; contains 83 characters
  app/Providers/EventServiceProvider.php:8: Line exceeds 80 characters; contains 84 characters
  app/Providers/RouteServiceProvider.php:6: Line exceeds 80 characters; contains 84 characters
  +3 issues omitted

• [Style] Alphabetically sorted uses:
  app/Providers/EventServiceProvider.php:6: Use statements should be sorted alphabetically. The first wrong one is Illuminate\Auth\Events\Registered.
  app/Providers/RouteServiceProvider.php:6: Use statements should be sorted alphabetically. The first wrong one is Illuminate\Foundation\Support\Providers\RouteServiceProvider.
  app/User.php:6: Use statements should be sorted alphabetically. The first wrong one is Illuminate\Contracts\Auth\MustVerifyEmail.
  +5 issues omitted

• [Style] Unused uses:
  app/Providers/AuthServiceProvider.php:5: Type Illuminate\Support\Facades\Gate is not used in this file.
  app/Providers/EventServiceProvider.php:5: Type Illuminate\Support\Facades\Event is not used in this file.
  app/User.php:6: Type Illuminate\Contracts\Auth\MustVerifyEmail is not used in this file.

✨ See something that needs to be improved? Create an issue or send us a pull request: https://github.com/nunomaduro/phpinsights

至目前階段,在Laravel 安裝後直接檢測,會出現一些警示 Useless overriding method, Unused parameter, Empty comment, Disallow mixed type hint, Type hint declaration, Static closure

這方面,目前預計在官方會有後續計畫,在Laravel 專案預設會忽略掉這些內容。

指定記憶體

執行過程可能需要較大的系統記憶體用量時,可以透過以下方式分配

php -d memory_limit=2000M ./vendor/bin/phpinsights