Passed
Push — master ( 8f5e6a...061d3e )
by Felipe
03:26
created

AccBarPlot   C

Complexity

Total Complexity 73

Size/Duplication

Total Lines 431
Duplicated Lines 24.83 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 107
loc 431
rs 5.5447
c 0
b 0
f 0
wmc 73
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 11 28 7
A Legend() 12 12 3
D Max() 12 43 9
C Min() 13 37 7
F Stroke() 59 297 47

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like AccBarPlot often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AccBarPlot, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Amenadiel\JpGraph\Plot;
3
4
//===================================================
5
// CLASS AccBarPlot
6
// Description: Produce accumulated bar plots
7
//===================================================
8
class AccBarPlot extends BarPlot
9
{
10
    public $plots     = null;
11
    private $nbrplots = 0;
12
    //---------------
13
    // CONSTRUCTOR
14
    public function __construct($plots)
15
    {
16
        $this->plots    = $plots;
17
        $this->nbrplots = count($plots);
18
        if ($this->nbrplots < 1) {
19
            Util\JpGraphError::RaiseL(2010); //('Cannot create AccBarPlot from empty plot array.');
20
        }
21 View Code Duplication
        for ($i = 0; $i < $this->nbrplots; ++$i) {
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...
22
            if (empty($this->plots[$i]) || !isset($this->plots[$i])) {
23
                Util\JpGraphError::RaiseL(2011, $i); //("Acc bar plot element nbr $i is undefined or empty.");
24
            }
25
        }
26
27
        // We can only allow individual plost which do not have specified X-positions
28 View Code Duplication
        for ($i = 0; $i < $this->nbrplots; ++$i) {
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...
29
            if (!empty($this->plots[$i]->coords[1])) {
30
                Util\JpGraphError::RaiseL(2015);
31
                //'Individual bar plots in an AccBarPlot or GroupBarPlot can not have specified X-positions.');
32
            }
33
        }
34
35
        // Use 0 weight by default which means that the individual bar
36
        // weights will be used per part n the accumulated bar
37
        $this->SetWeight(0);
38
39
        $this->numpoints = $plots[0]->numpoints;
40
        $this->value     = new DisplayValue();
41
    }
42
43
    //---------------
44
    // PUBLIC METHODS
45 View Code Duplication
    public function Legend($graph)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    {
47
        $n = count($this->plots);
48
        for ($i = $n - 1; $i >= 0; --$i) {
49
            $c = get_class($this->plots[$i]);
50
            if (!($this->plots[$i] instanceof BarPlot)) {
51
                Util\JpGraphError::RaiseL(2012, $c);
52
                //('One of the objects submitted to AccBar is not a BarPlot. Make sure that you create the AccBar plot from an array of BarPlot objects.(Class='.$c.')');
1 ignored issue
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
53
            }
54
            $this->plots[$i]->DoLegend($graph);
55
        }
56
    }
57
58
    public function Max()
59
    {
60
        list($xmax) = $this->plots[0]->Max();
61
        $nmax       = 0;
62 View Code Duplication
        for ($i = 0; $i < count($this->plots); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
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...
63
            $n       = count($this->plots[$i]->coords[0]);
64
            $nmax    = max($nmax, $n);
65
            list($x) = $this->plots[$i]->Max();
66
            $xmax    = max($xmax, $x);
67
        }
68
        for ($i = 0; $i < $nmax; $i++) {
69
            // Get y-value for bar $i by adding the
70
            // individual bars from all the plots added.
71
            // It would be wrong to just add the
72
            // individual plots max y-value since that
73
            // would in most cases give to large y-value.
74
            $y = 0;
75
            if (!isset($this->plots[0]->coords[0][$i])) {
76
                Util\JpGraphError::RaiseL(2014);
77
            }
78 View Code Duplication
            if ($this->plots[0]->coords[0][$i] > 0) {
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...
79
                $y = $this->plots[0]->coords[0][$i];
80
            }
81
82
            for ($j = 1; $j < $this->nbrplots; $j++) {
83
                if (!isset($this->plots[$j]->coords[0][$i])) {
84
                    Util\JpGraphError::RaiseL(2014);
85
                }
86 View Code Duplication
                if ($this->plots[$j]->coords[0][$i] > 0) {
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...
87
                    $y += $this->plots[$j]->coords[0][$i];
88
                }
89
            }
90
            $ymax[$i] = $y;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ymax was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ymax = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
91
        }
92
        $ymax = max($ymax);
0 ignored issues
show
Bug introduced by
The variable $ymax does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
93
94
        // Bar always start at baseline
95
        if ($ymax <= $this->ybase) {
96
            $ymax = $this->ybase;
97
        }
98
99
        return [$xmax, $ymax];
100
    }
101
102
    public function Min()
103
    {
104
        $nmax                 = 0;
105
        list($xmin, $ysetmin) = $this->plots[0]->Min();
106 View Code Duplication
        for ($i = 0; $i < count($this->plots); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
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...
107
            $n           = count($this->plots[$i]->coords[0]);
108
            $nmax        = max($nmax, $n);
109
            list($x, $y) = $this->plots[$i]->Min();
110
            $xmin        = Min($xmin, $x);
111
            $ysetmin     = Min($y, $ysetmin);
112
        }
113
        for ($i = 0; $i < $nmax; $i++) {
114
            // Get y-value for bar $i by adding the
115
            // individual bars from all the plots added.
116
            // It would be wrong to just add the
117
            // individual plots max y-value since that
118
            // would in most cases give to large y-value.
119
            $y = 0;
120 View Code Duplication
            if ($this->plots[0]->coords[0][$i] < 0) {
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...
121
                $y = $this->plots[0]->coords[0][$i];
122
            }
123
124
            for ($j = 1; $j < $this->nbrplots; $j++) {
125 View Code Duplication
                if ($this->plots[$j]->coords[0][$i] < 0) {
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...
126
                    $y += $this->plots[$j]->coords[0][$i];
127
                }
128
            }
129
            $ymin[$i] = $y;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ymin was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ymin = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
130
        }
131
        $ymin = Min($ysetmin, Min($ymin));
0 ignored issues
show
Bug introduced by
The variable $ymin does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
132
        // Bar always start at baseline
133
        if ($ymin >= $this->ybase) {
134
            $ymin = $this->ybase;
135
        }
136
137
        return [$xmin, $ymin];
138
    }
139
140
    // Stroke acc bar plot
141
    public function Stroke($img, $xscale, $yscale)
142
    {
143
        $pattern = null;
144
        $img->SetLineWeight($this->weight);
145
        $grad = null;
146
        for ($i = 0; $i < $this->numpoints - 1; $i++) {
147
            $accy     = 0;
148
            $accy_neg = 0;
149
            for ($j = 0; $j < $this->nbrplots; ++$j) {
150
                $img->SetColor($this->plots[$j]->color);
151
152
                if ($this->plots[$j]->coords[0][$i] >= 0) {
153
                    $yt    = $yscale->Translate($this->plots[$j]->coords[0][$i] + $accy);
154
                    $accyt = $yscale->Translate($accy);
155
                    $accy += $this->plots[$j]->coords[0][$i];
156
                } else {
157
                    //if ( $this->plots[$j]->coords[0][$i] < 0 || $accy_neg < 0 ) {
1 ignored issue
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
158
                    $yt    = $yscale->Translate($this->plots[$j]->coords[0][$i] + $accy_neg);
159
                    $accyt = $yscale->Translate($accy_neg);
160
                    $accy_neg += $this->plots[$j]->coords[0][$i];
161
                }
162
163
                $xt = $xscale->Translate($i);
164
165 View Code Duplication
                if ($this->abswidth > -1) {
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...
166
                    $abswidth = $this->abswidth;
167
                } else {
168
                    $abswidth = round($this->width * $xscale->scale_factor, 0);
169
                }
170
171
                $pts = [$xt, $accyt, $xt, $yt, $xt + $abswidth, $yt, $xt + $abswidth, $accyt];
172
173
                if ($this->bar_shadow) {
174
                    $ssh = $this->bar_shadow_hsize;
175
                    $ssv = $this->bar_shadow_vsize;
176
177
                    // We must also differ if we are a positive or negative bar.
178
                    if ($j === 0) {
179
                        // This gets extra complicated since we have to
180
                        // see all plots to see if we are negative. It could
181
                        // for example be that all plots are 0 until the very
182
                        // last one. We therefore need to save the initial setup
183
                        // for both the negative and positive case
184
185
                        // In case the final bar is positive
186
                        $sp[0] = $pts[6] + 1;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$sp was never initialized. Although not strictly required by PHP, it is generally a good practice to add $sp = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
187
                        $sp[1] = $pts[7];
0 ignored issues
show
Bug introduced by
The variable $sp does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
188
                        $sp[2] = $pts[6] + $ssh;
189
                        $sp[3] = $pts[7] - $ssv;
190
191
                        // In case the final bar is negative
192
                        $nsp[0]  = $pts[0];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$nsp was never initialized. Although not strictly required by PHP, it is generally a good practice to add $nsp = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
193
                        $nsp[1]  = $pts[1];
0 ignored issues
show
Bug introduced by
The variable $nsp does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
194
                        $nsp[2]  = $pts[0] + $ssh;
195
                        $nsp[3]  = $pts[1] - $ssv;
196
                        $nsp[4]  = $pts[6] + $ssh;
197
                        $nsp[5]  = $pts[7] - $ssv;
198
                        $nsp[10] = $pts[6] + 1;
199
                        $nsp[11] = $pts[7];
200
                    }
201
202
                    if ($j === $this->nbrplots - 1) {
203
                        // If this is the last plot of the bar and
204
                        // the total value is larger than 0 then we
205
                        // add the shadow.
206 View Code Duplication
                        if (is_array($this->bar_shadow_color)) {
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...
207
                            $numcolors = count($this->bar_shadow_color);
208
                            if ($numcolors == 0) {
209
                                Util\JpGraphError::RaiseL(2013); //('You have specified an empty array for shadow colors in the bar plot.');
210
                            }
211
                            $img->PushColor($this->bar_shadow_color[$i % $numcolors]);
212
                        } else {
213
                            $img->PushColor($this->bar_shadow_color);
214
                        }
215
216
                        if ($accy > 0) {
217
                            $sp[4]  = $pts[4] + $ssh;
218
                            $sp[5]  = $pts[5] - $ssv;
219
                            $sp[6]  = $pts[2] + $ssh;
220
                            $sp[7]  = $pts[3] - $ssv;
221
                            $sp[8]  = $pts[2];
222
                            $sp[9]  = $pts[3] - 1;
223
                            $sp[10] = $pts[4] + 1;
224
                            $sp[11] = $pts[5];
225
                            $img->FilledPolygon($sp, 4);
226
                        } elseif ($accy_neg < 0) {
227
                            $nsp[6] = $pts[4] + $ssh;
228
                            $nsp[7] = $pts[5] - $ssv;
229
                            $nsp[8] = $pts[4] + 1;
230
                            $nsp[9] = $pts[5];
231
                            $img->FilledPolygon($nsp, 4);
232
                        }
233
                        $img->PopColor();
234
                    }
235
                }
236
237
                // If value is NULL or 0, then don't draw a bar at all
238
                if ($this->plots[$j]->coords[0][$i] == 0) {
239
                    continue;
240
                }
241
242
                if ($this->plots[$j]->grad) {
243
                    if ($grad === null) {
244
                        $grad = new Gradient($img);
245
                    }
246
                    if (is_array($this->plots[$j]->grad_fromcolor)) {
247
                        // The first argument (grad_fromcolor) can be either an array or a single color. If it is an array
248
                        // then we have two choices. It can either a) be a single color specified as an RGB triple or it can be
249
                        // an array to specify both (from, to style) for each individual bar. The way to know the difference is
250
                        // to investgate the first element. If this element is an integer [0,255] then we assume it is an RGB
251
                        // triple.
252
                        $ng = count($this->plots[$j]->grad_fromcolor);
253
                        if ($ng === 3) {
254
                            if (is_numeric($this->plots[$j]->grad_fromcolor[0]) && $this->plots[$j]->grad_fromcolor[0] > 0 &&
255
                                $this->plots[$j]->grad_fromcolor[0] < 256) {
256
                                // RGB Triple
257
                                $fromcolor = $this->plots[$j]->grad_fromcolor;
258
                                $tocolor   = $this->plots[$j]->grad_tocolor;
259
                                $style     = $this->plots[$j]->grad_style;
260 View Code Duplication
                            } else {
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...
261
                                $fromcolor = $this->plots[$j]->grad_fromcolor[$i % $ng][0];
262
                                $tocolor   = $this->plots[$j]->grad_fromcolor[$i % $ng][1];
263
                                $style     = $this->plots[$j]->grad_fromcolor[$i % $ng][2];
264
                            }
265 View Code Duplication
                        } else {
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...
266
                            $fromcolor = $this->plots[$j]->grad_fromcolor[$i % $ng][0];
267
                            $tocolor   = $this->plots[$j]->grad_fromcolor[$i % $ng][1];
268
                            $style     = $this->plots[$j]->grad_fromcolor[$i % $ng][2];
269
                        }
270
                        $grad->FilledRectangle($pts[2], $pts[3],
271
                            $pts[6], $pts[7],
272
                            $fromcolor, $tocolor, $style);
273
                    } else {
274
                        $grad->FilledRectangle($pts[2], $pts[3],
275
                            $pts[6], $pts[7],
276
                            $this->plots[$j]->grad_fromcolor,
277
                            $this->plots[$j]->grad_tocolor,
278
                            $this->plots[$j]->grad_style);
279
                    }
280
                } else {
281
                    if (is_array($this->plots[$j]->fill_color)) {
282
                        $numcolors = count($this->plots[$j]->fill_color);
283
                        $fillcolor = $this->plots[$j]->fill_color[$i % $numcolors];
284
                        // If the bar is specified to be non filled then the fill color is false
285 View Code Duplication
                        if ($fillcolor !== false) {
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...
286
                            $img->SetColor($this->plots[$j]->fill_color[$i % $numcolors]);
287
                        }
288 View Code Duplication
                    } else {
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...
289
                        $fillcolor = $this->plots[$j]->fill_color;
290
                        if ($fillcolor !== false) {
291
                            $img->SetColor($this->plots[$j]->fill_color);
292
                        }
293
                    }
294
                    if ($fillcolor !== false) {
295
                        $img->FilledPolygon($pts);
296
                    }
297
                }
298
299
                $img->SetColor($this->plots[$j]->color);
300
301
                // Stroke the pattern
302
                if ($this->plots[$j]->iPattern > -1) {
303
                    if ($pattern === null) {
304
                        $pattern = new RectPatternFactory();
305
                    }
306
307
                    $prect = $pattern->Create($this->plots[$j]->iPattern, $this->plots[$j]->iPatternColor, 1);
308
                    $prect->SetDensity($this->plots[$j]->iPatternDensity);
309
                    if ($this->plots[$j]->coords[0][$i] < 0) {
310
                        $rx = $pts[0];
311
                        $ry = $pts[1];
312
                    } else {
313
                        $rx = $pts[2];
314
                        $ry = $pts[3];
315
                    }
316
                    $width  = abs($pts[4] - $pts[0]) + 1;
317
                    $height = abs($pts[1] - $pts[3]) + 1;
318
                    $prect->SetPos(new Util\Rectangle($rx, $ry, $width, $height));
319
                    $prect->Stroke($img);
320
                }
321
322
                // CSIM array
323
324
                if ($i < count($this->plots[$j]->csimtargets)) {
325
                    // Create the client side image map
326
                    $rpts      = $img->ArrRotate($pts);
327
                    $csimcoord = round($rpts[0]) . ", " . round($rpts[1]);
328 View Code Duplication
                    for ($k = 1; $k < 4; ++$k) {
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...
329
                        $csimcoord .= ", " . round($rpts[2 * $k]) . ", " . round($rpts[2 * $k + 1]);
330
                    }
331
                    if (!empty($this->plots[$j]->csimtargets[$i])) {
332
                        $this->csimareas .= '<area shape="poly" coords="' . $csimcoord . '" ';
333
                        $this->csimareas .= " href=\"" . $this->plots[$j]->csimtargets[$i] . "\" ";
334
335
                        if (!empty($this->plots[$j]->csimwintargets[$i])) {
336
                            $this->csimareas .= " target=\"" . $this->plots[$j]->csimwintargets[$i] . "\" ";
337
                        }
338
339
                        $sval = '';
340
                        if (!empty($this->plots[$j]->csimalts[$i])) {
341
                            $sval = sprintf($this->plots[$j]->csimalts[$i], $this->plots[$j]->coords[0][$i]);
342
                            $this->csimareas .= " title=\"$sval\" ";
343
                        }
344
                        $this->csimareas .= " alt=\"$sval\" />\n";
345
                    }
346
                }
347
348
                $pts[] = $pts[0];
349
                $pts[] = $pts[1];
350
                $img->SetLineWeight($this->plots[$j]->weight);
351
                $img->Polygon($pts);
352
                $img->SetLineWeight(1);
353
            }
354
355
            // Daw potential bar around the entire accbar bar
356
            if ($this->weight > 0) {
357
                $y = $yscale->Translate(0);
358
                $img->SetColor($this->color);
359
                $img->SetLineWeight($this->weight);
360
                $img->Rectangle($pts[0], $y, $pts[6], $pts[5]);
0 ignored issues
show
Bug introduced by
The variable $pts does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
361
            }
362
363
            // Draw labels for each acc.bar
364
365
            $x = $pts[2] + ($pts[4] - $pts[2]) / 2;
366
            if ($this->bar_shadow) {
367
                $x += $ssh;
0 ignored issues
show
Bug introduced by
The variable $ssh does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
368
            }
369
370
            // First stroke the accumulated value for the entire bar
371
            // This value is always placed at the top/bottom of the bars
372
            if ($accy_neg < 0) {
373
                $y = $yscale->Translate($accy_neg);
374
                $this->value->Stroke($img, $accy_neg, $x, $y);
375
            } else {
376
                $y = $yscale->Translate($accy);
377
                $this->value->Stroke($img, $accy, $x, $y);
378
            }
379
380
            $accy     = 0;
381
            $accy_neg = 0;
382
            for ($j = 0; $j < $this->nbrplots; ++$j) {
383
384
                // We don't print 0 values in an accumulated bar plot
385
                if ($this->plots[$j]->coords[0][$i] == 0) {
386
                    continue;
387
                }
388
389
                if ($this->plots[$j]->coords[0][$i] > 0) {
390
                    $yt    = $yscale->Translate($this->plots[$j]->coords[0][$i] + $accy);
391
                    $accyt = $yscale->Translate($accy);
392 View Code Duplication
                    if ($this->plots[$j]->valuepos == 'center') {
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...
393
                        $y = $accyt - ($accyt - $yt) / 2;
394
                    } elseif ($this->plots[$j]->valuepos == 'bottom') {
395
                        $y = $accyt;
396
                    } else {
397
                        // top or max
398
                        $y = $accyt - ($accyt - $yt);
399
                    }
400
                    $accy += $this->plots[$j]->coords[0][$i];
401
                    if ($this->plots[$j]->valuepos == 'center') {
402
                        $this->plots[$j]->value->SetAlign("center", "center");
403
                        $this->plots[$j]->value->SetMargin(0);
404 View Code Duplication
                    } elseif ($this->plots[$j]->valuepos == 'bottom') {
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...
405
                        $this->plots[$j]->value->SetAlign('center', 'bottom');
406
                        $this->plots[$j]->value->SetMargin(2);
407
                    } else {
408
                        $this->plots[$j]->value->SetAlign('center', 'top');
409
                        $this->plots[$j]->value->SetMargin(1);
410
                    }
411
                } else {
412
                    $yt    = $yscale->Translate($this->plots[$j]->coords[0][$i] + $accy_neg);
413
                    $accyt = $yscale->Translate($accy_neg);
414
                    $accy_neg += $this->plots[$j]->coords[0][$i];
415 View Code Duplication
                    if ($this->plots[$j]->valuepos == 'center') {
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...
416
                        $y = $accyt - ($accyt - $yt) / 2;
417
                    } elseif ($this->plots[$j]->valuepos == 'bottom') {
418
                        $y = $accyt;
419
                    } else {
420
                        $y = $accyt - ($accyt - $yt);
421
                    }
422
                    if ($this->plots[$j]->valuepos == 'center') {
423
                        $this->plots[$j]->value->SetAlign("center", "center");
424
                        $this->plots[$j]->value->SetMargin(0);
425 View Code Duplication
                    } elseif ($this->plots[$j]->valuepos == 'bottom') {
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...
426
                        $this->plots[$j]->value->SetAlign('center', $j == 0 ? 'bottom' : 'top');
427
                        $this->plots[$j]->value->SetMargin(-2);
428
                    } else {
429
                        $this->plots[$j]->value->SetAlign('center', 'bottom');
430
                        $this->plots[$j]->value->SetMargin(-1);
431
                    }
432
                }
433
                $this->plots[$j]->value->Stroke($img, $this->plots[$j]->coords[0][$i], $x, $y);
434
            }
435
        }
436
        return true;
437
    }
438
} // Class
439