Completed
Pull Request — master (#37)
by
unknown
03:17
created

Sequence   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 8.33 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 6
loc 72
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 23 4
A renderAscii() 0 9 2
B _tplAscii() 6 28 6

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 JMathai\PhpMultiCurl;
3
/**
4
 * Sequence displays sequence of http calls
5
 *
6
 * @author Jaisen Mathai <[email protected]>
7
 */
8
class Sequence
9
{
10
    private $_width = 100;
11
    private $_timers;
12
    private $_min;
13
    private $_max;
14
    private $_range;
15
    private $_step;
16
17
    public function __construct($timers) 
18
    {
19
        $this->_timers = $timers;
20
    
21
        $min = PHP_INT_MAX;
22
        $max = 0;
23
        foreach ($this->_timers as $timer) {
24
            if (!isset($timer['start'])) {
25
                $timer['start'] = PHP_INT_MAX;
26
            }
27
28
            if (!isset($timer['end'])) {
29
                $timer['end'] = 0;
30
            }
31
32
            $min = min($timer['start'], $min);
33
            $max = max($timer['end'], $max);
34
        }
35
        $this->_min = $min;
36
        $this->_max = $max;
37
        $this->_range = $max-$min;
38
        $this->_step = floatval($this->_range/$this->_width);
39
    }
40
41
    public function renderAscii()
42
    {
43
        $tpl = '';
44
        foreach ($this->_timers as $timer) {
45
            $tpl .= $this->_tplAscii($timer);
46
        }
47
    
48
        return $tpl;
49
    }
50
51
    private function _tplAscii($timer)
52
    {
53
        $lpad = $rpad = 0;
54
        $lspace = $chars = $rspace = '';
55 View Code Duplication
        if ($timer['start'] > $this->_min) {
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...
56
            $lpad = intval(($timer['start'] - $this->_min) / $this->_step);
57
        }
58 View Code Duplication
        if ($timer['end'] < $this->_max) {
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...
59
            $rpad = intval(($this->_max - $timer['end']) / $this->_step);
60
        }
61
        $mpad = $this->_width - $lpad - $rpad;
62
        if ($lpad > 0) {
63
            $lspace = str_repeat(' ', $lpad);
64
        }
65
        if ($mpad > 0) {
66
            $chars = str_repeat('=', $mpad);
67
        }
68
        if ($rpad > 0) {
69
            $rspace = str_repeat(' ', $rpad);
70
        }
71
    
72
        $tpl = <<<TPL
73
({$timer['api']} ::  code={$timer['code']}, start={$timer['start']}, end={$timer['end']}, total={$timer['time']})
74
[{$lspace}{$chars}{$rspace}]
75
76
TPL;
77
        return $tpl;
78
    }
79
}
80