Taylor Otwell recently made a post comparing the code complexity between Laravel and other frameworks. The tool he used to generate these reports is called phploc and it’s very easy to run on your own code base.
I decided as a means of comparison I would run that on the codebase for this site and just see what the results are.
phploc
First, install the composer package either your require-dev or globally:
1composer global require 'phploc/phploc=*'
Next, just cd into your application and run the phploc command:
1phploc ./app
Then it spits out the results. Here is what the app
directory for Laravel News looks like:
1phploc 3.0.1 by Sebastian Bergmann. 2 3Directories 14 4Files 72 5 6Size 7 Lines of Code (LOC) 3748 8 Comment Lines of Code (CLOC) 790 (21.08%) 9 Non-Comment Lines of Code (NCLOC) 2958 (78.92%)10 Logical Lines of Code (LLOC) 950 (25.35%)11 Classes 656 (69.05%)12 Average Class Length 913 Minimum Class Length 014 Maximum Class Length 8415 Average Method Length 216 Minimum Method Length 017 Maximum Method Length 2118 Functions 0 (0.00%)19 Average Function Length 020 Not in classes or functions 294 (30.95%)2122Cyclomatic Complexity23 Average Complexity per LLOC 0.1024 Average Complexity per Class 2.3325 Minimum Class Complexity 1.0026 Maximum Class Complexity 15.0027 Average Complexity per Method 1.4128 Minimum Method Complexity 1.0029 Maximum Method Complexity 6.003031Dependencies32 Global Accesses 033 Global Constants 0 (0.00%)34 Global Variables 0 (0.00%)35 Super-Global Variables 0 (0.00%)36 Attribute Accesses 43637 Non-Static 436 (100.00%)38 Static 0 (0.00%)39 Method Calls 57040 Non-Static 412 (72.28%)41 Static 158 (27.72%)4243Structure44 Namespaces 1545 Interfaces 046 Traits 047 Classes 7248 Abstract Classes 0 (0.00%)49 Concrete Classes 72 (100.00%)50 Methods 23351 Scope52 Non-Static Methods 226 (97.00%)53 Static Methods 7 (3.00%)54 Visibility55 Public Methods 194 (83.26%)56 Non-Public Methods 39 (16.74%)57 Functions 2458 Named Functions 0 (0.00%)59 Anonymous Functions 24 (100.00%)60 Constants 061 Global Constants 0 (0.00%)62 Class Constants 0 (0.00%)
The one downside to this tool is I was unable to have it give me the file name for the maximum cyclomatic complexity or the maximum method length. Another tool named PhpMetrics can be used to help you find these and let’s look at how it works.
PhpMetrics
Another option if you want even more reporting and dig deeper into your codebase is PhpMetrics. It creates a nice HTML-based report complete with graphs and file-based reports.
Installing it is just as easy thanks to Composer:
1composer global require 'phpmetrics/phpmetrics'
Then, run the following to parse your app folder:
1phpmetrics --report-html=myreport.html ./app
After that runs, just open myreport.html in the browser and you can browse through its results.
Wrap Up
These are just two tools but many others exist and the two other most popular ones are phpmd, and php depend.
What I find great about these is these are not just vanity metrics but give you real insight on what part of your code base needs improvements and what may cause you problems later.
Filed in: