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 |
||
| 9 | class CLog |
||
| 10 | { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Properties |
||
| 14 | * |
||
| 15 | */ |
||
| 16 | private $timestamp = array(); |
||
| 17 | private $pos = 0; |
||
| 18 | private $precision; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Constructor |
||
| 22 | * |
||
| 23 | * @param int $precision the precision of the rounding when presenting results. |
||
| 24 | */ |
||
| 25 | 4 | public function __construct($precision = 4) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Timestamp, log a event with a time. |
||
| 32 | * |
||
| 33 | * @param string $domain is the module or class. |
||
| 34 | * @param string $where a more specific place in the domain. |
||
| 35 | * @param string $comment on the timestamp. |
||
| 36 | * |
||
| 37 | */ |
||
| 38 | 4 | public function timestamp($domain, $where, $comment = null) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Print all timestamp to a table. |
||
| 60 | * |
||
| 61 | * @return string with a html-table to display all timestamps. |
||
| 62 | * |
||
| 63 | */ |
||
| 64 | 1 | public function timestampAsTable() |
|
| 65 | { |
||
| 66 | |||
| 67 | // Set up the table |
||
| 68 | 1 | $first = $this->timestamp[0]['when']; |
|
| 69 | $last = $this->timestamp[count($this->timestamp) - 1]['when']; |
||
| 70 | 1 | $html = "<table class='logtable'><caption>Timestamps</caption><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>"; |
|
| 71 | 1 | $right = ' style="text-align: right;"'; |
|
| 72 | 1 | $total = array('domain' => array(), 'where' => array()); |
|
| 73 | |||
| 74 | 1 | $totalDuration = $last - $first; |
|
| 75 | |||
| 76 | // Create the main table |
||
| 77 | 1 | foreach ($this->timestamp as $val) { |
|
| 78 | $when = $val['when'] - $first; |
||
| 79 | $duration = round($val['duration'], $this->precision); |
||
| 80 | $percent = round(($when) / $totalDuration * 100); |
||
| 81 | $memory = round($val['memory'] / 1024 / 1024, 2); |
||
| 82 | $peak = isset($val['memory-peak']) ? round($val['memory-peak'] / 1024 / 1024, 2): 0; |
||
| 83 | $when = round($when, $this->precision); |
||
| 84 | $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>"; |
||
| 85 | @$total['domain'][$val['domain']] += $val['duration']; |
||
|
|
|||
| 86 | @$total['where'][$val['where']] += $val['duration']; |
||
| 87 | } |
||
| 88 | |||
| 89 | 1 | $html .= "</table><br><table class='logtable'><caption>Duration per domain</caption><tr><th>Domain</th><th>Duration</th><th>Percent</th></tr>"; |
|
| 90 | |||
| 91 | |||
| 92 | // Create the table grouped by domain |
||
| 93 | arsort($total['domain']); |
||
| 94 | |||
| 95 | 1 | View Code Duplication | foreach ($total['domain'] as $key => $val) { |
| 96 | if ($totalDuration != 0) { |
||
| 97 | $percent = round($val / $totalDuration * 100, 1); |
||
| 98 | } else { |
||
| 99 | $percent = 100; |
||
| 100 | } |
||
| 101 | |||
| 102 | $roundedDuration = round($val, $this->precision); |
||
| 103 | $html .= "<tr><td>{$key}</td><td>{$roundedDuration}</td><td>{$percent}</td></tr>"; |
||
| 104 | } |
||
| 105 | |||
| 106 | 1 | $html .= "</table><br><table class='logtable'><caption>Duration per area</caption><tr><th>Area</th><th>Duration</th><th>Percent</th></tr>"; |
|
| 107 | |||
| 108 | // Create the table grouped by area |
||
| 109 | arsort($total['where']); |
||
| 110 | |||
| 111 | 1 | View Code Duplication | foreach ($total['where'] as $key => $val) { |
| 112 | if ($totalDuration != 0) { |
||
| 113 | $percent = round($val / $totalDuration * 100, 1); |
||
| 114 | } else { |
||
| 115 | $percent = 100; |
||
| 116 | } |
||
| 117 | |||
| 118 | $roundedDuration = round($val, $this->precision); |
||
| 119 | $html .= "<tr><td>{$key}</td><td>{$roundedDuration}</td><td>{$percent}</td></tr>"; |
||
| 120 | } |
||
| 121 | |||
| 122 | 1 | $html .= "</table>"; |
|
| 123 | |||
| 124 | 1 | return $html; |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Returns load time of the page |
||
| 129 | * |
||
| 130 | * @return The page load time. |
||
| 131 | * |
||
| 132 | */ |
||
| 133 | 1 | public function pageLoadTime() |
|
| 134 | { |
||
| 135 | 1 | $first = $this->timestamp[0]['when']; |
|
| 136 | $last = $this->timestamp[count($this->timestamp) - 1]['when']; |
||
| 137 | $loadtime = round($last - $first, 3); |
||
| 138 | 1 | return $loadtime; |
|
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get the memory peak. |
||
| 143 | * |
||
| 144 | * @return The memory peak. |
||
| 145 | * |
||
| 146 | */ |
||
| 147 | 1 | public function memoryPeak() |
|
| 148 | { |
||
| 149 | $peak = round(memory_get_peak_usage(true) / 1024 / 1024, 2); |
||
| 150 | 1 | return $peak; |
|
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Get the number of timestamps made so far. |
||
| 155 | * |
||
| 156 | * @return Number of timestamps. |
||
| 157 | * |
||
| 158 | */ |
||
| 159 | public function numberOfTimestamps() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get the most time consuming domain. |
||
| 166 | * |
||
| 167 | * @return The most time consuming domain. |
||
| 168 | * |
||
| 169 | */ |
||
| 170 | 1 | public function mostTimeConsumingDomain() |
|
| 192 | } |
||
| 193 |
If you suppress an error, we recommend checking for the error condition explicitly: