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

Image::PopColor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Amenadiel\JpGraph\Image;
4
5
use \Amenadiel\JpGraph\Text\LanguageConv;
6
use \Amenadiel\JpGraph\Text\TTF;
7
use \Amenadiel\JpGraph\Util;
8
use \Amenadiel\JpGraph\Util\ErrMsgText;
9
10
//=======================================================================
11
// File:        GD_IMAGE.INC.PHP
12
// Description: PHP Graph Plotting library. Low level image drawing routines
13
// Created:     2001-01-08, refactored 2008-03-29
14
// Ver:         $Id: gd_image.inc.php 1922 2010-01-11 11:42:50Z ljp $
15
//
16
// Copyright (c) Asial Corporation. All rights reserved.
17
//========================================================================
18
19
//========================================================================
20
// CLASS Image
21
// Description: The very coor image drawing class that encapsulates all
22
//              calls to the GD library
23
//              Note: The class used by the library is the decendant
24
//              class RotImage which extends the Image class with transparent
25
//              rotation.
26
//=========================================================================
27
class Image
28
{
29
    public $img = null;
30
    public $rgb = null;
31
    public $img_format;
32
    public $ttf        = null;
33
    public $line_style = LINESTYLE_SOLID;
34
    public $current_color, $current_color_name;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
35
    public $original_width = 0, $original_height = 0;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
36
    public $plotwidth      = 0, $plotheight      = 0;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
37
38
    // for __get, __set
39
    private $_left_margin = 30, $_right_margin = 30, $_top_margin = 20, $_bottom_margin = 30;
0 ignored issues
show
Unused Code introduced by
The property $_left_margin 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...
Unused Code introduced by
The property $_right_margin 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...
Unused Code introduced by
The property $_top_margin 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...
Unused Code introduced by
The property $_bottom_margin 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...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
40
    //private $_plotwidth=0,$_plotheight=0;
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...
41
    private $_width       = 0, $_height       = 0;
0 ignored issues
show
Unused Code introduced by
The property $_width 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...
Unused Code introduced by
The property $_height 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...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
42
    private $_line_weight = 1;
0 ignored issues
show
Unused Code introduced by
The property $_line_weight 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...
43
44
    protected $expired           = true;
45
    protected $lastx             = 0, $lasty             = 0;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
46
    protected $obs_list          = [];
47
    protected $font_size         = 12, $font_family         = FF_DEFAULT, $font_style         = FS_NORMAL;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
48
    protected $font_file         = '';
49
    protected $text_halign       = "left", $text_valign       = "bottom";
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
50
    protected $use_anti_aliasing = false;
51
    protected $quality           = null;
52
    protected $colorstack        = [], $colorstackidx        = 0;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
53
    protected $canvascolor       = 'white';
54
    protected $langconv          = null;
55
    protected $iInterlace        = false;
56
    protected $bbox_cache        = []; // STore the last found tetx bounding box
57
    protected $ff_font0;
58
    protected $ff_font0_bold;
59
    protected $ff_font1;
60
    protected $ff_font1_bold;
61
    protected $ff_font2;
62
    protected $ff_font2_bold;
63
64
    //---------------
65
    // CONSTRUCTOR
66
    public function __construct($aWidth = 0, $aHeight = 0, $aFormat = DEFAULT_GFORMAT, $aSetAutoMargin = true)
67
    {
68
69
        $this->original_width  = $aWidth;
70
        $this->original_height = $aHeight;
71
        $this->CreateImgCanvas($aWidth, $aHeight);
72
73
        if ($aSetAutoMargin) {
74
            $this->SetAutoMargin();
75
        }
76
77
        if (!$this->SetImgFormat($aFormat)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->SetImgFormat($aFormat) of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
78
            Util\JpGraphError::RaiseL(25081, $aFormat); //("JpGraph: Selected graphic format is either not supported or unknown [$aFormat]");
79
        }
80
        $this->ttf      = new TTF();
81
        $this->langconv = new LanguageConv();
82
83
        $this->ff_font0      = imageloadfont(dirname(dirname(__FILE__)) . "/fonts/FF_FONT0.gdf");
84
        $this->ff_font1      = imageloadfont(dirname(dirname(__FILE__)) . "/fonts/FF_FONT1.gdf");
85
        $this->ff_font2      = imageloadfont(dirname(dirname(__FILE__)) . "/fonts/FF_FONT2.gdf");
86
        $this->ff_font1_bold = imageloadfont(dirname(dirname(__FILE__)) . "/fonts/FF_FONT1-Bold.gdf");
87
        $this->ff_font2_bold = imageloadfont(dirname(dirname(__FILE__)) . "/fonts/FF_FONT2-Bold.gdf");
88
    }
89
90
    // Enable interlacing in images
91
    public function SetInterlace($aFlg = true)
92
    {
93
        $this->iInterlace = $aFlg;
94
    }
95
96
    // Should we use anti-aliasing. Note: This really slows down graphics!
97
    public function SetAntiAliasing($aFlg = true)
98
    {
99
        $this->use_anti_aliasing = $aFlg;
100
        if (function_exists('imageantialias')) {
101
            imageantialias($this->img, $aFlg);
102
        } /*else {
1 ignored issue
show
Unused Code Comprehensibility introduced by
58% 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...
103
    Util\JpGraphError::RaiseL(25128); //('The function imageantialias() is not available in your PHP installation. Use the GD version that comes with PHP and not the standalone version.')
104
    }*/
105
    }
106
107
    public function GetAntiAliasing()
108
    {
109
        return $this->use_anti_aliasing;
110
    }
111
112
    public function CreateRawCanvas($aWidth = 0, $aHeight = 0)
113
    {
114
115
        $aWidth *= SUPERSAMPLING_SCALE;
116
        $aHeight *= SUPERSAMPLING_SCALE;
117
118
        if ($aWidth <= 1 || $aHeight <= 1) {
119
            Util\JpGraphError::RaiseL(25082, $aWidth, $aHeight); //("Illegal sizes specified for width or height when creating an image, (width=$aWidth, height=$aHeight)");
120
        }
121
122
        $this->img = @imagecreatetruecolor($aWidth, $aHeight);
123
        if ($this->img < 1) {
124
            Util\JpGraphError::RaiseL(25126);
125
            //die("Can't create truecolor image. Check that you really have GD2 library installed.");
126
        }
127
        $this->SetAlphaBlending();
128
129
        if ($this->iInterlace) {
130
            imageinterlace($this->img, 1);
131
        }
132
        if ($this->rgb != null) {
133
            $this->rgb->img = $this->img;
134
        } else {
135
            $this->rgb = new RGB($this->img);
136
        }
137
    }
138
139
    public function CloneCanvasH()
140
    {
141
        $oldimage = $this->img;
142
        $this->CreateRawCanvas($this->width, $this->height);
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...
143
        imagecopy($this->img, $oldimage, 0, 0, 0, 0, $this->width, $this->height);
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...
144
        return $oldimage;
145
    }
146
147
    public function CreateImgCanvas($aWidth = 0, $aHeight = 0)
148
    {
149
150
        $old = [$this->img, $this->width, $this->height];
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...
151
152
        $aWidth  = round($aWidth);
153
        $aHeight = round($aHeight);
154
155
        $this->width  = $aWidth;
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...
156
        $this->height = $aHeight;
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...
157
158
        if ($aWidth == 0 || $aHeight == 0) {
159
            // We will set the final size later.
160
            // Note: The size must be specified before any other
161
            // img routines that stroke anything are called.
162
            $this->img = null;
163
            $this->rgb = null;
164
            return $old;
165
        }
166
167
        $this->CreateRawCanvas($aWidth, $aHeight);
168
        // Set canvas color (will also be the background color for a
169
        // a pallett image
170
        $this->SetColor($this->canvascolor);
171
        $this->FilledRectangle(0, 0, $this->width - 1, $this->height - 1);
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...
172
173
        return $old;
174
    }
175
176
    public function CopyCanvasH($aToHdl, $aFromHdl, $aToX, $aToY, $aFromX, $aFromY, $aWidth, $aHeight, $aw = -1, $ah = -1)
177
    {
178
        if ($aw === -1) {
179
            $aw = $aWidth;
180
            $ah = $aHeight;
181
            $f  = 'imagecopyresized';
182
        } else {
183
            $f = 'imagecopyresampled';
184
        }
185
        $f($aToHdl, $aFromHdl, $aToX, $aToY, $aFromX, $aFromY, $aWidth, $aHeight, $aw, $ah);
186
    }
187
188
    public function Copy($fromImg, $toX, $toY, $fromX, $fromY, $toWidth, $toHeight, $fromWidth = -1, $fromHeight = -1)
189
    {
190
        $this->CopyCanvasH($this->img, $fromImg, $toX, $toY, $fromX, $fromY, $toWidth, $toHeight, $fromWidth, $fromHeight);
191
    }
192
193
    public function CopyMerge($fromImg, $toX, $toY, $fromX, $fromY, $toWidth, $toHeight, $fromWidth = -1, $fromHeight = -1, $aMix = 100)
194
    {
195
        if ($aMix == 100) {
196
            $this->CopyCanvasH($this->img, $fromImg,
197
                $toX, $toY, $fromX, $fromY, $toWidth, $toHeight, $fromWidth, $fromHeight);
198
        } else {
199
            if (($fromWidth != -1 && ($fromWidth != $toWidth)) || ($fromHeight != -1 && ($fromHeight != $fromHeight))) {
200
                // Create a new canvas that will hold the re-scaled original from image
201
                if ($toWidth <= 1 || $toHeight <= 1) {
202
                    Util\JpGraphError::RaiseL(25083); //('Illegal image size when copying image. Size for copied to image is 1 pixel or less.');
203
                }
204
205
                $tmpimg = @imagecreatetruecolor($toWidth, $toHeight);
206
207
                if ($tmpimg < 1) {
208
                    Util\JpGraphError::RaiseL(25084); //('Failed to create temporary GD canvas. Out of memory ?');
209
                }
210
                $this->CopyCanvasH($tmpimg, $fromImg, 0, 0, 0, 0,
211
                    $toWidth, $toHeight, $fromWidth, $fromHeight);
212
                $fromImg = $tmpimg;
213
            }
214
            imagecopymerge($this->img, $fromImg, $toX, $toY, $fromX, $fromY, $toWidth, $toHeight, $aMix);
215
        }
216
    }
217
218
    public static function GetWidth($aImg = null)
219
    {
220
        if ($aImg === null) {
221
            $aImg = $this->img;
0 ignored issues
show
Bug introduced by
The variable $this 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...
222
        }
223
        return imagesx($aImg);
224
    }
225
226
    public static function GetHeight($aImg = null)
227
    {
228
        if ($aImg === null) {
229
            $aImg = $this->img;
0 ignored issues
show
Bug introduced by
The variable $this 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...
230
        }
231
        return imagesy($aImg);
232
    }
233
234
    public static function CreateFromString($aStr)
235
    {
236
        $img = imagecreatefromstring($aStr);
237
        if ($img === false) {
238
            Util\JpGraphError::RaiseL(25085);
239
            //('An image can not be created from the supplied string. It is either in a format not supported or the string is representing an corrupt image.');
240
        }
241
        return $img;
242
    }
243
244
    public function SetCanvasH($aHdl)
245
    {
246
        $this->img      = $aHdl;
247
        $this->rgb->img = $aHdl;
248
    }
249
250
    public function SetCanvasColor($aColor)
251
    {
252
        $this->canvascolor = $aColor;
253
    }
254
255
    public function SetAlphaBlending($aFlg = true)
256
    {
257
        ImageAlphaBlending($this->img, $aFlg);
258
    }
259
260
    public function SetAutoMargin()
261
    {
262
        $min_bm = 5;
263
        $lm     = min(40, $this->width / 7);
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...
264
        $rm     = min(20, $this->width / 10);
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...
265
        $tm     = max(5, $this->height / 7);
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...
266
        $bm     = max($min_bm, $this->height / 6);
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...
267
        $this->SetMargin($lm, $rm, $tm, $bm);
268
    }
269
270
    //---------------
271
    // PUBLIC METHODS
272
273
    public function SetFont($family, $style = FS_NORMAL, $size = 10)
274
    {
275
        $this->font_family = $family;
276
        $this->font_style  = $style;
277
        $this->font_size   = $size * SUPERSAMPLING_SCALE;
278
        $this->font_file   = '';
279
        if (($this->font_family == FF_FONT1 || $this->font_family == FF_FONT2) && $this->font_style == FS_BOLD) {
280
            ++$this->font_family;
281
        }
282
        if ($this->font_family > FF_FONT2 + 1) {
283
            // A TTF font so get the font file
284
285
            // Check that this PHP has support for TTF fonts
286
            if (!function_exists('imagettfbbox')) {
287
                // use internal font when php is configured without '--with-ttf'
288
                $this->font_family = FF_FONT1;
289
                //  Util\JpGraphError::RaiseL(25087);//('This PHP build has not been configured with TTF support. You need to recompile your PHP installation with FreeType support.');
1 ignored issue
show
Unused Code Comprehensibility introduced by
63% 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...
290
            } else {
291
                $this->font_file = $this->ttf->File($this->font_family, $this->font_style);
292
            }
293
        }
294
    }
295
296
    // Get the specific height for a text string
297
    public function GetTextHeight($txt = "", $angle = 0)
298
    {
299
        $tmp = preg_split('/\n/', $txt);
300
        $n   = count($tmp);
301
        $m   = 0;
302
        for ($i = 0; $i < $n; ++$i) {
303
            $m = max($m, strlen($tmp[$i]));
304
        }
305
306
        if ($this->font_family <= FF_FONT2 + 1) {
307 View Code Duplication
            if ($angle == 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...
308
                $h = imagefontheight($this->font_family);
309
                if ($h === false) {
310
                    Util\JpGraphError::RaiseL(25088); //('You have a misconfigured GD font support. The call to imagefontwidth() fails.');
311
                }
312
313
                return $n * $h;
314
            } else {
315
                $w = @imagefontwidth($this->font_family);
316
                if ($w === false) {
317
                    Util\JpGraphError::RaiseL(25088); //('You have a misconfigured GD font support. The call to imagefontwidth() fails.');
318
                }
319
320
                return $m * $w;
321
            }
322
        } else {
323
            $bbox = $this->GetTTFBBox($txt, $angle);
324
            return $bbox[1] - $bbox[5] + 1;
325
        }
326
    }
327
328
    // Estimate font height
329
    public function GetFontHeight($angle = 0)
330
    {
331
        $txt = "XOMg";
332
        return $this->GetTextHeight($txt, $angle);
333
    }
334
335
    // Approximate font width with width of letter "O"
336
    public function GetFontWidth($angle = 0)
337
    {
338
        $txt = 'O';
339
        return $this->GetTextWidth($txt, $angle);
340
    }
341
342
    // Get actual width of text in absolute pixels. Note that the width is the
343
    // texts projected with onto the x-axis. Call with angle=0 to get the true
344
    // etxt width.
345
    public function GetTextWidth($txt, $angle = 0)
346
    {
347
348
        $tmp = preg_split('/\n/', $txt);
349
        $n   = count($tmp);
350
        if ($this->font_family <= FF_FONT2 + 1) {
351
352
            $m = 0;
353
            for ($i = 0; $i < $n; ++$i) {
354
                $l = strlen($tmp[$i]);
355
                if ($l > $m) {
356
                    $m = $l;
357
                }
358
            }
359
360 View Code Duplication
            if ($angle == 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...
361
                $w = @imagefontwidth($this->font_family);
362
                if ($w === false) {
363
                    Util\JpGraphError::RaiseL(25088); //('You have a misconfigured GD font support. The call to imagefontwidth() fails.');
364
                }
365
                return $m * $w;
366
            } else {
367
                // 90 degrees internal so height becomes width
368
                $h = @imagefontheight($this->font_family);
369
                if ($h === false) {
370
                    Util\JpGraphError::RaiseL(25089); //('You have a misconfigured GD font support. The call to imagefontheight() fails.');
371
                }
372
                return $n * $h;
373
            }
374
        } else {
375
            // For TTF fonts we must walk through a lines and find the
376
            // widest one which we use as the width of the multi-line
377
            // paragraph
378
            $m = 0;
379
            for ($i = 0; $i < $n; ++$i) {
380
                $bbox = $this->GetTTFBBox($tmp[$i], $angle);
381
                $mm   = $bbox[2] - $bbox[0];
382
                if ($mm > $m) {
383
                    $m = $mm;
384
                }
385
386
            }
387
            return $m;
388
        }
389
    }
390
391
    // Draw text with a box around it
392
    public function StrokeBoxedText($x, $y, $txt, $dir = 0, $fcolor = "white", $bcolor = "black",
393
        $shadowcolor = false, $paragraph_align = "left",
394
        $xmarg = 6, $ymarg = 4, $cornerradius = 0, $dropwidth = 3) {
395
396
        $oldx = $this->lastx;
397
        $oldy = $this->lasty;
398
399 View Code Duplication
        if (!is_numeric($dir)) {
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...
400
            if ($dir == "h") {
401
                $dir = 0;
402
            } elseif ($dir == "v") {
403
                $dir = 90;
404
            } else {
405
                Util\JpGraphError::RaiseL(25090, $dir);
406
            }
407
            //(" Unknown direction specified in call to StrokeBoxedText() [$dir]");
408
        }
409
410
        if ($this->font_family >= FF_FONT0 && $this->font_family <= FF_FONT2 + 1) {
411
            $width  = $this->GetTextWidth($txt, $dir);
412
            $height = $this->GetTextHeight($txt, $dir);
413
        } else {
414
            $width  = $this->GetBBoxWidth($txt, $dir);
415
            $height = $this->GetBBoxHeight($txt, $dir);
416
        }
417
418
        $height += 2 * $ymarg;
419
        $width += 2 * $xmarg;
420
421
        if ($this->text_halign == "right") {
422
            $x -= $width;
423
        } elseif ($this->text_halign == "center") {
424
            $x -= $width / 2;
425
        }
426
427
        if ($this->text_valign == "bottom") {
428
            $y -= $height;
429
        } elseif ($this->text_valign == "center") {
430
            $y -= $height / 2;
431
        }
432
433
        $olda = $this->SetAngle(0);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Amenadiel\JpGraph\Image\Image as the method SetAngle() does only exist in the following sub-classes of Amenadiel\JpGraph\Image\Image: Amenadiel\JpGraph\Image\RotImage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
434
435
        if ($shadowcolor) {
436
            $this->PushColor($shadowcolor);
437
            $this->FilledRoundedRectangle($x - $xmarg + $dropwidth, $y - $ymarg + $dropwidth,
438
                $x + $width + $dropwidth, $y + $height - $ymarg + $dropwidth,
439
                $cornerradius);
440
            $this->PopColor();
441
            $this->PushColor($fcolor);
442
            $this->FilledRoundedRectangle($x - $xmarg, $y - $ymarg,
443
                $x + $width, $y + $height - $ymarg,
444
                $cornerradius);
445
            $this->PopColor();
446
            $this->PushColor($bcolor);
447
            $this->RoundedRectangle($x - $xmarg, $y - $ymarg,
448
                $x + $width, $y + $height - $ymarg, $cornerradius);
449
            $this->PopColor();
450
        } else {
451 View Code Duplication
            if ($fcolor) {
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...
452
                $oc = $this->current_color;
453
                $this->SetColor($fcolor);
454
                $this->FilledRoundedRectangle($x - $xmarg, $y - $ymarg, $x + $width, $y + $height - $ymarg, $cornerradius);
455
                $this->current_color = $oc;
456
            }
457 View Code Duplication
            if ($bcolor) {
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
                $oc = $this->current_color;
459
                $this->SetColor($bcolor);
460
                $this->RoundedRectangle($x - $xmarg, $y - $ymarg, $x + $width, $y + $height - $ymarg, $cornerradius);
461
                $this->current_color = $oc;
462
            }
463
        }
464
465
        $h = $this->text_halign;
466
        $v = $this->text_valign;
467
        $this->SetTextAlign("left", "top");
468
469
        $debug = false;
470
        $this->StrokeText($x, $y, $txt, $dir, $paragraph_align, $debug);
471
472
        $bb = [$x - $xmarg, $y + $height - $ymarg, $x + $width, $y + $height - $ymarg,
473
            $x + $width, $y - $ymarg, $x - $xmarg, $y - $ymarg];
474
        $this->SetTextAlign($h, $v);
475
476
        $this->SetAngle($olda);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Amenadiel\JpGraph\Image\Image as the method SetAngle() does only exist in the following sub-classes of Amenadiel\JpGraph\Image\Image: Amenadiel\JpGraph\Image\RotImage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
477
        $this->lastx = $oldx;
478
        $this->lasty = $oldy;
479
480
        return $bb;
481
    }
482
483
    // Draw text with a box around it. This time the box will be rotated
484
    // with the text. The previous method will just make a larger enough non-rotated
485
    // box to hold the text inside.
486
    public function StrokeBoxedText2($x, $y, $txt, $dir = 0, $fcolor = "white", $bcolor = "black",
487
        $shadowcolor = false, $paragraph_align = "left",
488
        $xmarg = 6, $ymarg = 4, $cornerradius = 0, $dropwidth = 3) {
0 ignored issues
show
Unused Code introduced by
The parameter $cornerradius 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...
489
490
        // This version of boxed text will stroke a rotated box round the text
491
        // thta will follow the angle of the text.
492
        // This has two implications:
493
        // 1) This methos will only support TTF fonts
494
        // 2) The only two alignment that makes sense are centered or baselined
495
496
        if ($this->font_family <= FF_FONT2 + 1) {
497
            Util\JpGraphError::RaiseL(25131); //StrokeBoxedText2() Only support TTF fonts and not built in bitmap fonts
498
        }
499
500
        $oldx = $this->lastx;
501
        $oldy = $this->lasty;
502
        $dir  = $this->NormAngle($dir);
503
504 View Code Duplication
        if (!is_numeric($dir)) {
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...
505
            if ($dir == "h") {
506
                $dir = 0;
507
            } elseif ($dir == "v") {
508
                $dir = 90;
509
            } else {
510
                Util\JpGraphError::RaiseL(25090, $dir);
511
            }
512
            //(" Unknown direction specified in call to StrokeBoxedText() [$dir]");
513
        }
514
515
        $width       = $this->GetTextWidth($txt, 0) + 2 * $xmarg;
516
        $height      = $this->GetTextHeight($txt, 0) + 2 * $ymarg;
517
        $rect_width  = $this->GetBBoxWidth($txt, $dir);
518
        $rect_height = $this->GetBBoxHeight($txt, $dir);
519
520
        $baseline_offset = $this->bbox_cache[1] - 1;
0 ignored issues
show
Unused Code introduced by
$baseline_offset 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...
521
522
        if ($this->text_halign == "center") {
523
            if ($dir >= 0 && $dir <= 90) {
524
525
                $x -= $rect_width / 2;
526
                $x += sin($dir * M_PI / 180) * $height;
527
                $y += $rect_height / 2;
528
529
            } elseif ($dir >= 270 && $dir <= 360) {
530
531
                $x -= $rect_width / 2;
532
                $y -= $rect_height / 2;
533
                $y += cos($dir * M_PI / 180) * $height;
534
535
            } elseif ($dir >= 90 && $dir <= 180) {
536
537
                $x += $rect_width / 2;
538
                $y += $rect_height / 2;
539
                $y += cos($dir * M_PI / 180) * $height;
540
541
            } else {
542
                // $dir > 180 &&  $dir < 270
1 ignored issue
show
Unused Code Comprehensibility introduced by
36% 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...
543
                $x += $rect_width / 2;
544
                $x += sin($dir * M_PI / 180) * $height;
545
                $y -= $rect_height / 2;
546
            }
547
        }
548
549
        // Rotate the box around this point
550
        $this->SetCenter($x, $y);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Amenadiel\JpGraph\Image\Image as the method SetCenter() does only exist in the following sub-classes of Amenadiel\JpGraph\Image\Image: Amenadiel\JpGraph\Image\RotImage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
551
        $olda = $this->SetAngle(-$dir);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Amenadiel\JpGraph\Image\Image as the method SetAngle() does only exist in the following sub-classes of Amenadiel\JpGraph\Image\Image: Amenadiel\JpGraph\Image\RotImage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
552
553
        // We need to use adjusted coordinats for the box to be able
554
        // to draw the box below the baseline. This cannot be done before since
555
        // the rotating point must be the original x,y since that is arounbf the
556
        // point where the text will rotate and we cannot change this since
557
        // that is where the GD/GreeType will rotate the text
558
559
        // For smaller <14pt font we need to do some additional
560
        // adjustments to make it look good
561
        if ($this->font_size < 14) {
562
            $x -= 2;
563
            $y += 2;
564
        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

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

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
565
            //  $y += $baseline_offset;
1 ignored issue
show
Unused Code Comprehensibility introduced by
38% 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...
566
        }
567
568
        if ($shadowcolor) {
569
            $this->PushColor($shadowcolor);
570
            $this->FilledRectangle($x - $xmarg + $dropwidth, $y + $ymarg + $dropwidth - $height,
571
                $x + $width + $dropwidth, $y + $ymarg + $dropwidth);
572
            //$cornerradius);
573
            $this->PopColor();
574
            $this->PushColor($fcolor);
575
            $this->FilledRectangle($x - $xmarg, $y + $ymarg - $height,
576
                $x + $width, $y + $ymarg);
577
            //$cornerradius);
578
            $this->PopColor();
579
            $this->PushColor($bcolor);
580
            $this->Rectangle($x - $xmarg, $y + $ymarg - $height,
581
                $x + $width, $y + $ymarg);
582
            //$cornerradius);
583
            $this->PopColor();
584
        } else {
585 View Code Duplication
            if ($fcolor) {
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...
586
                $oc = $this->current_color;
587
                $this->SetColor($fcolor);
588
                $this->FilledRectangle($x - $xmarg, $y + $ymarg - $height, $x + $width, $y + $ymarg); //,$cornerradius);
589
                $this->current_color = $oc;
590
            }
591 View Code Duplication
            if ($bcolor) {
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...
592
                $oc = $this->current_color;
593
                $this->SetColor($bcolor);
594
                $this->Rectangle($x - $xmarg, $y + $ymarg - $height, $x + $width, $y + $ymarg); //,$cornerradius);
595
                $this->current_color = $oc;
596
            }
597
        }
598
599
        if ($this->font_size < 14) {
600
            $x += 2;
601
            $y -= 2;
602
        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

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

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
603
604
            // Restore the original y before we stroke the text
605
            // $y -= $baseline_offset;
1 ignored issue
show
Unused Code Comprehensibility introduced by
43% 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...
606
607
        }
608
609
        $this->SetCenter(0, 0);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Amenadiel\JpGraph\Image\Image as the method SetCenter() does only exist in the following sub-classes of Amenadiel\JpGraph\Image\Image: Amenadiel\JpGraph\Image\RotImage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
610
        $this->SetAngle($olda);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Amenadiel\JpGraph\Image\Image as the method SetAngle() does only exist in the following sub-classes of Amenadiel\JpGraph\Image\Image: Amenadiel\JpGraph\Image\RotImage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
611
612
        $h = $this->text_halign;
613
        $v = $this->text_valign;
614
        if ($this->text_halign == 'center') {
615
            $this->SetTextAlign('center', 'basepoint');
616
        } else {
617
            $this->SetTextAlign('basepoint', 'basepoint');
618
        }
619
620
        $debug = false;
621
        $this->StrokeText($x, $y, $txt, $dir, $paragraph_align, $debug);
622
623
        $bb = [$x - $xmarg, $y + $height - $ymarg,
624
            $x + $width, $y + $height - $ymarg,
625
            $x + $width, $y - $ymarg,
626
            $x - $xmarg, $y - $ymarg];
627
628
        $this->SetTextAlign($h, $v);
629
        $this->SetAngle($olda);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Amenadiel\JpGraph\Image\Image as the method SetAngle() does only exist in the following sub-classes of Amenadiel\JpGraph\Image\Image: Amenadiel\JpGraph\Image\RotImage. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
630
631
        $this->lastx = $oldx;
632
        $this->lasty = $oldy;
633
634
        return $bb;
635
    }
636
637
    // Set text alignment
638
    public function SetTextAlign($halign, $valign = "bottom")
639
    {
640
        $this->text_halign = $halign;
641
        $this->text_valign = $valign;
642
    }
643
644
    public function _StrokeBuiltinFont($x, $y, $txt, $dir, $paragraph_align, &$aBoundingBox, $aDebug = false)
645
    {
646
647
        if (is_numeric($dir) && $dir != 90 && $dir != 0) {
648
            Util\JpGraphError::RaiseL(25091);
649
        }
650
        //(" Internal font does not support drawing text at arbitrary angle. Use TTF fonts instead.");
651
652
        $h  = $this->GetTextHeight($txt);
653
        $fh = $this->GetFontHeight();
654
        $w  = $this->GetTextWidth($txt);
655
656 View Code Duplication
        if ($this->text_halign == "right") {
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...
657
            $x -= $dir == 0 ? $w : $h;
658
        } elseif ($this->text_halign == "center") {
659
            // For center we subtract 1 pixel since this makes the middle
660
            // be prefectly in the middle
661
            $x -= $dir == 0 ? $w / 2 - 1 : $h / 2;
662
        }
663 View Code Duplication
        if ($this->text_valign == "top") {
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...
664
            $y += $dir == 0 ? $h : $w;
665
        } elseif ($this->text_valign == "center") {
666
            $y += $dir == 0 ? $h / 2 : $w / 2;
667
        }
668
669
        $use_font = $this->font_family;
670
671
        if ($dir == 90) {
672
            imagestringup($this->img, $use_font, $x, $y, $txt, $this->current_color);
673
            $aBoundingBox = [round($x), round($y), round($x), round($y - $w), round($x + $h), round($y - $w), round($x + $h), round($y)];
674
            if ($aDebug) {
675
                // Draw bounding box
676
                $this->PushColor('green');
677
                $this->Polygon($aBoundingBox, true);
678
                $this->PopColor();
679
            }
680
        } else {
681
            if (preg_match('/\n/', $txt)) {
682
                $tmp = preg_split('/\n/', $txt);
683
                for ($i = 0; $i < count($tmp); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

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

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

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
684
                    $w1 = $this->GetTextWidth($tmp[$i]);
685
                    if ($paragraph_align == "left") {
686
                        imagestring($this->img, $use_font, $x, $y - $h + 1 + $i * $fh, $tmp[$i], $this->current_color);
687
                    } elseif ($paragraph_align == "right") {
688
                        imagestring($this->img, $use_font, $x + ($w - $w1), $y - $h + 1 + $i * $fh, $tmp[$i], $this->current_color);
689
                    } else {
690
                        imagestring($this->img, $use_font, $x + $w / 2 - $w1 / 2, $y - $h + 1 + $i * $fh, $tmp[$i], $this->current_color);
691
                    }
692
                }
693
            } else {
694
                //Put the text
695
                imagestring($this->img, $use_font, $x, $y - $h + 1, $txt, $this->current_color);
696
            }
697
            if ($aDebug) {
698
                // Draw the bounding rectangle and the bounding box
699
                $p1 = [round($x), round($y), round($x), round($y - $h), round($x + $w), round($y - $h), round($x + $w), round($y)];
700
701
                // Draw bounding box
702
                $this->PushColor('green');
703
                $this->Polygon($p1, true);
704
                $this->PopColor();
705
706
            }
707
            $aBoundingBox = [round($x), round($y), round($x), round($y - $h), round($x + $w), round($y - $h), round($x + $w), round($y)];
708
        }
709
    }
710
711
    public function AddTxtCR($aTxt)
712
    {
713
        // If the user has just specified a '\n'
714
        // instead of '\n\t' we have to add '\r' since
715
        // the width will be too muchy otherwise since when
716
        // we print we stroke the individually lines by hand.
717
        $e = explode("\n", $aTxt);
718
        $n = count($e);
719
        for ($i = 0; $i < $n; ++$i) {
720
            $e[$i] = str_replace("\r", "", $e[$i]);
721
        }
722
        return implode("\n\r", $e);
723
    }
724
725
    public function NormAngle($a)
726
    {
727
        // Normalize angle in degrees
728
        // Normalize angle to be between 0-360
729
        while ($a > 360) {
730
            $a -= 360;
731
        }
732
733
        while ($a < -360) {
734
            $a += 360;
735
        }
736
737
        if ($a < 0) {
738
            $a = 360 + $a;
739
        }
740
741
        return $a;
742
    }
743
744
    public function imagettfbbox_fixed($size, $angle, $fontfile, $text)
745
    {
746
747
        if (!USE_LIBRARY_IMAGETTFBBOX) {
748
749
            $bbox = @imagettfbbox($size, $angle, $fontfile, $text);
750
            if ($bbox === false) {
751
                Util\JpGraphError::RaiseL(25092, $this->font_file);
752
                //("There is either a configuration problem with TrueType or a problem reading font file (".$this->font_file."). Make sure file exists and is in a readable place for the HTTP process. (If 'basedir' restriction is enabled in PHP then the font file must be located in the document root.). It might also be a wrongly installed FreeType library. Try uppgrading to at least FreeType 2.1.13 and recompile GD with the correct setup so it can find the new FT library.");
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...
753
            }
754
            $this->bbox_cache = $bbox;
755
            return $bbox;
756
        }
757
758
        // The built in imagettfbbox is buggy for angles != 0 so
759
        // we calculate this manually by getting the bounding box at
760
        // angle = 0 and then rotate the bounding box manually
761
        $bbox = @imagettfbbox($size, 0, $fontfile, $text);
762
        if ($bbox === false) {
763
            Util\JpGraphError::RaiseL(25092, $this->font_file);
764
            //("There is either a configuration problem with TrueType or a problem reading font file (".$this->font_file."). Make sure file exists and is in a readable place for the HTTP process. (If 'basedir' restriction is enabled in PHP then the font file must be located in the document root.). It might also be a wrongly installed FreeType library. Try uppgrading to at least FreeType 2.1.13 and recompile GD with the correct setup so it can find the new FT library.");
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...
765
        }
766
767
        $angle = $this->NormAngle($angle);
768
769
        $a   = $angle * M_PI / 180;
770
        $ca  = cos($a);
771
        $sa  = sin($a);
772
        $ret = [];
773
774
        // We always add 1 pixel to the left since the left edge of the bounding
775
        // box is sometimes coinciding with the first pixel of the text
776
        //$bbox[0] -= 1;
1 ignored issue
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
777
        //$bbox[6] -= 1;
1 ignored issue
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
778
779
        // For roatated text we need to add extra width for rotated
780
        // text since the kerning and stroking of the TTF is not the same as for
781
        // text at a 0 degree angle
782
783
        if ($angle > 0.001 && abs($angle - 360) > 0.001) {
784
            $h = abs($bbox[7] - $bbox[1]);
785
            $w = abs($bbox[2] - $bbox[0]);
786
787
            $bbox[0] -= 2;
788
            $bbox[6] -= 2;
789
            // The width is underestimated so compensate for that
790
            $bbox[2] += round($w * 0.06);
791
            $bbox[4] += round($w * 0.06);
792
793
            // and we also need to compensate with increased height
794
            $bbox[5] -= round($h * 0.1);
795
            $bbox[7] -= round($h * 0.1);
796
797
            if ($angle > 90) {
798
                // For angles > 90 we also need to extend the height further down
799
                // by the baseline since that is also one more problem
800
                $bbox[1] += round($h * 0.15);
801
                $bbox[3] += round($h * 0.15);
802
803
                // and also make it slighty less height
804
                $bbox[7] += round($h * 0.05);
805
                $bbox[5] += round($h * 0.05);
806
807
                // And we need to move the box slightly top the rright (from a tetx perspective)
808
                $bbox[0] += round($w * 0.02);
809
                $bbox[6] += round($w * 0.02);
810
811
                if ($angle > 180) {
812
                    // And we need to move the box slightly to the left (from a text perspective)
813
                    $bbox[0] -= round($w * 0.02);
814
                    $bbox[6] -= round($w * 0.02);
815
                    $bbox[2] -= round($w * 0.02);
816
                    $bbox[4] -= round($w * 0.02);
817
818
                }
819
820
            }
821
            for ($i = 0; $i < 7; $i += 2) {
822
                $ret[$i]     = round($bbox[$i] * $ca + $bbox[$i + 1] * $sa);
823
                $ret[$i + 1] = round($bbox[$i + 1] * $ca - $bbox[$i] * $sa);
824
            }
825
            $this->bbox_cache = $ret;
826
            return $ret;
827
        } else {
828
            $this->bbox_cache = $bbox;
829
            return $bbox;
830
        }
831
    }
832
833
    // Deprecated
834
    public function GetTTFBBox($aTxt, $aAngle = 0)
835
    {
836
        $bbox = $this->imagettfbbox_fixed($this->font_size, $aAngle, $this->font_file, $aTxt);
837
        return $bbox;
838
    }
839
840
    public function GetBBoxTTF($aTxt, $aAngle = 0)
841
    {
842
        // Normalize the bounding box to become a minimum
843
        // enscribing rectangle
844
845
        $aTxt = $this->AddTxtCR($aTxt);
846
847
        if (!is_readable($this->font_file)) {
848
            Util\JpGraphError::RaiseL(25093, $this->font_file);
849
            //('Can not read font file ('.$this->font_file.') in call to Image::GetBBoxTTF. Please make sure that you have set a font before calling this method and that the font is installed in the TTF directory.');
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...
850
        }
851
        $bbox = $this->imagettfbbox_fixed($this->font_size, $aAngle, $this->font_file, $aTxt);
852
853
        if ($aAngle == 0) {
854
            return $bbox;
855
        }
856
857
        if ($aAngle >= 0) {
858 View Code Duplication
            if ($aAngle <= 90) {
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...
859
                //<=0
860
                $bbox = [$bbox[6], $bbox[1], $bbox[2], $bbox[1],
861
                    $bbox[2], $bbox[5], $bbox[6], $bbox[5]];
862
            } elseif ($aAngle <= 180) {
863
                //<= 2
864
                $bbox = [$bbox[4], $bbox[7], $bbox[0], $bbox[7],
865
                    $bbox[0], $bbox[3], $bbox[4], $bbox[3]];
866
            } elseif ($aAngle <= 270) {
867
                //<= 3
868
                $bbox = [$bbox[2], $bbox[5], $bbox[6], $bbox[5],
869
                    $bbox[6], $bbox[1], $bbox[2], $bbox[1]];
870
            } else {
871
                $bbox = [$bbox[0], $bbox[3], $bbox[4], $bbox[3],
872
                    $bbox[4], $bbox[7], $bbox[0], $bbox[7]];
873
            }
874
        } elseif ($aAngle < 0) {
875 View Code Duplication
            if ($aAngle <= -270) {
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...
876
                // <= -3
877
                $bbox = [$bbox[6], $bbox[1], $bbox[2], $bbox[1],
878
                    $bbox[2], $bbox[5], $bbox[6], $bbox[5]];
879
            } elseif ($aAngle <= -180) {
880
                // <= -2
881
                $bbox = [$bbox[0], $bbox[3], $bbox[4], $bbox[3],
882
                    $bbox[4], $bbox[7], $bbox[0], $bbox[7]];
883
            } elseif ($aAngle <= -90) {
884
                // <= -1
885
                $bbox = [$bbox[2], $bbox[5], $bbox[6], $bbox[5],
886
                    $bbox[6], $bbox[1], $bbox[2], $bbox[1]];
887
            } else {
888
                $bbox = [$bbox[0], $bbox[3], $bbox[4], $bbox[3],
889
                    $bbox[4], $bbox[7], $bbox[0], $bbox[7]];
890
            }
891
        }
892
        return $bbox;
893
    }
894
895
    public function GetBBoxHeight($aTxt, $aAngle = 0)
896
    {
897
        $box = $this->GetBBoxTTF($aTxt, $aAngle);
898
        return abs($box[7] - $box[1]);
899
    }
900
901
    public function GetBBoxWidth($aTxt, $aAngle = 0)
902
    {
903
        $box = $this->GetBBoxTTF($aTxt, $aAngle);
904
        return $box[2] - $box[0] + 1;
905
    }
906
907
    public function _StrokeTTF($x, $y, $txt, $dir, $paragraph_align, &$aBoundingBox, $debug = false)
908
    {
909
910
        // Setup default inter line margin for paragraphs to be
911
        // 3% of the font height.
912
        $ConstLineSpacing = 0.03;
913
914
        // Remember the anchor point before adjustment
915
        if ($debug) {
916
            $ox = $x;
917
            $oy = $y;
918
        }
919
920
        if (!preg_match('/\n/', $txt) || ($dir > 0 && preg_match('/\n/', $txt))) {
921
            // Format a single line
922
923
            $txt    = $this->AddTxtCR($txt);
924
            $bbox   = $this->GetBBoxTTF($txt, $dir);
925
            $width  = $this->GetBBoxWidth($txt, $dir);
926
            $height = $this->GetBBoxHeight($txt, $dir);
927
928
            // The special alignment "basepoint" is mostly used internally
929
            // in the library. This will put the anchor position at the left
930
            // basepoint of the tetx. This is the default anchor point for
931
            // TTF text.
932
933
            if ($this->text_valign != 'basepoint') {
934
                // Align x,y ot lower left corner of bbox
935
936
                if ($this->text_halign == 'right') {
937
                    $x -= $width;
938
                    $x -= $bbox[0];
939
                } elseif ($this->text_halign == 'center') {
940
                    $x -= $width / 2;
941
                    $x -= $bbox[0];
942
                } elseif ($this->text_halign == 'baseline') {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

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

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

Loading history...
943
                    // This is only support for text at 90 degree !!
944
                    // Do nothing the text is drawn at baseline by default
945
                }
946
947
                if ($this->text_valign == 'top') {
948
                    $y -= $bbox[1]; // Adjust to bottom of text
949
                    $y += $height;
950
                } elseif ($this->text_valign == 'center') {
951
                    $y -= $bbox[1]; // Adjust to bottom of text
952
                    $y += $height / 2;
953
                } elseif ($this->text_valign == 'baseline') {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

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

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

Loading history...
954
                    // This is only support for text at 0 degree !!
955
                    // Do nothing the text is drawn at baseline by default
956
                }
957
            }
958
            ImageTTFText($this->img, $this->font_size, $dir, $x, $y,
959
                $this->current_color, $this->font_file, $txt);
960
961
            // Calculate and return the co-ordinates for the bounding box
962
            $box = $this->imagettfbbox_fixed($this->font_size, $dir, $this->font_file, $txt);
963
            $p1  = [];
964
965
            for ($i = 0; $i < 4; ++$i) {
966
                $p1[] = round($box[$i * 2] + $x);
967
                $p1[] = round($box[$i * 2 + 1] + $y);
968
            }
969
            $aBoundingBox = $p1;
970
971
            // Debugging code to highlight the bonding box and bounding rectangle
972
            // For text at 0 degrees the bounding box and bounding rectangle are the
973
            // same
974
            if ($debug) {
975
                // Draw the bounding rectangle and the bounding box
976
977
                $p  = [];
978
                $p1 = [];
979
980
                for ($i = 0; $i < 4; ++$i) {
981
                    $p[]  = $bbox[$i * 2] + $x;
982
                    $p[]  = $bbox[$i * 2 + 1] + $y;
983
                    $p1[] = $box[$i * 2] + $x;
984
                    $p1[] = $box[$i * 2 + 1] + $y;
985
                }
986
987
                // Draw bounding box
988
                $this->PushColor('green');
989
                $this->Polygon($p1, true);
990
                $this->PopColor();
991
992
                // Draw bounding rectangle
993
                $this->PushColor('darkgreen');
994
                $this->Polygon($p, true);
995
                $this->PopColor();
996
997
                // Draw a cross at the anchor point
998
                $this->PushColor('red');
999
                $this->Line($ox - 15, $oy, $ox + 15, $oy);
0 ignored issues
show
Bug introduced by
The variable $ox 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 $oy 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...
1000
                $this->Line($ox, $oy - 15, $ox, $oy + 15);
1001
                $this->PopColor();
1002
            }
1003
        } else {
1004
            // Format a text paragraph
1005
            $fh = $this->GetFontHeight();
1006
1007
            // Line margin is 25% of font height
1008
            $linemargin = round($fh * $ConstLineSpacing);
1009
            $fh += $linemargin;
1010
            $w = $this->GetTextWidth($txt);
1011
1012
            $y -= $linemargin / 2;
1013
            $tmp = preg_split('/\n/', $txt);
1014
            $nl  = count($tmp);
1015
            $h   = $nl * $fh;
1016
1017 View Code Duplication
            if ($this->text_halign == 'right') {
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...
1018
                $x -= $dir == 0 ? $w : $h;
1019
            } elseif ($this->text_halign == 'center') {
1020
                $x -= $dir == 0 ? $w / 2 : $h / 2;
1021
            }
1022
1023 View Code Duplication
            if ($this->text_valign == 'top') {
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...
1024
                $y += $dir == 0 ? $h : $w;
1025
            } elseif ($this->text_valign == 'center') {
1026
                $y += $dir == 0 ? $h / 2 : $w / 2;
1027
            }
1028
1029
            // Here comes a tricky bit.
1030
            // Since we have to give the position for the string at the
1031
            // baseline this means thaht text will move slightly up
1032
            // and down depending on any of it's character descend below
1033
            // the baseline, for example a 'g'. To adjust the Y-position
1034
            // we therefore adjust the text with the baseline Y-offset
1035
            // as used for the current font and size. This will keep the
1036
            // baseline at a fixed positoned disregarding the actual
1037
            // characters in the string.
1038
            $standardbox  = $this->GetTTFBBox('Gg', $dir);
1039
            $yadj         = $standardbox[1];
1040
            $xadj         = $standardbox[0];
0 ignored issues
show
Unused Code introduced by
$xadj 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...
1041
            $aBoundingBox = [];
1042
            for ($i = 0; $i < $nl; ++$i) {
1043
                $wl   = $this->GetTextWidth($tmp[$i]);
1044
                $bbox = $this->GetTTFBBox($tmp[$i], $dir);
1045
                if ($paragraph_align == 'left') {
1046
                    $xl = $x;
1047
                } elseif ($paragraph_align == 'right') {
1048
                    $xl = $x + ($w - $wl);
1049
                } else {
1050
                    // Center
1051
                    $xl = $x + $w / 2 - $wl / 2;
1052
                }
1053
1054
                // In theory we should adjust with full pre-lead to get the lines
1055
                // lined up but this doesn't look good so therfore we only adjust with
1056
                // half th pre-lead
1057
                $xl -= $bbox[0] / 2;
1058
                $yl = $y - $yadj;
1059
                //$xl = $xl- $xadj;
1 ignored issue
show
Unused Code Comprehensibility introduced by
45% 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...
1060
                ImageTTFText($this->img, $this->font_size, $dir, $xl, $yl - ($h - $fh) + $fh * $i,
1061
                    $this->current_color, $this->font_file, $tmp[$i]);
1062
1063
                // echo "xl=$xl,".$tmp[$i]." <br>";
1 ignored issue
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
1064
                if ($debug) {
1065
                    // Draw the bounding rectangle around each line
1066
                    $box = @ImageTTFBBox($this->font_size, $dir, $this->font_file, $tmp[$i]);
0 ignored issues
show
Unused Code introduced by
$box 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...
1067
                    $p   = [];
1068
                    for ($j = 0; $j < 4; ++$j) {
1069
                        $p[] = $bbox[$j * 2] + $xl;
1070
                        $p[] = $bbox[$j * 2 + 1] + $yl - ($h - $fh) + $fh * $i;
1071
                    }
1072
1073
                    // Draw bounding rectangle
1074
                    $this->PushColor('darkgreen');
1075
                    $this->Polygon($p, true);
1076
                    $this->PopColor();
1077
                }
1078
            }
1079
1080
            // Get the bounding box
1081
            $bbox = $this->GetBBoxTTF($txt, $dir);
1082
            for ($j = 0; $j < 4; ++$j) {
1083
                $bbox[$j * 2] += round($x);
1084
                $bbox[$j * 2 + 1] += round($y - ($h - $fh) - $yadj);
1085
            }
1086
            $aBoundingBox = $bbox;
1087
1088
            if ($debug) {
1089
                // Draw a cross at the anchor point
1090
                $this->PushColor('red');
1091
                $this->Line($ox - 25, $oy, $ox + 25, $oy);
1092
                $this->Line($ox, $oy - 25, $ox, $oy + 25);
1093
                $this->PopColor();
1094
            }
1095
1096
        }
1097
    }
1098
1099
    public function StrokeText($x, $y, $txt, $dir = 0, $paragraph_align = "left", $debug = false)
1100
    {
1101
1102
        $x = round($x);
1103
        $y = round($y);
1104
1105
        // Do special language encoding
1106
        $txt = $this->langconv->Convert($txt, $this->font_family);
1107
1108
        if (!is_numeric($dir)) {
1109
            Util\JpGraphError::RaiseL(25094); //(" Direction for text most be given as an angle between 0 and 90.");
1110
        }
1111
1112
        if ($this->font_family >= FF_FONT0 && $this->font_family <= FF_FONT2 + 1) {
1113
            $this->_StrokeBuiltinFont($x, $y, $txt, $dir, $paragraph_align, $boundingbox, $debug);
1114
        } elseif ($this->font_family >= _FIRST_FONT && $this->font_family <= _LAST_FONT) {
1115
            $this->_StrokeTTF($x, $y, $txt, $dir, $paragraph_align, $boundingbox, $debug);
1116
        } else {
1117
            Util\JpGraphError::RaiseL(25095); //(" Unknown font font family specification. ");
1118
        }
1119
        return $boundingbox;
0 ignored issues
show
Bug introduced by
The variable $boundingbox 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...
1120
    }
1121
1122
    public function SetMargin($lm, $rm, $tm, $bm)
1123
    {
1124
1125
        $this->left_margin   = $lm;
0 ignored issues
show
Bug introduced by
The property left_margin does not seem to exist. Did you mean _left_margin?

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...
1126
        $this->right_margin  = $rm;
0 ignored issues
show
Bug introduced by
The property right_margin does not seem to exist. Did you mean _right_margin?

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...
1127
        $this->top_margin    = $tm;
0 ignored issues
show
Bug introduced by
The property top_margin does not seem to exist. Did you mean _top_margin?

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...
1128
        $this->bottom_margin = $bm;
0 ignored issues
show
Bug introduced by
The property bottom_margin does not seem to exist. Did you mean _bottom_margin?

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...
1129
1130
        $this->plotwidth  = $this->width - $this->left_margin - $this->right_margin;
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 left_margin does not seem to exist. Did you mean _left_margin?

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 right_margin does not seem to exist. Did you mean _right_margin?

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...
Documentation Bug introduced by
It seems like $this->width - $this->le...n - $this->right_margin can also be of type double. However, the property $plotwidth is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
1131
        $this->plotheight = $this->height - $this->top_margin - $this->bottom_margin;
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...
Bug introduced by
The property top_margin does not seem to exist. Did you mean _top_margin?

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 bottom_margin does not seem to exist. Did you mean _bottom_margin?

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...
Documentation Bug introduced by
It seems like $this->height - $this->t... - $this->bottom_margin can also be of type double. However, the property $plotheight is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
1132
1133
        if ($this->width > 0 && $this->height > 0) {
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...
1134
            if ($this->plotwidth < 0 || $this->plotheight < 0) {
1135
                Util\JpGraphError::RaiseL(25130, $this->plotwidth, $this->plotheight);
1136
                //Util\JpGraphError::raise("To small plot area. ($lm,$rm,$tm,$bm : $this->plotwidth x $this->plotheight). With the given image size and margins there is to little space left for the plot. Increase the plot size or reduce the margins.");
1 ignored issue
show
Unused Code Comprehensibility introduced by
72% 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...
1137
            }
1138
        }
1139
    }
1140
1141
    public function SetTransparent($color)
1142
    {
1143
        imagecolortransparent($this->img, $this->rgb->allocate($color));
1144
    }
1145
1146
    public function SetColor($color, $aAlpha = 0)
1147
    {
1148
        $this->current_color_name = $color;
1149
        $this->current_color      = $this->rgb->allocate($color, $aAlpha);
1150
        if ($this->current_color == -1) {
1151
            $tc = imagecolorstotal($this->img);
0 ignored issues
show
Unused Code introduced by
$tc 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...
1152
            Util\JpGraphError::RaiseL(25096);
1153
            //("Can't allocate any more colors. Image has already allocated maximum of <b>$tc colors</b>. This might happen if you have anti-aliasing turned on together with a background image or perhaps gradient fill since this requires many, many colors. Try to turn off anti-aliasing. If there is still a problem try downgrading the quality of the background image to use a smaller pallete to leave some entries for your graphs. You should try to limit the number of colors in your background image to 64. If there is still problem set the constant DEFINE(\"USE_APPROX_COLORS\",true); in jpgraph.php This will use approximative colors when the palette is full. Unfortunately there is not much JpGraph can do about this since the palette size is a limitation of current graphic format and what the underlying GD library suppports.");
1 ignored issue
show
Unused Code Comprehensibility introduced by
84% 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...
1154
        }
1155
        return $this->current_color;
1156
    }
1157
1158
    public function PushColor($color)
1159
    {
1160
        if ($color != "") {
1161
            $this->colorstack[$this->colorstackidx]     = $this->current_color_name;
1162
            $this->colorstack[$this->colorstackidx + 1] = $this->current_color;
1163
            $this->colorstackidx += 2;
1164
            $this->SetColor($color);
1165
        } else {
1166
            Util\JpGraphError::RaiseL(25097); //("Color specified as empty string in PushColor().");
1167
        }
1168
    }
1169
1170
    public function PopColor()
1171
    {
1172
        if ($this->colorstackidx < 1) {
1173
            Util\JpGraphError::RaiseL(25098); //(" Negative Color stack index. Unmatched call to PopColor()");
1174
        }
1175
        $this->current_color      = $this->colorstack[--$this->colorstackidx];
1176
        $this->current_color_name = $this->colorstack[--$this->colorstackidx];
1177
    }
1178
1179
    public function SetLineWeight($weight)
1180
    {
1181
        $old = $this->line_weight;
0 ignored issues
show
Bug introduced by
The property line_weight does not seem to exist. Did you mean _line_weight?

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...
1182
        imagesetthickness($this->img, $weight);
1183
        $this->line_weight = $weight;
0 ignored issues
show
Bug introduced by
The property line_weight does not seem to exist. Did you mean _line_weight?

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...
1184
        return $old;
1185
    }
1186
1187
    public function SetStartPoint($x, $y)
1188
    {
1189
        $this->lastx = round($x);
0 ignored issues
show
Documentation Bug introduced by
The property $lastx was declared of type integer, but round($x) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
1190
        $this->lasty = round($y);
0 ignored issues
show
Documentation Bug introduced by
The property $lasty was declared of type integer, but round($y) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
1191
    }
1192
1193
    public function Arc($cx, $cy, $w, $h, $s, $e)
1194
    {
1195
        // GD Arc doesn't like negative angles
1196
        while ($s < 0) {
1197
            $s += 360;
1198
        }
1199
1200
        while ($e < 0) {
1201
            $e += 360;
1202
        }
1203
1204
        imagearc($this->img, round($cx), round($cy), round($w), round($h), $s, $e, $this->current_color);
1205
    }
1206
1207
    public function FilledArc($xc, $yc, $w, $h, $s, $e, $style = '')
1208
    {
1209
        $s = round($s);
1210
        $e = round($e);
1211
        while ($s < 0) {
1212
            $s += 360;
1213
        }
1214
1215
        while ($e < 0) {
1216
            $e += 360;
1217
        }
1218
1219
        if ($style == '') {
1220
            $style = IMG_ARC_PIE;
1221
        }
1222
1223
        if (abs($s - $e) > 0) {
1224
            imagefilledarc($this->img, round($xc), round($yc), round($w), round($h), $s, $e, $this->current_color, $style);
1225
            //            $this->DrawImageSmoothArc($this->img,round($xc),round($yc),round($w),round($h),$s,$e,$this->current_color,$style);
1 ignored issue
show
Unused Code Comprehensibility introduced by
80% 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...
1226
        }
1227
    }
1228
1229
    public function FilledCakeSlice($cx, $cy, $w, $h, $s, $e)
1230
    {
1231
        $this->CakeSlice($cx, $cy, $w, $h, $s, $e, $this->current_color_name);
1232
    }
1233
1234
    public function CakeSlice($xc, $yc, $w, $h, $s, $e, $fillcolor = "", $arccolor = "")
1235
    {
1236
        $s  = round($s);
1237
        $e  = round($e);
1238
        $w  = round($w);
1239
        $h  = round($h);
1240
        $xc = round($xc);
1241
        $yc = round($yc);
1242
        if ($s == $e) {
1243
            // A full circle. We draw this a plain circle
1244
            $this->PushColor($fillcolor);
1245
            imagefilledellipse($this->img, $xc, $yc, 2 * $w, 2 * $h, $this->current_color);
1246
1247
            // If antialiasing is used then we often don't have any color no the surrounding
1248
            // arc. So, we need to check for this special case so we don't send an empty
1249
            // color to the push function. In this case we use the fill color for the arc as well
1250
            if ($arccolor != '') {
1251
                $this->PopColor();
1252
                $this->PushColor($arccolor);
1253
            }
1254
            imageellipse($this->img, $xc, $yc, 2 * $w, 2 * $h, $this->current_color);
1255
            $this->Line($xc, $yc, cos($s * M_PI / 180) * $w + $xc, $yc + sin($s * M_PI / 180) * $h);
1256
            $this->PopColor();
1257
        } else {
1258
            $this->PushColor($fillcolor);
1259
            $this->FilledArc($xc, $yc, 2 * $w, 2 * $h, $s, $e);
1260
            $this->PopColor();
1261
            if ($arccolor != "") {
1262
                $this->PushColor($arccolor);
1263
                // We add 2 pixels to make the Arc() better aligned with
1264
                // the filled arc.
1265
                imagefilledarc($this->img, $xc, $yc, 2 * $w, 2 * $h, $s, $e, $this->current_color, IMG_ARC_NOFILL | IMG_ARC_EDGED);
1266
                $this->PopColor();
1267
            }
1268
        }
1269
    }
1270
1271
    public function Ellipse($xc, $yc, $w, $h)
1272
    {
1273
        $this->Arc($xc, $yc, $w, $h, 0, 360);
1274
    }
1275
1276
    public function Circle($xc, $yc, $r)
1277
    {
1278
        imageellipse($this->img, round($xc), round($yc), $r * 2, $r * 2, $this->current_color);
1279
        //        $this->DrawImageSmoothArc($this->img,round($xc),round($yc),$r*2+1,$r*2+1,0,360,$this->current_color);
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...
1280
        //        $this->imageSmoothCircle($this->img, round($xc),round($yc), $r*2+1, $this->current_color);
1 ignored issue
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
1281
    }
1282
1283
    public function FilledCircle($xc, $yc, $r)
1284
    {
1285
        imagefilledellipse($this->img, round($xc), round($yc), 2 * $r, 2 * $r, $this->current_color);
1286
        //        $this->DrawImageSmoothArc($this->img, round($xc), round($yc), 2*$r, 2*$r, 0, 360, $this->current_color);
1 ignored issue
show
Unused Code Comprehensibility introduced by
66% 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...
1287
    }
1288
1289
    // Linear Color InterPolation
1290
    public function lip($f, $t, $p)
1291
    {
1292
        $p = round($p, 1);
1293
        $r = $f[0] + ($t[0] - $f[0]) * $p;
1294
        $g = $f[1] + ($t[1] - $f[1]) * $p;
1295
        $b = $f[2] + ($t[2] - $f[2]) * $p;
1296
        return [$r, $g, $b];
1297
    }
1298
1299
    // Set line style dashed, dotted etc
1300
    public function SetLineStyle($s)
1301
    {
1302
        if (is_numeric($s)) {
1303
            if ($s < 1 || $s > 4) {
1304
                Util\JpGraphError::RaiseL(25101, $s); //(" Illegal numeric argument to SetLineStyle(): ($s)");
1305
            }
1306
        } elseif (is_string($s)) {
1307
            if ($s == "solid") {
1308
                $s = 1;
1309
            } elseif ($s == "dotted") {
1310
                $s = 2;
1311
            } elseif ($s == "dashed") {
1312
                $s = 3;
1313
            } elseif ($s == "longdashed") {
1314
                $s = 4;
1315
            } else {
1316
                Util\JpGraphError::RaiseL(25102, $s); //(" Illegal string argument to SetLineStyle(): $s");
1317
            }
1318
        } else {
1319
            Util\JpGraphError::RaiseL(25103, $s); //(" Illegal argument to SetLineStyle $s");
1320
        }
1321
        $old              = $this->line_style;
1322
        $this->line_style = $s;
1323
        return $old;
1324
    }
1325
1326
    // Same as Line but take the line_style into account
1327
    public function StyleLine($x1, $y1, $x2, $y2, $aStyle = '', $from_grid_class = false)
1328
    {
1329
        if ($this->line_weight <= 0) {
0 ignored issues
show
Bug introduced by
The property line_weight does not seem to exist. Did you mean _line_weight?

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...
1330
            return;
1331
        }
1332
1333
        if ($aStyle === '') {
1334
            $aStyle = $this->line_style;
1335
        }
1336
1337
        $dashed_line_method = 'DashedLine';
1338
        if ($from_grid_class) {
1339
            $dashed_line_method = 'DashedLineForGrid';
1340
        }
1341
1342
        // Add error check since dashed line will only work if anti-alias is disabled
1343
        // this is a limitation in GD
1344
1345
        if ($aStyle == 1) {
1346
            // Solid style. We can handle anti-aliasing for this
1347
            $this->Line($x1, $y1, $x2, $y2);
1348
        } else {
1349
            // Since the GD routines doesn't handle AA for styled line
1350
            // we have no option than to turn it off to get any lines at
1351
            // all if the weight > 1
1352
            $oldaa = $this->GetAntiAliasing();
1353
            if ($oldaa && $this->line_weight > 1) {
0 ignored issues
show
Bug introduced by
The property line_weight does not seem to exist. Did you mean _line_weight?

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...
1354
                $this->SetAntiAliasing(false);
1355
            }
1356
1357
            switch ($aStyle) {
1358
                case 2: // Dotted
1359
                    $this->$dashed_line_method($x1, $y1, $x2, $y2, 2, 6);
1360
                    break;
1361
                case 3: // Dashed
1362
                    $this->$dashed_line_method($x1, $y1, $x2, $y2, 5, 9);
1363
                    break;
1364
                case 4: // Longdashes
1365
                    $this->$dashed_line_method($x1, $y1, $x2, $y2, 9, 13);
1366
                    break;
1367
                default:
1368
                    Util\JpGraphError::RaiseL(25104, $this->line_style); //(" Unknown line style: $this->line_style ");
1369
                    break;
1370
            }
1371
            if ($oldaa) {
1372
                $this->SetAntiAliasing(true);
1373
            }
1374
        }
1375
    }
1376
1377
    public function DashedLine($x1, $y1, $x2, $y2, $dash_length = 1, $dash_space = 4)
1378
    {
1379
1380
        if ($this->line_weight <= 0) {
0 ignored issues
show
Bug introduced by
The property line_weight does not seem to exist. Did you mean _line_weight?

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...
1381
            return;
1382
        }
1383
1384
        // Add error check to make sure anti-alias is not enabled.
1385
        // Dashed line does not work with anti-alias enabled. This
1386
        // is a limitation in GD.
1387
        if ($this->use_anti_aliasing) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

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

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

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
1388
            //            Util\JpGraphError::RaiseL(25129); // Anti-alias can not be used with dashed lines. Please disable anti-alias or use solid lines.
1 ignored issue
show
Unused Code Comprehensibility introduced by
56% 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...
1389
        }
1390
1391
        $x1 = round($x1);
1392
        $x2 = round($x2);
1393
        $y1 = round($y1);
1394
        $y2 = round($y2);
1395
1396
        $dash_length *= SUPERSAMPLING_SCALE;
1397
        $dash_space *= SUPERSAMPLING_SCALE;
1398
1399
        $style = array_fill(0, $dash_length, $this->current_color);
1400
        $style = array_pad($style, $dash_space, IMG_COLOR_TRANSPARENT);
1401
        imagesetstyle($this->img, $style);
1402
        imageline($this->img, $x1, $y1, $x2, $y2, IMG_COLOR_STYLED);
1403
1404
        $this->lastx = $x2;
0 ignored issues
show
Documentation Bug introduced by
The property $lastx was declared of type integer, but $x2 is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
1405
        $this->lasty = $y2;
0 ignored issues
show
Documentation Bug introduced by
The property $lasty was declared of type integer, but $y2 is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
1406
    }
