原文來自:https://www.codecasts.com/blo...php
若是說你想知道一個 PHP 項目的代碼複雜度是什麼樣子的,我推薦你能夠使用 phploc 和 PhpMetrics 來檢測一下。html
這是一個標準的 composer package,不過我推薦你們能夠直接使用 composer 全局安裝:git
composer global require 'phploc/phploc=*'
而後安裝完畢,你就能夠使用 phploc
命令來檢測你的代碼複雜度了:github
phploc ./app
好比上面這行代碼就是檢測你的項目中 app/
目錄的代碼複雜度;若是是一個 Laravel 的項目的話,大概會是這個樣子的結果輸出:app
phploc 3.0.1 by Sebastian Bergmann. Directories 14 Files 72 Size Lines of Code (LOC) 3748 Comment Lines of Code (CLOC) 790 (21.08%) Non-Comment Lines of Code (NCLOC) 2958 (78.92%) Logical Lines of Code (LLOC) 950 (25.35%) Classes 656 (69.05%) Average Class Length 9 Minimum Class Length 0 Maximum Class Length 84 Average Method Length 2 Minimum Method Length 0 Maximum Method Length 21 Functions 0 (0.00%) Average Function Length 0 Not in classes or functions 294 (30.95%) Cyclomatic Complexity Average Complexity per LLOC 0.10 Average Complexity per Class 2.33 Minimum Class Complexity 1.00 Maximum Class Complexity 15.00 Average Complexity per Method 1.41 Minimum Method Complexity 1.00 Maximum Method Complexity 6.00 Dependencies Global Accesses 0 Global Constants 0 (0.00%) Global Variables 0 (0.00%) Super-Global Variables 0 (0.00%) Attribute Accesses 436 Non-Static 436 (100.00%) Static 0 (0.00%) Method Calls 570 Non-Static 412 (72.28%) Static 158 (27.72%) Structure Namespaces 15 Interfaces 0 Traits 0 Classes 72 Abstract Classes 0 (0.00%) Concrete Classes 72 (100.00%) Methods 233 Scope Non-Static Methods 226 (97.00%) Static Methods 7 (3.00%) Visibility Public Methods 194 (83.26%) Non-Public Methods 39 (16.74%) Functions 24 Named Functions 0 (0.00%) Anonymous Functions 24 (100.00%) Constants 0 Global Constants 0 (0.00%) Class Constants 0 (0.00%)
不過你可能也感受到,這個 phploc 的一大不便之處就是,目前來講,他還不能把相關的測試結果可視化或者說自定義檢測的最高複雜度。因此,PhpMetrics 就應運而生了。composer
首先須要說明的是,PhpMetrics 能夠更深刻到你的代碼中,而且會生成一個 html 文件做爲分析的結果,這樣咱們查看檢測結果就會很是的直觀。post
安裝 PhpMetrics 也是能夠直接 composer 全局安裝:測試
composer global require 'phpmetrics/phpmetrics'
安裝完畢以後,能夠這樣來運行命令分析代碼複雜度:ui
phpmetrics --report-html=report.html ./app
等待 phpmetrics
運行結束,用 Chrome 打開 report.html
就能夠查看相對應的結果,大概是這個樣子:spa