TLog   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 12.31 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 92.5%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 0
cbo 0
dl 8
loc 65
ccs 37
cts 40
cp 0.925
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C renderTimestampAsTable() 8 39 7
A nothingLogged() 0 4 1
A saySomething() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Mcknubb\Log;
3
/**
4
 * Tlog
5
 * Print table for CLog
6
 *
7
 */
8
class TLog
9
{
10
  /**
11
   * Print all timestamp to a table.
12
   *
13
   * @return string with a html-table to display all timestamps.
14
   *
15
   */
16 1
  public function renderTimestampAsTable($timestamps, $memoryPeak, $pageLoadTime) {
17 1
    $prev = $first = $timestamps[0]['when'];
0 ignored issues
show
Unused Code introduced by
$prev is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
18 1
    $last = $timestamps[count($timestamps) - 1]['when'];
19 1
    if($last === $first) {
20 1
      $last = microtime(true);
21
    }
22 1
    $html = "<div class='log' style='background-color:#fff; color:#000; margin-top:50px; padding:20px;'><table class=table><h2>Timestamps</h2><tr><th>Domain</th><th>Where</th><th>When (sec)</th><th>Duration (sec)</th><th>Percent</th><th>Memory (MB)</th><th>Memory peak (MB)</th><th>Comment</th></tr>";
23 1
    $right = ' style="text-align: right;"';
24 1
    $total = array('domain' => array(), 'where' => array());
25 1
    foreach($timestamps as $val) {
26 1
      $when     = $val['when'] - $first;
27 1
      $duration = isset($val['duration']) ?  round($val['duration'], 3) : null;
28 1
      $percent  = round(($when) / ($last - $first) * 100);
29 1
      $memory   = round($val['memory'] / 1024 / 1024, 2);
30 1
      $peak     = isset($val['memory-peak']) ? round($val['memory-peak'] / 1024 / 1024, 2) : NULL;
31 1
      $when     = round($when, 3);
32 1
      $html .= "<tr><td>{$val['domain']}</td><td>{$val['where']}</td><td{$right}>{$when}</td><td{$right}>{$duration}</td><td{$right}>{$percent}</td><td{$right}>{$memory}</td><td{$right}>{$peak}</td><td>{$val['comment']}</td></tr>";
33 1
      $prev = $val['when'];
0 ignored issues
show
Unused Code introduced by
$prev is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
34 1
      @$total['domain'][$val['domain']] += $duration;
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
35 1
      @$total['where'][$val['where']] += $duration;
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
36
    }
37 1
    $html .= "</table><table class=table><h2>Duration per domain</h2><tr><th>Domain</th><th>Duration</th><th>Percent</th></tr>";
38 1
    arsort($total['domain']);
39 1 View Code Duplication
    foreach($total['domain'] as $key => $val) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40 1
      $percent = round($val / ($last - $first) * 100, 1);
41 1
      $html .= "<tr><td>{$key}</td><td>{$val}</td><td>{$percent}</td></tr>";
42
    }
43 1
    $html .= "</table><table class=table><h2>Duration per area</h2><tr><th>Area</th><th>Duration</th><th>Percent</th></tr>";
44 1
    arsort($total['where']);
45 1 View Code Duplication
    foreach($total['where'] as $key => $val) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46 1
      $percent = round($val / ($last - $first) * 100, 1);
47 1
      $html .= "<tr><td>{$key}</td><td>{$val}</td><td>{$percent}</td></tr>";
48
    }
49 1
    $html .= "</table>";
50 1
    $html .= "<p>Peek memory consumption was {$memoryPeak} MB.</p>";
51 1
    $html .= "<p>Page was loaded in {$pageLoadTime} secs.</p>";
52 1
    $html .= "</div>";
53 1
    return $html;
54
  }
55
56
57
  /**
58
   *
59
   * @return String with information that no timestamps have been logged.
60
   */
61 1
  public function nothingLogged() {
62 1
    $html = "<p>No timestamps have been logged. Log is empty.</p>";
63 1
    return $html;
64
  }
65
66
  /*
67
   * Test if class is loaded
68
   */
69
  public function saySomething($word) {
70
      echo $word;
71
  }
72
} 
73