Potherca
10/8/2017 - 2:21 PM

Code complexity reports for the comparison of various styles of writing a `clamp` function in PHP Raw Raw

Code complexity reports for the comparison of various styles of writing a clamp function in PHP Raw Raw

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>PhpMetrics report</title>
  <meta content="width=device-width, initial-scale=1" name="viewport">
  <link
    crossorigin="anonymous"
    href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.5.3/css/bulma.min.css"
    integrity="sha256-spCEAaZMKebC3rE/ZTt8jITn65b0Zan45WXblWjyDyQ="
    rel="stylesheet"
  />
  <link
    crossorigin="anonymous"
    href="https://rawgit.com/tristen/tablesort/8fac56d4/tablesort.css"
    integrity="sha384-fEamSb6ckugl8gVUnIx3yq0S1UbtJ4am10TSni/9LZQuOJowf18Ian7zELfKqw1N"
    rel="stylesheet"
  />
  <style>.content {margin: 1em 3em;}</style>
</head>
<body class="content has-text-centered">
  <h1>Code Metrics</h1>
  <h2>
    <svg height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
      <defs><path d="M0 0h24v24H0z" id="a"></path></defs>
      <clippath id="b"><use overflow="visible" xlink:href="#a"></use></clippath>
      <path clip-path="url(#b)" d="M11 5.08V2c-5 .5-9 4.81-9 10s4 9.5 9 10v-3.08c-3-.48-6-3.4-6-6.92s3-6.44 6-6.92zM18.97 11H22c-.47-5-4-8.53-9-9v3.08C16 5.51 18.54 8 18.97 11zM13 18.92V22c5-.47 8.53-4 9-9h-3.03c-.43 3-2.97 5.49-5.97 5.92z"></path>
    </svg>
    Size &amp; volume
  </h2>
  <table class="js-sort-table table is-bordered is-striped is-narrow is-hoverable is-fullwidth" id="table-length">
    <thead>
      <tr>
        <th>Class</th>
        <th class="js-sort-number">LLOC</th>
        <th class="js-sort-number">CLOC</th>
        <th class="js-sort-number">Volume</th>
        <th class="js-sort-number">Intelligent content</th>
        <th class="js-sort-number">Comment Weight</th>
      </tr>
    </thead>
    <tr>
      <td>clamp_832B9C44</td>
      <td>8</td>
      <td>0</td>
      <td>14</td>
      <td>14</td>
      <td>0</td>
    </tr>
    <tr>
      <td>clamp_CAB599E9</td>
      <td>14</td>
      <td>0</td>
      <td>44.92</td>
      <td>6.74</td>
      <td>0</td>
    </tr>
    <tr>
      <td>clamp_8389C469</td>
      <td>13</td>
      <td>0</td>
      <td>54</td>
      <td>5.4</td>
      <td>0</td>
    </tr>
    <tr>
      <td>clamp_83C742FF</td>
      <td>14</td>
      <td>0</td>
      <td>47.73</td>
      <td>7.16</td>
      <td>0</td>
    </tr>
    <tr>
      <td>clamp_60AA891E</td>
      <td>8</td>
      <td>0</td>
      <td>33.6</td>
      <td>6.72</td>
      <td>0</td>
    </tr>
    <tr>
      <td>clamp_7BC1F85B</td>
      <td>14</td>
      <td>0</td>
      <td>57</td>
      <td>5.7</td>
      <td>0</td>
    </tr>
    <tr>
      <td>clamp_A13E087E</td>
      <td>14</td>
      <td>0</td>
      <td>47.73</td>
      <td>7.16</td>
      <td>0</td>
    </tr>
  </table>
  <h2>
    <svg height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
    <path d="M0 0h24v24H0V0z" fill="none"></path>
    <path d="M19.07 4.93l-1.41 1.41C19.1 7.79 20 9.79 20 12c0 4.42-3.58 8-8 8s-8-3.58-8-8c0-4.08 3.05-7.44 7-7.93v2.02C8.16 6.57 6 9.03 6 12c0 3.31 2.69 6 6 6s6-2.69 6-6c0-1.66-.67-3.16-1.76-4.24l-1.41 1.41C15.55 9.9 16 10.9 16 12c0 2.21-1.79 4-4 4s-4-1.79-4-4c0-1.86 1.28-3.41 3-3.86v2.14c-.6.35-1 .98-1 1.72 0 1.1.9 2 2 2s2-.9 2-2c0-.74-.4-1.38-1-1.72V2h-1C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10c0-2.76-1.12-5.26-2.93-7.07z"></path>
    </svg>
    Complexity &amp; defects
  </h2>
  <table class="js-sort-table table is-bordered is-striped is-narrow is-hoverable is-fullwidth" id="table-length">
    <thead>
      <tr>
        <th>Class</th>
        <th class="js-sort-number">Class cycl.</th>
        <th class="js-sort-number">Max method cycl.</th>
        <th class="js-sort-number">Relative system complexity</th>
        <th class="js-sort-number">Relative data complexity</th>
        <th class="js-sort-number">Relative structural complexity</th>
        <th class="js-sort-number">Bugs</th>
        <th class="js-sort-number">Defects</th>
      </tr>
    </thead>
    <tr>
      <td>clamp_832B9C44</td>
      <td>1</td>
      <td>1</td>
      <td>4</td>
      <td>4</td>
      <td>0</td>
      <td>0</td>
      <td>0.15</td>
    </tr>
    <tr>
      <td>clamp_CAB599E9</td>
      <td>2</td>
      <td>2</td>
      <td>6</td>
      <td>6</td>
      <td>0</td>
      <td>0.01</td>
      <td>0.22</td>
    </tr>
    <tr>
      <td>clamp_8389C469</td>
      <td>2</td>
      <td>2</td>
      <td>4</td>
      <td>4</td>
      <td>0</td>
      <td>0.02</td>
      <td>0.22</td>
    </tr>
    <tr>
      <td>clamp_83C742FF</td>
      <td>3</td>
      <td>3</td>
      <td>6</td>
      <td>6</td>
      <td>0</td>
      <td>0.02</td>
      <td>0.29</td>
    </tr>
    <tr>
      <td>clamp_60AA891E</td>
      <td>1</td>
      <td>1</td>
      <td>4</td>
      <td>4</td>
      <td>0</td>
      <td>0.01</td>
      <td>0.15</td>
    </tr>
    <tr>
      <td>clamp_7BC1F85B</td>
      <td>3</td>
      <td>3</td>
      <td>4</td>
      <td>4</td>
      <td>0</td>
      <td>0.02</td>
      <td>0.29</td>
    </tr>
    <tr>
      <td>clamp_A13E087E</td>
      <td>3</td>
      <td>3</td>
      <td>6</td>
      <td>6</td>
      <td>0</td>
      <td>0.02</td>
      <td>0.29</td>
    </tr>
  </table>

  <h2>
    <svg height="24" viewbox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
      <path d="M0 0h24v24H0z" fill="none"></path>
      <path d="M17 6c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zM5 8c-2.21 0-4 1.79-4 4s1.79 4 4 4 4-1.79 4-4-1.79-4-4-4zm0 6c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2z"></path>
    </svg>
    Object oriented metrics
  </h2>
  <table class="js-sort-table table is-bordered is-striped is-narrow is-hoverable is-fullwidth" id="table-length">
    <thead>
      <tr>
        <th>Class</th>
        <th class="js-sort-number">LCOM</th>
        <th class="js-sort-number">Volume</th>
        <th class="js-sort-number">Class cycl.</th>
        <th class="js-sort-number">Max method cycl.</th>
        <th class="js-sort-number">Bugs</th>
        <th class="js-sort-number">Difficulty</th>
      </tr>
    </thead>
    <tr>
      <td>clamp_832B9C44</td>
      <td>1</td>
      <td>14</td>
      <td>1</td>
      <td>1</td>
      <td>0</td>
      <td>1</td>
    </tr>
    <tr>
      <td>clamp_CAB599E9</td>
      <td>1</td>
      <td>44.92</td>
      <td>2</td>
      <td>2</td>
      <td>0.01</td>
      <td>6.67</td>
    </tr>
    <tr>
      <td>clamp_8389C469</td>
      <td>1</td>
      <td>54</td>
      <td>2</td>
      <td>2</td>
      <td>0.02</td>
      <td>10</td>
    </tr>
    <tr>
      <td>clamp_83C742FF</td>
      <td>1</td>
      <td>47.73</td>
      <td>3</td>
      <td>3</td>
      <td>0.02</td>
      <td>6.67</td>
    </tr>
    <tr>
      <td>clamp_60AA891E</td>
      <td>1</td>
      <td>33.6</td>
      <td>1</td>
      <td>1</td>
      <td>0.01</td>
      <td>5</td>
    </tr>
    <tr>
      <td>clamp_7BC1F85B</td>
      <td>1</td>
      <td>57</td>
      <td>3</td>
      <td>3</td>
      <td>0.02</td>
      <td>10</td>
    </tr>
    <tr>
      <td>clamp_A13E087E</td>
      <td>1</td>
      <td>47.73</td>
      <td>3</td>
      <td>3</td>
      <td>0.02</td>
      <td>6.67</td>
    </tr>
  </table>
  <p class="is-pulled-right	">Created with <a href="https://www.phpmetrics.org/">PHPMetrics</a></p>