1407
1408
    public function DashedLineForGrid($x1, $y1, $x2, $y2, $dash_length = 1, $dash_space = 4)
0 ignored issues
show
Unused Code introduced by
The parameter $dash_length 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...
1409
    {
1410
1411
        if ($this->line_weight <= 0) {
0 ignored issues
show
Bug introduced by
The property line_weight does not seem to exist. Did you mean _line_weight?

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...
1412
            return;
1413
        }
1414
1415
        // Add error check to make sure anti-alias is not enabled.
1416
        // Dashed line does not work with anti-alias enabled. This
1417
        // is a limitation in GD.
1418
        if ($this->use_anti_aliasing) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

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

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

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
1419
            //            Util\JpGraphError::RaiseL(25129); // Anti-alias can not be used with dashed lines. Please disable anti-alias or use solid lines.
1 ignored issue
show
Unused Code Comprehensibility introduced by
56% 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...
1420
        }
1421
1422
        $x1 = round($x1);
1423
        $x2 = round($x2);
1424
        $y1 = round($y1);
1425
        $y2 = round($y2);
1426
1427
        /*
1 ignored issue
show
Unused Code Comprehensibility introduced by
45% 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...
1428
        $dash_length *= $this->scale;
1429
        $dash_space  *= $this->scale;
1430
         */
