Completed
Push — develop ( 86946f...95aba9 )
by Franck
14:04 queued 05:55
created

Slide   C

Complexity

Total Complexity 41

Size/Duplication

Total Lines 538
Duplicated Lines 0 %

Coupling/Cohesion

Components 7
Dependencies 10

Test Coverage

Coverage 97.56%

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 41
c 6
b 0
f 2
lcom 7
cbo 10
dl 0
loc 538
ccs 120
cts 123
cp 0.9756
rs 6.987

35 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A getShapeCollection() 0 4 1
A addShape() 0 6 1
A createRichTextShape() 0 7 1
A createLineShape() 0 7 1
A createChartShape() 0 7 1
A createDrawingShape() 0 7 1
A createTableShape() 0 7 1
A createGroup() 0 7 1
A getParent() 0 4 1
A rebindParent() 0 7 1
A getSlideLayout() 0 4 1
A setSlideLayout() 0 6 1
A getSlideMasterId() 0 4 1
A setSlideMasterId() 0 6 1
A getHashCode() 0 4 1
A getHashIndex() 0 4 1
A setHashIndex() 0 4 1
A copy() 0 6 1
A getOffsetX() 0 9 2
A getOffsetY() 0 9 2
A getExtentX() 0 9 2
A getExtentY() 0 9 2
A getNote() 0 7 2
A setNote() 0 7 2
A getTransition() 0 4 1
A setTransition() 0 6 1
A getName() 0 4 1
A setName() 0 5 1
A getBackground() 0 4 1
A setBackground() 0 5 1
A isVisible() 0 4 1
A setIsVisible() 0 5 1
A addAnimation() 0 5 1
A getAnimations() 0 4 1

How to fix   Complexity   

Complex Class

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

1
<?php
2
/**
3
 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4
 * presentations documents.
5
 *
6
 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7
 * General Public License version 3 as published by the Free Software Foundation.
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code. For the full list of
11
 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12
 *
13
 * @link        https://github.com/PHPOffice/PHPPresentation
14
 * @copyright   2009-2015 PHPPresentation contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpPresentation;
19
20
use PhpOffice\PhpPresentation\GeometryCalculator;
21
use PhpOffice\PhpPresentation\Shape\Chart;
22
use PhpOffice\PhpPresentation\Shape\Drawing;
23
use PhpOffice\PhpPresentation\Shape\Group;
24
use PhpOffice\PhpPresentation\Shape\Line;
25
use PhpOffice\PhpPresentation\Shape\RichText;
26
use PhpOffice\PhpPresentation\Shape\Table;
27
use PhpOffice\PhpPresentation\Slide\AbstractBackground;
28
use PhpOffice\PhpPresentation\Slide\Layout;
29
use PhpOffice\PhpPresentation\Slide\Note;
30
use PhpOffice\PhpPresentation\Slide\Transition;
31
32
/**
33
 * Slide class
34
 */
