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

LinearScale::IntCalcTicks()   C

Complexity

Conditions 8
Paths 64

Size

Total Lines 44
Code Lines 31

Duplication

Lines 17
Ratio 38.64 %

Importance

Changes 0
Metric Value
cc 8
eloc 31
nc 64
nop 5
dl 17
loc 44
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
namespace Amenadiel\JpGraph\Graph;
4
5
use Amenadiel\JpGraph\Util;
6
7
//===================================================
8
// CLASS LinearScale
9
// Description: Handle linear scaling between screen and world
10
//===================================================
11
class LinearScale
12
{
13
    public $textscale = false; // Just a flag to let the Plot class find out if
14
    // we are a textscale or not. This is a cludge since
15
    // this information is available in Graph::axtype but
16
    // we don't have access to the graph object in the Plots
17
    // stroke method. So we let graph store the status here
18
    // when the linear scale is created. A real cludge...
19
    public $type; // is this x or y scale ?
20
    public $ticks          = null; // Store ticks
21
    public $text_scale_off = 0;
22
    public $scale_abs      = array(0, 0);
23
    public $scale_factor; // Scale factor between world and screen
24
    public $off; // Offset between image edge and plot area
25
    public $scale      = array(0, 0);
26
    public $name       = 'lin';
27
    public $auto_ticks = false; // When using manual scale should the ticks be automatically set?
28
    public $world_abs_size; // Plot area size in pixels (Needed public in jpgraph_radar.php)
29
    public $intscale         = false; // Restrict autoscale to integers
30
    protected $autoscale_min = false; // Forced minimum value, auto determine max
31
    protected $autoscale_max = false; // Forced maximum value, auto determine min
32
    private $gracetop        = 0;
33
    private $gracebottom        = 0;
34
35
    private $_world_size; // Plot area size in world coordinates
0 ignored issues
show
Unused Code introduced by
The property $_world_size is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
36
37 View Code Duplication
    public function __construct($aMin = 0, $aMax = 0, $aType = 'y')
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...
38
    {
39
        assert($aType == 'x' || $aType == 'y');
40
        assert($aMin <= $aMax);
41
42
        $this->type       = $aType;
43
        $this->scale      = array($aMin, $aMax);
44
        $this->world_size = $aMax - $aMin;
0 ignored issues
show
Bug introduced by
The property world_size does not seem to exist. Did you mean _world_size?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
45
        $this->ticks      = new LinearTicks();
46
    }
47
48
    // Check if scale is set or if we should autoscale
49
    // We should do this is either scale or ticks has not been set
50
    public function IsSpecified()
51
    {
52
        if ($this->GetMinVal() == $this->GetMaxVal()) {
53
            // Scale not set
54
            return false;
55
        }
56
        return true;
57
    }
58
59
    // Set the minimum data value when the autoscaling is used.
60
    // Usefull if you want a fix minimum (like 0) but have an
61
    // automatic maximum
62
    public function SetAutoMin($aMin)
63
    {
64
        $this->autoscale_min = $aMin;
65
    }
66
67
    // Set the minimum data value when the autoscaling is used.
68
    // Usefull if you want a fix minimum (like 0) but have an
69
    // automatic maximum
70
    public function SetAutoMax($aMax)
71
    {
72
        $this->autoscale_max = $aMax;
73
    }
74
75
    // If the user manually specifies a scale should the ticks
76
    // still be set automatically?
77
    public function SetAutoTicks($aFlag = true)
78
    {
79
        $this->auto_ticks = $aFlag;
80
    }
81
82
    // Specify scale "grace" value (top and bottom)
83
    public function SetGrace($aGraceTop, $aGraceBottom = 0)
84
    {
85
        if ($aGraceTop < 0 || $aGraceBottom < 0) {
86
            Util\JpGraphError::RaiseL(25069); //(" Grace must be larger then 0");
87
        }
88
        $this->gracetop    = $aGraceTop;
89
        $this->gracebottom = $aGraceBottom;
90
    }
91
92
    // Get the minimum value in the scale
93
    public function GetMinVal()
94
    {
95
        return $this->scale[0];
96
    }
97
98
    // get maximum value for scale
99
    public function GetMaxVal()
100
    {
101
        return $this->scale[1];
102
    }
103
104
    // Specify a new min/max value for sclae
105
    public function Update($aImg, $aMin, $aMax)
106
    {
107
        $this->scale      = array($aMin, $aMax);
108
        $this->world_size = $aMax - $aMin;
0 ignored issues
show
Bug introduced by
The property world_size does not seem to exist. Did you mean _world_size?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
109
        $this->InitConstants($aImg);
110
    }
111
112
    // Translate between world and screen
113 View Code Duplication
    public function Translate($aCoord)
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...
114
    {
115
        if (!is_numeric($aCoord)) {
116
            if ($aCoord != '' && $aCoord != '-' && $aCoord != 'x') {
117
                Util\JpGraphError::RaiseL(25070); //('Your data contains non-numeric values.');
118
            }
119
            return 0;
120
        } else {
121
            return round($this->off + ($aCoord - $this->scale[0]) * $this->scale_factor);
122
        }
123
    }
124
125
    // Relative translate (don't include offset) usefull when we just want
126
    // to know the relative position (in pixels) on the axis
127 View Code Duplication
    public function RelTranslate($aCoord)
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...
128
    {
129
        if (!is_numeric($aCoord)) {
130
            if ($aCoord != '' && $aCoord != '-' && $aCoord != 'x') {
131
                Util\JpGraphError::RaiseL(25070); //('Your data contains non-numeric values.');
132
            }
133
            return 0;
134
        } else {
135
            return ($aCoord - $this->scale[0]) * $this->scale_factor;
136
        }
137
    }
138
139
    // Restrict autoscaling to only use integers
140
    public function SetIntScale($aIntScale = true)
141
    {
142
        $this->intscale = $aIntScale;
143
    }
144
145
    // Calculate an integer autoscale
146
    public function IntAutoScale($img, $min, $max, $maxsteps, $majend = true)
147
    {
148
        // Make sure limits are integers
149
        $min = floor($min);
150
        $max = ceil($max);
151
        if (abs($min - $max) == 0) {
152
            --$min;
153
            ++$max;
154
        }
155
        $maxsteps = floor($maxsteps);
156
157
        $gracetop    = round(($this->gracetop / 100.0) * abs($max - $min));
158
        $gracebottom = round(($this->gracebottom / 100.0) * abs($max - $min));
159
        if (is_numeric($this->autoscale_min)) {
160
            $min = ceil($this->autoscale_min);
161
            if ($min >= $max) {
162
                Util\JpGraphError::RaiseL(25071); //('You have specified a min value with SetAutoMin() which is larger than the maximum value used for the scale. This is not possible.');
163
            }
164
        }
165
166
        if (is_numeric($this->autoscale_max)) {
167
            $max = ceil($this->autoscale_max);
168
            if ($min >= $max) {
169
                Util\JpGraphError::RaiseL(25072); //('You have specified a max value with SetAutoMax() which is smaller than the miminum value used for the scale. This is not possible.');
170
            }
171
        }
172
173
        if (abs($min - $max) == 0) {
174
            ++$max;
175
            --$min;
176
        }
177
178
        $min -= $gracebottom;
179
        $max += $gracetop;
180
181
        // First get tickmarks as multiples of 1, 10, ...
182
        if ($majend) {
183
            list($num1steps, $adj1min, $adj1max, $maj1step) = $this->IntCalcTicks($maxsteps, $min, $max, 1);
184
        } else {
185
            $adj1min                    = $min;
186
            $adj1max                    = $max;
187
            list($num1steps, $maj1step) = $this->IntCalcTicksFreeze($maxsteps, $min, $max, 1);
188
        }
189
190
        if (abs($min - $max) > 2) {
191
            // Then get tick marks as 2:s 2, 20, ...
1 ignored issue
show
Unused Code Comprehensibility introduced by
37% 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...
192
            if ($majend) {
193
                list($num2steps, $adj2min, $adj2max, $maj2step) = $this->IntCalcTicks($maxsteps, $min, $max, 5);
194
            } else {
195
                $adj2min                    = $min;
196
                $adj2max                    = $max;
197
                list($num2steps, $maj2step) = $this->IntCalcTicksFreeze($maxsteps, $min, $max, 5);
198
            }
199
        } else {
200
            $num2steps = 10000; // Dummy high value so we don't choose this
201
        }
202
203
        if (abs($min - $max) > 5) {
204
            // Then get tickmarks as 5:s 5, 50, 500, ...
1 ignored issue
show
Unused Code Comprehensibility introduced by
44% 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...
205
            if ($majend) {
206
                list($num5steps, $adj5min, $adj5max, $maj5step) = $this->IntCalcTicks($maxsteps, $min, $max, 2);
207
            } else {
208
                $adj5min                    = $min;
209
                $adj5max                    = $max;
210
                list($num5steps, $maj5step) = $this->IntCalcTicksFreeze($maxsteps, $min, $max, 2);
211
            }
212
        } else {
213
            $num5steps = 10000; // Dummy high value so we don't choose this
214
        }
215
216
        // Check to see whichof 1:s, 2:s or 5:s fit better with
217
        // the requested number of major ticks
218
        $match1 = abs($num1steps - $maxsteps);
219
        $match2 = abs($num2steps - $maxsteps);
220
        if (!empty($maj5step) && $maj5step > 1) {
221
            $match5 = abs($num5steps - $maxsteps);
222
        } else {
223
            $match5 = 10000; // Dummy high value
224
        }
225
226
        // Compare these three values and see which is the closest match
227
        // We use a 0.6 weight to gravitate towards multiple of 5:s
228
        if ($match1 < $match2) {
229
            if ($match1 < $match5) {
230
                $r = 1;
231
            } else {
232
                $r = 3;
233
            }
234
        } else {
235
            if ($match2 < $match5) {
236
                $r = 2;
237
            } else {
238
                $r = 3;
239
            }
240
        }
241
        // Minsteps are always the same as maxsteps for integer scale
242 View Code Duplication
        switch ($r) {
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...
243
            case 1:
244
                $this->ticks->Set($maj1step, $maj1step);
0 ignored issues
show
Documentation introduced by
$maj1step is of type integer|double, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method Set does only exist in Amenadiel\JpGraph\Graph\...\Graph\RadarLinearTicks, but not in Amenadiel\JpGraph\Graph\...aph\Graph\RadarLogTicks.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
245
                $this->Update($img, $adj1min, $adj1max);
246
                break;
247
            case 2:
248
                $this->ticks->Set($maj2step, $maj2step);
0 ignored issues
show
Bug introduced by
The variable $maj2step 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...
Documentation introduced by
$maj2step is of type integer|double, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
249
                $this->Update($img, $adj2min, $adj2max);
0 ignored issues
show
Bug introduced by
The variable $adj2min 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 $adj2max 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...
250
                break;
251
            case 3:
252
                $this->ticks->Set($maj5step, $maj5step);
0 ignored issues
show
Bug introduced by
The variable $maj5step 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...
Documentation introduced by
$maj5step is of type integer|double, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
253
                $this->Update($img, $adj5min, $adj5max);
0 ignored issues
show
Bug introduced by
The variable $adj5min 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 $adj5max 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...
254
                break;
255
            default:
256
                Util\JpGraphError::RaiseL(25073, $r); //('Internal error. Integer scale algorithm comparison out of bound (r=$r)');
257
        }
258
    }
259
260
    // Calculate autoscale. Used if user hasn't given a scale and ticks
261
    // $maxsteps is the maximum number of major tickmarks allowed.
262
    public function AutoScale($img, $min, $max, $maxsteps, $majend = true)
263
    {
264
        if (!is_numeric($min) || !is_numeric($max)) {
265
            Util\JpGraphError::Raise(25044);
266
        }
267
268
        if ($this->intscale) {
269
            $this->IntAutoScale($img, $min, $max, $maxsteps, $majend);
270
            return;
271
        }
272
        if (abs($min - $max) < 0.00001) {
273
            // We need some difference to be able to autoscale
274
            // make it 5% above and 5% below value
275
            if ($min == 0 && $max == 0) {
276
                // Special case
277
                $min = -1;
278
                $max = 1;
279
            } else {
280
                $delta = (abs($max) + abs($min)) * 0.005;
281
                $min -= $delta;
282
                $max += $delta;
283
            }
284
        }
285
286
        $gracetop    = ($this->gracetop / 100.0) * abs($max - $min);
287
        $gracebottom = ($this->gracebottom / 100.0) * abs($max - $min);
288 View Code Duplication
        if (is_numeric($this->autoscale_min)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
289
            $min = $this->autoscale_min;
290
            if ($min >= $max) {
291
                Util\JpGraphError::RaiseL(25071); //('You have specified a min value with SetAutoMin() which is larger than the maximum value used for the scale. This is not possible.');
292
            }
293
            if (abs($min - $max) < 0.001) {
294
                $max *= 1.2;
295
            }
296
        }
297
298 View Code Duplication
        if (is_numeric($this->autoscale_max)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
299
            $max = $this->autoscale_max;
300
            if ($min >= $max) {
301
                Util\JpGraphError::RaiseL(25072); //('You have specified a max value with SetAutoMax() which is smaller than the miminum value used for the scale. This is not possible.');
302
            }
303
            if (abs($min - $max) < 0.001) {
304
                $min *= 0.8;
305
            }
306
        }
307
308
        $min -= $gracebottom;
309
        $max += $gracetop;
310
311
        // First get tickmarks as multiples of 0.1, 1, 10, ...
312 View Code Duplication
        if ($majend) {
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...
313
            list($num1steps, $adj1min, $adj1max, $min1step, $maj1step) = $this->CalcTicks($maxsteps, $min, $max, 1, 2);
314
        } else {
315
            $adj1min                               = $min;
316
            $adj1max                               = $max;
317
            list($num1steps, $min1step, $maj1step) = $this->CalcTicksFreeze($maxsteps, $min, $max, 1, 2, false);
0 ignored issues
show
Unused Code introduced by
The call to LinearScale::CalcTicksFreeze() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
318
        }
319
320
        // Then get tick marks as 2:s 0.2, 2, 20, ...
1 ignored issue
show
Unused Code Comprehensibility introduced by
40% 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...
321 View Code Duplication
        if ($majend) {
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...
322
            list($num2steps, $adj2min, $adj2max, $min2step, $maj2step) = $this->CalcTicks($maxsteps, $min, $max, 5, 2);
323
        } else {
324
            $adj2min                               = $min;
325
            $adj2max                               = $max;
326
            list($num2steps, $min2step, $maj2step) = $this->CalcTicksFreeze($maxsteps, $min, $max, 5, 2, false);
0 ignored issues
show
Unused Code introduced by
The call to LinearScale::CalcTicksFreeze() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
327
        }
328
329
        // Then get tickmarks as 5:s 0.05, 0.5, 5, 50, ...
1 ignored issue
show
Unused Code Comprehensibility introduced by
47% 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...
330 View Code Duplication
        if ($majend) {
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...
331
            list($num5steps, $adj5min, $adj5max, $min5step, $maj5step) = $this->CalcTicks($maxsteps, $min, $max, 2, 5);
332
        } else {
333
            $adj5min                               = $min;
334
            $adj5max                               = $max;
335
            list($num5steps, $min5step, $maj5step) = $this->CalcTicksFreeze($maxsteps, $min, $max, 2, 5, false);
0 ignored issues
show
Unused Code introduced by
The call to LinearScale::CalcTicksFreeze() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
336
        }
337
338
        // Check to see whichof 1:s, 2:s or 5:s fit better with
339
        // the requested number of major ticks
340
        $match1 = abs($num1steps - $maxsteps);
341
        $match2 = abs($num2steps - $maxsteps);
342
        $match5 = abs($num5steps - $maxsteps);
343
344
        // Compare these three values and see which is the closest match
345
        // We use a 0.8 weight to gravitate towards multiple of 5:s
346
        $r = $this->MatchMin3($match1, $match2, $match5, 0.8);
347 View Code Duplication
        switch ($r) {
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...
348
            case 1:
349
                $this->Update($img, $adj1min, $adj1max);
350
                $this->ticks->Set($maj1step, $min1step);
0 ignored issues
show
Documentation introduced by
$min1step is of type integer|double, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method Set does only exist in Amenadiel\JpGraph\Graph\...\Graph\RadarLinearTicks, but not in Amenadiel\JpGraph\Graph\...aph\Graph\RadarLogTicks.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
351
                break;
352
            case 2:
353
                $this->Update($img, $adj2min, $adj2max);
354
                $this->ticks->Set($maj2step, $min2step);
0 ignored issues
show
Documentation introduced by
$min2step is of type integer|double, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
355
                break;
356
            case 3:
357
                $this->Update($img, $adj5min, $adj5max);
358
                $this->ticks->Set($maj5step, $min5step);
0 ignored issues
show
Documentation introduced by
$min5step is of type integer|double, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
359
                break;
360
        }
361
    }
362
363
    //---------------
364
    // PRIVATE METHODS
365
366
    // This method recalculates all constants that are depending on the
367
    // margins in the image. If the margins in the image are changed
368
    // this method should be called for every scale that is registred with
369
    // that image. Should really be installed as an observer of that image.
370
    public function InitConstants($img)
371
    {
372
        if ($this->type == 'x') {
373
            $this->world_abs_size = $img->width - $img->left_margin - $img->right_margin;
374
            $this->off            = $img->left_margin;
375
            $this->scale_factor   = 0;
376
            if ($this->world_size > 0) {
0 ignored issues
show
Bug introduced by
The property world_size does not seem to exist. Did you mean _world_size?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
377
                $this->scale_factor = $this->world_abs_size / ($this->world_size * 1.0);
0 ignored issues
show
Bug introduced by
The property world_size does not seem to exist. Did you mean _world_size?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
378
            }
379
        } else {
380
            // y scale
381
            $this->world_abs_size = $img->height - $img->top_margin - $img->bottom_margin;
382
            $this->off            = $img->top_margin + $this->world_abs_size;
383
            $this->scale_factor   = 0;
384
            if ($this->world_size > 0) {
0 ignored issues
show
Bug introduced by
The property world_size does not seem to exist. Did you mean _world_size?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
385
                $this->scale_factor = -$this->world_abs_size / ($this->world_size * 1.0);
0 ignored issues
show
Bug introduced by
The property world_size does not seem to exist. Did you mean _world_size?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
386
            }
387
        }
388
        $size            = $this->world_size * $this->scale_factor;
0 ignored issues
show
Bug introduced by
The property world_size does not seem to exist. Did you mean _world_size?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
389
        $this->scale_abs = array($this->off, $this->off + $size);
390
    }
391
392
    // Initialize the conversion constants for this scale
393
    // This tries to pre-calculate as much as possible to speed up the
394
    // actual conversion (with Translate()) later on
395
    // $start =scale start in absolute pixels (for x-scale this is an y-position
396
    //     and for an y-scale this is an x-position
397
    // $len   =absolute length in pixels of scale
398
    public function SetConstants($aStart, $aLen)
399
    {
400
        $this->world_abs_size = $aLen;
401
        $this->off            = $aStart;
402
403
        if ($this->world_size <= 0) {
0 ignored issues
show
Bug introduced by
The property world_size does not seem to exist. Did you mean _world_size?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
404
            // This should never ever happen !!
405
            Util\JpGraphError::RaiseL(25074);
406
            //("You have unfortunately stumbled upon a bug in JpGraph. It seems like the scale range is ".$this->world_size." [for ".$this->type." scale] <br> Please report Bug #01 to [email protected] and include the script that gave this error. This problem could potentially be caused by trying to use \"illegal\" values in the input data arrays (like trying to send in strings or only NULL values) which causes the autoscaling to fail.");
1 ignored issue
show
Unused Code Comprehensibility introduced by
62% 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...
407
        }
408
409
        // scale_factor = number of pixels per world unit
410
        $this->scale_factor = $this->world_abs_size / ($this->world_size * 1.0);
0 ignored issues
show
Bug introduced by
The property world_size does not seem to exist. Did you mean _world_size?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
411
412
        // scale_abs = start and end points of scale in absolute pixels
413
        $this->scale_abs = array($this->off, $this->off + $this->world_size * $this->scale_factor);
0 ignored issues
show
Bug introduced by
The property world_size does not seem to exist. Did you mean _world_size?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
414
    }
415
416
    // Calculate number of ticks steps with a specific division
417
    // $a is the divisor of 10**x to generate the first maj tick intervall
418
    // $a=1, $b=2 give major ticks with multiple of 10, ...,0.1,1,10,...
1 ignored issue
show
Unused Code Comprehensibility introduced by
48% 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...
419
    // $a=5, $b=2 give major ticks with multiple of 2:s ...,0.2,2,20,...
1 ignored issue
show
Unused Code Comprehensibility introduced by
46% 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...
420
    // $a=2, $b=5 give major ticks with multiple of 5:s ...,0.5,5,50,...
1 ignored issue
show
Unused Code Comprehensibility introduced by
46% 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...
421
    // We return a vector of
422
    //  [$numsteps,$adjmin,$adjmax,$minstep,$majstep]
1 ignored issue
show
Unused Code Comprehensibility introduced by
92% 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...
423
    // If $majend==true then the first and last marks on the axis will be major
424
    // labeled tick marks otherwise it will be adjusted to the closest min tick mark
425
    public function CalcTicks($maxsteps, $min, $max, $a, $b, $majend = true)
426
    {
427
        $diff = $max - $min;
428
        if ($diff == 0) {
429
            $ld = 0;
430
        } else {
431
            $ld = floor(log10($diff));
432
        }
433
434
        // Gravitate min towards zero if we are close
435
        if ($min > 0 && $min < pow(10, $ld)) {
436
            $min = 0;
437
        }
438
439
        //$majstep=pow(10,$ld-1)/$a;
1 ignored issue
show
Unused Code Comprehensibility introduced by
70% 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...
440
        $majstep = pow(10, $ld) / $a;
441
        $minstep = $majstep / $b;
442
443
        $adjmax   = ceil($max / $minstep) * $minstep;
444
        $adjmin   = floor($min / $minstep) * $minstep;
445
        $adjdiff  = $adjmax - $adjmin;
446
        $numsteps = $adjdiff / $majstep;
447
448 View Code Duplication
        while ($numsteps > $maxsteps) {
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
            $majstep  = pow(10, $ld) / $a;
450
            $numsteps = $adjdiff / $majstep;
451
            ++$ld;
452
        }
453
454
        $minstep = $majstep / $b;
455
        $adjmin  = floor($min / $minstep) * $minstep;
456
        $adjdiff = $adjmax - $adjmin;
0 ignored issues
show
Unused Code introduced by
$adjdiff 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...
457 View Code Duplication
        if ($majend) {
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...
458
            $adjmin  = floor($min / $majstep) * $majstep;
459
            $adjdiff = $adjmax - $adjmin;
460
            $adjmax  = ceil($adjdiff / $majstep) * $majstep + $adjmin;
461
        } else {
462
            $adjmax = ceil($max / $minstep) * $minstep;
463
        }
464
465
        return array($numsteps, $adjmin, $adjmax, $minstep, $majstep);
466
    }
467
468
    public function CalcTicksFreeze($maxsteps, $min, $max, $a, $b)
469
    {
470
        // Same as CalcTicks but don't adjust min/max values
471
        $diff = $max - $min;
472
        if ($diff == 0) {
473
            $ld = 0;
474
        } else {
475
            $ld = floor(log10($diff));
476
        }
477
478
        //$majstep=pow(10,$ld-1)/$a;
1 ignored issue
show
Unused Code Comprehensibility introduced by
70% 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...
479
        $majstep  = pow(10, $ld) / $a;
480
        $minstep  = $majstep / $b;
0 ignored issues
show
Unused Code introduced by
$minstep 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...
481
        $numsteps = floor($diff / $majstep);
482
483 View Code Duplication
        while ($numsteps > $maxsteps) {
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...
484
            $majstep  = pow(10, $ld) / $a;
485
            $numsteps = floor($diff / $majstep);
486
            ++$ld;
487
        }
488
        $minstep = $majstep / $b;
489
        return array($numsteps, $minstep, $majstep);
490
    }
491
492
    public function IntCalcTicks($maxsteps, $min, $max, $a, $majend = true)
493
    {
494
        $diff = $max - $min;
495
        if ($diff == 0) {
496
            Util\JpGraphError::RaiseL(25075); //('Can\'t automatically determine ticks since min==max.');
497
        } else {
498
            $ld = floor(log10($diff));
499
        }
500
501
        // Gravitate min towards zero if we are close
502
        if ($min > 0 && $min < pow(10, $ld)) {
0 ignored issues
show
Bug introduced by
The variable $ld 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...
503
            $min = 0;
504
        }
505
        if ($ld == 0) {
506
            $ld = 1;
507
        }
508 View Code Duplication
        if ($a == 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...
509
            $majstep = 1;
510
        } else {
511
            $majstep = pow(10, $ld) / $a;
512
        }
513
        $adjmax = ceil($max / $majstep) * $majstep;
514
515
        $adjmin   = floor($min / $majstep) * $majstep;
516
        $adjdiff  = $adjmax - $adjmin;
517
        $numsteps = $adjdiff / $majstep;
518 View Code Duplication
        while ($numsteps > $maxsteps) {
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...
519
            $majstep  = pow(10, $ld) / $a;
520
            $numsteps = $adjdiff / $majstep;
521
            ++$ld;
522
        }
523
524
        $adjmin  = floor($min / $majstep) * $majstep;
525
        $adjdiff = $adjmax - $adjmin;
0 ignored issues
show
Unused Code introduced by
$adjdiff 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...
526 View Code Duplication
        if ($majend) {
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...
527
            $adjmin  = floor($min / $majstep) * $majstep;
528
            $adjdiff = $adjmax - $adjmin;
529
            $adjmax  = ceil($adjdiff / $majstep) * $majstep + $adjmin;
530
        } else {
531
            $adjmax = ceil($max / $majstep) * $majstep;
532
        }
533
534
        return array($numsteps, $adjmin, $adjmax, $majstep);
535
    }
536
537
    public function IntCalcTicksFreeze($maxsteps, $min, $max, $a)
538
    {
539
        // Same as IntCalcTick but don't change min/max values
540
        $diff = $max - $min;
541
        if ($diff == 0) {
542
            Util\JpGraphError::RaiseL(25075); //('Can\'t automatically determine ticks since min==max.');
543
        } else {
544
            $ld = floor(log10($diff));
545
        }
546
        if ($ld == 0) {
0 ignored issues
show
Bug introduced by
The variable $ld 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...
547
            $ld = 1;
548
        }
549 View Code Duplication
        if ($a == 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...
550
            $majstep = 1;
551
        } else {
552
            $majstep = pow(10, $ld) / $a;
553
        }
554
555
        $numsteps = floor($diff / $majstep);
556 View Code Duplication
        while ($numsteps > $maxsteps) {
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...
557
            $majstep  = pow(10, $ld) / $a;
558
            $numsteps = floor($diff / $majstep);
559
            ++$ld;
560
        }
561
562
        return array($numsteps, $majstep);
563
    }
564
565
    // Determine the minimum of three values witha  weight for last value
566
    public function MatchMin3($a, $b, $c, $weight)
567
    {
568
        if ($a < $b) {
569
            if ($a < ($c * $weight)) {
570
                return 1; // $a smallest
571
            } else {
572
                return 3; // $c smallest
573
            }
574
        } elseif ($b < ($c * $weight)) {
575
            return 2; // $b smallest
576
        }
577
        return 3; // $c smallest
578
    }
579
580
    public function __get($name)
581
    {
582
        $variable_name = '_' . $name;
583
584
        if (isset($this->$variable_name)) {
585
            return $this->$variable_name * SUPERSAMPLING_SCALE;
586
        } else {
587
            Util\JpGraphError::RaiseL('25132', $name);
588
        }
589
    }
590
591
    public function __set($name, $value)
592
    {
593
        $this->{'_' . $name} = $value;
594
    }
595
} // Class
596