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

LinePlot::SetFillGradient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 4
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Amenadiel\JpGraph\Plot;
3
4
use Amenadiel\JpGraph\Util;
5
6
/*=======================================================================
7
// File:           JPGRAPH_LINE.PHP
8
// Description: Line plot extension for JpGraph
9
// Created:       2001-01-08
10
// Ver:           $Id: jpgraph_line.php 1921 2009-12-11 11:46:39Z ljp $
11
//
12
// Copyright (c) Asial Corporation. All rights reserved.
13
//========================================================================
14
 */
15
16
// constants for the (filled) area
17
DEFINE("LP_AREA_FILLED", true);
18
DEFINE("LP_AREA_NOT_FILLED", false);
19
DEFINE("LP_AREA_BORDER", false);
20
DEFINE("LP_AREA_NO_BORDER", true);
21
22
//===================================================
23
// CLASS LinePlot
24
// Description:
25
//===================================================
26
class LinePlot extends Plot
27
{
28
    public $mark           = null;
29
    protected $filled      = false;
30
    protected $fill_color  = 'blue';
31
    protected $step_style  = false;
32
    protected $center  = false;
33
    protected $line_style  = 1; // Default to solid
34
    protected $filledAreas = array(); // array of arrays(with min,max,col,filled in them)
35
    public $barcenter      = false; // When we mix line and bar. Should we center the line in the bar.
36
    protected $fillFromMin = false;
37
    protected $fillFromMax = false;
38
    protected $fillgrad    = false;
39
    protected $fillgrad_fromcolor    = 'navy';
40
    protected $fillgrad_tocolor    = 'silver';
41
    protected $fillgrad_numcolors    = 100;
42
    protected $iFastStroke = false;
43
44
    //---------------
45
    // CONSTRUCTOR
46
    public function __construct($datay, $datax = false)
47
    {
48
        parent::__construct($datay, $datax);
49
        $this->mark       = new PlotMark();
50
        $this->color      = Util\ColorFactory::getColor();
51
        $this->fill_color = $this->color;
52
    }
53
54
    //---------------
55
    // PUBLIC METHODS
56
57
    public function SetFilled($aFlg = true)
58
    {
59
        $this->filled = $aFlg;
60
    }
61
62
    public function SetBarCenter($aFlag = true)
63
    {
64
        $this->barcenter = $aFlag;
65
    }
66
67
    public function SetStyle($aStyle)
68
    {
69
        $this->line_style = $aStyle;
70
    }
71
72
    public function SetStepStyle($aFlag = true)
73
    {
74
        $this->step_style = $aFlag;
75
    }
76
77
    public function SetColor($aColor)
78
    {
79
        parent::SetColor($aColor);
80
    }
81
82
    public function SetFillFromYMin($f = true)
83
    {
84
        $this->fillFromMin = $f;
85
    }
86
87
    public function SetFillFromYMax($f = true)
88
    {
89
        $this->fillFromMax = $f;
90
    }
91
92
    public function SetFillColor($aColor, $aFilled = true)
93
    {
94
        //$this->color = $aColor;
1 ignored issue
show
Unused Code Comprehensibility introduced by
50% 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...
95
        $this->fill_color = $aColor;
96
        $this->filled     = $aFilled;
97
    }
98
99
    public function SetFillGradient($aFromColor, $aToColor, $aNumColors = 100, $aFilled = true)
100
    {
101
        $this->fillgrad_fromcolor = $aFromColor;
102
        $this->fillgrad_tocolor   = $aToColor;
103
        $this->fillgrad_numcolors = $aNumColors;
104
        $this->filled             = $aFilled;
105
        $this->fillgrad           = true;
106
    }
107
108
    public function Legend($graph)
109
    {
110
        if ($this->legend != "") {
111
            if ($this->filled && !$this->fillgrad) {
112
                $graph->legend->Add($this->legend,
113
                    $this->fill_color, $this->mark, 0,
114
                    $this->legendcsimtarget, $this->legendcsimalt, $this->legendcsimwintarget);
115
            } elseif ($this->fillgrad) {
116
                $color = array($this->fillgrad_fromcolor, $this->fillgrad_tocolor);
117
                // In order to differentiate between gradients and cooors specified as an RGB triple
118
                $graph->legend->Add($this->legend, $color, "", -2/* -GRAD_HOR */,
119
                    $this->legendcsimtarget, $this->legendcsimalt, $this->legendcsimwintarget);
120
            } else {
121
                $graph->legend->Add($this->legend,
122
                    $this->color, $this->mark, $this->line_style,
123
                    $this->legendcsimtarget, $this->legendcsimalt, $this->legendcsimwintarget);
124
            }
125
        }
126
    }
127
128
    public function AddArea($aMin = 0, $aMax = 0, $aFilled = LP_AREA_NOT_FILLED, $aColor = "gray9", $aBorder = LP_AREA_BORDER)
129
    {
130
        if ($aMin > $aMax) {
131
            // swap
132
            $tmp  = $aMin;
133
            $aMin = $aMax;
134
            $aMax = $tmp;
135
        }
136
        $this->filledAreas[] = array($aMin, $aMax, $aColor, $aFilled, $aBorder);
137
    }
138
139
    // Gets called before any axis are stroked
140
    public function PreStrokeAdjust($graph)
141
    {
142
143
        // If another plot type have already adjusted the
144
        // offset we don't touch it.
145
        // (We check for empty in case the scale is  a log scale
146
        // and hence doesn't contain any xlabel_offset)
147
        if (empty($graph->xaxis->scale->ticks->xlabel_offset) || $graph->xaxis->scale->ticks->xlabel_offset == 0) {
148
            if ($this->center) {
149
                ++$this->numpoints;
150
                $a = 0.5;
151
                $b = 0.5;
152
            } else {
153
                $a = 0;
154
                $b = 0;
155
            }
156
            $graph->xaxis->scale->ticks->SetXLabelOffset($a);
157
            $graph->SetTextScaleOff($b);
158
            //$graph->xaxis->scale->ticks->SupressMinorTickMarks();
1 ignored issue
show
Unused Code Comprehensibility introduced by
67% 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...
159
        }
160
    }
161
162
    public function SetFastStroke($aFlg = true)
163
    {
164
        $this->iFastStroke = $aFlg;
165
    }
166
167
    public function FastStroke($img, $xscale, $yscale, $aStartPoint = 0, $exist_x = true)
168
    {
169
        // An optimized stroke for many data points with no extra
170
        // features but 60% faster. You can't have values or line styles, or null
171
        // values in plots.
172
        $numpoints = count($this->coords[0]);
173
        if ($this->barcenter) {
174
            $textadj = 0.5 - $xscale->text_scale_off;
175
        } else {
176
            $textadj = 0;
177
        }
178
179
        $img->SetColor($this->color);
180
        $img->SetLineWeight($this->weight);
181
        $pnts = $aStartPoint;
182
        while ($pnts < $numpoints) {
183 View Code Duplication
            if ($exist_x) {
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...
184
                $x = $this->coords[1][$pnts];
185
            } else {
186
                $x = $pnts + $textadj;
187
            }
188
            $xt = $xscale->Translate($x);
189
            $y  = $this->coords[0][$pnts];
190
            $yt = $yscale->Translate($y);
191
            if (is_numeric($y)) {
192
                $cord[] = $xt;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$cord was never initialized. Although not strictly required by PHP, it is generally a good practice to add $cord = 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
                $cord[] = $yt;
0 ignored issues
show
Bug introduced by
The variable $cord 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
            } elseif ($y == '-' && $pnts > 0) {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
195
                // Just ignore
196
            } else {
197
                Util\JpGraphError::RaiseL(10002); //('Plot too complicated for fast line Stroke. Use standard Stroke()');
198
            }
199
            ++$pnts;
200
        } // WHILE
201
202
        $img->Polygon($cord, false, true);
203
    }
204
205
    public function Stroke($img, $xscale, $yscale)
206
    {
207
        $idx       = 0;
208
        $numpoints = count($this->coords[0]);
209 View Code Duplication
        if (isset($this->coords[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...
210
            if (count($this->coords[1]) != $numpoints) {
211
                Util\JpGraphError::RaiseL(2003, count($this->coords[1]), $numpoints);
212
                //("Number of X and Y points are not equal. Number of X-points:".count($this->coords[1])." Number of Y-points:$numpoints");
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...
213
            } else {
214
                $exist_x = true;
215
            }
216
        } else {
217
            $exist_x = false;
218
        }
219
220
        if ($this->barcenter) {
221
            $textadj = 0.5 - $xscale->text_scale_off;
222
        } else {
223
            $textadj = 0;
224
        }
225
226
        // Find the first numeric data point
227
        $startpoint = 0;
228
        while ($startpoint < $numpoints && !is_numeric($this->coords[0][$startpoint])) {
229
            ++$startpoint;
230
        }
231
232
        // Bail out if no data points
233
        if ($startpoint == $numpoints) {
234
            return;
235
        }
236
237
        if ($this->iFastStroke) {
238
            $this->FastStroke($img, $xscale, $yscale, $startpoint, $exist_x);
0 ignored issues
show
Bug introduced by
The variable $exist_x 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...
239
            return;
240
        }
241
242
        if ($exist_x) {
243
            $xs = $this->coords[1][$startpoint];
244
        } else {
245
            $xs = $textadj + $startpoint;
246
        }
247
248
        $img->SetStartPoint($xscale->Translate($xs),
249
            $yscale->Translate($this->coords[0][$startpoint]));
250
251
        if ($this->filled) {
252
            if ($this->fillFromMax) {
253
                //$max = $yscale->GetMaxVal();
1 ignored issue
show
Unused Code Comprehensibility introduced by
60% 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...
254
                $cord[$idx++] = $xscale->Translate($xs);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$cord was never initialized. Although not strictly required by PHP, it is generally a good practice to add $cord = 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...
255
                $cord[$idx++] = $yscale->scale_abs[1];
256
            } else {
257
                $min = $yscale->GetMinVal();
258
                if ($min > 0 || $this->fillFromMin) {
259
                    $fillmin = $yscale->scale_abs[0]; //Translate($min);
260
                } else {
261
                    $fillmin = $yscale->Translate(0);
262
                }
263
264
                $cord[$idx++] = $xscale->Translate($xs);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$cord was never initialized. Although not strictly required by PHP, it is generally a good practice to add $cord = 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...
265
                $cord[$idx++] = $fillmin;
266
            }
267
        }
268
        $xt           = $xscale->Translate($xs);
269
        $yt           = $yscale->Translate($this->coords[0][$startpoint]);
270
        $cord[$idx++] = $xt;
0 ignored issues
show
Bug introduced by
The variable $cord 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...
271
        $cord[$idx++] = $yt;
272
        $yt_old       = $yt;
273
        $xt_old       = $xt;
274
        $y_old        = $this->coords[0][$startpoint];
275
276
        $this->value->Stroke($img, $this->coords[0][$startpoint], $xt, $yt);
277
278
        $img->SetColor($this->color);
279
        $img->SetLineWeight($this->weight);
280
        $img->SetLineStyle($this->line_style);
281
        $pnts           = $startpoint + 1;
282
        $firstnonumeric = false;
283
284
        while ($pnts < $numpoints) {
285 View Code Duplication
            if ($exist_x) {
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
                $x = $this->coords[1][$pnts];
287
            } else {
288
                $x = $pnts + $textadj;
289
            }
290
            $xt = $xscale->Translate($x);
291
            $yt = $yscale->Translate($this->coords[0][$pnts]);
292
293
            $y = $this->coords[0][$pnts];
294
            if ($this->step_style) {
295
                // To handle null values within step style we need to record the
296
                // first non numeric value so we know from where to start if the
297
                // non value is '-'.
298
                if (is_numeric($y)) {
299
                    $firstnonumeric = false;
300
                    if (is_numeric($y_old)) {
301
                        $img->StyleLine($xt_old, $yt_old, $xt, $yt_old);
302
                        $img->StyleLine($xt, $yt_old, $xt, $yt);
303
                    } elseif ($y_old == '-') {
304
                        $img->StyleLine($xt_first, $yt_first, $xt, $yt_first);
0 ignored issues
show
Bug introduced by
The variable $xt_first 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...
Bug introduced by
The variable $yt_first 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...
305
                        $img->StyleLine($xt, $yt_first, $xt, $yt);
306
                    } else {
307
                        $yt_old = $yt;
308
                        $xt_old = $xt;
0 ignored issues
show
Unused Code introduced by
$xt_old is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
309
                    }
310
                    $cord[$idx++] = $xt;
311
                    $cord[$idx++] = $yt_old;
312
                    $cord[$idx++] = $xt;
313
                    $cord[$idx++] = $yt;
314
                } elseif ($firstnonumeric == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
315
                    $firstnonumeric = true;
316
                    $yt_first       = $yt_old;
317
                    $xt_first       = $xt_old;
318
                }
319
            } else {
320
                $tmp1 = $y;
321
                $prev = $this->coords[0][$pnts - 1];
322 View Code Duplication
                if ($tmp1 === '' || $tmp1 === null || $tmp1 === 'X') {
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...
323
                    $tmp1 = 'x';
324
                }
325
326 View Code Duplication
                if ($prev === '' || $prev === null || $prev === 'X') {
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...
327
                    $prev = 'x';
328
                }
329
330
                if (is_numeric($y) || (is_string($y) && $y != '-')) {
331
                    if (is_numeric($y) && (is_numeric($prev) || $prev === '-')) {
332
                        $img->StyleLineTo($xt, $yt);
333
                    } else {
334
                        $img->SetStartPoint($xt, $yt);
335
                    }
336
                }
337
                if ($this->filled && $tmp1 !== '-') {
338
                    if ($tmp1 === 'x') {
339
                        $cord[$idx++] = $cord[$idx - 3];
340
                        $cord[$idx++] = $fillmin;
0 ignored issues
show
Bug introduced by
The variable $fillmin 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...
341
                    } elseif ($prev === 'x') {
342
                        $cord[$idx++] = $xt;
343
                        $cord[$idx++] = $fillmin;
344
                        $cord[$idx++] = $xt;
345
                        $cord[$idx++] = $yt;
346
                    } else {
347
                        $cord[$idx++] = $xt;
348
                        $cord[$idx++] = $yt;
349
                    }
350
                } else {
351
                    if (is_numeric($tmp1) && (is_numeric($prev) || $prev === '-')) {
352
                        $cord[$idx++] = $xt;
353
                        $cord[$idx++] = $yt;
354
                    }
355
                }
356
            }
357
            $yt_old = $yt;
358
            $xt_old = $xt;
359
            $y_old  = $y;
360
361
            $this->StrokeDataValue($img, $this->coords[0][$pnts], $xt, $yt);
362
363
            ++$pnts;
364
        }
365
366
        if ($this->filled) {
367
            $cord[$idx++] = $xt;
368
            if ($this->fillFromMax) {
369
                $cord[$idx++] = $yscale->scale_abs[1];
370
            } else {
371
                if ($min > 0 || $this->fillFromMin) {
372
                    $cord[$idx++] = $yscale->Translate($min);
0 ignored issues
show
Bug introduced by
The variable $min 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...
373
                } else {
374
                    $cord[$idx++] = $yscale->Translate(0);
375
                }
376
            }
377
            if ($this->fillgrad) {
378
                $img->SetLineWeight(1);
379
                $grad = new Gradient($img);
380
                $grad->SetNumColors($this->fillgrad_numcolors);
381
                $grad->FilledFlatPolygon($cord, $this->fillgrad_fromcolor, $this->fillgrad_tocolor);
382
                $img->SetLineWeight($this->weight);
383
            } else {
384
                $img->SetColor($this->fill_color);
385
                $img->FilledPolygon($cord);
386
            }
387
            if ($this->weight > 0) {
388
                $img->SetLineWeight($this->weight);
389
                $img->SetColor($this->color);
390
                // Remove first and last coordinate before drawing the line
391
                // sine we otherwise get the vertical start and end lines which
392
                // doesn't look appropriate
393
                $img->Polygon(array_slice($cord, 2, count($cord) - 4));
394
            }
395
        }
396
397
        if (!empty($this->filledAreas)) {
398
            $minY   = $yscale->Translate($yscale->GetMinVal());
399
            $factor = ($this->step_style ? 4 : 2);
400
401
            for ($i = 0; $i < sizeof($this->filledAreas); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() 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...
402
                // go through all filled area elements ordered by insertion
403
                // fill polygon array
404
                $areaCoords[] = $cord[$this->filledAreas[$i][0] * $factor];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$areaCoords was never initialized. Although not strictly required by PHP, it is generally a good practice to add $areaCoords = 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...
405
                $areaCoords[] = $minY;
0 ignored issues
show
Bug introduced by
The variable $areaCoords 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...
406
407
                $areaCoords =
408
                array_merge($areaCoords,
409
                    array_slice($cord,
410
                        $this->filledAreas[$i][0] * $factor,
411
                        ($this->filledAreas[$i][1] - $this->filledAreas[$i][0] + ($this->step_style ? 0 : 1)) * $factor));
412
                $areaCoords[] = $areaCoords[sizeof($areaCoords) - 2]; // last x
413
                $areaCoords[] = $minY; // last y
414
415
                if ($this->filledAreas[$i][3]) {
416
                    $img->SetColor($this->filledAreas[$i][2]);
417
                    $img->FilledPolygon($areaCoords);
418
                    $img->SetColor($this->color);
419
                }
420
                // Check if we should draw the frame.
421
                // If not we still re-draw the line since it might have been
422
                // partially overwritten by the filled area and it doesn't look
423
                // very good.
424
                if ($this->filledAreas[$i][4]) {
425
                    $img->Polygon($areaCoords);
426
                } else {
427
                    $img->Polygon($cord);
428
                }
429
430
                $areaCoords = array();
431
            }
432
        }
433
434
        if (!is_object($this->mark) || $this->mark->type == -1 || $this->mark->show == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
435
            return;
436
        }
437
438
        for ($pnts = 0; $pnts < $numpoints; ++$pnts) {
439 View Code Duplication
            if ($exist_x) {
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...
440
                $x = $this->coords[1][$pnts];
441
            } else {
442
                $x = $pnts + $textadj;
443
            }
444
            $xt = $xscale->Translate($x);
445
            $yt = $yscale->Translate($this->coords[0][$pnts]);
446
447
            if (is_numeric($this->coords[0][$pnts])) {
448 View Code Duplication
                if (!empty($this->csimtargets[$pnts])) {
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...
449
                    if (!empty($this->csimwintargets[$pnts])) {
450
                        $this->mark->SetCSIMTarget($this->csimtargets[$pnts], $this->csimwintargets[$pnts]);
451
                    } else {
452
                        $this->mark->SetCSIMTarget($this->csimtargets[$pnts]);
453
                    }
454
                    $this->mark->SetCSIMAlt($this->csimalts[$pnts]);
455
                }
456
                if ($exist_x) {
457
                    $x = $this->coords[1][$pnts];
458
                } else {
459
                    $x = $pnts;
460
                }
461
                $this->mark->SetCSIMAltVal($this->coords[0][$pnts], $x);
462
                $this->mark->Stroke($img, $xt, $yt);
463
                $this->csimareas .= $this->mark->GetCSIMAreas();
464
            }
465
        }
466
    }
467
} // Class
468