eq_pie::MakePie()   F
last analyzed

Complexity

Conditions 22
Paths 9505

Size

Total Lines 167
Code Lines 99

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
eloc 99
nc 9505
nop 7
dl 0
loc 167
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace XoopsModules\Pedigree;
4
5
// eq_pie written by ellardus (C) 2005
6
// for more info look at www.eq-home.com
7
// or email at [email protected]
8
// Feel free to use it, a reference to me would be nice.
9
// Thank you and good luck!
10
11
/**
12
 * Class eq_pie
13
 */
14
class eq_pie
15
{
16
    /**
17
     * eq_pie constructor.
18
     */
19
    public function __construct()
20
    {
21
    }
22
23
    /**
24
     * @param $number
25
     *
26
     * @return mixed
27
     */
28
    public function getColor($number)
29
    {
30
        $color = [
31
            '#ff0000',
32
            '#00ff00',
33
            '#0000ff',
34
            '#ffff00',
35
            '#ff00ff',
36
            '#00ffff',
37
            '#cc0000',
38
            '#00cc00',
39
            '#0000cc',
40
            '#990000',
41
            '#009900',
42
            '#000099',
43
            '#660000',
44
            '#006600',
45
            '#000066',
46
            '#330000',
47
            '#003300',
48
            '#000033',
49
        ];
50
51
        return $color[$number];
52
    }
53
54
    /**
55
     * @param $filename
56
     * @param $pieWidth
57
     * @param $pieHeight
58
     * @param $ShadowDistance
59
     * @param $pieBackgroundColor
60
     * @param $EQpieData
61
     * @param $legend
62
     */
63
    public function MakePie(
64
        $filename,
65
        $pieWidth,
66
        $pieHeight,
67
        $ShadowDistance,
68
        $pieBackgroundColor,
69
        $EQpieData,
70
        $legend
71
    ) {
72
        if (!\function_exists('imagecreatetruecolor')) {
73
            exit('Error, GD Library 2 needed.');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
74
        }
75
76
        //set some limitations
77
        if ($pieWidth < 100 | $pieWidth > 500) {
0 ignored issues
show
Bug introduced by
Are you sure you want to use the bitwise | or did you mean ||?
Loading history...
78
            $pieWidth = 100;
79
        }
80
        if ($pieHeight < 100 | $pieHeight > 500) {
0 ignored issues
show
Bug introduced by
Are you sure you want to use the bitwise | or did you mean ||?
Loading history...
81
            $pieHeight = 100;
82
        }
83
        if ($ShadowDistance < 1 | $ShadowDistance > 50) {
0 ignored issues
show
Bug introduced by
Are you sure you want to use the bitwise | or did you mean ||?
Loading history...
84
            $ShadowDistance = 10;
85
        }
86
87
        $pieWidth           *= 3;
88
        $pieHeight          *= 3;
89
        $ShadowDistance     *= 3;
90
        $pieBackgroundColor = $pieBackgroundColor;
91
92
        $pie = @\imagecreatetruecolor($pieWidth, $pieHeight + $ShadowDistance);
93
94
        $colR  = \hexdec(mb_substr($pieBackgroundColor, 1, 2));
95
        $colG  = \hexdec(mb_substr($pieBackgroundColor, 3, 2));
96
        $colB  = \hexdec(mb_substr($pieBackgroundColor, 5, 2));
97
        $pieBG = \imagefilledarc($pie, $colR, $colG, $colB);
0 ignored issues
show
Bug introduced by
It seems like $pie can also be of type false; however, parameter $image of imagefilledarc() does only seem to accept GdImage|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        $pieBG = \imagefilledarc(/** @scrutinizer ignore-type */ $pie, $colR, $colG, $colB);
Loading history...
Bug introduced by
It seems like $colR can also be of type double; however, parameter $cx of imagefilledarc() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        $pieBG = \imagefilledarc($pie, /** @scrutinizer ignore-type */ $colR, $colG, $colB);
Loading history...
Bug introduced by
It seems like $colB can also be of type double; however, parameter $width of imagefilledarc() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        $pieBG = \imagefilledarc($pie, $colR, $colG, /** @scrutinizer ignore-type */ $colB);
Loading history...
Bug introduced by
It seems like $colG can also be of type double; however, parameter $cy of imagefilledarc() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        $pieBG = \imagefilledarc($pie, $colR, /** @scrutinizer ignore-type */ $colG, $colB);
Loading history...
Bug introduced by
The call to imagefilledarc() has too few arguments starting with height. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
        $pieBG = /** @scrutinizer ignore-call */ \imagefilledarc($pie, $colR, $colG, $colB);

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

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

Loading history...
98
        \imagefill($pie, 0, 0, $pieBG);
0 ignored issues
show
Bug introduced by
It seems like $pie can also be of type false; however, parameter $image of imagefill() does only seem to accept GdImage|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
        \imagefill(/** @scrutinizer ignore-type */ $pie, 0, 0, $pieBG);
Loading history...
Bug introduced by
$pieBG of type boolean is incompatible with the type integer expected by parameter $color of imagefill(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
        \imagefill($pie, 0, 0, /** @scrutinizer ignore-type */ $pieBG);
Loading history...
99
100
        // get the total value for percentage calculations
101
        $this->total = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property total does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
102
103
        $maxStringLenght = 0;
104
        foreach ($EQpieData as $i => $value) {
105
            $this->total += $value[1];
106
            if (mb_strlen($value[0]) > $maxStringLenght) {
107
                $maxStringLenght = mb_strlen($value[0]);
108
            }
109
        }
110
111
        $pieParts = $i + 1;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $i seems to be defined by a foreach iteration on line 104. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
112
        \reset($EQpieData);
113
        $legendWidth = (($legend > 0) ? \imagefontwidth(2) * ($maxStringLenght + 6) + 40 : 0);
114
115
        // the first pie-part starts with offset in degrees up from horizantal right, looks better this way
116
        $pieStart = 135;
117
118
        foreach ($EQpieData as $i => $value) {
119
            // the name  for each part is $value[0]
120
            // the value for each part is $value[1]
121
            // the color for each part is $value[2]
122
123
            $piePart = $value[1];
124
            if (isset($this->total) && $this->total > 0) {
125
                $piePart100 = \round($piePart / $this->total * 100, 2);  // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps
126
            } else {
127
                $piePart100 = 0;
128
            }
129
130
            $piePart360 = $piePart100 * 3.6;                    // in degrees
131
132
            $colR      = \hexdec(mb_substr($value[2], 1, 2));
133
            $colG      = \hexdec(mb_substr($value[2], 3, 2));
134
            $colB      = \hexdec(mb_substr($value[2], 5, 2));
135
            $PartColor = \imagefilledarc($pie, $colR, $colG, $colB);
0 ignored issues
show
Unused Code introduced by
The assignment to $PartColor is dead and can be removed.
Loading history...
136
137
            $ShadowColR = (($colR > 79) ? $colR - 80 : 0);
138
            $ShadowColG = (($colG > 79) ? $colG - 80 : 0);
139
            $ShadowColB = (($colB > 79) ? $colB - 80 : 0);
140
141
            $ShadowColor = \imagefilledarc($pie, $ShadowColR, $ShadowColG, $ShadowColB);
142
143
            //Here we create the shadow down-worths
144
            for ($i = 0; $i < $ShadowDistance; ++$i) {
145
                \imagefilledarc($pie, $pieWidth / 2, $pieHeight / 2 + $i, $pieWidth - 20, $pieHeight - 20, \round($pieStart), \round($pieStart + $piePart360), $ShadowColor, \IMG_ARC_NOFILL);
0 ignored issues
show
Bug introduced by
$ShadowColor of type boolean is incompatible with the type integer expected by parameter $color of imagefilledarc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

145
                \imagefilledarc($pie, $pieWidth / 2, $pieHeight / 2 + $i, $pieWidth - 20, $pieHeight - 20, \round($pieStart), \round($pieStart + $piePart360), /** @scrutinizer ignore-type */ $ShadowColor, \IMG_ARC_NOFILL);
Loading history...
Bug introduced by
round($pieStart) of type double is incompatible with the type integer expected by parameter $start of imagefilledarc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

145
                \imagefilledarc($pie, $pieWidth / 2, $pieHeight / 2 + $i, $pieWidth - 20, $pieHeight - 20, /** @scrutinizer ignore-type */ \round($pieStart), \round($pieStart + $piePart360), $ShadowColor, \IMG_ARC_NOFILL);
Loading history...
Bug introduced by
round($pieStart + $piePart360) of type double is incompatible with the type integer expected by parameter $end of imagefilledarc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

145
                \imagefilledarc($pie, $pieWidth / 2, $pieHeight / 2 + $i, $pieWidth - 20, $pieHeight - 20, \round($pieStart), /** @scrutinizer ignore-type */ \round($pieStart + $piePart360), $ShadowColor, \IMG_ARC_NOFILL);
Loading history...
146
            }
147
148
            $pieStart += $piePart360;
149
        }
150
        \reset($EQpieData);
151
152
        $pieStart = 135;
153
154
        foreach ($EQpieData as $i => $value) {
155
            $piePart = $value[1];
156
            if (isset($this->total) && $this->total > 0) {
157
                $piePart100 = \round($piePart / $this->total * 100, 2);  // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps
158
            } else {
159
                $piePart100 = 0;
160
            }
161
            $piePart360 = $piePart100 * 3.6;                    // in degrees
162
163
            $colR      = \hexdec(mb_substr($value[2], 1, 2));
164
            $colG      = \hexdec(mb_substr($value[2], 3, 2));
165
            $colB      = \hexdec(mb_substr($value[2], 5, 2));
166
            $PartColor = \imagefilledarc($pie, $colR, $colG, $colB);
167
168
            //Here we create the real pie chart
169
            \imagefilledarc($pie, $pieWidth / 2, $pieHeight / 2, $pieWidth - 20, $pieHeight - 20, \round($pieStart), \round($pieStart + $piePart360), $PartColor, \IMG_ARC_PIE);
170
171
            $pieStart += $piePart360;
172
        }
173
        \reset($EQpieData);
174
175
        // create final pie picture with proper background color
176
        $finalPie = \imagecreatetruecolor($pieWidth / 3 + $legendWidth, ($pieHeight + $ShadowDistance) / 3);
177
        \imagefill($finalPie, 0, 0, $pieBG);
178
179
        // resample with pieGraph inside (3x smaller)
180
        \imagecopyresampled($finalPie, $pie, 0, 0, 0, 0, $pieWidth / 3, ($pieHeight + $ShadowDistance) / 3, $pieWidth, $pieHeight + $ShadowDistance);
0 ignored issues
show
Bug introduced by
It seems like $pie can also be of type false; however, parameter $src_image of imagecopyresampled() does only seem to accept GdImage|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

180
        \imagecopyresampled($finalPie, /** @scrutinizer ignore-type */ $pie, 0, 0, 0, 0, $pieWidth / 3, ($pieHeight + $ShadowDistance) / 3, $pieWidth, $pieHeight + $ShadowDistance);
Loading history...
181
182
        // Create the ledgend ...
183
        if ($legendWidth > 0) {
184
            // Legend Box
185
            $leg_width   = $legendWidth - 10;
186
            $leg_height  = $pieParts * (\imagefontheight(2) + 2) + 2;
187
            $legendImage = \imagecreatetruecolor($leg_width, $leg_height);
188
            //ImageFill($legendImage, 0, 0, $pieBG);
189
190
            $borderColor = \imagefilledarc($pie, '155', '155', '155');
191
            $boxColor    = \imagefilledarc($pie, '255', '255', '255');
192
            $textColor   = \imagefilledarc($pie, '55', '55', '55');
193
194
            \imagefilledrectangle($legendImage, 0, 0, $leg_width, $leg_height, $boxColor);
0 ignored issues
show
Bug introduced by
$boxColor of type boolean is incompatible with the type integer expected by parameter $color of imagefilledrectangle(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

194
            \imagefilledrectangle($legendImage, 0, 0, $leg_width, $leg_height, /** @scrutinizer ignore-type */ $boxColor);
Loading history...
195
            \imagerectangle($legendImage, 0, 0, $leg_width - 1, $leg_height - 1, $borderColor);
0 ignored issues
show
Bug introduced by
$borderColor of type boolean is incompatible with the type integer expected by parameter $color of imagerectangle(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

195
            \imagerectangle($legendImage, 0, 0, $leg_width - 1, $leg_height - 1, /** @scrutinizer ignore-type */ $borderColor);
Loading history...
196
197
            $box_width  = \imagefontwidth(2) - 5;
198
            $box_height = \imagefontheight(2) - 5;
199
            $yOffset    = 2;
200
201
            foreach ($EQpieData as $i => $value) {
202
                $piePart = $value[1];
203
                if (isset($this->total) && $this->total > 0) {
204
                    $piePart100 = \round($piePart / $this->total * 100, 2);  // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps
205
                } else {
206
                    $piePart100 = 0;
207
                }
208
                $colR      = \hexdec(mb_substr($value[2], 1, 2));
209
                $colG      = \hexdec(mb_substr($value[2], 3, 2));
210
                $colB      = \hexdec(mb_substr($value[2], 5, 2));
211
                $PartColor = \imagefilledarc($legendImage, $colR, $colG, $colB);
212
213
                \imagefilledrectangle($legendImage, 5, $yOffset + 2, 5 + $box_width, $yOffset + $box_height + 2, $PartColor);
214
                \imagerectangle($legendImage, 5, $yOffset + 2, 5 + $box_width, $yOffset + $box_height + 2, $borderColor);
215
216
                $text = $value[0] . ' ' . $piePart100 . '%';
217
                \imagestring($legendImage, 2, '20', $yOffset, $text, $textColor);
0 ignored issues
show
Bug introduced by
$textColor of type boolean is incompatible with the type integer expected by parameter $color of imagestring(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

217
                \imagestring($legendImage, 2, '20', $yOffset, $text, /** @scrutinizer ignore-type */ $textColor);
Loading history...
218
                $yOffset += 15;
219
            }
220
221
            \reset($EQpieData); // reset pointer in array to first
222
223
            \imagecopyresampled($finalPie, $legendImage, $pieWidth / 3, 10, 0, 0, $leg_width, $leg_height, $leg_width, $leg_height);
224
            \imagedestroy($legendImage);
225
        }
226
        \header('Content-type: image/png');
227
        \imagepng($finalPie, $filename);
228
        \imagedestroy($pie);
0 ignored issues
show
Bug introduced by
It seems like $pie can also be of type false; however, parameter $image of imagedestroy() does only seem to accept GdImage|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

228
        \imagedestroy(/** @scrutinizer ignore-type */ $pie);
Loading history...
229
        \imagedestroy($finalPie);
230
    }
231
}
232