1431
1432
        $dash_length = 2;
0 ignored issues
show
Unused Code introduced by
$dash_length 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...
1433
        $dash_length = 4;
1434
        imagesetthickness($this->img, 1);
1435
        $style = array_fill(0, $dash_length, $this->current_color); //hexdec('CCCCCC'));
1 ignored issue
show
Unused Code Comprehensibility introduced by
84% 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...
1436
        $style = array_pad($style, $dash_space, IMG_COLOR_TRANSPARENT);
1437
        imagesetstyle($this->img, $style);
1438
        imageline($this->img, $x1, $y1, $x2, $y2, IMG_COLOR_STYLED);
1439
1440
        $this->lastx = $x2;
0 ignored issues
show
Documentation Bug introduced by
The property $lastx was declared of type integer, but $x2 is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
1441
        $this->lasty = $y2;
0 ignored issues
show
Documentation Bug introduced by
The property $lasty was declared of type integer, but $y2 is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
1442
    }
1443
1444
    public function Line($x1, $y1, $x2, $y2)
1445
    {
1446
1447
        if ($this->line_weight <= 0) {
0 ignored issues
show
Bug introduced by
The property line_weight does not seem to exist. Did you mean _line_weight?

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...
1448
            return;
1449
        }
1450
1451
        $x1 = round($x1);
1452
        $x2 = round($x2);
1453
        $y1 = round($y1);
1454
        $y2 = round($y2);
1455
1456
        imageline($this->img, $x1, $y1, $x2, $y2, $this->current_color);
1457
        //        $this->DrawLine($this->img, $x1, $y1, $x2, $y2, $this->line_weight, $this->current_color);
1 ignored issue
show
Unused Code Comprehensibility introduced by
66% 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...
1458
        $this->lastx = $x2;
0 ignored issues
show
Documentation Bug introduced by
The property $lastx was declared of type integer, but $x2 is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
1459
        $this->lasty = $y2;
0 ignored issues
show
Documentation Bug introduced by
The property $lasty was declared of type integer, but $y2 is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
1460
    }
