Completed
Push — master ( c8eead...62889a )
by Michael
04:13
created

eq_pie   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 219
Duplicated Lines 6.85 %

Coupling/Cohesion

Components 1
Dependencies 0
Metric Value
wmc 24
lcom 1
cbo 0
dl 15
loc 219
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A eq_pie() 0 3 1
B GetColor() 0 25 1
F MakePie() 15 171 22

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
// eq_pie written by ellardus (C) 2005
4
// for more info look at www.eq-home.com
5
// or email at [email protected]
6
// Feel free to use it, a reference to me would be nice.
7
// Thank you and good luck!
8
9
/**
10
 * Class eq_pie
11
 */
12
class eq_pie
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
13
{
14
    function eq_pie()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
15
    {
16
    }
17
18
    /**
19
     * @param $number
20
     *
21
     * @return mixed
22
     */
23
    function GetColor($number)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
24
    {
25
        $color = array(
26
            '#ff0000',
27
            '#00ff00',
28
            '#0000ff',
29
            '#ffff00',
30
            '#ff00ff',
31
            '#00ffff',
32
            '#cc0000',
33
            '#00cc00',
34
            '#0000cc',
35
            '#990000',
36
            '#009900',
37
            '#000099',
38
            '#660000',
39
            '#006600',
40
            '#000066',
41
            '#330000',
42
            '#003300',
43
            '#000033'
44
        );
45
46
        return $color[$number];
47
    }
48
49
    /**
50
     * @param $filename
51
     * @param $pieWidth
52
     * @param $pieHeight
53
     * @param $ShadowDistance
54
     * @param $pieBackgroundColor
55
     * @param $EQpieData
56
     * @param $legend
57
     */
58
    function MakePie($filename, $pieWidth, $pieHeight, $ShadowDistance, $pieBackgroundColor, $EQpieData, $legend)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
59
    {
60
        if (!function_exists("imagecreatetruecolor")) {
61
            die("Error, GD Library 2 needed.");
0 ignored issues
show
Coding Style Compatibility introduced by
The method MakePie() 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...
62
        }
63
64
        //set some limitations
65
        if ($pieWidth < 100 | $pieWidth > 500) {
66
            $pieWidth = 100;
67
        }
68
        if ($pieHeight < 100 | $pieHeight > 500) {
69
            $pieHeight = 100;
70
        }
71
        if ($ShadowDistance < 1 | $ShadowDistance > 50) {
72
            $ShadowDistance = 10;
73
        }
74
75
        $pieWidth           = $pieWidth * 3;
76
        $pieHeight          = $pieHeight * 3;
77
        $ShadowDistance     = $ShadowDistance * 3;
78
        $pieBackgroundColor = $pieBackgroundColor;
0 ignored issues
show
Bug introduced by
Why assign $pieBackgroundColor to itself?

This checks looks for cases where a variable has been assigned to itself.

This assignement can be removed without consequences.

Loading history...
79
80
        $pie = @ImageCreateTrueColor($pieWidth, $pieHeight + $ShadowDistance);
81
82
        $colR  = hexdec(substr($pieBackgroundColor, 1, 2));
83
        $colG  = hexdec(substr($pieBackgroundColor, 3, 2));
84
        $colB  = hexdec(substr($pieBackgroundColor, 5, 2));
85
        $pieBG = ImageColorAllocate($pie, $colR, $colG, $colB);
86
        ImageFill($pie, 0, 0, $pieBG);
87
88
        // get the total value for percentage calculations
89
        $this->total = 0;
0 ignored issues
show
Bug introduced by
The property total does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
90
91
        $maxStringLenght = 0;
92
        foreach ($EQpieData as $i => $value) {
93
            $this->total += $value[1];
94
            if (strlen($value[0]) > $maxStringLenght) {
95
                $maxStringLenght = strlen($value[0]);
96
            }
97
98
        }
99
100
        $pieParts = $i + 1;
0 ignored issues
show
Bug introduced by
The variable $i seems to be defined by a foreach iteration on line 92. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
101
        reset($EQpieData);
102
        $legendWidth = (($legend > 0) ? ImageFontWidth(2) * ($maxStringLenght + 6) + 40 : 0);
103
104
        // the first pie-part starts with offset in degrees up from horizantal right, looks better this way
105
        $pieStart = 135;
106
107
        foreach ($EQpieData as $i => $value) {
108
109
            // the name  for each part is $value[0]
110
            // the value for each part is $value[1]
111
            // the color for each part is $value[2]
112
113
            $piePart = $value[1];
114 View Code Duplication
            if (isset($this->total) && $this->total > 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...
115
                $piePart100 = round(($piePart / $this->total * 100), 2);  // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps
116
            } else {
117
                $piePart100 = 0;
118
            }
119
120
            $piePart360 = $piePart100 * 3.6;                    // in degrees
121
122
            $colR      = hexdec(substr($value[2], 1, 2));
123
            $colG      = hexdec(substr($value[2], 3, 2));
124
            $colB      = hexdec(substr($value[2], 5, 2));
125
            $PartColor = ImageColorAllocate($pie, $colR, $colG, $colB);
0 ignored issues
show
Unused Code introduced by
$PartColor 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...
126
127
            $ShadowColR = (($colR > 79) ? $colR - 80 : 0);
128
            $ShadowColG = (($colG > 79) ? $colG - 80 : 0);
129
            $ShadowColB = (($colB > 79) ? $colB - 80 : 0);
130
131
            $ShadowColor = ImageColorAllocate($pie, $ShadowColR, $ShadowColG, $ShadowColB);
132
133
            //Here we create the shadow down-worths
134
            for ($i = 0; $i < $ShadowDistance; ++$i) {
135
                ImageFilledArc($pie, $pieWidth / 2, ($pieHeight / 2 + $i), $pieWidth - 20, $pieHeight - 20, round($pieStart), round($pieStart + $piePart360), $ShadowColor, IMG_ARC_NOFILL);
136
            }
137
138
            $pieStart = $pieStart + $piePart360;
139
140
        }
141
        reset($EQpieData);
142
143
        $pieStart = 135;
144
145
        foreach ($EQpieData as $i => $value) {
146
147
            $piePart = $value[1];
148 View Code Duplication
            if (isset($this->total) && $this->total > 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...
149
                $piePart100 = round(($piePart / $this->total * 100), 2);  // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps
150
            } else {
151
                $piePart100 = 0;
152
            }
153
            $piePart360 = $piePart100 * 3.6;                    // in degrees
154
155
            $colR      = hexdec(substr($value[2], 1, 2));
156
            $colG      = hexdec(substr($value[2], 3, 2));
157
            $colB      = hexdec(substr($value[2], 5, 2));
158
            $PartColor = ImageColorAllocate($pie, $colR, $colG, $colB);
159
160
            //Here we create the real pie chart
161
            ImageFilledArc($pie, $pieWidth / 2, $pieHeight / 2, $pieWidth - 20, $pieHeight - 20, round($pieStart), round($pieStart + $piePart360), $PartColor, IMG_ARC_PIE);
162
163
            $pieStart = $pieStart + $piePart360;
164
165
        }
166
        reset($EQpieData);
167
168
        // create final pie picture with proper background color
169
        $finalPie = ImageCreateTrueColor($pieWidth / 3 + $legendWidth, ($pieHeight + $ShadowDistance) / 3);
170
        ImageFill($finalPie, 0, 0, $pieBG);
171
172
        // resample with pieGraph inside (3x smaller)
173
        ImageCopyResampled($finalPie, $pie, 0, 0, 0, 0, $pieWidth / 3, ($pieHeight + $ShadowDistance) / 3, $pieWidth, ($pieHeight + $ShadowDistance));
174
175
        // Create the ledgend ...
176
        if ($legendWidth > 0) {
177
            // Legend Box
178
            $leg_width   = $legendWidth - 10;
179
            $leg_height  = $pieParts * (ImageFontHeight(2) + 2) + 2;
180
            $legendImage = ImageCreateTrueColor($leg_width, $leg_height);
181
            //ImageFill($legendImage, 0, 0, $pieBG);
0 ignored issues
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...
182
183
            $borderColor = ImageColorAllocate($pie, '155', '155', '155');
184
            $boxColor    = ImageColorAllocate($pie, '255', '255', '255');
185
            $textColor   = ImageColorAllocate($pie, '55', '55', '55');
186
187
            ImageFilledRectangle($legendImage, 0, 0, $leg_width, $leg_height, $boxColor);
188
            ImageRectangle($legendImage, 0, 0, $leg_width - 1, $leg_height - 1, $borderColor);
189
190
            $box_width  = ImageFontHeight(2) - 5;
191
            $box_height = ImageFontHeight(2) - 5;
192
            $yOffset    = 2;
193
194
            foreach ($EQpieData as $i => $value) {
195
196
                $piePart = $value[1];
197 View Code Duplication
                if (isset($this->total) && $this->total > 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...
198
                    $piePart100 = round(($piePart / $this->total * 100), 2);  // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps
199
                } else {
200
                    $piePart100 = 0;
201
                }
202
                $colR      = hexdec(substr($value[2], 1, 2));
203
                $colG      = hexdec(substr($value[2], 3, 2));
204
                $colB      = hexdec(substr($value[2], 5, 2));
205
                $PartColor = ImageColorAllocate($legendImage, $colR, $colG, $colB);
206
207
                ImageFilledRectangle($legendImage, 5, $yOffset + 2, 5 + $box_width, $yOffset + $box_height + 2, $PartColor);
208
                ImageRectangle($legendImage, 5, $yOffset + 2, 5 + $box_width, $yOffset + $box_height + 2, $borderColor);
209
210
                $text = $value[0] . " " . $piePart100 . "%";
211
                ImageString($legendImage, 2, '20', $yOffset, $text, $textColor);
212
                $yOffset = $yOffset + 15;
213
214
            }
215
216
            reset($EQpieData); // reset pointer in array to first
217
218
            ImageCopyResampled($finalPie, $legendImage, $pieWidth / 3, 10, 0, 0, $leg_width, $leg_height, $leg_width, $leg_height);
219
            ImageDestroy($legendImage);
220
221
        }
222
        header('Content-type: image/png');
223
        imagepng($finalPie, $filename);
224
        ImageDestroy($pie);
225
        ImageDestroy($finalPie);
226
227
        return;
228
    }
229
230
}
231