Axis::Stroke()   F
last analyzed

Complexity

Conditions 24
Paths 589

Size

Total Lines 100
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 56
CRAP Score 24.7898

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 24
eloc 68
c 1
b 0
f 0
nc 589
nop 2
dl 0
loc 100
ccs 56
cts 63
cp 0.8889
crap 24.7898
rs 0.5708

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
/**
4
 * JPGraph v4.0.3
5
 */
6
7
namespace Amenadiel\JpGraph\Graph;
8
9
use Amenadiel\JpGraph\Util;
10
11
/**
12
 * @class Axis
13
 * // Description: Defines X and Y axis. Notes that at the
14
 * // moment the code is not really good since the axis on
15
 * // several occasion must know wheter it's an X or Y axis.
16
 * // This was a design decision to make the code easier to
17
 * // follow.
18
 */
19
class Axis extends AxisPrototype
20
{
21 19
    public function __construct($img, $aScale, $color = 'black')
22
    {
23 19
        parent::__construct($img, $aScale, $color);
24 19
    }
25
26
    // Stroke the axis.
27 19
    public function Stroke($aOtherAxisScale, $aStrokeLabels = true)
28
    {
29 19
        if ($this->hide) {
30 1
            return;
31
        }
32
33 19
        if (is_numeric($this->pos)) {
0 ignored issues
show
introduced by
The condition is_numeric($this->pos) is always false.
Loading history...
34 17
            $pos = $aOtherAxisScale->Translate($this->pos);
35
        } else {
36
            // Default to minimum of other scale if pos not set
37 19
            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...
38 18
                $pos = $aOtherAxisScale->scale_abs[0];
39 6
            } elseif ($this->pos == 'max') {
40 5
                $pos = $aOtherAxisScale->scale_abs[1];
41
            } else {
42
                // If negative set x-axis at 0
43 4
                $this->pos = 0;
44 4
                $pos       = $aOtherAxisScale->Translate(0);
45
            }
46
        }
47
48 19
        $pos += $this->iDeltaAbsPos;
49 19
        $this->img->SetLineWeight($this->weight);
50 19
        $this->img->SetColor($this->color);
51 19
        $this->img->SetFont($this->font_family, $this->font_style, $this->font_size);
52
53 19
        if ($this->scale->type == 'x') {
54 19
            if (!$this->hide_line) {
55
                // Stroke X-axis
56 19
                $this->img->FilledRectangle(
57 19
                    $this->img->left_margin,
58 19
                    $pos,
59 19
                    $this->img->width - $this->img->right_margin,
60 19
                    $pos + $this->weight - 1
61
                );
62
            }
63 19
            if ($this->title_side == SIDE_DOWN) {
64 19
                $y      = $pos + $this->img->GetFontHeight() + $this->title_margin + $this->title->margin;
65 19
                $yalign = 'top';
66
            } else {
67
                $y      = $pos - $this->img->GetFontHeight() - $this->title_margin - $this->title->margin;
68
                $yalign = 'bottom';
69
            }
70
71 19
            if ($this->title_adjust == 'high') {
72 19
                $this->title->SetPos($this->img->width - $this->img->right_margin, $y, 'right', $yalign);
73 1
            } elseif ($this->title_adjust == 'middle' || $this->title_adjust == 'center') {
74
                $this->title->SetPos(($this->img->width - $this->img->left_margin - $this->img->right_margin) / 2 + $this->img->left_margin, $y, 'center', $yalign);
75 1
            } elseif ($this->title_adjust == 'low') {
76 1
                $this->title->SetPos($this->img->left_margin, $y, 'left', $yalign);
77
            } else {
78 19
                Util\JpGraphError::RaiseL(25060, $this->title_adjust); //('Unknown alignment specified for X-axis title. ('.$this->title_adjust.')');
79
            }
80 19
        } elseif ($this->scale->type == 'y') {
81
            // Add line weight to the height of the axis since
82
            // the x-axis could have a width>1 and we want the axis to fit nicely together.
83 19
            if (!$this->hide_line) {
84
                // Stroke Y-axis
85 18
                $this->img->FilledRectangle(
86 18
                    $pos - $this->weight + 1,
87 18
                    $this->img->top_margin,
88 18
                    $pos,
89 18
                    $this->img->height - $this->img->bottom_margin + $this->weight - 1
90
                );
91
            }
92
93 19
            $x = $pos;
94 19
            if ($this->title_side == SIDE_LEFT) {
95 19
                $x -= $this->title_margin;
96 19
                $x -= $this->title->margin;
97 19
                $halign = 'right';
98
            } else {
99 3
                $x += $this->title_margin;
100 3
                $x += $this->title->margin;
101 3
                $halign = 'left';
102
            }
103
            // If the user has manually specified an hor. align
104
            // then we override the automatic settings with this
105
            // specifed setting. Since default is 'left' we compare
106
            // with that. (This means a manually set 'left' align
107
            // will have no effect.)
108 19
            if ($this->title->halign != 'left') {
109 19
                $halign = $this->title->halign;
110
            }
111 19
            if ($this->title_adjust == 'high') {
112
                $this->title->SetPos($x, $this->img->top_margin, $halign, 'top');
113 19
            } elseif ($this->title_adjust == 'middle' || $this->title_adjust == 'center') {
114 19
                $this->title->SetPos($x, ($this->img->height - $this->img->top_margin - $this->img->bottom_margin) / 2 + $this->img->top_margin, $halign, 'center');
115
            } elseif ($this->title_adjust == 'low') {
116
                $this->title->SetPos($x, $this->img->height - $this->img->bottom_margin, $halign, 'bottom');
117
            } else {
118
                Util\JpGraphError::RaiseL(25061, $this->title_adjust); //('Unknown alignment specified for Y-axis title. ('.$this->title_adjust.')');
119
            }
120
        }
121 19
        $this->scale->ticks->Stroke($this->img, $this->scale, $pos);
122 19
        if ($aStrokeLabels) {
123 19
            if (!$this->hide_labels) {
124 19
                $this->StrokeLabels($pos);
125
            }
126 19
            $this->title->Stroke($this->img);
127
        }
128 19
    }