1461
1462
    public function Polygon($p, $closed = false, $fast = false)
1463
    {
1464
1465
        if ($this->line_weight <= 0) {
0 ignored issues
show
Bug introduced by
The property line_weight does not seem to exist. Did you mean _line_weight?

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...
1466
            return;
1467
        }
1468
1469
        $n    = count($p);
1470
        $oldx = $p[0];
1471
        $oldy = $p[1];
1472
        if ($fast) {
1473
            for ($i = 2; $i < $n; $i += 2) {
1474
                imageline($this->img, $oldx, $oldy, $p[$i], $p[$i + 1], $this->current_color);
1475
                $oldx = $p[$i];
1476
                $oldy = $p[$i + 1];
1477
            }
1478
            if ($closed) {
1479
                imageline($this->img, $p[$n * 2 - 2], $p[$n * 2 - 1], $p[0], $p[1], $this->current_color);
1480
            }
1481
        } else {
1482 View Code Duplication
            for ($i = 2; $i < $n; $i += 2) {
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...
1483
                $this->StyleLine($oldx, $oldy, $p[$i], $p[$i + 1]);
1484
                $oldx = $p[$i];
1485
                $oldy = $p[$i + 1];
1486
            }
1487
            if ($closed) {
1488
                $this->StyleLine($oldx, $oldy, $p[0], $p[1]);
1489
            }
1490
        }
1491
    }
