Failed Conditions
Pull Request — master (#3876)
by Abdul Malik
12:15
created

Axis   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 332
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 42
eloc 131
c 0
b 0
f 0
dl 0
loc 332
rs 9.0399
ccs 96
cts 96
cp 1

How to fix   Complexity   

Complex Class

Complex classes like Axis often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Axis, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Chart;
4
5
/**
6
 * Created by PhpStorm.
7
 * User: Wiktor Trzonkowski
8
 * Date: 6/17/14
9
 * Time: 12:11 PM.
10
 */
11
class Axis extends Properties
12
{
13
    public const AXIS_TYPE_CATEGORY = 'catAx';
14
    public const AXIS_TYPE_DATE = 'dateAx';
15
    public const AXIS_TYPE_VALUE = 'valAx';
16
17
    public const TIME_UNIT_DAYS = 'days';
18
    public const TIME_UNIT_MONTHS = 'months';
19
    public const TIME_UNIT_YEARS = 'years';
20
21 111
    public function __construct()
22
    {
23 111
        parent::__construct();
24 111
        $this->fillColor = new ChartColor();
25
    }
26
27
    /**
28
     * Chart Major Gridlines as.
29
     */
30
    private ?GridLines $majorGridlines = null;
31
32
    /**
33
     * Chart Minor Gridlines as.
34
     */
35
    private ?GridLines $minorGridlines = null;
36
37
    /**
38
     * Axis Number.
39
     *
40
     * @var mixed[]
41
     */
42
    private array $axisNumber = [
43
        'format' => self::FORMAT_CODE_GENERAL,
44
        'source_linked' => 1,
45
        'numeric' => null,
46
    ];
47
48
    private string $axisType = '';
49
50
    private ?AxisText $axisText = null;
51
52
    private ?Title $dispUnitsTitle = null;
53
54
    /**
55
     * Axis Options.
56
     *
57
     * @var array<string, null|string>
58
     */
59
    private array $axisOptions = [
60
        'minimum' => null,
61
        'maximum' => null,
62
        'major_unit' => null,
63
        'minor_unit' => null,
64
        'orientation' => self::ORIENTATION_NORMAL,
65
        'minor_tick_mark' => self::TICK_MARK_NONE,
66
        'major_tick_mark' => self::TICK_MARK_NONE,
67
        'axis_labels' => self::AXIS_LABELS_NEXT_TO,
68
        'horizontal_crosses' => self::HORIZONTAL_CROSSES_AUTOZERO,
69
        'horizontal_crosses_value' => null,
70
        'textRotation' => null,
71
        'hidden' => null,
72
        'majorTimeUnit' => self::TIME_UNIT_YEARS,
73
        'minorTimeUnit' => self::TIME_UNIT_MONTHS,
74
        'baseTimeUnit' => self::TIME_UNIT_DAYS,
75
        'logBase' => null,
76
        'dispUnitsBuiltIn' => null,
77
    ];
78
    public const DISP_UNITS_HUNDREDS = 'hundreds';
79
    public const DISP_UNITS_THOUSANDS = 'thousands';
80
    public const DISP_UNITS_TEN_THOUSANDS = 'tenThousands';
81
    public const DISP_UNITS_HUNDRED_THOUSANDS = 'hundredThousands';
82
    public const DISP_UNITS_MILLIONS = 'millions';
83
    public const DISP_UNITS_TEN_MILLIONS = 'tenMillions';
84
    public const DISP_UNITS_HUNDRED_MILLIONS = 'hundredMillions';
85
    public const DISP_UNITS_BILLIONS = 'billions';
86
    public const DISP_UNITS_TRILLIONS = 'trillions';
87
    public const TRILLION_INDEX = (PHP_INT_SIZE > 4) ? 1_000_000_000_000 : '1000000000000';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING on line 87 at column 56
Loading history...
88
    public const DISP_UNITS_BUILTIN_INT = [
89
        100 => self::DISP_UNITS_HUNDREDS,
90
        1000 => self::DISP_UNITS_THOUSANDS,
91
        10000 => self::DISP_UNITS_TEN_THOUSANDS,
92
        100000 => self::DISP_UNITS_HUNDRED_THOUSANDS,
93
        1_000_000 => self::DISP_UNITS_MILLIONS,
94
        10_000_000 => self::DISP_UNITS_TEN_MILLIONS,
95
        100_000_000 => self::DISP_UNITS_HUNDRED_MILLIONS,
96
        1_000_000_000 => self::DISP_UNITS_BILLIONS,
97
        self::TRILLION_INDEX => self::DISP_UNITS_TRILLIONS, // overflow for 32-bit
98
    ];
99
100
    /**
101
     * Fill Properties.
102
     */
103
    private ChartColor $fillColor;
104
105
    private const NUMERIC_FORMAT = [
106
        Properties::FORMAT_CODE_NUMBER,
107
        Properties::FORMAT_CODE_DATE,
108
        Properties::FORMAT_CODE_DATE_ISO8601,
109
    ];
110
111
    private bool $noFill = false;
112
113
    /**
114
     * Get Series Data Type.
115 69
     */
116
    public function setAxisNumberProperties(string $format_code, ?bool $numeric = null, int $sourceLinked = 0): void
117 69
    {
118 69
        $format = $format_code;
119 69
        $this->axisNumber['format'] = $format;
120 69
        $this->axisNumber['source_linked'] = $sourceLinked;
121 8
        if (is_bool($numeric)) {
122 65
            $this->axisNumber['numeric'] = $numeric;
123 8
        } elseif (in_array($format, self::NUMERIC_FORMAT, true)) {
124
            $this->axisNumber['numeric'] = true;
125
        }
126
    }
127
128
    /**
129
     * Get Axis Number Format Data Type.
130 81
     */
131
    public function getAxisNumberFormat(): string
132 81
    {
133
        return $this->axisNumber['format'];
134
    }
135
136
    /**
137
     * Get Axis Number Source Linked.
138 81
     */
139
    public function getAxisNumberSourceLinked(): string
140 81
    {
141
        return (string) $this->axisNumber['source_linked'];
142
    }
143 45
144
    public function getAxisIsNumericFormat(): bool
145 45
    {
146
        return $this->axisType === self::AXIS_TYPE_DATE || (bool) $this->axisNumber['numeric'];
147
    }
148 68
149
    public function setAxisOption(string $key, null|float|int|string $value): void
150 68
    {
151 67
        if ($value !== null && $value !== '') {
152
            $this->axisOptions[$key] = (string) $value;
153
        }
154
    }
155
156
    /**
157
     * Set Axis Options Properties.
158 4
     */
159
    public function setAxisOptionsProperties(
160
        string $axisLabels,
161
        ?string $horizontalCrossesValue = null,
162
        ?string $horizontalCrosses = null,
163
        ?string $axisOrientation = null,
164
        ?string $majorTmt = null,
165
        ?string $minorTmt = null,
166
        null|float|int|string $minimum = null,
167
        null|float|int|string $maximum = null,
168
        null|float|int|string $majorUnit = null,
169
        null|float|int|string $minorUnit = null,
170
        null|float|int|string $textRotation = null,
171
        ?string $hidden = null,
172
        ?string $baseTimeUnit = null,
173
        ?string $majorTimeUnit = null,
174
        ?string $minorTimeUnit = null,
175
        null|float|int|string $logBase = null,
176
        ?string $dispUnitsBuiltIn = null
177 4
    ): void {
178 4
        $this->axisOptions['axis_labels'] = $axisLabels;
179 4
        $this->setAxisOption('horizontal_crosses_value', $horizontalCrossesValue);
180 4
        $this->setAxisOption('horizontal_crosses', $horizontalCrosses);
181 4
        $this->setAxisOption('orientation', $axisOrientation);
182 4
        $this->setAxisOption('major_tick_mark', $majorTmt);
183 4
        $this->setAxisOption('minor_tick_mark', $minorTmt);
184 4
        $this->setAxisOption('minimum', $minimum);
185 4
        $this->setAxisOption('maximum', $maximum);
186 4
        $this->setAxisOption('major_unit', $majorUnit);
187 4
        $this->setAxisOption('minor_unit', $minorUnit);
188 4
        $this->setAxisOption('textRotation', $textRotation);
189 4
        $this->setAxisOption('hidden', $hidden);
190 4
        $this->setAxisOption('baseTimeUnit', $baseTimeUnit);
191 4
        $this->setAxisOption('majorTimeUnit', $majorTimeUnit);
192 4
        $this->setAxisOption('minorTimeUnit', $minorTimeUnit);
193 4
        $this->setAxisOption('logBase', $logBase);
194
        $this->setAxisOption('dispUnitsBuiltIn', $dispUnitsBuiltIn);
195
    }
196
197
    /**
198
     * Get Axis Options Property.
199 81
     */
200
    public function getAxisOptionsProperty(string $property): ?string
201 81
    {
202 81
        if ($property === 'textRotation') {
203 10
            if ($this->axisText !== null) {
204 7
                if ($this->axisText->getRotation() !== null) {
205
                    return (string) $this->axisText->getRotation();
206
                }
207
            }
208
        }
209 81
210
        return $this->axisOptions[$property];
211
    }
212
213
    /**
214
     * Set Axis Orientation Property.
215 1
     */
216
    public function setAxisOrientation(string $orientation): void
217 1
    {
218
        $this->axisOptions['orientation'] = (string) $orientation;
219
    }
220 81
221
    public function getAxisType(): string
222 81
    {
223
        return $this->axisType;
224
    }
225 66
226
    public function setAxisType(string $type): self
227 66
    {
228 66
        if ($type === self::AXIS_TYPE_CATEGORY || $type === self::AXIS_TYPE_VALUE || $type === self::AXIS_TYPE_DATE) {
229
            $this->axisType = $type;
230 3
        } else {
231
            $this->axisType = '';
232
        }
233 66
234
        return $this;
235
    }
236
237
    /**
238
     * Set Fill Property.
239 4
     */
240
    public function setFillParameters(?string $color, ?int $alpha = null, ?string $AlphaType = ChartColor::EXCEL_COLOR_TYPE_RGB): void
241 4
    {
242
        $this->fillColor->setColorProperties($color, $alpha, $AlphaType);
243
    }
244
245
    /**
246
     * Get Fill Property.
247 1
     */
248
    public function getFillProperty(string $property): string
249 1
    {
250
        return (string) $this->fillColor->getColorProperty($property);
251
    }
252 81
253
    public function getFillColorObject(): ChartColor
254 81
    {
255
        return $this->fillColor;
256
    }
257
258
    private string $crossBetween = ''; // 'between' or 'midCat' might be better
259 46
260
    public function setCrossBetween(string $crossBetween): self
261 46
    {
262
        $this->crossBetween = $crossBetween;
263 46
264
        return $this;
265
    }
266 81
267
    public function getCrossBetween(): string
268 81
    {
269
        return $this->crossBetween;
270
    }
271 81
272
    public function getMajorGridlines(): ?GridLines
273 81
    {
274
        return $this->majorGridlines;
275
    }
276 81
277
    public function getMinorGridlines(): ?GridLines
278 81
    {
279
        return $this->minorGridlines;
280
    }
281 52
282
    public function setMajorGridlines(?GridLines $gridlines): self
283 52
    {
284
        $this->majorGridlines = $gridlines;
285 52
286
        return $this;
287
    }
288 11
289
    public function setMinorGridlines(?GridLines $gridlines): self
290 11
    {
291
        $this->minorGridlines = $gridlines;
292 11
293
        return $this;
294
    }
295 81
296
    public function getAxisText(): ?AxisText
297 81
    {
298
        return $this->axisText;
299
    }
300 16
301
    public function setAxisText(?AxisText $axisText): self
302 16
    {
303
        $this->axisText = $axisText;
304 16
305
        return $this;
306
    }
307 8
308
    public function setNoFill(bool $noFill): self
309 8
    {
310
        $this->noFill = $noFill;
311 8
312
        return $this;
313
    }
314 82
315
    public function getNoFill(): bool
316 82
    {
317
        return $this->noFill;
318
    }
319 3
320
    public function setDispUnitsTitle(?Title $dispUnitsTitle): self
321 3
    {
322
        $this->dispUnitsTitle = $dispUnitsTitle;
323 3
324
        return $this;
325
    }
326 3
327
    public function getDispUnitsTitle(): ?Title
328 3
    {
329
        return $this->dispUnitsTitle;
330
    }
331
332
    /**
333
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
334 5
     */
335
    public function __clone()
336 5
    {
337 5
        parent::__clone();
338 5
        $this->majorGridlines = ($this->majorGridlines === null) ? null : clone $this->majorGridlines;
339 5
        $this->majorGridlines = ($this->minorGridlines === null) ? null : clone $this->minorGridlines;
340 5
        $this->axisText = ($this->axisText === null) ? null : clone $this->axisText;
341 5
        $this->dispUnitsTitle = ($this->dispUnitsTitle === null) ? null : clone $this->dispUnitsTitle;
342
        $this->fillColor = clone $this->fillColor;
343
    }
344
}
345