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

RadarGraph::Add()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 19
Code Lines 13

Duplication

Lines 12
Ratio 63.16 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 12
nop 1
dl 12
loc 19
rs 8.2222
c 0
b 0
f 0
1
<?php
2
namespace Amenadiel\JpGraph\Graph;
3
4
use Amenadiel\JpGraph\Util;
5
use \Amenadiel\JpGraph\ImgTrans;
6
7
//===================================================
8
// CLASS RadarGraph
9
// Description: Main container for a radar graph
10
//===================================================
11
class RadarGraph extends Graph
12
{
13
    public $grid;
14
    public $axis = null;
15
    private $posx;
16
    private $posy;
17
    private $len;
18
    private $axis_title = null;
19
20
    public function __construct($width = 300, $height = 200, $cachedName = "", $timeout = 0, $inline = 1)
21
    {
22
        parent::__construct($width, $height, $cachedName, $timeout, $inline);
0 ignored issues
show
Documentation introduced by
$inline is of type integer, 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...
23
        $this->posx = $width / 2;
24
        $this->posy = $height / 2;
25
        $this->len  = min($width, $height) * 0.35;
26
        $this->SetColor([255, 255, 255]);
27
        $this->SetTickDensity(TICKD_NORMAL);
28
        $this->SetScale('lin');
29
        $this->SetGridDepth(DEPTH_FRONT);
30
    }
31
32
    public function HideTickMarks($aFlag = true)
33
    {
34
        $this->axis->scale->ticks->SupressTickMarks($aFlag);
35
    }
36
37
    public function ShowMinorTickmarks($aFlag = true)
38
    {
39
        $this->yscale->ticks->SupressMinorTickMarks(!$aFlag);
40
    }
41
42
    public function SetScale($axtype, $ymin = 1, $ymax = 1, $dummy1 = null, $dumy2 = null)
43
    {
44
        if ($axtype != 'lin' && $axtype != 'log') {
45
            Util\JpGraphError::RaiseL(18003, $axtype);
46
            //("Illegal scale for radarplot ($axtype). Must be \"lin\" or \"log\"");
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...
47
        }
48
        if ($axtype == 'lin') {
49
            $this->yscale        = new LinearScale($ymin, $ymax);
50
            $this->yscale->ticks = new RadarLinearTicks();
51
            $this->yscale->ticks->SupressMinorTickMarks();
52
        } elseif ($axtype == 'log') {
53
            $this->yscale        = new LogScale($ymin, $ymax);
54
            $this->yscale->ticks = new RadarLogTicks();
55
        }
56
57
        $this->axis = new RadarAxis($this->img, $this->yscale);
58
        $this->grid = new RadarGrid();
59
    }
60
61
    public function SetSize($aSize)
62
    {
63
        if ($aSize < 0.1 || $aSize > 1) {
64
            Util\JpGraphError::RaiseL(18004, $aSize);
65
            //("Radar Plot size must be between 0.1 and 1. (Your value=$s)");
66
        }
67
        $this->len = min($this->img->width, $this->img->height) * $aSize / 2;
0 ignored issues
show
Bug introduced by
The property width does not seem to exist. Did you mean _width?

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...
Bug introduced by
The property height does not seem to exist. Did you mean original_height?

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...
68
    }
69
70
    public function SetPlotSize($aSize)
71
    {
72
        $this->SetSize($aSize);
73
    }
74
75
    public function SetTickDensity($densy = TICKD_NORMAL, $dummy1 = null)
76
    {
77
        $this->ytick_factor = 25;
78 View Code Duplication
        switch ($densy) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
79
            case TICKD_DENSE:
80
                $this->ytick_factor = 12;
81
                break;
82
            case TICKD_NORMAL:
83
                $this->ytick_factor = 25;
84
                break;
85
            case TICKD_SPARSE:
86
                $this->ytick_factor = 40;
87
                break;
88
            case TICKD_VERYSPARSE:
89
                $this->ytick_factor = 70;
90
                break;
91
            default:
92
                Util\JpGraphError::RaiseL(18005, $densy);
93
                //("RadarPlot Unsupported Tick density: $densy");
94
        }
95
    }
96
97
    public function SetPos($px, $py = 0.5)
98
    {
99
        $this->SetCenter($px, $py);
100
    }
101
102
    public function SetCenter($px, $py = 0.5)