1492
1493
    public function FilledPolygon($pts)
1494
    {
1495
        $n = count($pts);
1496
        if ($n == 0) {
1497
            Util\JpGraphError::RaiseL(25105); //('NULL data specified for a filled polygon. Check that your data is not NULL.');
1498
        }
1499
        for ($i = 0; $i < $n; ++$i) {
1500
            $pts[$i] = round($pts[$i]);
1501
        }
1502
        $old = $this->line_weight;
0 ignored issues
show
Bug introduced by
The property line_weight does not seem to exist. Did you mean _line_weight?

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...
1503
        imagesetthickness($this->img, 1);
1504
        imagefilledpolygon($this->img, $pts, count($pts) / 2, $this->current_color);
1505
        $this->line_weight = $old;
0 ignored issues
show
Bug introduced by
The property line_weight does not seem to exist. Did you mean _line_weight?

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...
1506
        imagesetthickness($this->img, $old);
1507
    }
1508
1509
    public function Rectangle($xl, $yu, $xr, $yl)
1510
    {
1511
        $this->Polygon([$xl, $yu, $xr, $yu, $xr, $yl, $xl, $yl, $xl, $yu]);
1512
    }
1513
1514
    public function FilledRectangle($xl, $yu, $xr, $yl)
1515
    {
1516
        $this->FilledPolygon([$xl, $yu, $xr, $yu, $xr, $yl, $xl, $yl]);
1517
    }
