phploc can be used to track project size metrics, such as Lines of Code (LOC), over time. Example 4-2 shows how to invoke it from our Apache Ant build script.
Example 4-2. The phploc build target for PHPLOC
<target name="phploc" description="Measure project size using PHPLOC"> <exec executable="phploc"> <arg value="--log-csv" /> <arg value="${basedir}/build/logs/phploc.csv" /> <arg path="${basedir}/src" /> </exec> </target>
In the above, we let PHPLOC write its data to a CSV file. The data from that file can be plotted using Jenkins' Plot plugin.
As software developers our focus is generally not on the external aspects of software quality such as functionality and usability. Instead we care about the internal aspects of software quality. This means that we are interested in readable code that is easy to understand, adapt, and extend. Implementing new features (or changing existing ones) will become more and more difficult and thus expensive over time if the internal quality of the software is neglected.
Internal software quality is measured through software metrics. A software metric is, in general, a function that maps a software unit onto a numeric value. The Cyclomatic Complexity and NPath Complexity software metrics measure, for instance, the complexity of a unit of code.
The cyclomatic complexity is the number of possible decision paths in a unit of code, usually a method or class. It is calculated by counting the control structures and boolean operators in a program unit and represents the structural complexity of a program unit. The idea is that a sequence of commands is easier to understand than a branch in the control flow.
A large cyclomatic complexity indicates that a program unit is susceptible for defects and hard to test. The more execution paths a program unit has, the more tests are required. The NPath complexity counts the number of acyclic execution paths and provides a lower bound for the amount of unit testing required for the unit of code.
PHP_Depend is the tool of choice to calculate a wide variety of software metrics for PHP code. Example 4-3 shows how to invoke it from our Apache Ant build script.
Example 4-3. The pdepend build target for PHP_Depend
<target name="pdepend" description="Calculate software metrics using PHP_Depend"> <exec executable="pdepend"> <arg value="--jdepend-xml=${basedir}/build/logs/jdepend.xml" /> <arg value="--jdepend-chart=${basedir}/build/pdepend/dependencies.svg" /> <arg value="--overview-pyramid=${basedir}/build/pdepend/overview-pyramid.svg" /> <arg path="${basedir}/src" /> </exec> </target>
Figure 4-1 shows how to configure a post-build action in our Jenkins job to publish the results of the analysis performed by PHP_Depend.
Figure 4-2 and Figure 4-3 show examples of the visualizations generated by PHP_Depend. These can be displayed on the project overview page of Jenkins by putting the HTML code below into the project description:
<embed height="300" src="http://localhost:8080/job/job-name/ws/build/pdepend/overview-pyramid.svg" type="image/svg+xml" width="500"/> <embed height="300" src="http://localhost:8080/job/job-name/ws/build/pdepend/dependencies.svg" type="image/svg+xml" width="500"/>
In the above, you need to replace job-name
with the name of the Jenkins job. In
our example this would be bankaccount
.