Passed
Push — develop ( 1b96c9...048947 )
by Adrien
27:52
created

BaseDrawing   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 527
Duplicated Lines 0 %

Test Coverage

Coverage 76.74%

Importance

Changes 0
Metric Value
eloc 117
dl 0
loc 527
ccs 99
cts 129
cp 0.7674
rs 8.5599
c 0
b 0
f 0
wmc 48

29 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
A getOffsetY() 0 3 1
A getImageIndex() 0 3 1
A getHeight() 0 3 1
A getCoordinates() 0 3 1
A getShadow() 0 3 1
A getWorksheet() 0 3 1
A getOffsetX() 0 3 1
A getWidth() 0 3 1
A getRotation() 0 3 1
A getName() 0 3 1
A getResizeProportional() 0 3 1
A setWorksheet() 0 29 5
A setHeight() 0 12 4
A setShadow() 0 5 1
A setOffsetY() 0 5 1
A getHashCode() 0 14 1
A setName() 0 5 1
B setWidthAndHeight() 0 18 7
A setDescription() 0 5 1
A setCoordinates() 0 5 1
A __construct() 0 18 1
A setOffsetX() 0 5 1
A setWidth() 0 12 4
A setResizeProportional() 0 5 1
A setRotation() 0 5 1
A setHyperlink() 0 3 1
A getHyperlink() 0 3 1
A __clone() 0 10 4

How to fix   Complexity   

Complex Class