1518
1519
    public function FilledRectangle2($xl, $yu, $xr, $yl, $color1, $color2, $style = 1)
1520
    {
1521
        // Fill a rectangle with lines of two colors
1522
        if ($style === 1) {
1523
            // Horizontal stripe
1524
            if ($yl < $yu) {
1525
                $t  = $yl;
1526
                $yl = $yu;
1527
                $yu = $t;
1528
            }
1529 View Code Duplication
            for ($y = $yu; $y <= $yl; ++$y) {
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...
1530
                $this->SetColor($color1);
1531
                $this->Line($xl, $y, $xr, $y);
1532
                ++$y;
1533
                $this->SetColor($color2);
1534
                $this->Line($xl, $y, $xr, $y);
1535
            }
1536
        } else {
1537
            if ($xl < $xl) {
1538
                $t  = $xl;
1539
                $xl = $xr;
1540
                $xr = $t;
1541
            }
1542 View Code Duplication
            for ($x = $xl; $x <= $xr; ++$x) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
1543
                $this->SetColor($color1);
1544
                $this->Line($x, $yu, $x, $yl);
1545
                ++$x;
1546
                $this->SetColor($color2);
1547
                $this->Line($x, $yu, $x, $yl);
1548
            }
1549
        }
1550
    }
1551
1552
    public function ShadowRectangle($xl, $yu, $xr, $yl, $fcolor = false, $shadow_width = 4, $shadow_color = 'darkgray', $useAlpha = true)