35
class Slide implements ComparableInterface, ShapeContainerInterface
36
{
37
    /**
38
     * The slide is shown in presentation
39
     * @var bool
40
     */
41
    protected $isVisible = true;
42
43
    /**
44
     * Parent presentation
45
     *
46
     * @var PhpPresentation
47
     */
48
    private $parent;
49
50
    /**
51
     * Collection of shapes
52
     *
53
     * @var \ArrayObject|\PhpOffice\PhpPresentation\AbstractShape[]
54
     */
55
    private $shapeCollection = null;
56
57
    /**
58
     * Slide identifier
59
     *
60
     * @var string
61
     */
62
    private $identifier;
63
64
    /**
65
     * Slide layout
66
     *
67
     * @var string
68
     */
69
    private $slideLayout;
70
71
    /**
72
     * Slide master id
73
     *
74
     * @var integer
75
     */
76
    private $slideMasterId = 1;
77
78
    /**
79
     *
80
     * @var \PhpOffice\PhpPresentation\Slide\Note
81
     */
82
    private $slideNote;
83
84
    /**
85
     *
86
     * @var \PhpOffice\PhpPresentation\Slide\Transition
87
     */
88
    private $slideTransition;
89
  
90
    /**
91
     *
92
     * @var \PhpOffice\PhpPresentation\Slide\Animation
93
     */
94
    private $animations = array();
95
    
96
    /**
97
     * Hash index
98
     *
99
     * @var string
100
     */
101
    private $hashIndex;
102
103
    /**
104
     * Offset X
105
     *
106
     * @var int
107
     */
108
    protected $offsetX;
109
110
    /**
111
     * Offset Y
112
     *
113
     * @var int
114
     */
115
    protected $offsetY;
116
117
    /**
118
     * Extent X
119
     *
120
     * @var int
121
     */
122
    protected $extentX;
123
124
    /**
125
     * Extent Y
126
     *
127
     * @var int
128
     */
129
    protected $extentY;
130
131
    /**
132
     * Name of the title
133
     *
134
     * @var string
135
     */
136
    protected $name;
137
138
    /**
139
     * Background of the slide
140
     *
141
     * @var AbstractBackground
142
     */
143
    protected $background;
144
145
    /**
146
     * Create a new slide
147
     *
148
     * @param PhpPresentation $pParent
149
     */
150 204
    public function __construct(PhpPresentation $pParent = null)
151
    {
152
        // Set parent
153 204
        $this->parent = $pParent;
154
155 204
        $this->slideLayout = Slide\Layout::BLANK;
156
157
        // Shape collection
158 204
        $this->shapeCollection = new \ArrayObject();
159
160
        // Set identifier
161 204
        $this->identifier = md5(rand(0, 9999) . time());
162 204
    }
163
164
    /**
165
     * Get collection of shapes
166
     *
167
     * @return \ArrayObject|\PhpOffice\PhpPresentation\AbstractShape[]
168
     */
169 173
    public function getShapeCollection()
170
    {
171 173
        return $this->shapeCollection;
172
    }
173
174
    /**
175
     * Add shape to slide
176
     *
177
     * @param  \PhpOffice\PhpPresentation\AbstractShape $shape
178
     * @return \PhpOffice\PhpPresentation\AbstractShape
179
     */
180 139
    public function addShape(AbstractShape $shape)
181
    {
182 139
        $shape->setContainer($this);
183
184 139
        return $shape;
185
    }
186
187
    /**
188
     * Create rich text shape
189
     *
190
     * @return \PhpOffice\PhpPresentation\Shape\RichText
191
     */
192 41
    public function createRichTextShape()
193
    {
194 41
        $shape = new RichText();
195 41
        $this->addShape($shape);
196
197 41
        return $shape;
198
    }
199
200
    /**
201
     * Create line shape
202
     *
203
     * @param  int                      $fromX Starting point x offset
204
     * @param  int                      $fromY Starting point y offset
205
     * @param  int                      $toX   Ending point x offset
206
     * @param  int                      $toY   Ending point y offset
207
     * @return \PhpOffice\PhpPresentation\Shape\Line
208
     */
209 2
    public function createLineShape($fromX, $fromY, $toX, $toY)
210
    {
211 2
        $shape = new Line($fromX, $fromY, $toX, $toY);
212 2
        $this->addShape($shape);
213
214 2
        return $shape;
215
    }
216
217
    /**
218
     * Create chart shape
219
     *
220
     * @return \PhpOffice\PhpPresentation\Shape\Chart
221
     */
222 46
    public function createChartShape()
223
    {
224 46
        $shape = new Chart();
225 46
        $this->addShape($shape);
226
227 46
        return $shape;
228
    }
229
230
    /**
231
     * Create drawing shape
232
     *
233
     * @return \PhpOffice\PhpPresentation\Shape\Drawing\File
234
     */
235 13
    public function createDrawingShape()
236
    {
237 13
        $shape = new Drawing\File();
238 13
        $this->addShape($shape);
239
240 13
        return $shape;
241
    }
242
243
    /**
244
     * Create table shape
245
     *
246
     * @param  int                       $columns Number of columns
247
     * @return \PhpOffice\PhpPresentation\Shape\Table
248
     */
249 16
    public function createTableShape($columns = 1)
250
    {
251 16
        $shape = new Table($columns);
252 16
        $this->addShape($shape);
253
254 16
        return $shape;
255
    }
256
    
257
    /**
258
     * Creates a group within this slide
259
     *
260
     * @return \PhpOffice\PhpPresentation\Shape\Group
261
     */
262 2
    public function createGroup()
263
    {
264 2
        $shape = new Group();
265 2
        $this->addShape($shape);
266
        
267 2
        return $shape;
268
    }
269
270
    /**
271
     * Get parent
272
     *
273
     * @return PhpPresentation
274
     */
275 95
    public function getParent()
276
    {
277 95
        return $this->parent;
278
    }
279
280
    /**
281
     * Re-bind parent
282
     *
283
     * @param  \PhpOffice\PhpPresentation\PhpPresentation       $parent
284
     * @return \PhpOffice\PhpPresentation\Slide
285
     */
286 2
    public function rebindParent(PhpPresentation $parent)
287
    {
288 2
        $this->parent->removeSlideByIndex($this->parent->getIndex($this));
289 2
        $this->parent = $parent;
290
291 2
        return $this;
292
    }
293
294
    /**
295
     * Get slide layout
296
     *
297
     * @return string
298
     */
299 95
    public function getSlideLayout()
300
    {
301 95
        return $this->slideLayout;
302
    }
303
304
    /**
305
     * Set slide layout
306
     *
307
     * @param  string              $layout
308
     * @return \PhpOffice\PhpPresentation\Slide
309
     */
310 4
    public function setSlideLayout($layout = Layout::BLANK)
311
    {
312 4
        $this->slideLayout = $layout;
313
314 4
        return $this;
315
    }
316
317
    /**
318
     * Get slide master id
319
     *
320
     * @return int
321
     */
322 98
    public function getSlideMasterId()
323
    {
324 98
        return $this->slideMasterId;
325
    }
326
327
    /**
328
     * Set slide master id
329
     *
330
     * @param  int                 $masterId
331
     * @return \PhpOffice\PhpPresentation\Slide
332
     */
333 1
    public function setSlideMasterId($masterId = 1)
334
    {
335 1
        $this->slideMasterId = $masterId;
336
337 1
        return $this;
338
    }
339
340
    /**
341
     * Get hash code
342
     *
343
     * @return string Hash code
344
     */
345 134
    public function getHashCode()
346
    {
347 134
        return md5($this->identifier . __CLASS__);
348
    }
349
350
    /**
351
     * Get hash index
352
     *
353
     * Note that this index may vary during script execution! Only reliable moment is
354
     * while doing a write of a workbook and when changes are not allowed.
355
     *
356
     * @return string Hash index
357
     */
358 4
    public function getHashIndex()
359
    {
360 4
        return $this->hashIndex;
361
    }
362
363
    /**
364
     * Set hash index
365
     *
366
     * Note that this index may vary during script execution! Only reliable moment is
367
     * while doing a write of a workbook and when changes are not allowed.
368
     *
369
     * @param string $value Hash index
370
     */
371 4
    public function setHashIndex($value)
372
    {
373 4
        $this->hashIndex = $value;
374 4
    }
375
376
    /**
377
     * Copy slide (!= clone!)
378
     *
379
     * @return \PhpOffice\PhpPresentation\Slide
380
     */
381 1
    public function copy()
382
    {
383 1
        $copied = clone $this;
384
385 1
        return $copied;
386
    }
387
388
    /**
389
     * Get X Offset
390
     *
391
     * @return int
392
     */
393 95
    public function getOffsetX()
394
    {
395 95
        if ($this->offsetX === null) {
396 95
            $offsets = GeometryCalculator::calculateOffsets($this);
397 95
            $this->offsetX = $offsets[GeometryCalculator::X];
398 95
            $this->offsetY = $offsets[GeometryCalculator::Y];
399 95
        }
400 95
        return $this->offsetX;
401
    }
402
403
    /**
404
     * Get Y Offset
405
     *
406
     * @return int
407
     */
408 95
    public function getOffsetY()
409
    {
410 95
        if ($this->offsetY === null) {
411 1
            $offsets = GeometryCalculator::calculateOffsets($this);
412 1
            $this->offsetX = $offsets[GeometryCalculator::X];
413 1
            $this->offsetY = $offsets[GeometryCalculator::Y];
414 1
        }
415 95
        return $this->offsetY;
416
    }
417
418
    /**
419
     * Get X Extent
420
     *
421
     * @return int
422
     */
423 95
    public function getExtentX()
424
    {
425 95
        if ($this->extentX === null) {
426 95
            $extents = GeometryCalculator::calculateExtents($this);
427 95
            $this->extentX = $extents[GeometryCalculator::X];
428 95
            $this->extentY = $extents[GeometryCalculator::Y];
429 95
        }
430 95
        return $this->extentX;
431
    }
432
433
    /**
434
     * Get Y Extent
435
     *
436
     * @return int
437
     */
438 95
    public function getExtentY()
439
    {
440 95
        if ($this->extentY === null) {
441 1
            $extents = GeometryCalculator::calculateExtents($this);
442 1
            $this->extentX = $extents[GeometryCalculator::X];
443 1
            $this->extentY = $extents[GeometryCalculator::Y];
444 1
        }
445 95
        return $this->extentY;
446
    }
447
448
    /**
449
     *
450
     * @return \PhpOffice\PhpPresentation\Slide\Note
451
     */
452 156
    public function getNote()
453
    {
454 156
        if (is_null($this->slideNote)) {
455 156
            $this->setNote();
456 156
        }
457 156
        return $this->slideNote;
458
    }
459
460
    /**
461
     *
462
     * @param \PhpOffice\PhpPresentation\Slide\Note $note
463
     * @return \PhpOffice\PhpPresentation\Slide
464
     */
465 157
    public function setNote(Note $note = null)
466
    {
467 157
        $this->slideNote = (is_null($note) ? new Note() : $note);
468 157
        $this->slideNote->setParent($this);
469
470 157
        return $this;
471
    }
472
473
    /**
474
     *
475
     * @return \PhpOffice\PhpPresentation\Slide\Transition
476
     */
477 154
    public function getTransition()
478
    {
479 154
        return $this->slideTransition;
480
    }
481
482
    /**
483
     *
484
     * @param \PhpOffice\PhpPresentation\Slide\Transition $transition
485
     * @return \PhpOffice\PhpPresentation\Slide
486
     */
487 3
    public function setTransition(Transition $transition = null)
488
    {
489 3
        $this->slideTransition = $transition;
490
491 3
        return $this;
492
    }
493
494
    /**
495
     * Get the name of the slide
496
     * @return string
497
     */
498 61
    public function getName()
499
    {
500 61
        return $this->name;
501
    }
502
503
    /**
504
     * Set the name of the slide
505
     * @param string $name
506
     * @return $this
507
     */
508 5
    public function setName($name = null)
509
    {
510 5
        $this->name = $name;
511 5
        return $this;
512
    }
513
514
    /**
515
     * @return AbstractBackground
516
     */
517 154
    public function getBackground()
518
    {
519 154
        return $this->background;
520
    }
521
522
    /**
523
     * @param AbstractBackground $background
524
     * @return $this
525
     */
526 5
    public function setBackground(AbstractBackground $background = null)
527
    {
528 5
        $this->background = $background;
529 5
        return $this;
530
    }
531
532
    /**
533
     * @return boolean
534
     */
535 154
    public function isVisible()
536
    {
537 154
        return $this->isVisible;
538
    }
539
540
    /**
541
     * @param boolean $value
542
     * @return Slide
543
     */
544 3
    public function setIsVisible($value = true)
545
    {
546 3
        $this->isVisible = (bool)$value;
547 3
        return $this;
548
    }
549
550
    /**
551
     * Add an animation to the slide
552
     *
553
     * @param  \PhpOffice\PhpPresentation\Slide\Animation
554
     * @return \PhpOffice\PhpPresentation\Slide\Animation
555
     */
556
    public function addAnimation($animation)
557
    {
558
        $this->animations[] = $animation;
559
        return $animation;
560
    }
561
    
562
    /**
563
     * Get collection of animations
564
     *
565
     * @return \PhpOffice\PhpPresentation\Slide\Animation
566
     */
567
    
568 94
    public function getAnimations()
569
    {
570 94
        return $this->animations;
571
    }
572
}
573