Failed Conditions
Pull Request — master (#3876)
by Abdul Malik
22:45 queued 13:31
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
     */
116 69
    public function setAxisNumberProperties(string $format_code, ?bool $numeric = null, int $sourceLinked = 0): void
117
    {
118 69
        $format = $format_code;
119 69
        $this->axisNumber['format'] = $format;
120 69
        $this->axisNumber['source_linked'] = $sourceLinked;
121 69
        if (is_bool($numeric)) {
122 8
            $this->axisNumber['numeric'] = $numeric;
123 65
        } elseif (in_array($format, self::NUMERIC_FORMAT, true)) {
124 8
            $this->axisNumber['numeric'] = true;
125
        }
126
    }
127
128
    /**
129
     * Get Axis Number Format Data Type.
130
     */
131 81
    public function getAxisNumberFormat(): string
132
    {
133 81
        return $this->axisNumber['format'];
134
    }
135
136
    /**
137
     * Get Axis Number Source Linked.
138
     */
139 81
    public function getAxisNumberSourceLinked(): string
140
    {
141 81
        return (string) $this->axisNumber['source_linked'];
142
    }
143
144 45
    public function getAxisIsNumericFormat(): bool
145
    {
146 45
        return $this->axisType === self::AXIS_TYPE_DATE || (bool) $this->axisNumber['numeric'];
147
    }
148
149 68
    public function setAxisOption(string $key, null|float|int|string $value): void
150
    {
151 68
        if ($value !== null && $value !== '') {
152 67
            $this->axisOptions[$key] = (string) $value;
153
        }
154
    }
155
156
    /**
157
     * Set Axis Options Properties.
158
     */
159 4
    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
    ): 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 4
        $this->setAxisOption('dispUnitsBuiltIn', $dispUnitsBuiltIn);
195
    }
196
197
    /**
198
     * Get Axis Options Property.
199
     */
200 81
    public function getAxisOptionsProperty(string $property): ?string
201
    {
202 81
        if ($property === 'textRotation') {
203 81
            if ($this->axisText !== null) {
204 10
                if ($this->axisText->getRotation() !== null) {
205 7
                    return (string) $this->axisText->getRotation();
206
                }
207
            }
208
        }
209
210 81
        return $this->axisOptions[$property];
211
    }
212
213
    /**
214
     * Set Axis Orientation Property.
215
     */
216 1
    public function setAxisOrientation(string $orientation): void
217
    {
218 1
        $this->axisOptions['orientation'] = (string) $orientation;
219
    }
220
221 81
    public function getAxisType(): string
222
    {
223 81
        return $this->axisType;
224
    }
225
226 66
    public function setAxisType(string $type): self
227
    {
228 66
        if ($type === self::AXIS_TYPE_CATEGORY || $type === self::AXIS_TYPE_VALUE || $type === self::AXIS_TYPE_DATE) {
229 66
            $this->axisType = $type;
230
        } else {
231 3
            $this->axisType = '';
232
        }
233
234 66
        return $this;
235
    }
236
237
    /**
238
     * Set Fill Property.
239
     */
240 4
    public function setFillParameters(?string $color, ?int $alpha = null, ?string $AlphaType = ChartColor::EXCEL_COLOR_TYPE_RGB): void
241
    {
242 4
        $this->fillColor->setColorProperties($color, $alpha, $AlphaType);
243
    }
244
245
    /**
246
     * Get Fill Property.
247
     */
248 1
    public function getFillProperty(string $property): string
249
    {
250 1
        return (string) $this->fillColor->getColorProperty($property);
251
    }
252
253 81
    public function getFillColorObject(): ChartColor
254
    {
255 81
        return $this->fillColor;
256
    }
257
258
    private string $crossBetween = ''; // 'between' or 'midCat' might be better
259
260 46
    public function setCrossBetween(string $crossBetween): self
261
    {
262 46
        $this->crossBetween = $crossBetween;
263
264 46
        return $this;
265
    }
266
267 81
    public function getCrossBetween(): string
268
    {
269 81
        return $this->crossBetween;
270
    }
271
272 81
    public function getMajorGridlines(): ?GridLines
273
    {
274 81
        return $this->majorGridlines;
275
    }
276
277 81
    public function getMinorGridlines(): ?GridLines
278
    {
279 81
        return $this->minorGridlines;
280
    }
281
282 52
    public function setMajorGridlines(?GridLines $gridlines): self
283
    {
284 52
        $this->majorGridlines = $gridlines;
285
286 52
        return $this;
287
    }
288
289 11
    public function setMinorGridlines(?GridLines $gridlines): self
290
    {
291 11
        $this->minorGridlines = $gridlines;
292
293 11
        return $this;
294
    }
295
296 81
    public function getAxisText(): ?AxisText
297
    {
298 81
        return $this->axisText;
299
    }
300
301 16
    public function setAxisText(?AxisText $axisText): self
302
    {
303 16
        $this->axisText = $axisText;
304
305 16
        return $this;
306
    }
307
308 8
    public function setNoFill(bool $noFill): self
309
    {
310 8
        $this->noFill = $noFill;
311
312 8
        return $this;
313
    }
314
315 82
    public function getNoFill(): bool
316
    {
317 82
        return $this->noFill;
318
    }
319
320 3
    public function setDispUnitsTitle(?Title $dispUnitsTitle): self
321
    {
322 3
        $this->dispUnitsTitle = $dispUnitsTitle;
323
324 3
        return $this;
325
    }
326
327 3
    public function getDispUnitsTitle(): ?Title
328
    {
329 3
        return $this->dispUnitsTitle;
330
    }
331
332
    /**
333
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
334
     */
335 5
    public function __clone()
336
    {
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 5
        $this->fillColor = clone $this->fillColor;
343
    }
344
}
345