103
    {
104
        if ($px >= 0 && $px <= 1) {
105
            $this->posx = $this->img->width * $px;
0 ignored issues
show
Bug introduced by
The property width does not seem to exist. Did you mean _width?

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...
106
        } else {
107
            $this->posx = $px;
108
        }
109
        if ($py >= 0 && $py <= 1) {
110
            $this->posy = $this->img->height * $py;
0 ignored issues
show
Bug introduced by
The property height does not seem to exist. Did you mean original_height?

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...
111
        } else {
112
            $this->posy = $py;
113
        }
114
    }
115
116
    public function SetColor($aColor)
117
    {
118
        $this->SetMarginColor($aColor);
119
    }
120
121
    public function SetTitles($aTitleArray)
122
    {
123
        $this->axis_title = $aTitleArray;
124
    }
125
126
    public function Add($aPlot)
127
    {
128
        if ($aPlot == null) {
129
            Util\JpGraphError::RaiseL(25010); //("Graph::Add() You tried to add a null plot to the graph.");
130
        }
131 View Code Duplication
        if (is_array($aPlot) && count($aPlot) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
132
            $cl = $aPlot[0];
133
        } else {
134
            $cl = $aPlot;
135
        }
136
137 View Code Duplication
        if ($cl instanceof Text) {
0 ignored issues
show
Bug introduced by
The class Amenadiel\JpGraph\Graph\Text does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
138
            $this->AddText($aPlot);
139
        } elseif (class_exists('IconPlot', false) && ($cl instanceof IconPlot)) {
0 ignored issues
show
Bug introduced by
The class Amenadiel\JpGraph\Graph\IconPlot does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
140
            $this->AddIcon($aPlot);
141
        } else {
142
            $this->plots[] = $aPlot;
143
        }
144
    }
145
146
    public function GetPlotsYMinMax($aPlots)
147
    {
148
        $min = $aPlots[0]->Min();
149
        $max = $aPlots[0]->Max();
150
        foreach ($this->plots as $p) {
151
            $max = max($max, $p->Max());
152
            $min = min($min, $p->Min());
153
        }
154
        if ($min < 0) {
155
            Util\JpGraphError::RaiseL(18006, $min);
156
            //("Minimum data $min (Radar plots should only be used when all data points > 0)");
157
        }
158
        return [$min, $max];
159
    }
160
161 View Code Duplication
    public function StrokeIcons()
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...
162
    {
163
        if ($this->iIcons != null) {
164
            $n = count($this->iIcons);
165
            for ($i = 0; $i < $n; ++$i) {
166
                $this->iIcons[$i]->Stroke($this->img);
167
            }
168
        }
169
    }
170
171 View Code Duplication
    public function StrokeTexts()
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...
172
    {
173
        if ($this->texts != null) {
174
            $n = count($this->texts);
175
            for ($i = 0; $i < $n; ++$i) {
176
                $this->texts[$i]->Stroke($this->img);
177
            }
178
        }
179
    }
180
181
    // Stroke the Radar graph
182
    public function Stroke($aStrokeFileName = '')
