Completed
Push — develop ( 29208e...50a0ec )
by Adrien
05:34
created

Alignment   C

Complexity

Total Complexity 59

Size/Duplication

Total Lines 459
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 70.99%

Importance

Changes 0
Metric Value
dl 0
loc 459
rs 6.1904
c 0
b 0
f 0
ccs 93
cts 131
cp 0.7099
wmc 59
lcom 1
cbo 4

19 Methods

Rating   Name   Duplication   Size   Complexity  
A getSharedComponent() 0 4 1
A getStyleArray() 0 4 1
A getHorizontal() 0 8 2
A setHorizontal() 0 15 3
A getVertical() 0 8 2
A getTextRotation() 0 8 2
B setTextRotation() 0 21 6
A setWrapText() 0 14 3
A setShrinkToFit() 0 14 3
A getIndent() 0 8 2
B setIndent() 0 18 6
A setReadOrder() 0 14 4
A getHashCode() 0 17 4
A __construct() 0 11 2
D applyFromArray() 0 31 9
A setVertical() 0 15 3
A getWrapText() 0 8 2
A getShrinkToFit() 0 8 2
A getReadOrder() 0 8 2

How to fix   Complexity   

Complex Class

Complex classes like Alignment 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 Alignment, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style;
4
5
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
6
use PhpOffice\PhpSpreadsheet\IComparable;
7
8
class Alignment extends Supervisor implements IComparable
9
{
10
    /* Horizontal alignment styles */
11
    const HORIZONTAL_GENERAL = 'general';
12
    const HORIZONTAL_LEFT = 'left';
13
    const HORIZONTAL_RIGHT = 'right';
14
    const HORIZONTAL_CENTER = 'center';
15
    const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous';
16
    const HORIZONTAL_JUSTIFY = 'justify';
17
    const HORIZONTAL_FILL = 'fill';
18
    const HORIZONTAL_DISTRIBUTED = 'distributed'; // Excel2007 only
19
20
    /* Vertical alignment styles */
21
    const VERTICAL_BOTTOM = 'bottom';
22
    const VERTICAL_TOP = 'top';
23
    const VERTICAL_CENTER = 'center';
24
    const VERTICAL_JUSTIFY = 'justify';
25
    const VERTICAL_DISTRIBUTED = 'distributed'; // Excel2007 only
26
27
    /* Read order */
28
    const READORDER_CONTEXT = 0;
29
    const READORDER_LTR = 1;
30
    const READORDER_RTL = 2;
31
32
    /**
33
     * Horizontal alignment.
34
     *
35
     * @var string
36
     */
37
    protected $horizontal = self::HORIZONTAL_GENERAL;
38
39
    /**
40
     * Vertical alignment.
41
     *
42
     * @var string
43
     */
44
    protected $vertical = self::VERTICAL_BOTTOM;
45
46
    /**
47
     * Text rotation.
48
     *
49
     * @var int
50
     */
51
    protected $textRotation = 0;
52
53
    /**
54
     * Wrap text.
55
     *
56
     * @var bool
57
     */
58
    protected $wrapText = false;
59
60
    /**
61
     * Shrink to fit.
62
     *
63
     * @var bool
64
     */
65
    protected $shrinkToFit = false;
66
67
    /**
68
     * Indent - only possible with horizontal alignment left and right.
69
     *
70
     * @var int
71
     */
72
    protected $indent = 0;
73
74
    /**
75
     * Read order.
76
     *
77
     * @var int
78
     */
79
    protected $readOrder = 0;
80
81
    /**
82
     * Create a new Alignment.
83
     *
84
     * @param bool $isSupervisor Flag indicating if this is a supervisor or not
85
     *                                       Leave this value at default unless you understand exactly what
86
     *                                          its ramifications are
87
     * @param bool $isConditional Flag indicating if this is a conditional style or not
88
     *                                       Leave this value at default unless you understand exactly what
89
     *                                          its ramifications are
90
     */
91 28
    public function __construct($isSupervisor = false, $isConditional = false)
92
    {
93
        // Supervisor?
94 28
        parent::__construct($isSupervisor);
95
96 28
        if ($isConditional) {
97
            $this->horizontal = null;
98
            $this->vertical = null;
99
            $this->textRotation = null;
100
        }
101 28
    }
102
103
    /**
104
     * Get the shared style component for the currently active cell in currently active sheet.
105
     * Only used for style supervisor.
106
     *
107
     * @return Alignment
108
     */
109
    public function getSharedComponent()
110
    {
111
        return $this->parent->getSharedComponent()->getAlignment();
112
    }
113
114
    /**
115
     * Build style array from subcomponents.
116
     *
117
     * @param array $array
118
     *
119
     * @return array
120
     */
121 1
    public function getStyleArray($array)
122
    {
123 1
        return ['alignment' => $array];
124
    }
125
126
    /**
127
     * Apply styles from array.
128
     * <code>
129
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
130
     *        array(
131
     *            'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
132
     *            'vertical'   => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
133
     *            'textRotation'   => 0,
134
     *            'wrapText'            => TRUE
135
     *        )
136
     * );
137
     * </code>.
138
     *
139
     * @param array $pStyles Array containing style information
140
     *
141
     * @throws PhpSpreadsheetException
142
     *
143
     * @return Alignment
144
     */
145 2
    public function applyFromArray(array $pStyles)
146
    {
147 2
        if ($this->isSupervisor) {
148
            $this->getActiveSheet()->getStyle($this->getSelectedCells())
149
                ->applyFromArray($this->getStyleArray($pStyles));
150
        } else {
151 2
            if (isset($pStyles['horizontal'])) {
152 2
                $this->setHorizontal($pStyles['horizontal']);
153
            }
154 2
            if (isset($pStyles['vertical'])) {
155 2
                $this->setVertical($pStyles['vertical']);
156
            }
157 2
            if (isset($pStyles['textRotation'])) {
158
                $this->setTextRotation($pStyles['textRotation']);
159
            }
160 2
            if (isset($pStyles['wrapText'])) {
161 2
                $this->setWrapText($pStyles['wrapText']);
162
            }
163 2
            if (isset($pStyles['shrinkToFit'])) {
164 1
                $this->setShrinkToFit($pStyles['shrinkToFit']);
165
            }
166 2
            if (isset($pStyles['indent'])) {
167
                $this->setIndent($pStyles['indent']);
168
            }
169 2
            if (isset($pStyles['readOrder'])) {
170
                $this->setReadOrder($pStyles['readOrder']);
171
            }
172
        }
173
174 2
        return $this;
175
    }
176
177
    /**
178
     * Get Horizontal.
179
     *
180
     * @return string
181
     */
182 2
    public function getHorizontal()
183
    {
184 2
        if ($this->isSupervisor) {
185
            return $this->getSharedComponent()->getHorizontal();
186
        }
187
188 2
        return $this->horizontal;
189
    }
190
191
    /**
192
     * Set Horizontal.
193
     *
194
     * @param string $pValue see self::HORIZONTAL_*
195
     *
196
     * @return Alignment
197
     */
198 3
    public function setHorizontal($pValue)
199
    {
200 3
        if ($pValue == '') {
201 1
            $pValue = self::HORIZONTAL_GENERAL;
202
        }
203
204 3
        if ($this->isSupervisor) {
205 1
            $styleArray = $this->getStyleArray(['horizontal' => $pValue]);
206 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
207
        } else {
208 3
            $this->horizontal = $pValue;
209
        }
210
211 3
        return $this;
212
    }
213
214
    /**
215
     * Get Vertical.
216
     *
217
     * @return string
218
     */
219 1
    public function getVertical()
220
    {
221 1
        if ($this->isSupervisor) {
222
            return $this->getSharedComponent()->getVertical();
223
        }
224
225 1
        return $this->vertical;
226
    }
227
228
    /**
229
     * Set Vertical.
230
     *
231
     * @param string $pValue see self::VERTICAL_*
232
     *
233
     * @return Alignment
234
     */
235 3
    public function setVertical($pValue)
236
    {
237 3
        if ($pValue == '') {
238
            $pValue = self::VERTICAL_BOTTOM;
239
        }
240
241 3
        if ($this->isSupervisor) {
242 1
            $styleArray = $this->getStyleArray(['vertical' => $pValue]);
243 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
244
        } else {
245 3
            $this->vertical = $pValue;
246
        }
247
248 3
        return $this;
249
    }
250
251
    /**
252
     * Get TextRotation.
253
     *
254
     * @return int
255
     */
256 1
    public function getTextRotation()
257
    {
258 1
        if ($this->isSupervisor) {
259
            return $this->getSharedComponent()->getTextRotation();
260
        }
261
262 1
        return $this->textRotation;
263
    }
264
265
    /**
266
     * Set TextRotation.
267
     *
268
     * @param int $pValue
269
     *
270
     * @throws PhpSpreadsheetException
271
     *
272
     * @return Alignment
273
     */
274 1
    public function setTextRotation($pValue)
275
    {
276
        // Excel2007 value 255 => PhpSpreadsheet value -165
277 1
        if ($pValue == 255) {
278
            $pValue = -165;
279
        }
280
281
        // Set rotation
282 1
        if (($pValue >= -90 && $pValue <= 90) || $pValue == -165) {
283 1
            if ($this->isSupervisor) {
284
                $styleArray = $this->getStyleArray(['textRotation' => $pValue]);
285
                $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
286
            } else {
287 1
                $this->textRotation = $pValue;
288
            }
289
        } else {
290
            throw new PhpSpreadsheetException('Text rotation should be a value between -90 and 90.');
291
        }
292
293 1
        return $this;
294
    }
295
296
    /**
297
     * Get Wrap Text.
298
     *
299
     * @return bool
300
     */
301
    public function getWrapText()
302
    {
303
        if ($this->isSupervisor) {
304
            return $this->getSharedComponent()->getWrapText();
305
        }
306
307
        return $this->wrapText;
308
    }
309
310
    /**
311
     * Set Wrap Text.
312
     *
313
     * @param bool $pValue
314
     *
315
     * @return Alignment
316
     */
317 3
    public function setWrapText($pValue)
318
    {
319 3
        if ($pValue == '') {
320 1
            $pValue = false;
321
        }
322 3
        if ($this->isSupervisor) {
323 1
            $styleArray = $this->getStyleArray(['wrapText' => $pValue]);
324 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
325
        } else {
326 3
            $this->wrapText = $pValue;
327
        }
328
329 3
        return $this;
330
    }
331
332
    /**
333
     * Get Shrink to fit.
334
     *
335
     * @return bool
336
     */
337
    public function getShrinkToFit()
338
    {
339
        if ($this->isSupervisor) {
340
            return $this->getSharedComponent()->getShrinkToFit();
341
        }
342
343
        return $this->shrinkToFit;
344
    }
345
346
    /**
347
     * Set Shrink to fit.
348
     *
349
     * @param bool $pValue
350
     *
351
     * @return Alignment
352
     */
353 2
    public function setShrinkToFit($pValue)
354
    {
355 2
        if ($pValue == '') {
356 1
            $pValue = false;
357
        }
358 2
        if ($this->isSupervisor) {
359 1
            $styleArray = $this->getStyleArray(['shrinkToFit' => $pValue]);
360 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
361
        } else {
362 2
            $this->shrinkToFit = $pValue;
363
        }
364
365 2
        return $this;
366
    }
367
368
    /**
369
     * Get indent.
370
     *
371
     * @return int
372
     */
373 1
    public function getIndent()
374
    {
375 1
        if ($this->isSupervisor) {
376
            return $this->getSharedComponent()->getIndent();
377
        }
378
379 1
        return $this->indent;
380
    }
381
382
    /**
383
     * Set indent.
384
     *
385
     * @param int $pValue
386
     *
387
     * @return Alignment
388
     */
389 1
    public function setIndent($pValue)
390
    {
391 1
        if ($pValue > 0) {
392 1
            if ($this->getHorizontal() != self::HORIZONTAL_GENERAL &&
393 1
                $this->getHorizontal() != self::HORIZONTAL_LEFT &&
394 1
                $this->getHorizontal() != self::HORIZONTAL_RIGHT) {
395
                $pValue = 0; // indent not supported
396
            }
397
        }
398 1
        if ($this->isSupervisor) {
399
            $styleArray = $this->getStyleArray(['indent' => $pValue]);
400
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
401
        } else {
402 1
            $this->indent = $pValue;
403
        }
404
405 1
        return $this;
406
    }
407
408
    /**
409
     * Get read order.
410
     *
411
     * @return int
412
     */
413
    public function getReadOrder()
414
    {
415
        if ($this->isSupervisor) {
416
            return $this->getSharedComponent()->getReadOrder();
417
        }
418
419
        return $this->readOrder;
420
    }
421
422
    /**
423
     * Set read order.
424
     *
425
     * @param int $pValue
426
     *
427
     * @return Alignment
428
     */
429 1
    public function setReadOrder($pValue)
430
    {
431 1
        if ($pValue < 0 || $pValue > 2) {
432
            $pValue = 0;
433
        }
434 1
        if ($this->isSupervisor) {
435
            $styleArray = $this->getStyleArray(['readOrder' => $pValue]);
436
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
437
        } else {
438 1
            $this->readOrder = $pValue;
439
        }
440
441 1
        return $this;
442
    }
443
444
    /**
445
     * Get hash code.
446
     *
447
     * @return string Hash code
448
     */
449 10
    public function getHashCode()
450
    {
451 10
        if ($this->isSupervisor) {
452
            return $this->getSharedComponent()->getHashCode();
453
        }
454
455 10
        return md5(
456 10
            $this->horizontal .
457 10
            $this->vertical .
458 10
            $this->textRotation .
459 10
            ($this->wrapText ? 't' : 'f') .
460 10
            ($this->shrinkToFit ? 't' : 'f') .
461 10
            $this->indent .
462 10
            $this->readOrder .
463 10
            __CLASS__
464
        );
465
    }
466
}
467