<script
  crossorigin="anonymous"
  integrity="sha256-9K8PNtEmsUnNmfTBL9Qn58jQokYgaReYVbONQ+VEhaM="
  src="https://cdnjs.cloudflare.com/ajax/libs/tablesort/5.0.1/tablesort.min.js"
></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tablesort/5.0.1/sorts/tablesort.number.min.js" integrity="sha256-iN7KurZsC57TrLxXl68DsJd47XVqpfCZDAFexSr619Q=" crossorigin="anonymous"></script>
<script>
    Array.from(document.getElementsByTagName('table')).forEach(function(p_oTable){new Tablesort(p_oTable);})
</script>
</body>
</html>

📝 This text is part of the article Comparison of various styles of writing a clamp function in PHP

Summary

Code complexity report was created using PHP Metrics.

The final result was:

subjectcyclo.Relative system complexityBugsDefectsVolumeDifficulty
832B9C441400.15141
60AA891E140.010.1533.65
CAB599E9260.010.2244.926.67
8389C469240.020.225410
83C742FF360.020.2947.736.67
A13E087E360.020.2947.736.67
7BC1F85B340.020.295710

A more complete report has been added to this gist, it can be viewed here.

Steps taken

The following steps were taken in order to create code-complexity reports for the clamp variations:

  1. Create separate classes for each variation
  2. Run phpmetrics
  3. Grab data from the relevant reports

Each step is described in detail below.

1. Create separate classes for each variation

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)
    {
        // ...
    }
}

2. Run phpmetrics

PhpMetrics was run using the following command:

phpmetrics --report-html=phpmetrics ./

This generated various metrics in a directory called phpmetrics.

3. Grab data from the relevant reports

The reports we are interested in are " Complexity & defects" and " Object oriented metrics"

Combining the result from these section we get:

subjectcyclo.Relative system complexityBugsDefectsVolumeDifficulty
832B9C441400.15141
60AA891E140.010.1533.65
CAB599E9260.010.2244.926.67
8389C469240.020.225410
7BC1F85B340.020.295710
83C742FF360.020.2947.736.67
A13E087E360.020.2947.736.67