CLog   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 184
Duplicated Lines 10.87 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 72.09%

Importance

Changes 3
Bugs 2 Features 2
Metric Value
dl 20
loc 184
ccs 31
cts 43
cp 0.7209
rs 10
c 3
b 2
f 2
wmc 17
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A timestamp() 0 19 2
A numberOfTimestamps() 0 4 1
B timestampAsTable() 20 62 7
B mostTimeConsumingDomain() 0 22 4
A pageLoadTime() 0 7 1
A memoryPeak() 0 5 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
3
namespace ultimadark\Logger;
4
5
/**
6
 * Class to log what happens.
7
 *
8
 */
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)
26
    {
27 4
        $this->precision = $precision;
28
    }
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)
39
    {
40
        $now = microtime(true);
41
        $this->timestamp[] = array(
42
          'domain'  => $domain,
43
          'where'   => $where,
44
          'comment' => $comment,
45
          'when'    => $now,
46
          'memory'  => memory_get_usage(true),
47
          'duration'=> 0,
48
        );
49
50
        if ($this->pos > 0) {
51
            $this->timestamp[$this->pos - 1]['memory-peak'] = memory_get_peak_usage(true);
52 4
            $this->timestamp[$this->pos - 1]['duration']    = $now - $this->timestamp[$this->pos - 1]['when'];
53
        }
54
    
55
        $this->pos++;
56
    }
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'];
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...
86
            @$total['where'][$val['where']] += $val['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...
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) {
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...
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) {
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...
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()
160
    {
161
        return count($this->timestamp);
162
    }
163
164
    /**
165
     * Get the most time consuming domain.
166
     *
167
     * @return The most time consuming domain.
168
     *
169
     */
170 1
    public function mostTimeConsumingDomain()
171
    {
172 1
        $total = array();
173
174
        // Gather total duration data of each domain
175 1
        foreach ($this->timestamp as $val) {
176
            $duration = round($val['duration'], $this->precision);
177
            @$total[$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...
178
        }
179
180
        // Get the key of the most time consuming element
181 1
        $mostConsumingTime = 0;
182 1
        $mostConsumingDomain = "";
183
        foreach ($total as $key => $val) {
184 1
            if ($val >= $mostConsumingTime) {
185 1
                $mostConsumingDomain = $key;
186 1
                $mostConsumingTime = $val;
187
            }
188
        }
189
190 1
        return $mostConsumingDomain;
191
    }
192
}
193