Checking the Code Complexity of your App

Developer Tools

January 10th, 2017

CyclomaticComplexity.png

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 9
13 Minimum Class Length 0
14 Maximum Class Length 84
15 Average Method Length 2
16 Minimum Method Length 0
17 Maximum Method Length 21
18 Functions 0 (0.00%)
19 Average Function Length 0
20 Not in classes or functions 294 (30.95%)
21
22Cyclomatic Complexity
23 Average Complexity per LLOC 0.10
24 Average Complexity per Class 2.33
25 Minimum Class Complexity 1.00
26 Maximum Class Complexity 15.00
27 Average Complexity per Method 1.41
28 Minimum Method Complexity 1.00
29 Maximum Method Complexity 6.00
30
31Dependencies
32 Global Accesses 0
33 Global Constants 0 (0.00%)
34 Global Variables 0 (0.00%)
35 Super-Global Variables 0 (0.00%)
36 Attribute Accesses 436
37 Non-Static 436 (100.00%)
38 Static 0 (0.00%)
39 Method Calls 570
40 Non-Static 412 (72.28%)
41 Static 158 (27.72%)
42
43Structure
44 Namespaces 15
45 Interfaces 0
46 Traits 0
47 Classes 72
48 Abstract Classes 0 (0.00%)
49 Concrete Classes 72 (100.00%)
50 Methods 233
51 Scope
52 Non-Static Methods 226 (97.00%)
53 Static Methods 7 (3.00%)
54 Visibility
55 Public Methods 194 (83.26%)
56 Non-Public Methods 39 (16.74%)
57 Functions 24
58 Named Functions 0 (0.00%)
59 Anonymous Functions 24 (100.00%)
60 Constants 0
61 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:

Eric L. Barnes

Eric is the creator of Laravel News and has been covering Laravel since 2012.