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

Plot::SetWeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Amenadiel\JpGraph\Plot;
4
5
//===================================================
6
// CLASS Plot
7
// Description: Abstract base class for all concrete plot classes
8
//===================================================
9
class Plot
10
{
11
    public $numpoints = 0;
12
    public $value;
13
    public $legend           = '';
14
    public $coords           = array();
15
    public $color            = 'black';
16
    public $hidelegend       = false;
17
    public $line_weight      = 1;
18
    public $csimtargets      = array();
19
    public $csimwintargets      = array(); // Array of targets for CSIM
20
    public $csimareas        = ''; // Resultant CSIM area tags
21
    public $csimalts         = null; // ALT:s for corresponding target
22
    public $legendcsimtarget = '';
23
    public $legendcsimwintarget = '';
24
    public $legendcsimalt    = '';
25
    protected $weight        = 1;
26
    protected $center        = false;
27
28
    protected $inputValues;
29
    protected $isRunningClear = false;
30
31
    public function __construct($aDatay, $aDatax = false)
32
    {
33
        $this->numpoints = count($aDatay);
34
        if ($this->numpoints == 0) {
35
            Util\JpGraphError::RaiseL(25121); //("Empty input data array specified for plot. Must have at least one data point.");
36
        }
37
38
        if (!$this->isRunningClear) {
39
            $this->inputValues           = array();
40
            $this->inputValues['aDatay'] = $aDatay;
41
            $this->inputValues['aDatax'] = $aDatax;
42
        }
43
44
        $this->coords[0] = $aDatay;
45
        if (is_array($aDatax)) {
46
            $this->coords[1] = $aDatax;
47
            $n               = count($aDatax);
48
            for ($i = 0; $i < $n; ++$i) {
49
                if (!is_numeric($aDatax[$i])) {
50
                    Util\JpGraphError::RaiseL(25070);
51
                }
52
            }
53
        }
54
        $this->value = new DisplayValue();
55
    }
56
57
    // Stroke the plot
58
    // "virtual" function which must be implemented by
59
    // the subclasses
60
    public function Stroke($aImg, $aXScale, $aYScale)
61
    {
62
        Util\JpGraphError::RaiseL(25122); //("JpGraph: Stroke() must be implemented by concrete subclass to class Plot");
63
    }
64
65
    public function HideLegend($f = true)
66
    {
67
        $this->hidelegend = $f;
68
    }
69
70
    public function DoLegend($graph)
71
    {
72
        if (!$this->hidelegend) {
73
            $this->Legend($graph);
74
        }
75
    }
76
77
    public function StrokeDataValue($img, $aVal, $x, $y)
78
    {
79
        $this->value->Stroke($img, $aVal, $x, $y);
80
    }
81
82
    // Set href targets for CSIM
83
    public function SetCSIMTargets($aTargets, $aAlts = '', $aWinTargets = '')
84
    {
85
        $this->csimtargets    = $aTargets;
86
        $this->csimwintargets = $aWinTargets;
0 ignored issues
show
Documentation Bug introduced by
It seems like $aWinTargets of type string is incompatible with the declared type array of property $csimwintargets.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
87
        $this->csimalts       = $aAlts;
88
    }
89
90
    // Get all created areas
91
    public function GetCSIMareas()
92
    {
93
        return $this->csimareas;
94
    }
95
96
    // "Virtual" function which gets called before any scale
97
    // or axis are stroked used to do any plot specific adjustment
98
    public function PreStrokeAdjust($aGraph)
99
    {
100
        if (substr($aGraph->axtype, 0, 4) == "text" && (isset($this->coords[1]))) {
101
            Util\JpGraphError::RaiseL(25123); //("JpGraph: You can't use a text X-scale with specified X-coords. Use a \"int\" or \"lin\" scale instead.");
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...
102
        }
103
        return true;
104
    }
105
106
    // Virtual function to the the concrete plot class to make any changes to the graph
107
    // and scale before the stroke process begins
108
    public function PreScaleSetup($aGraph)
109
    {
110
        // Empty
111
    }
112
113
    // Get minimum values in plot
114 View Code Duplication
    public function Min()
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...
115
    {
116
        if (isset($this->coords[1])) {
117
            $x = $this->coords[1];
118
        } else {
119
            $x = '';
120
        }
121
        if ($x != '' && count($x) > 0) {
122
            $xm = min($x);
123
        } else {
124
            $xm = 0;
125
        }
126
        $y   = $this->coords[0];
127
        $cnt = count($y);
128
        if ($cnt > 0) {
129
            $i = 0;
130
            while ($i < $cnt && !is_numeric($ym = $y[$i])) {
131
                $i++;
132
            }
133
            while ($i < $cnt) {
134
                if (is_numeric($y[$i])) {
135
                    $ym = min($ym, $y[$i]);
0 ignored issues
show
Bug introduced by
The variable $ym 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...
136
                }
137
                ++$i;
138
            }
139
        } else {
140
            $ym = '';
141
        }
142
        return array($xm, $ym);
143
    }
144
145
    // Get maximum value in plot
146 View Code Duplication
    public function Max()
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...
147
    {
148
        if (isset($this->coords[1])) {
149
            $x = $this->coords[1];
150
        } else {
151
            $x = '';
152
        }
153
154
        if ($x != '' && count($x) > 0) {
155
            $xm = max($x);
156
        } else {
157
            $xm = $this->numpoints - 1;
158
        }
159
        $y = $this->coords[0];
160
        if (count($y) > 0) {
161
            $cnt = count($y);
162
            $i   = 0;
163
            while ($i < $cnt && !is_numeric($ym = $y[$i])) {
164
                $i++;
165
            }
166
            while ($i < $cnt) {
167
                if (is_numeric($y[$i])) {
168
                    $ym = max($ym, $y[$i]);
0 ignored issues
show
Bug introduced by
The variable $ym 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...
169
                }
170
                ++$i;
171
            }
172
        } else {
173
            $ym = '';
174
        }
175
        return array($xm, $ym);
176
    }
177
178
    public function SetColor($aColor)
179
    {
180
        $this->color = $aColor;
181
    }
182
183 View Code Duplication
    public function SetLegend($aLegend, $aCSIM = '', $aCSIMAlt = '', $aCSIMWinTarget = '')
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...
184
    {
185
        $this->legend              = $aLegend;
186
        $this->legendcsimtarget    = $aCSIM;
187
        $this->legendcsimwintarget = $aCSIMWinTarget;
188
        $this->legendcsimalt       = $aCSIMAlt;
189
    }
190
191
    public function SetWeight($aWeight)
192
    {
193
        $this->weight = $aWeight;
194
    }
195
196
    public function SetLineWeight($aWeight = 1)
197
    {
198
        $this->line_weight = $aWeight;
199
    }
200
201
    public function SetCenter($aCenter = true)
202
    {
203
        $this->center = $aCenter;
204
    }
205
206
    // This method gets called by Graph class to plot anything that should go
207
    // into the margin after the margin color has been set.
208
    public function StrokeMargin($aImg)
0 ignored issues
show
Unused Code introduced by
The parameter $aImg is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
209
    {
210
        return true;
211
    }
212
213
    // Framework function the chance for each plot class to set a legend
214
    public function Legend($aGraph)
215
    {
216
        if ($this->legend != '') {
217
            $aGraph->legend->Add($this->legend, $this->color, '', 0, $this->legendcsimtarget, $this->legendcsimalt, $this->legendcsimwintarget);
218
        }
219
    }
220
221
    public function Clear()
222
    {
223
        $this->isRunningClear = true;
224
        $this->__construct($this->inputValues['aDatay'], $this->inputValues['aDatax']);
225
        $this->isRunningClear = false;
226
    }
227
} // Class
228