129
130
    /**
131
     * PRIVATE METHODS
132
     * // Draw all the tick labels on major tick marks.
133
     *
134
     * @param mixed $aPos
135
     * @param mixed $aMinor
136
     * @param mixed $aAbsLabel
137
     */
138 19
    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. ( Ignorable by Annotation )

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

138
    public function StrokeLabels($aPos, /** @scrutinizer ignore-unused */ $aMinor = false, $aAbsLabel = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
    {
140 19
        if (is_array($this->label_color) && safe_count($this->label_color) > 3) {
141 1
            $this->ticks_label_colors = $this->label_color;
142 1
            $this->img->SetColor($this->label_color[0]);
143
        } else {
144 19
            $this->img->SetColor($this->label_color);
145
        }
146 19
        $this->img->SetFont($this->font_family, $this->font_style, $this->font_size);
147 19
        $yoff = $this->img->GetFontHeight() / 2;
0 ignored issues
show
Unused Code introduced by
The assignment to $yoff is dead and can be removed.
Loading history...
148
149
        // Only draw labels at major tick marks
150 19
        $nbr = safe_count($this->scale->ticks->maj_ticks_label);
151
152
        // We have the option to not-display the very first mark
153
        // (Usefull when the first label might interfere with another
154
        // axis.)
155 19
        $i = $this->show_first_label ? 0 : 1;
156 19
        if (!$this->show_last_label) {
157 1
            --$nbr;
158
        }
159
        // Now run through all labels making sure we don't overshoot the end
160
        // of the scale.
161 19
        $ncolor = 0;
162 19
        if (isset($this->ticks_label_colors)) {
163 1
            $ncolor = safe_count($this->ticks_label_colors);
164
        }
165 19
        while ($i < $nbr) {
166
            // $tpos holds the absolute text position for the label
167 19
            $tpos = $this->scale->ticks->maj_ticklabels_pos[$i];
168
169
            // Note. the $limit is only used for the x axis since we
170
            // might otherwise overshoot if the scale has been centered
171
            // This is due to us "loosing" the last tick mark if we center.
172 19
            if ($this->scale->type == 'x' && $tpos > $this->img->width - $this->img->right_margin + 1) {
173 7
                return;
174
            }
175
            // we only draw every $label_step label
176 19
            if (($i % $this->label_step) == 0) {
177
                // Set specific label color if specified
178 19
                if ($ncolor > 0) {
179 1
                    $this->img->SetColor($this->ticks_label_colors[$i % $ncolor]);
180
                }
181
182
                // If the label has been specified use that and in other case
183
                // just label the mark with the actual scale value
184 19
                $m = $this->scale->ticks->GetMajor();
185
186
                // ticks_label has an entry for each data point and is the array
187
                // that holds the labels set by the user. If the user hasn't
188
                // specified any values we use whats in the automatically asigned
189
                // labels in the maj_ticks_label
190 19
                if (isset($this->ticks_label[$i * $m])) {
191 8
                    $label = $this->ticks_label[$i * $m];
192
                } else {
193 19
                    if ($aAbsLabel) {
194
                        $label = abs($this->scale->ticks->maj_ticks_label[$i]);
195
                    } else {
196 19
                        $label = $this->scale->ticks->maj_ticks_label[$i];
197
                    }
198
199
                    // We number the scale from 1 and not from 0 so increase by one
200 19
                    if ($this->scale->textscale &&
201 19
                        $this->scale->ticks->label_formfunc == '' &&
202 19
                        !$this->scale->ticks->HaveManualLabels()) {
203 7
                        ++$label;
204
                    }
205
                }
206
207 19
                if ($this->scale->type == 'x') {
208 19
                    if ($this->labelPos == SIDE_DOWN) {
209 19
                        if ($this->label_angle == 0 || $this->label_angle == 90) {
210 18
                            if ($this->label_halign == '' && $this->label_valign == '') {
211 18
                                $this->img->SetTextAlign('center', 'top');
212
                            } else {
213 18
                                $this->img->SetTextAlign($this->label_halign, $this->label_valign);
214
                            }
215
                        } else {
216 2
                            if ($this->label_halign == '' && $this->label_valign == '') {
217 2
                                $this->img->SetTextAlign('right', 'top');
218
                            } else {
219
                                $this->img->SetTextAlign($this->label_halign, $this->label_valign);
220
                            }
221
                        }
222 19
                        $this->img->StrokeText(
223 19
                            $tpos,
224 19
                            $aPos + $this->tick_label_margin,
225 19
                            $label,
226 19
                            $this->label_angle,
227 19
                            $this->label_para_align
228
                        );
229
                    } else {
230 3
                        if ($this->label_angle == 0 || $this->label_angle == 90) {
231 3
                            if ($this->label_halign == '' && $this->label_valign == '') {
232 3
                                $this->img->SetTextAlign('center', 'bottom');
233
                            } else {
234 3
                                $this->img->SetTextAlign($this->label_halign, $this->label_valign);
235
                            }
236
                        } else {
237
                            if ($this->label_halign == '' && $this->label_valign == '') {
238
                                $this->img->SetTextAlign('right', 'bottom');
239
                            } else {
240
                                $this->img->SetTextAlign($this->label_halign, $this->label_valign);
241
                            }
242
                        }
243 3
                        $this->img->StrokeText(
244 3
                            $tpos,
245 3
                            $aPos - $this->tick_label_margin - 1,
246 3
                            $label,
247 3
                            $this->label_angle,
248 19
                            $this->label_para_align
249
                        );
250
                    }
251
                } else {
252
                    // scale->type == "y"
253
                    //if( $this->label_angle!=0 )
254
                    //Util\JpGraphError::Raise(" Labels at an angle are not supported on Y-axis");
255 19
                    if ($this->labelPos == SIDE_LEFT) {
256
                        // To the left of y-axis
257 19
                        if ($this->label_halign == '' && $this->label_valign == '') {
258 19
                            $this->img->SetTextAlign('right', 'center');
259
                        } else {
260 1
                            $this->img->SetTextAlign($this->label_halign, $this->label_valign);
261
                        }
262 19
                        $this->img->StrokeText($aPos - $this->tick_label_margin, $tpos, $label, $this->label_angle, $this->label_para_align);
263
                    } else {
264
                        // To the right of the y-axis
265 5
                        if ($this->label_halign == '' && $this->label_valign == '') {
266 5
                            $this->img->SetTextAlign('left', 'center');
267
                        } else {
268
                            $this->img->SetTextAlign($this->label_halign, $this->label_valign);
269
                        }
270 5
                        $this->img->StrokeText($aPos + $this->tick_label_margin, $tpos, $label, $this->label_angle, $this->label_para_align);
271
                    }
272
                }
273
            }
274 19
            ++$i;
275
        }
276 19
    }
277
}
278