Analyzing PHP Code

There are a number of ways to analyze php code to get a better feel for how complex it is and clues for what it may take to work on it. For example the following linux shell command will tell you how many lines of php code there is in a given folder.

find -type f \( -iname "*.php" -o -iname "*.html" \) -exec wc -l {} \; | awk '{lines += $1 ; files += 1 ; print }; END { print "Lines total is: ", lines ," in ", files ," files"}'

Another option is using a tool such as phploc, which makes it easy and quick to measure the size and analyze the structure of a php project. It will tell you things like number of lines of code, how many functions you have, how many classes, what percentage of your code is comments vs plain code, etc. It's also ran on the command line like this:

$ php phploc.php some/folder/

Here is what the output might look like:

Directories                                          3Files                                                8Size  Lines of Code (LOC)                             1858  Comment Lines of Code (CLOC)                     560 (30.14%)  Non-Comment Lines of Code (NCLOC)               1298 (69.86%)  Logical Lines of Code (LLOC)                     289 (15.55%)    Classes                                        260 (89.97%)      Average Class Length                          37      Average Method Length                          9    Functions                                        5 (1.73%)      Average Function Length                        5    Not in classes or functions                     24 (8.30%)Complexity  Cyclomatic Complexity / LLOC                    0.67  Cyclomatic Complexity / Number of Methods       7.86Dependencies  Global Accesses                                    2    Global Constants                                 2 (100.00%)    Global Variables                                 0 (0.00%)    Super-Global Variables                           0 (0.00%)  Attribute Accesses                                48    Non-Static                                      48 (100.00%)    Static                                           0 (0.00%)  Method Calls                                      96    Non-Static                                      91 (94.79%)    Static                                           5 (5.21%)Structure  Namespaces                                         4  Interfaces                                         0  Traits                                             0  Classes                                            7    Abstract Classes                                 0 (0.00%)    Concrete Classes                                 7 (100.00%)  Methods                                           28    Scope      Non-Static Methods                            28 (100.00%)      Static Methods                                 0 (0.00%)    Visibility      Public Method                                 10 (35.71%)      Non-Public Methods                            18 (64.29%)  Functions                                          1    Named Functions                                  0 (0.00%)    Anonymous Functions                              1 (100.00%)  Constants                                          1    Global Constants                                 1 (100.00%)    Class Constants                                  0 (0.00%)

Tags: Linux, PHP