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

Axis::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Amenadiel\JpGraph\Graph;
3
4
use Amenadiel\JpGraph\Util;
5
6
//===================================================
7
// CLASS Axis
8
// Description: Defines X and Y axis. Notes that at the
9
// moment the code is not really good since the axis on
10
// several occasion must know wheter it's an X or Y axis.
11
// This was a design decision to make the code easier to
12
// follow.
13
//===================================================
14
class Axis extends AxisPrototype
15
{
16
    public function __construct($img, $aScale, $color = 'black')
17
    {
18
        parent::__construct($img, $aScale, $color);
0 ignored issues
show
Documentation introduced by
$color is of type string, but the function expects a array.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
19
    }
20
21
    // Stroke the axis.
22
    public function Stroke($aOtherAxisScale, $aStrokeLabels = true)
23
    {
24
        if ($this->hide) {
25
            return;
26
        }
27
28
        if (is_numeric($this->pos)) {
29
            $pos = $aOtherAxisScale->Translate($this->pos);
30
        } else {
31
            // Default to minimum of other scale if pos not set
32
            if (($aOtherAxisScale->GetMinVal() >= 0 && $this->pos == false) || $this->pos == 'min') {
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...
33
                $pos = $aOtherAxisScale->scale_abs[0];
34
            } elseif ($this->pos == "max") {
35
                $pos = $aOtherAxisScale->scale_abs[1];
36
            } else {
37
                // If negative set x-axis at 0
38
                $this->pos = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $pos was declared of type boolean, but 0 is of type integer. 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...
39
                $pos       = $aOtherAxisScale->Translate(0);
40
            }
41
        }
42
43
        $pos += $this->iDeltaAbsPos;
44
        $this->img->SetLineWeight($this->weight);
45
        $this->img->SetColor($this->color);
46
        $this->img->SetFont($this->font_family, $this->font_style, $this->font_size);
47
48
        if ($this->scale->type == "x") {
49 View Code Duplication
            if (!$this->hide_line) {
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...
50
                // Stroke X-axis
51
                $this->img->FilledRectangle(
52
                    $this->img->left_margin,
53
                    $pos,
54
                    $this->img->width - $this->img->right_margin,
55
                    $pos + $this->weight - 1
56
                );
57
            }
58
            if ($this->title_side == SIDE_DOWN) {
59
                $y      = $pos + $this->img->GetFontHeight() + $this->title_margin + $this->title->margin;
0 ignored issues
show
Bug introduced by
The property margin does not seem to exist. Did you mean _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...
60
                $yalign = 'top';
61
            } else {
62
                $y      = $pos - $this->img->GetFontHeight() - $this->title_margin - $this->title->margin;
0 ignored issues
show
Bug introduced by
The property margin does not seem to exist. Did you mean _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...
63
                $yalign = 'bottom';
64
            }
65
66 View Code Duplication
            if ($this->title_adjust == 'high') {
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...
67
                $this->title->SetPos($this->img->width - $this->img->right_margin, $y, 'right', $yalign);
68
            } elseif ($this->title_adjust == 'middle' || $this->title_adjust == 'center') {
69
                $this->title->SetPos(($this->img->width - $this->img->left_margin - $this->img->right_margin) / 2 + $this->img->left_margin, $y, 'center', $yalign);
70
            } elseif ($this->title_adjust == 'low') {
71
                $this->title->SetPos($this->img->left_margin, $y, 'left', $yalign);
72
            } else {
73
                Util\JpGraphError::RaiseL(25060, $this->title_adjust); //('Unknown alignment specified for X-axis title. ('.$this->title_adjust.')');
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...
74
            }
75
        } elseif ($this->scale->type == "y") {
76
            // Add line weight to the height of the axis since
77
            // the x-axis could have a width>1 and we want the axis to fit nicely together.
78
            if (!$this->hide_line) {
79
                // Stroke Y-axis
80
                $this->img->FilledRectangle(
81
                    $pos - $this->weight + 1,
82
                    $this->img->top_margin,
83
                    $pos,
84
                    $this->img->height - $this->img->bottom_margin + $this->weight - 1
85
                );
86
            }
87
88
            $x = $pos;
89
            if ($this->title_side == SIDE_LEFT) {
90
                $x -= $this->title_margin;
91
                $x -= $this->title->margin;
0 ignored issues
show
Bug introduced by
The property margin does not seem to exist. Did you mean _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...
92
                $halign = 'right';
93
            } else {
94
                $x += $this->title_margin;
95
                $x += $this->title->margin;
0 ignored issues
show
Bug introduced by
The property margin does not seem to exist. Did you mean _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...
96
                $halign = 'left';
97
            }
98
            // If the user has manually specified an hor. align
99
            // then we override the automatic settings with this
100
            // specifed setting. Since default is 'left' we compare
101
            // with that. (This means a manually set 'left' align
102
            // will have no effect.)
103
            if ($this->title->halign != 'left') {
104
                $halign = $this->title->halign;
105
            }
106 View Code Duplication
            if ($this->title_adjust == 'high') {
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...
107
                $this->title->SetPos($x, $this->img->top_margin, $halign, 'top');
108
            } elseif ($this->title_adjust == 'middle' || $this->title_adjust == 'center') {
109
                $this->title->SetPos($x, ($this->img->height - $this->img->top_margin - $this->img->bottom_margin) / 2 + $this->img->top_margin, $halign, "center");
110
            } elseif ($this->title_adjust == 'low') {
111
                $this->title->SetPos($x, $this->img->height - $this->img->bottom_margin, $halign, 'bottom');
112
            } else {
113
                Util\JpGraphError::RaiseL(25061, $this->title_adjust); //('Unknown alignment specified for Y-axis title. ('.$this->title_adjust.')');
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...
114
            }
115
        }
116
        $this->scale->ticks->Stroke($this->img, $this->scale, $pos);
117
        if ($aStrokeLabels) {
118
            if (!$this->hide_labels) {
119
                $this->StrokeLabels($pos);
120
            }
121
            $this->title->Stroke($this->img);
122
        }
123
    }
124
125
    //---------------
126
    // PRIVATE METHODS
127
    // Draw all the tick labels on major tick marks
128
    public function StrokeLabels($aPos, $aMinor = false, $aAbsLabel = false)
0 ignored issues
show
Unused Code introduced by
The parameter $aMinor 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...
129
    {
130
        if (is_array($this->label_color) && count($this->label_color) > 3) {
131
            $this->ticks_label_colors = $this->label_color;
132
            $this->img->SetColor($this->label_color[0]);
133
        } else {
134
            $this->img->SetColor($this->label_color);
135
        }
136
        $this->img->SetFont($this->font_family, $this->font_style, $this->font_size);
137
        $yoff = $this->img->GetFontHeight() / 2;
0 ignored issues
show
Unused Code introduced by
$yoff 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...
138
139
        // Only draw labels at major tick marks
140
        $nbr = count($this->scale->ticks->maj_ticks_label);
141
142
        // We have the option to not-display the very first mark
143
        // (Usefull when the first label might interfere with another
144
        // axis.)
145
        $i = $this->show_first_label ? 0 : 1;
146
        if (!$this->show_last_label) {
147
            --$nbr;
148
        }
149
        // Now run through all labels making sure we don't overshoot the end
150
        // of the scale.
151
        $ncolor = 0;
152
        if (isset($this->ticks_label_colors)) {
153
            $ncolor = count($this->ticks_label_colors);
154
        }
155
        while ($i < $nbr) {
156
            // $tpos holds the absolute text position for the label
157
            $tpos = $this->scale->ticks->maj_ticklabels_pos[$i];
158
159
            // Note. the $limit is only used for the x axis since we
160
            // might otherwise overshoot if the scale has been centered
161
            // This is due to us "loosing" the last tick mark if we center.
162
            if ($this->scale->type == 'x' && $tpos > $this->img->width - $this->img->right_margin + 1) {
163
                return;
164
            }
165
            // we only draw every $label_step label
166
            if (($i % $this->label_step) == 0) {
167
168
                // Set specific label color if specified
169
                if ($ncolor > 0) {
170
                    $this->img->SetColor($this->ticks_label_colors[$i % $ncolor]);
171
                }
172
173
                // If the label has been specified use that and in other case
174
                // just label the mark with the actual scale value
175
                $m = $this->scale->ticks->GetMajor();
176
177
                // ticks_label has an entry for each data point and is the array
178
                // that holds the labels set by the user. If the user hasn't
179
                // specified any values we use whats in the automatically asigned
180
                // labels in the maj_ticks_label
181
                if (isset($this->ticks_label[$i * $m])) {
182
                    $label = $this->ticks_label[$i * $m];
183
                } else {
184
                    if ($aAbsLabel) {
185
                        $label = abs($this->scale->ticks->maj_ticks_label[$i]);
186
                    } else {
187
                        $label = $this->scale->ticks->maj_ticks_label[$i];
188
                    }
189
190
                    // We number the scale from 1 and not from 0 so increase by one
191
                    if ($this->scale->textscale &&
192
                        $this->scale->ticks->label_formfunc == '' &&
193
                        !$this->scale->ticks->HaveManualLabels()) {
194
                        ++$label;
195
                    }
196
                }
197
198
                if ($this->scale->type == "x") {
199
                    if ($this->labelPos == SIDE_DOWN) {
200 View Code Duplication
                        if ($this->label_angle == 0 || $this->label_angle == 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...
201
                            if ($this->label_halign == '' && $this->label_valign == '') {
202
                                $this->img->SetTextAlign('center', 'top');
203
                            } else {
204
                                $this->img->SetTextAlign($this->label_halign, $this->label_valign);
205
                            }
206
                        } else {
207
                            if ($this->label_halign == '' && $this->label_valign == '') {
208
                                $this->img->SetTextAlign("right", "top");
209
                            } else {
210
                                $this->img->SetTextAlign($this->label_halign, $this->label_valign);
211
                            }
212
                        }
213
                        $this->img->StrokeText($tpos, $aPos + $this->tick_label_margin, $label,
214
                            $this->label_angle, $this->label_para_align);
215
                    } else {
216 View Code Duplication
                        if ($this->label_angle == 0 || $this->label_angle == 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...
217
                            if ($this->label_halign == '' && $this->label_valign == '') {
218
                                $this->img->SetTextAlign("center", "bottom");
219
                            } else {
220
                                $this->img->SetTextAlign($this->label_halign, $this->label_valign);
221
                            }
222
                        } else {
223
                            if ($this->label_halign == '' && $this->label_valign == '') {
224
                                $this->img->SetTextAlign("right", "bottom");
225
                            } else {
226
                                $this->img->SetTextAlign($this->label_halign, $this->label_valign);
227
                            }
228
                        }
229
                        $this->img->StrokeText($tpos, $aPos - $this->tick_label_margin - 1, $label,
230
                            $this->label_angle, $this->label_para_align);
231
                    }
232
                } else {
233
                    // scale->type == "y"
234
                    //if( $this->label_angle!=0 )
1 ignored issue
show
Unused Code Comprehensibility introduced by
64% 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...
235
                    //Util\JpGraphError::Raise(" Labels at an angle are not supported on Y-axis");
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...
236
                    if ($this->labelPos == SIDE_LEFT) {
237
                        // To the left of y-axis
238
                        if ($this->label_halign == '' && $this->label_valign == '') {
239
                            $this->img->SetTextAlign("right", "center");
240
                        } else {
241
                            $this->img->SetTextAlign($this->label_halign, $this->label_valign);
242
                        }
243
                        $this->img->StrokeText($aPos - $this->tick_label_margin, $tpos, $label, $this->label_angle, $this->label_para_align);
244
                    } else {
245
                        // To the right of the y-axis
246
                        if ($this->label_halign == '' && $this->label_valign == '') {
247
                            $this->img->SetTextAlign("left", "center");
248
                        } else {
249
                            $this->img->SetTextAlign($this->label_halign, $this->label_valign);
250
                        }
251
                        $this->img->StrokeText($aPos + $this->tick_label_margin, $tpos, $label, $this->label_angle, $this->label_para_align);
252
                    }
253
                }
254
            }
255
            ++$i;
256
        }
257
    }
258
}
259