183
    {
184
185
        // If the filename is the predefined value = '_csim_special_'
186
        // we assume that the call to stroke only needs to do enough
187
        // to correctly generate the CSIM maps.
188
        // We use this variable to skip things we don't strictly need
189
        // to do to generate the image map to improve performance
190
        // a best we can. Therefor you will see a lot of tests !$_csim in the
191
        // code below.
192
        $_csim = ($aStrokeFileName === _CSIM_SPECIALFILE);
193
194
        // We need to know if we have stroked the plot in the
195
        // GetCSIMareas. Otherwise the CSIM hasn't been generated
196
        // and in the case of GetCSIM called before stroke to generate
197
        // CSIM without storing an image to disk GetCSIM must call Stroke.
198
        $this->iHasStroked = true;
199
200
        $n = count($this->plots);
201
        // Set Y-scale
202
203
        if (!$this->yscale->IsSpecified() && count($this->plots) > 0) {
204
            list($min, $max) = $this->GetPlotsYMinMax($this->plots);
0 ignored issues
show
Unused Code introduced by
The assignment to $min is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
205
            $this->yscale->AutoScale($this->img, 0, $max, $this->len / $this->ytick_factor);
206
        } elseif ($this->yscale->IsSpecified() &&
207
            ($this->yscale->auto_ticks || !$this->yscale->ticks->IsSpecified())) {
208
209
            // The tick calculation will use the user suplied min/max values to determine
210
            // the ticks. If auto_ticks is false the exact user specifed min and max
211
            // values will be used for the scale.
212
            // If auto_ticks is true then the scale might be slightly adjusted
213
            // so that the min and max values falls on an even major step.
214
            $min = $this->yscale->scale[0];
215
            $max = $this->yscale->scale[1];
216
            $this->yscale->AutoScale($this->img, $min, $max,
217
                $this->len / $this->ytick_factor,
218
                $this->yscale->auto_ticks);
219
        }
220
221
        // Set start position end length of scale (in absolute pixels)
222
        $this->yscale->SetConstants($this->posx, $this->len);
223
224
        // We need as many axis as there are data points
225
        $nbrpnts = $this->plots[0]->GetCount();
226
227
        // If we have no titles just number the axis 1,2,3,...
228
        if ($this->axis_title == null) {
229
            for ($i = 0; $i < $nbrpnts; ++$i) {
230
                $this->axis_title[$i] = $i + 1;
231
            }
232
        } elseif (count($this->axis_title) < $nbrpnts) {
233
            Util\JpGraphError::RaiseL(18007);
234
            // ("Number of titles does not match number of points in plot.");
235
        }
236
        for ($i = 0; $i < $n; ++$i) {
237
            if ($nbrpnts != $this->plots[$i]->GetCount()) {
238
                Util\JpGraphError::RaiseL(18008);
239
                //("Each radar plot must have the same number of data points.");
240
            }
241
        }
242
243
        if (!$_csim) {
244
            if ($this->background_image != '') {
245
                $this->StrokeFrameBackground();
246
            } else {
247
                $this->StrokeFrame();
248
                $this->StrokeBackgroundGrad();
249
            }
250
        }
251
        $astep = 2 * M_PI / $nbrpnts;
0 ignored issues
show
Unused Code introduced by
$astep 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...
252
253
        if (!$_csim) {
254
            if ($this->iIconDepth == DEPTH_BACK) {
255
                $this->StrokeIcons();
256
            }
257
258
            // Prepare legends
259
            for ($i = 0; $i < $n; ++$i) {
260
                $this->plots[$i]->Legend($this);
261
            }
262
            $this->legend->Stroke($this->img);
263
            $this->footer->Stroke($this->img);
264
        }
265
266
        if (!$_csim) {
267 View Code Duplication
            if ($this->grid_depth == DEPTH_BACK) {
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...
268
                // Draw axis and grid
269
                for ($i = 0, $a = M_PI / 2; $i < $nbrpnts; ++$i, $a += $astep) {
270
                    $this->axis->Stroke($this->posy, $a, $grid[$i], $this->axis_title[$i], $i == 0);
0 ignored issues
show
Bug introduced by
The variable $grid does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
271
                }
272
                $this->grid->Stroke($this->img, $grid);
273
            }
274
            if ($this->iIconDepth == DEPTH_BACK) {
275
                $this->StrokeIcons();
276
            }
277
        }
278
279
        // Plot points
280
        $a = M_PI / 2;
281 View Code Duplication
        for ($i = 0; $i < $n; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
282
            $this->plots[$i]->Stroke($this->img, $this->posy, $this->yscale, $a);
283
        }
284
285
        if (!$_csim) {
286 View Code Duplication
            if ($this->grid_depth != DEPTH_BACK) {
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...
287
                // Draw axis and grid
288
                for ($i = 0, $a = M_PI / 2; $i < $nbrpnts; ++$i, $a += $astep) {
289
                    $this->axis->Stroke($this->posy, $a, $grid[$i], $this->axis_title[$i], $i == 0);
290
                }
291
                $this->grid->Stroke($this->img, $grid);
292
            }
293
294
            $this->StrokeTitles();
295
            $this->StrokeTexts();
296
            if ($this->iIconDepth == DEPTH_FRONT) {
297
                $this->StrokeIcons();
298
            }
299
        }
300
301
        // Should we do any final image transformation
302 View Code Duplication
        if ($this->iImgTrans && !$_csim) {
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...
303
            $tform          = new ImgTrans($this->img->img);
304
            $this->img->img = $tform->Skew3D($this->iImgTransHorizon, $this->iImgTransSkewDist,
305
                $this->iImgTransDirection, $this->iImgTransHighQ,
306
                $this->iImgTransMinSize, $this->iImgTransFillColor,
307
                $this->iImgTransBorder);
308
        }
309
310 View Code Duplication
        if (!$_csim) {
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...
311
            // If the filename is given as the special "__handle"
312
            // then the image handler is returned and the image is NOT
313
            // streamed back
314
            if ($aStrokeFileName == _IMG_HANDLER) {
315
                return $this->img->img;
316
            } else {
317
                // Finally stream the generated picture
318
                $this->cache->PutAndStream($this->img, $this->cache_name, $this->inline, $aStrokeFileName);
319
            }
320
        }
321
    }
322
} // Class
323