Complex classes like BaseDrawing 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 BaseDrawing, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Worksheet;
4
5
use PhpOffice\PhpSpreadsheet\Cell\Hyperlink;
6
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
7
use PhpOffice\PhpSpreadsheet\IComparable;
8
9
class BaseDrawing implements IComparable
10
{
11
    /**
12
     * Image counter.
13
     *
14
     * @var int
15
     */
16
    private static $imageCounter = 0;
17
18
    /**
19
     * Image index.
20
     *
21
     * @var int
22
     */
23
    private $imageIndex = 0;
24
25
    /**
26
     * Name.
27
     *
28
     * @var string
29
     */
30
    protected $name;
31
32
    /**
33
     * Description.
34
     *
35
     * @var string
36
     */
37
    protected $description;
38
39
    /**
40
     * Worksheet.
41
     *
42
     * @var Worksheet
43
     */
44
    protected $worksheet;
45
46
    /**
47
     * Coordinates.
48
     *
49
     * @var string
50
     */
51
    protected $coordinates;
52
53
    /**
54
     * Offset X.
55
     *
56
     * @var int
57
     */
58
    protected $offsetX;
59
60
    /**
61
     * Offset Y.
62
     *
63
     * @var int
64
     */
65
    protected $offsetY;
66
67
    /**
68
     * Width.
69
     *
70
     * @var int
71
     */
72
    protected $width;
73
74
    /**
75
     * Height.
76
     *
77
     * @var int
78
     */
79
    protected $height;
80
81
    /**
82
     * Proportional resize.
83
     *
84
     * @var bool
85
     */
86
    protected $resizeProportional;
87
88
    /**
89
     * Rotation.
90
     *
91
     * @var int
92
     */
93
    protected $rotation;
94
95
    /**
96
     * Shadow.
97
     *
98
     * @var Drawing\Shadow
99
     */
100
    protected $shadow;
101
102
    /**
103
     * Image hyperlink.
104
     *
105
     * @var null|Hyperlink
106
     */
107
    private $hyperlink;
108
109
    /**
110
     * Create a new BaseDrawing.
111
     */
112 24
    public function __construct()
113
    {
114
        // Initialise values
115 24
        $this->name = '';
116 24
        $this->description = '';
117 24
        $this->worksheet = null;
118 24
        $this->coordinates = 'A1';
119 24
        $this->offsetX = 0;
120 24
        $this->offsetY = 0;
121 24
        $this->width = 0;
122 24
        $this->height = 0;
123 24
        $this->resizeProportional = true;
124 24
        $this->rotation = 0;
125 24
        $this->shadow = new Drawing\Shadow();
126
127
        // Set image index
128 24
        ++self::$imageCounter;
129 24
        $this->imageIndex = self::$imageCounter;
130 24
    }
131
132
    /**
133
     * Get image index.
134
     *
135
     * @return int
136
     */
137 13
    public function getImageIndex()
138
    {
139 13
        return $this->imageIndex;
140
    }
141
142
    /**
143
     * Get Name.
144
     *
145
     * @return string
146
     */
147 13
    public function getName()
148
    {
149 13
        return $this->name;
150
    }
151
152
    /**
153
     * Set Name.
154
     *
155
     * @param string $pValue
156
     *
157
     * @return BaseDrawing
158
     */
159 19
    public function setName($pValue)
160
    {
161 19
        $this->name = $pValue;
162
163 19
        return $this;
164
    }
165
166
    /**
167
     * Get Description.
168
     *
169
     * @return string
170
     */
171 12
    public function getDescription()
172
    {
173 12
        return $this->description;
174
    }
175
176
    /**
177
     * Set Description.
178
     *
179
     * @param string $description
180
     *
181
     * @return BaseDrawing
182
     */
183 18
    public function setDescription($description)
184
    {
185 18
        $this->description = $description;
186
187 18
        return $this;
188
    }
189
190
    /**
191
     * Get Worksheet.
192
     *
193
     * @return Worksheet
194
     */
195
    public function getWorksheet()
196
    {
197
        return $this->worksheet;
198
    }
199
200
    /**
201
     * Set Worksheet.
202
     *
203
     * @param Worksheet $pValue
204
     * @param bool $pOverrideOld If a Worksheet has already been assigned, overwrite it and remove image from old Worksheet?
205
     *
206
     * @throws PhpSpreadsheetException
207
     *
208
     * @return BaseDrawing
209
     */
210 23
    public function setWorksheet(Worksheet $pValue = null, $pOverrideOld = false)
211
    {
212 23
        if ($this->worksheet === null) {
213
            // Add drawing to \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
214 23
            $this->worksheet = $pValue;
215 23
            $this->worksheet->getCell($this->coordinates);
1 ignored issue
show
Bug introduced by
The method getCell() does not exist on null. ( Ignorable by Annotation )

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

215
            $this->worksheet->/** @scrutinizer ignore-call */ 
216
                              getCell($this->coordinates);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
216 23
            $this->worksheet->getDrawingCollection()->append($this);
217
        } else {
218
            if ($pOverrideOld) {
219
                // Remove drawing from old \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
220
                $iterator = $this->worksheet->getDrawingCollection()->getIterator();
221
222
                while ($iterator->valid()) {
223
                    if ($iterator->current()->getHashCode() == $this->getHashCode()) {
224
                        $this->worksheet->getDrawingCollection()->offsetUnset($iterator->key());
225
                        $this->worksheet = null;
226
227
                        break;
228
                    }
229
                }
230
231
                // Set new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
232
                $this->setWorksheet($pValue);
233
            } else {
234
                throw new PhpSpreadsheetException('A Worksheet has already been assigned. Drawings can only exist on one \\PhpOffice\\PhpSpreadsheet\\Worksheet.');
235
            }
236
        }
237
238 23
        return $this;
239
    }
240
241
    /**
242
     * Get Coordinates.
243
     *
244
     * @return string
245
     */
246 18
    public function getCoordinates()
247
    {
248 18
        return $this->coordinates;
249
    }
250
251
    /**
252
     * Set Coordinates.
253
     *
254
     * @param string $pValue eg: 'A1'
255
     *
256
     * @return BaseDrawing
257
     */
258 22
    public function setCoordinates($pValue)
259
    {
260 22
        $this->coordinates = $pValue;
261
262 22
        return $this;
263
    }
264
265
    /**
266
     * Get OffsetX.
267
     *
268
     * @return int
269
     */
270 18
    public function getOffsetX()
271
    {
272 18
        return $this->offsetX;
273
    }
274
275
    /**
276
     * Set OffsetX.
277
     *
278
     * @param int $pValue
279
     *
280
     * @return BaseDrawing
281
     */
282 18
    public function setOffsetX($pValue)
283
    {
284 18
        $this->offsetX = $pValue;
285
286 18
        return $this;
287
    }
288
289
    /**
290
     * Get OffsetY.
291
     *
292
     * @return int
293
     */
294 18
    public function getOffsetY()
295
    {
296 18
        return $this->offsetY;
297
    }
298
299
    /**
300
     * Set OffsetY.
301
     *
302
     * @param int $pValue
303
     *
304
     * @return BaseDrawing
305
     */
306 9
    public function setOffsetY($pValue)
307
    {
308 9
        $this->offsetY = $pValue;
309
310 9
        return $this;
311
    }
312
313
    /**
314
     * Get Width.
315
     *
316
     * @return int
317
     */
318 18
    public function getWidth()
319
    {
320 18
        return $this->width;
321
    }
322
323
    /**
324
     * Set Width.
325
     *
326
     * @param int $pValue
327
     *
328
     * @return BaseDrawing
329
     */
330 9
    public function setWidth($pValue)
331
    {
332
        // Resize proportional?
333 9
        if ($this->resizeProportional && $pValue != 0) {
334
            $ratio = $this->height / ($this->width != 0 ? $this->width : 1);
335
            $this->height = round($ratio * $pValue);
336
        }
337
338
        // Set width
339 9
        $this->width = $pValue;
340
341 9
        return $this;
342
    }
343
344
    /**
345
     * Get Height.
346
     *
347
     * @return int
348
     */
349 18
    public function getHeight()
350
    {
351 18
        return $this->height;
352
    }
353
354
    /**
355
     * Set Height.
356
     *
357
     * @param int $pValue
358
     *
359
     * @return BaseDrawing
360
     */
361 21
    public function setHeight($pValue)
362
    {
363
        // Resize proportional?
364 21
        if ($this->resizeProportional && $pValue != 0) {
365 17
            $ratio = $this->width / ($this->height != 0 ? $this->height : 1);
366 17
            $this->width = round($ratio * $pValue);
367
        }
368
369
        // Set height
370 21
        $this->height = $pValue;
371
372 21
        return $this;
373
    }
374
375
    /**
376
     * Set width and height with proportional resize.
377
     *
378
     * Example:
379
     * <code>
380
     * $objDrawing->setResizeProportional(true);
381
     * $objDrawing->setWidthAndHeight(160,120);
382
     * </code>
383
     *
384
     * @author Vincent@luo MSN:[email protected]
385
     *
386
     * @param int $width
387
     * @param int $height
388
     *
389
     * @return BaseDrawing
390
     */
391
    public function setWidthAndHeight($width, $height)
392
    {
393
        $xratio = $width / ($this->width != 0 ? $this->width : 1);
394
        $yratio = $height / ($this->height != 0 ? $this->height : 1);
395
        if ($this->resizeProportional && !($width == 0 || $height == 0)) {
396
            if (($xratio * $this->height) < $height) {
397
                $this->height = ceil($xratio * $this->height);
398
                $this->width = $width;
399
            } else {
400
                $this->width = ceil($yratio * $this->width);
401
                $this->height = $height;
402
            }
403
        } else {
404
            $this->width = $width;
405
            $this->height = $height;
406
        }
407
408
        return $this;
409
    }
410
411
    /**
412
     * Get ResizeProportional.
413
     *
414
     * @return bool
415
     */
416
    public function getResizeProportional()
417
    {
418
        return $this->resizeProportional;
419
    }
420
421
    /**
422
     * Set ResizeProportional.
423
     *
424
     * @param bool $pValue
425
     *
426
     * @return BaseDrawing
427
     */
428 9
    public function setResizeProportional($pValue)
429
    {
430 9
        $this->resizeProportional = $pValue;
431
432 9
        return $this;
433
    }
434
435
    /**
436
     * Get Rotation.
437
     *
438
     * @return int
439
     */
440 12
    public function getRotation()
441
    {
442 12
        return $this->rotation;
443
    }
444
445
    /**
446
     * Set Rotation.
447
     *
448
     * @param int $pValue
449
     *
450
     * @return BaseDrawing
451
     */
452 16
    public function setRotation($pValue)
453
    {
454 16
        $this->rotation = $pValue;
455
456 16
        return $this;
457
    }
458
459
    /**
460
     * Get Shadow.
461
     *
462
     * @return Drawing\Shadow
463
     */
464 18
    public function getShadow()
465
    {
466 18
        return $this->shadow;
467
    }
468
469
    /**
470
     * Set Shadow.
471
     *
472
     * @param Drawing\Shadow $pValue
473
     *
474
     * @return BaseDrawing
475
     */
476
    public function setShadow(Drawing\Shadow $pValue = null)
477
    {
478
        $this->shadow = $pValue;
479
480
        return $this;
481
    }
482
483
    /**
484
     * Get hash code.
485
     *
486
     * @return string Hash code
487
     */
488 12
    public function getHashCode()
489
    {
490 12
        return md5(
491 12
            $this->name .
492 12
            $this->description .
493 12
            $this->worksheet->getHashCode() .
494 12
            $this->coordinates .
495 12
            $this->offsetX .
496 12
            $this->offsetY .
497 12
            $this->width .
498 12
            $this->height .
499 12
            $this->rotation .
500 12
            $this->shadow->getHashCode() .
501 12
            __CLASS__
502
        );
503
    }
504
505
    /**
506
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
507
     */
508 1
    public function __clone()
509
    {
510 1
        $vars = get_object_vars($this);
511 1
        foreach ($vars as $key => $value) {
512 1
            if ($key == 'worksheet') {
513 1
                $this->worksheet = null;
514 1
            } elseif (is_object($value)) {
515 1
                $this->$key = clone $value;
516
            } else {
517 1
                $this->$key = $value;
518
            }
519
        }
520 1
    }
521
522
    /**
523
     * @param null|Hyperlink $pHyperlink
524
     */
525 2
    public function setHyperlink(Hyperlink $pHyperlink = null)
526
    {
527 2
        $this->hyperlink = $pHyperlink;
528 2
    }
529
530
    /**
531
     * @return null|Hyperlink
532
     */
533 12
    public function getHyperlink()
534
    {
535 12
        return $this->hyperlink;
536
    }
537
}
538