1553
    {
1554
        // This is complicated by the fact that we must also handle the case where
1555
        // the reactangle has no fill color
1556
        $xl = floor($xl);
1557
        $yu = floor($yu);
1558
        $xr = floor($xr);
1559
        $yl = floor($yl);
1560
        $this->PushColor($shadow_color);
1561
        $shadowAlpha = 0;
1562
        $this->SetLineWeight(1);
1563
        $this->SetLineStyle('solid');
1564
        $basecolor    = $this->rgb->Color($shadow_color);
1565
        $shadow_color = [$basecolor[0], $basecolor[1], $basecolor[2]];
1566
        for ($i = 0; $i < $shadow_width; ++$i) {
1567
            $this->SetColor($shadow_color, $shadowAlpha);
1568
            $this->Line($xr - $shadow_width + $i, $yu + $shadow_width,
1569
                $xr - $shadow_width + $i, $yl - $shadow_width - 1 + $i);
1570
            $this->Line($xl + $shadow_width, $yl - $shadow_width + $i,
1571
                $xr - $shadow_width + $i, $yl - $shadow_width + $i);
1572
            if ($useAlpha) {
1573
                $shadowAlpha += 1.0 / $shadow_width;
1574
            }
1575
1576
        }
1577
1578
        $this->PopColor();
1579
        if ($fcolor == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

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

Loading history...
1580
            $this->Rectangle($xl, $yu, $xr - $shadow_width - 1, $yl - $shadow_width - 1);
1581
        } else {
1582
            $this->PushColor($fcolor);
1583
            $this->FilledRectangle($xl, $yu, $xr - $shadow_width - 1, $yl - $shadow_width - 1);
1584
            $this->PopColor();
1585
            $this->Rectangle($xl, $yu, $xr - $shadow_width - 1, $yl - $shadow_width - 1);
1586
        }
1587
    }
1588
1589
    public function FilledRoundedRectangle($xt, $yt, $xr, $yl, $r = 5)
1590
    {
1591
        if ($r == 0) {
1592
            $this->FilledRectangle($xt, $yt, $xr, $yl);
1593
            return;
1594
        }
1595
1596
        // To avoid overlapping fillings (which will look strange
1597
        // when alphablending is enabled) we have no choice but
1598
        // to fill the five distinct areas one by one.
1599
1600
        // Center square
1601
        $this->FilledRectangle($xt + $r, $yt + $r, $xr - $r, $yl - $r);
1602
        // Top band
1603
        $this->FilledRectangle($xt + $r, $yt, $xr - $r, $yt + $r);
1604
        // Bottom band
1605
        $this->FilledRectangle($xt + $r, $yl - $r, $xr - $r, $yl);
1606
        // Left band
1607
        $this->FilledRectangle($xt, $yt + $r, $xt + $r, $yl - $r);
1608
        // Right band
1609
        $this->FilledRectangle($xr - $r, $yt + $r, $xr, $yl - $r);
1610
1611
        // Topleft & Topright arc
1612
        $this->FilledArc($xt + $r, $yt + $r, $r * 2, $r * 2, 180, 270);
1613
        $this->FilledArc($xr - $r, $yt + $r, $r * 2, $r * 2, 270, 360);
1614
1615
        // Bottomleft & Bottom right arc
1616
        $this->FilledArc($xt + $r, $yl - $r, $r * 2, $r * 2, 90, 180);
1617
        $this->FilledArc($xr - $r, $yl - $r, $r * 2, $r * 2, 0, 90);
1618
1619
    }
1620
1621
    public function RoundedRectangle($xt, $yt, $xr, $yl, $r = 5)
1622
    {
1623
1624
        if ($r == 0) {
1625
            $this->Rectangle($xt, $yt, $xr, $yl);
1626
            return;
1627
        }
1628
1629
        // Top & Bottom line
1630
        $this->Line($xt + $r, $yt, $xr - $r, $yt);
1631
        $this->Line($xt + $r, $yl, $xr - $r, $yl);
1632
1633
        // Left & Right line
1634
        $this->Line($xt, $yt + $r, $xt, $yl - $r);
1635
        $this->Line($xr, $yt + $r, $xr, $yl - $r);
1636
1637
        // Topleft & Topright arc
1638
        $this->Arc($xt + $r, $yt + $r, $r * 2, $r * 2, 180, 270);
1639
        $this->Arc($xr - $r, $yt + $r, $r * 2, $r * 2, 270, 360);
1640
1641
        // Bottomleft & Bottomright arc
1642
        $this->Arc($xt + $r, $yl - $r, $r * 2, $r * 2, 90, 180);
1643
        $this->Arc($xr - $r, $yl - $r, $r * 2, $r * 2, 0, 90);
1644
    }
1645
1646
    public function FilledBevel($x1, $y1, $x2, $y2, $depth = 2, $color1 = '[email protected]', $color2 = '[email protected]')
1647
    {
1648
        $this->FilledRectangle($x1, $y1, $x2, $y2);
1649
        $this->Bevel($x1, $y1, $x2, $y2, $depth, $color1, $color2);
1650
    }
1651
1652
    public function Bevel($x1, $y1, $x2, $y2, $depth = 2, $color1 = '[email protected]', $color2 = '[email protected]')
1653
    {
1654
        $this->PushColor($color1);
1655
        for ($i = 0; $i < $depth; ++$i) {
1656
            $this->Line($x1 + $i, $y1 + $i, $x1 + $i, $y2 - $i);
1657
            $this->Line($x1 + $i, $y1 + $i, $x2 - $i, $y1 + $i);
1658
        }
1659
        $this->PopColor();
1660
1661
        $this->PushColor($color2);
1662
        for ($i = 0; $i < $depth; ++$i) {
1663
            $this->Line($x1 + $i, $y2 - $i, $x2 - $i, $y2 - $i);
1664
            $this->Line($x2 - $i, $y1 + $i, $x2 - $i, $y2 - $i - 1);
1665
        }
1666
        $this->PopColor();
1667
    }
1668
1669
    public function StyleLineTo($x, $y)
1670
    {
1671
        $this->StyleLine($this->lastx, $this->lasty, $x, $y);
1672
        $this->lastx = $x;
1673
        $this->lasty = $y;
1674
    }
1675
1676
    public function LineTo($x, $y)
1677
    {
1678
        $this->Line($this->lastx, $this->lasty, $x, $y);
1679
        $this->lastx = $x;
1680
        $this->lasty = $y;
1681
    }
1682
1683
    public function Point($x, $y)
1684
    {
1685
        imagesetpixel($this->img, round($x), round($y), $this->current_color);
1686
    }
1687
1688
    public function Fill($x, $y)
1689
    {
1690
        imagefill($this->img, round($x), round($y), $this->current_color);
1691
    }
1692
1693
    public function FillToBorder($x, $y, $aBordColor)
1694
    {
1695
        $bc = $this->rgb->allocate($aBordColor);
1696
        if ($bc == -1) {
1697
            Util\JpGraphError::RaiseL(25106); //('Image::FillToBorder : Can not allocate more colors');
1698
        }
1699
        imagefilltoborder($this->img, round($x), round($y), $bc, $this->current_color);
1700
    }
1701
1702
    public function SetExpired($aFlg = true)
1703
    {
1704
        $this->expired = $aFlg;
1705
    }
1706
1707
    // Generate image header
1708
    public function Headers()
1709
    {
1710
1711
        // In case we are running from the command line with the client version of
1712
        // PHP we can't send any headers.
1713
        $sapi = php_sapi_name();
1714
        if ($sapi == 'cli') {
1715
            return;
1716
        }
1717
1718
        // These parameters are set by headers_sent() but they might cause
1719
        // an undefined variable error unless they are initilized
1720
        $file   = '';
1721
        $lineno = '';
1722
        if (headers_sent($file, $lineno)) {
1723
            $file = basename($file);
1724
            $t    = new ErrMsgText();
1725
            $msg  = $t->Get(10, $file, $lineno);
1726
            die($msg);
0 ignored issues
show
Coding Style Compatibility introduced by
The method Headers() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
1727
        }
1728
1729
        if ($this->expired) {
1730
            header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
1731
            header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
1732
            header("Cache-Control: no-cache, must-revalidate");
1733
            header("Pragma: no-cache");
1734
        }
1735
        header("Content-type: image/$this->img_format");
1736
    }
1737
1738
    // Adjust image quality for formats that allow this
1739
    public function SetQuality($q)
1740
    {
1741
        $this->quality = $q;
1742
    }
1743
1744
    // Stream image to browser or to file
1745
    public function Stream($aFile = "")
1746
    {
1747
        $this->DoSupersampling();
1748
1749
        $func = "image" . $this->img_format;
1750
        if ($this->img_format == "jpeg" && $this->quality != null) {
1751
            $res = @$func($this->img, $aFile, $this->quality);
0 ignored issues
show
Unused Code introduced by
$res 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...
1752
        } else {
1753
            if ($aFile != "") {
1754
                $res = @$func($this->img, $aFile);
1755
                if (!$res) {
1756
                    Util\JpGraphError::RaiseL(25107, $aFile); //("Can't write to file '$aFile'. Check that the process running PHP has enough permission.");
1757
                }
1758
            } else {
1759
                $res = @$func($this->img);
1760
                if (!$res) {
1761
                    Util\JpGraphError::RaiseL(25108); //("Can't stream image. This is most likely due to a faulty PHP/GD setup. Try to recompile PHP and use the built-in GD library that comes with PHP.");
1762
                }
1763
1764
            }
1765
        }
1766
    }
1767
1768
    // Do SuperSampling using $scale
1769
    public function DoSupersampling()
1770
    {
1771
        if (SUPERSAMPLING_SCALE <= 1) {
1772
            return $this->img;
1773
        }
1774
1775
        $dst_img = @imagecreatetruecolor($this->original_width, $this->original_height);
1776
        imagecopyresampled($dst_img, $this->img, 0, 0, 0, 0, $this->original_width, $this->original_height, $this->width, $this->height);
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...
1777
        $this->Destroy();
1778
        return $this->img = $dst_img;
1779
    }
1780
1781
    // Clear resources used by image (this is normally not used since all resources are/should be
1782
    // returned when the script terminates
1783
    public function Destroy()
1784
    {
1785
        imagedestroy($this->img);
1786
    }
1787
1788
    // Specify image format. Note depending on your installation
1789
    // of PHP not all formats may be supported.
1790
    public function SetImgFormat($aFormat, $aQuality = 75)
