📝 This text is part of the article Comparison of various styles of writing a clamp
function in PHP
Code complexity report was created using PHP Metrics.
The final result was:
subject | cyclo. | Relative system complexity | Bugs | Defects | Volume | Difficulty |
---|---|---|---|---|---|---|
832B9C44 | 1 | 4 | 0 | 0.15 | 14 | 1 |
60AA891E | 1 | 4 | 0.01 | 0.15 | 33.6 | 5 |
CAB599E9 | 2 | 6 | 0.01 | 0.22 | 44.92 | 6.67 |
8389C469 | 2 | 4 | 0.02 | 0.22 | 54 | 10 |
83C742FF | 3 | 6 | 0.02 | 0.29 | 47.73 | 6.67 |
A13E087E | 3 | 6 | 0.02 | 0.29 | 47.73 | 6.67 |
7BC1F85B | 3 | 4 | 0.02 | 0.29 | 57 | 10 |
A more complete report has been added to this gist, it can be viewed here.
The following steps were taken in order to create code-complexity reports for the clamp
variations:
- Create separate classes for each variation
- Run
phpmetrics
- Grab data from the relevant reports
Each step is described in detail below.
As PHP Metrics scans classes, not functions, a separate class was created for each clamp function:
<?php
class clamp_0ABCDEF1
{
public function clamp($value, $min, $max)
{
// ...
}
}
PhpMetrics was run using the following command:
phpmetrics --report-html=phpmetrics ./
This generated various metrics in a directory called phpmetrics
.
The reports we are interested in are " Complexity & defects" and " Object oriented metrics"
Combining the result from these section we get:
subject | cyclo. | Relative system complexity | Bugs | Defects | Volume | Difficulty |
---|---|---|---|---|---|---|
832B9C44 | 1 | 4 | 0 | 0.15 | 14 | 1 |
60AA891E | 1 | 4 | 0.01 | 0.15 | 33.6 | 5 |
CAB599E9 | 2 | 6 | 0.01 | 0.22 | 44.92 | 6.67 |
8389C469 | 2 | 4 | 0.02 | 0.22 | 54 | 10 |
7BC1F85B | 3 | 4 | 0.02 | 0.29 | 57 | 10 |
83C742FF | 3 | 6 | 0.02 | 0.29 | 47.73 | 6.67 |
A13E087E | 3 | 6 | 0.02 | 0.29 | 47.73 | 6.67 |