MultiCurlSequence   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 6.15 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

3 Methods

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