1791
    {
1792
        $this->quality = $aQuality;
1793
        $aFormat       = strtolower($aFormat);
1794
        $tst           = true;
0 ignored issues
show
Unused Code introduced by
$tst 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...
1795
        $supported     = imagetypes();
1796
        if ($aFormat == "auto") {
1797
            if ($supported & IMG_PNG) {
1798
                $this->img_format = "png";
1799
            } elseif ($supported & IMG_JPG) {
1800
                $this->img_format = "jpeg";
1801
            } elseif ($supported & IMG_GIF) {
1802
                $this->img_format = "gif";
1803
            } elseif ($supported & IMG_WBMP) {
1804
                $this->img_format = "wbmp";
1805
            } elseif ($supported & IMG_XPM) {
1806
                $this->img_format = "xpm";
1807
            } else {
1808
                Util\JpGraphError::RaiseL(25109); //("Your PHP (and GD-lib) installation does not appear to support any known graphic formats. You need to first make sure GD is compiled as a module to PHP. If you also want to use JPEG images you must get the JPEG library. Please see the PHP docs for details.");
1809
            }
1810
            return true;
1811
        } else {
1812
            if ($aFormat == "jpeg" || $aFormat == "png" || $aFormat == "gif") {
1813
                if ($aFormat == "jpeg" && !($supported & IMG_JPG)) {
1814
                    $tst = false;
1815
                } elseif ($aFormat == "png" && !($supported & IMG_PNG)) {
1816
                    $tst = false;
1817
                } elseif ($aFormat == "gif" && !($supported & IMG_GIF)) {
1818
                    $tst = false;
1819
                } elseif ($aFormat == "wbmp" && !($supported & IMG_WBMP)) {
1820
                    $tst = false;
1821
                } elseif ($aFormat == "xpm" && !($supported & IMG_XPM)) {
1822
                    $tst = false;
1823
                } else {
1824
                    $this->img_format = $aFormat;
1825
                    return true;
1826
                }
1827
            } else {
1828
                $tst = false;
1829
            }
1830
            if (!$tst) {
1831
                Util\JpGraphError::RaiseL(25110, $aFormat); //(" Your PHP installation does not support the chosen graphic format: $aFormat");
1832
            }
1833
        }
1834
    }
1835
1836
    /**
1837
     * Draw Line
1838
     */
1839
    public function DrawLine($im, $x1, $y1, $x2, $y2, $weight, $color)
1840
    {
1841
        if ($weight == 1) {
1842
            return imageline($im, $x1, $y1, $x2, $y2, $color);
1843
        }
1844
1845
        $angle = (atan2(($y1 - $y2), ($x2 - $x1)));
1846
1847
        $dist_x = $weight * (sin($angle)) / 2;
1848
        $dist_y = $weight * (cos($angle)) / 2;
1849
1850
        $p1x = ceil(($x1 + $dist_x));
1851
        $p1y = ceil(($y1 + $dist_y));
1852
        $p2x = ceil(($x2 + $dist_x));
1853
        $p2y = ceil(($y2 + $dist_y));
1854
        $p3x = ceil(($x2 - $dist_x));
1855
        $p3y = ceil(($y2 - $dist_y));
1856
        $p4x = ceil(($x1 - $dist_x));
1857
        $p4y = ceil(($y1 - $dist_y));
1858
1859
        $array = [$p1x, $p1y, $p2x, $p2y, $p3x, $p3y, $p4x, $p4y];
1860
        imagefilledpolygon($im, $array, (count($array) / 2), $color);
1861
1862
        // for antialias
1863
        imageline($im, $p1x, $p1y, $p2x, $p2y, $color);
1864
        imageline($im, $p3x, $p3y, $p4x, $p4y, $color);
1865
        return;
1866
1867
        return imageline($this->img, $x1, $y1, $x2, $y2, $this->current_color);
0 ignored issues
show
Unused Code introduced by
return imageline($this->... $this->current_color); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
1868
        $weight = 8;
0 ignored issues
show
Unused Code introduced by
$weight = 8; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
1869
        if ($weight <= 1) {
1870
            return imageline($this->img, $x1, $y1, $x2, $y2, $this->current_color);
1871
        }
1872
1873
        $pts = [];
0 ignored issues
show
Unused Code introduced by
$pts 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...
1874
1875
        $weight /= 2;
1876
1877
        if ($y2 - $y1 == 0) {
1878
            // x line
1879
            $pts   = [];
1880
            $pts[] = $x1;
1881
            $pts[] = $y1 - $weight;
1882
            $pts[] = $x1;
1883
            $pts[] = $y1 + $weight;
1884
            $pts[] = $x2;
1885
            $pts[] = $y2 + $weight;
1886
            $pts[] = $x2;
1887
            $pts[] = $y2 - $weight;
1888
1889
        } elseif ($x2 - $x1 == 0) {
1890
            // y line
1891
            $pts   = [];
1892
            $pts[] = $x1 - $weight;
1893
            $pts[] = $y1;
1894
            $pts[] = $x1 + $weight;
1895
            $pts[] = $y1;
1896
            $pts[] = $x2 + $weight;
1897
            $pts[] = $y2;
1898
            $pts[] = $x2 - $weight;
1899
            $pts[] = $y2;
1900
1901
        } else {
1902
1903
            $length = sqrt(pow($x2 - $x1, 2) + pow($y2 - $y1, 2));
0 ignored issues
show
Unused Code introduced by
$length 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...
1904
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method DrawLine() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
1905
1906
            /*
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...
1907
        $lean = ($y2 - $y1) / ($x2 - $x1);
1908
        $lean2 = -1 / $lean;
1909
        $sin = $lean / ($y2 - $y1);
1910
        $cos = $lean / ($x2 - $x1);
1911
1912
        $pts[] = $x1 + (-$weight * $sin); $pts[] = $y1 + (-$weight * $cos);
1913
        $pts[] = $x1 + (+$weight * $sin); $pts[] = $y1 + (+$weight * $cos);
1914
        $pts[] = $x2 + (+$weight * $sin); $pts[] = $y2 + (+$weight * $cos);
1915
        $pts[] = $x2 + (-$weight * $sin); $pts[] = $y2 + (-$weight * $cos);
1916
         */
1917
        }
1918
1919
        //print_r($pts);exit;
1 ignored issue
show
Unused Code Comprehensibility introduced by
86% 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...
1920
        if (count($pts) / 2 < 3) {
1921
            return;
1922
        }
1923
1924
        imagesetthickness($im, 1);
1925
        imagefilledpolygon($im, $pts, count($pts) / 2, $color);
1926
1927
        $weight *= 2;
1928
1929
        //        $this->DrawImageSmoothArc($im, $x1, $y1, $weight, $weight, 0, 360, $color);
1 ignored issue
show
Unused Code Comprehensibility introduced by
69% 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...
1930
        //        $this->DrawImageSmoothArc($im, $x2, $y2, $weight, $weight, 0, 360, $color);
1 ignored issue
show
Unused Code Comprehensibility introduced by
69% 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...
1931
    }
1932
1933
    public function DrawImageSmoothArc($im, $xc, $yc, $w, $h, $s, $e, $color, $style = null)
0 ignored issues
show
Unused Code introduced by
The parameter $style 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...
1934
    {
1935
        $tmp = $s;
1936
        $s   = (360 - $e) / 180 * M_PI;
1937
        $e   = (360 - $tmp) / 180 * M_PI;
1938
        return imageSmoothArc($im, round($xc), round($yc), round($w), round($h), $this->CreateColorForImageSmoothArc($color), $s, $e);
1939
    }
1940
1941
    public function CreateColorForImageSmoothArc($color)
1942
    {
1943
        $alpha = $color >> 24 & 0xFF;
1944
        $red   = $color >> 16 & 0xFF;
1945
        $green = $color >> 8 & 0xFF;
1946
        $blue  = $color & 0xFF;
1947
1948
        return [$red, $green, $blue, $alpha];
1949
    }
1950
1951
    public function imageSmoothCircle(&$img, $cx, $cy, $cr, $color)
1952
    {
1953
        $ir   = $cr;
1954
        $ix   = 0;
1955
        $iy   = $ir;
1956
        $ig   = 2 * $ir - 3;
1957
        $idgr = -6;
1958
        $idgd = 4 * $ir - 10;
1959
        $fill = imageColorExactAlpha($img, $color['R'], $color['G'], $color['B'], 0);
1960
        imageLine($img, $cx + $cr - 1, $cy, $cx, $cy, $fill);
1961
        imageLine($img, $cx - $cr + 1, $cy, $cx - 1, $cy, $fill);
1962
        imageLine($img, $cx, $cy + $cr - 1, $cx, $cy + 1, $fill);
1963
        imageLine($img, $cx, $cy - $cr + 1, $cx, $cy - 1, $fill);
1964
        $draw = imageColorExactAlpha($img, $color['R'], $color['G'], $color['B'], 42);
1965
        imageSetPixel($img, $cx + $cr, $cy, $draw);
1966
        imageSetPixel($img, $cx - $cr, $cy, $draw);
1967
        imageSetPixel($img, $cx, $cy + $cr, $draw);
1968
        imageSetPixel($img, $cx, $cy - $cr, $draw);
1969
        while ($ix <= $iy - 2) {
1970
            if ($ig < 0) {
1971
                $ig += $idgd;
1972
                $idgd -= 8;
1973
                $iy--;
1974
            } else {
1975
                $ig += $idgr;
1976
                $idgd -= 4;
1977
            }
1978
            $idgr -= 4;
1979
            $ix++;
1980
            imageLine($img, $cx + $ix, $cy + $iy - 1, $cx + $ix, $cy + $ix, $fill);
1981
            imageLine($img, $cx + $ix, $cy - $iy + 1, $cx + $ix, $cy - $ix, $fill);
1982
            imageLine($img, $cx - $ix, $cy + $iy - 1, $cx - $ix, $cy + $ix, $fill);
1983
            imageLine($img, $cx - $ix, $cy - $iy + 1, $cx - $ix, $cy - $ix, $fill);
1984
            imageLine($img, $cx + $iy - 1, $cy + $ix, $cx + $ix, $cy + $ix, $fill);
1985
            imageLine($img, $cx + $iy - 1, $cy - $ix, $cx + $ix, $cy - $ix, $fill);
1986
            imageLine($img, $cx - $iy + 1, $cy + $ix, $cx - $ix, $cy + $ix, $fill);
1987
            imageLine($img, $cx - $iy + 1, $cy - $ix, $cx - $ix, $cy - $ix, $fill);
1988
            $filled = 0;
1989
            for ($xx = $ix - 0.45; $xx < $ix + 0.5; $xx += 0.2) {
1990
                for ($yy = $iy - 0.45; $yy < $iy + 0.5; $yy += 0.2) {
1991
                    if (sqrt(pow($xx, 2) + pow($yy, 2)) < $cr) {
1992
                        $filled += 4;
1993
                    }
1994
1995
                }
1996
            }
1997
            $draw = imageColorExactAlpha($img, $color['R'], $color['G'], $color['B'], (100 - $filled));
1998
            imageSetPixel($img, $cx + $ix, $cy + $iy, $draw);
1999
            imageSetPixel($img, $cx + $ix, $cy - $iy, $draw);
2000
            imageSetPixel($img, $cx - $ix, $cy + $iy, $draw);
2001
            imageSetPixel($img, $cx - $ix, $cy - $iy, $draw);
2002
            imageSetPixel($img, $cx + $iy, $cy + $ix, $draw);
2003
            imageSetPixel($img, $cx + $iy, $cy - $ix, $draw);
2004
            imageSetPixel($img, $cx - $iy, $cy + $ix, $draw);
2005
            imageSetPixel($img, $cx - $iy, $cy - $ix, $draw);
2006
        }
2007
    }
2008
2009 View Code Duplication
    public function __get($name)
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...
2010
    {
2011
2012
        if (strpos($name, 'raw_') !== false) {
2013
            // if $name == 'raw_left_margin' , return $this->_left_margin;
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...
2014
            $variable_name = '_' . str_replace('raw_', '', $name);
2015
            return $this->$variable_name;
2016
        }
2017
2018
        $variable_name = '_' . $name;
2019
2020
        if (isset($this->$variable_name)) {
2021
            return $this->$variable_name * SUPERSAMPLING_SCALE;
2022
        } else {
2023
            Util\JpGraphError::RaiseL('25132', $name);
2024
        }
2025
    }
2026
2027
    public function __set($name, $value)
2028
    {
2029
        $this->{'_' . $name} = $value;
2030
    }
2031
2032
} // CLASS
2033