Complex classes like AbstractShape 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 AbstractShape, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | abstract class AbstractShape implements ComparableInterface |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Container |
||
| 32 | * |
||
| 33 | * @var \PhpOffice\PhpPresentation\ShapeContainerInterface |
||
| 34 | */ |
||
| 35 | protected $container; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Offset X |
||
| 39 | * |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | protected $offsetX; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Offset Y |
||
| 46 | * |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | protected $offsetY; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Width |
||
| 53 | * |
||
| 54 | * @var int |
||
| 55 | */ |
||
| 56 | protected $width; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Height |
||
| 60 | * |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | protected $height; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Fill |
||
| 67 | * |
||
| 68 | * @var \PhpOffice\PhpPresentation\Style\Fill |
||
| 69 | */ |
||
| 70 | private $fill; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Border |
||
| 74 | * |
||
| 75 | * @var \PhpOffice\PhpPresentation\Style\Border |
||
| 76 | */ |
||
| 77 | private $border; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Rotation |
||
| 81 | * |
||
| 82 | * @var int |
||
| 83 | */ |
||
| 84 | protected $rotation; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Shadow |
||
| 88 | * |
||
| 89 | * @var \PhpOffice\PhpPresentation\Style\Shadow |
||
| 90 | */ |
||
| 91 | protected $shadow; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Hyperlink |
||
| 95 | * |
||
| 96 | * @var \PhpOffice\PhpPresentation\Shape\Hyperlink |
||
| 97 | */ |
||
| 98 | protected $hyperlink; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * PlaceHolder |
||
| 102 | * @var \PhpOffice\PhpPresentation\Shape\Placeholder |
||
| 103 | */ |
||
| 104 | protected $placeholder; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Hash index |
||
| 108 | * |
||
| 109 | * @var string |
||
| 110 | */ |
||
| 111 | private $hashIndex; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Create a new self |
||
| 115 | */ |
||
| 116 | 220 | public function __construct() |
|
| 117 | { |
||
| 118 | // Initialise values |
||
| 119 | 220 | $this->container = null; |
|
| 120 | 220 | $this->offsetX = 0; |
|
| 121 | 220 | $this->offsetY = 0; |
|
| 122 | 220 | $this->width = 0; |
|
| 123 | 220 | $this->height = 0; |
|
| 124 | 220 | $this->rotation = 0; |
|
| 125 | 220 | $this->fill = new Style\Fill(); |
|
| 126 | 220 | $this->border = new Style\Border(); |
|
| 127 | 220 | $this->shadow = new Style\Shadow(); |
|
| 128 | |||
| 129 | 220 | $this->border->setLineStyle(Style\Border::LINE_NONE); |
|
| 130 | 220 | } |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Magic Method : clone |
||
| 134 | */ |
||
| 135 | 1 | public function __clone() |
|
| 136 | { |
||
| 137 | 1 | $this->container = null; |
|
| 138 | 1 | $this->fill = clone $this->fill; |
|
| 139 | 1 | $this->border = clone $this->border; |
|
| 140 | 1 | $this->shadow = clone $this->shadow; |
|
| 141 | 1 | } |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Get Container, Slide or Group |
||
| 145 | * |
||
| 146 | * @return \PhpOffice\PhpPresentation\Container |
||
| 147 | */ |
||
| 148 | 2 | public function getContainer() |
|
| 149 | { |
||
| 150 | 2 | return $this->container; |
|
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Set Container, Slide or Group |
||
| 155 | * |
||
| 156 | * @param \PhpOffice\PhpPresentation\ShapeContainerInterface $pValue |
||
| 157 | * @param bool $pOverrideOld If a Slide has already been assigned, overwrite it and remove image from old Slide? |
||
| 158 | * @throws \Exception |
||
| 159 | * @return self |
||
| 160 | */ |
||
| 161 | 157 | public function setContainer(ShapeContainerInterface $pValue = null, $pOverrideOld = false) |
|
| 162 | { |
||
| 163 | 157 | if (is_null($this->container)) { |
|
| 164 | // Add drawing to \PhpOffice\PhpPresentation\ShapeContainerInterface |
||
| 165 | 157 | $this->container = $pValue; |
|
| 166 | 157 | if (!is_null($this->container)) { |
|
| 167 | 157 | $this->container->getShapeCollection()->append($this); |
|
| 168 | } |
||
| 169 | } else { |
||
| 170 | 2 | if ($pOverrideOld) { |
|
| 171 | // Remove drawing from old \PhpOffice\PhpPresentation\ShapeContainerInterface |
||
| 172 | 1 | $iterator = $this->container->getShapeCollection()->getIterator(); |
|
| 173 | |||
| 174 | 1 | while ($iterator->valid()) { |
|
| 175 | 1 | if ($iterator->current()->getHashCode() == $this->getHashCode()) { |
|
| 176 | 1 | $this->container->getShapeCollection()->offsetUnset($iterator->key()); |
|
| 177 | 1 | $this->container = null; |
|
| 178 | 1 | break; |
|
| 179 | } |
||
| 180 | 1 | $iterator->next(); |
|
| 181 | } |
||
| 182 | |||
| 183 | // Set new \PhpOffice\PhpPresentation\Slide |
||
| 184 | 1 | $this->setContainer($pValue); |
|
| 185 | } else { |
||
| 186 | 1 | throw new \Exception("A \PhpOffice\PhpPresentation\ShapeContainerInterface has already been assigned. Shapes can only exist on one \PhpOffice\PhpPresentation\ShapeContainerInterface."); |
|
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | 157 | return $this; |
|
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get OffsetX |
||
| 195 | * |
||
| 196 | * @return int |
||
| 197 | */ |
||
| 198 | 139 | public function getOffsetX() |
|
| 199 | { |
||
| 200 | 139 | return $this->offsetX; |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Set OffsetX |
||
| 205 | * |
||
| 206 | * @param int $pValue |
||
| 207 | * @return self |
||
| 208 | */ |
||
| 209 | 66 | public function setOffsetX($pValue = 0) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Get OffsetY |
||
| 218 | * |
||
| 219 | * @return int |
||
| 220 | */ |
||
| 221 | 139 | public function getOffsetY() |
|
| 222 | { |
||
| 223 | 139 | return $this->offsetY; |
|
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Set OffsetY |
||
| 228 | * |
||
| 229 | * @param int $pValue |
||
| 230 | * @return self |
||
| 231 | */ |
||
| 232 | 66 | public function setOffsetY($pValue = 0) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Get Width |
||
| 241 | * |
||
| 242 | * @return int |
||
| 243 | */ |
||
| 244 | 133 | public function getWidth() |
|
| 245 | { |
||
| 246 | 133 | return $this->width; |
|
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set Width |
||
| 251 | * |
||
| 252 | * @param int $pValue |
||
| 253 | * @return self |
||
| 254 | */ |
||
| 255 | 31 | public function setWidth($pValue = 0) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Get Height |
||
| 263 | * |
||
| 264 | * @return int |
||
| 265 | */ |
||
| 266 | 133 | public function getHeight() |
|
| 267 | { |
||
| 268 | 133 | return $this->height; |
|
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Set Height |
||
| 273 | * |
||
| 274 | * @param int $pValue |
||
| 275 | * @return self |
||
| 276 | */ |
||
| 277 | 31 | public function setHeight($pValue = 0) |
|
| 278 | { |
||
| 279 | 31 | $this->height = $pValue; |
|
| 280 | 31 | return $this; |
|
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Set width and height with proportional resize |
||
| 285 | * |
||
| 286 | * @param int $width |
||
| 287 | * @param int $height |
||
| 288 | * @example $objDrawing->setWidthAndHeight(160,120); |
||
| 289 | * @return self |
||
| 290 | */ |
||
| 291 | 1 | public function setWidthAndHeight($width = 0, $height = 0) |
|
| 292 | { |
||
| 293 | 1 | $this->width = $width; |
|
| 294 | 1 | $this->height = $height; |
|
| 295 | 1 | return $this; |
|
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Get Rotation |
||
| 300 | * |
||
| 301 | * @return int |
||
| 302 | */ |
||
| 303 | 59 | public function getRotation() |
|
| 304 | { |
||
| 305 | 59 | return $this->rotation; |
|
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Set Rotation |
||
| 310 | * |
||
| 311 | * @param int $pValue |
||
| 312 | * @return self |
||
| 313 | */ |
||
| 314 | 5 | public function setRotation($pValue = 0) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Get Fill |
||
| 322 | * |
||
| 323 | * @return \PhpOffice\PhpPresentation\Style\Fill |
||
| 324 | */ |
||
| 325 | 108 | public function getFill() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Set Fill |
||
| 332 | * @param \PhpOffice\PhpPresentation\Style\Fill $pValue |
||
| 333 | * @return \PhpOffice\PhpPresentation\AbstractShape |
||
| 334 | */ |
||
| 335 | 1 | public function setFill(Fill $pValue = null) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Get Border |
||
| 343 | * |
||
| 344 | * @return \PhpOffice\PhpPresentation\Style\Border |
||
| 345 | */ |
||
| 346 | 82 | public function getBorder() |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Get Shadow |
||
| 353 | * |
||
| 354 | * @return \PhpOffice\PhpPresentation\Style\Shadow |
||
| 355 | */ |
||
| 356 | 91 | public function getShadow() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Set Shadow |
||
| 363 | * |
||
| 364 | * @param \PhpOffice\PhpPresentation\Style\Shadow $pValue |
||
| 365 | * @throws \Exception |
||
| 366 | * @return self |
||
| 367 | */ |
||
| 368 | 3 | public function setShadow(Shadow $pValue = null) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Has Hyperlink? |
||
| 376 | * |
||
| 377 | * @return boolean |
||
| 378 | */ |
||
| 379 | 85 | public function hasHyperlink() |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Get Hyperlink |
||
| 386 | * |
||
| 387 | * @return \PhpOffice\PhpPresentation\Shape\Hyperlink |
||
| 388 | */ |
||
| 389 | 5 | public function getHyperlink() |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Set Hyperlink |
||
| 399 | * |
||
| 400 | * @param \PhpOffice\PhpPresentation\Shape\Hyperlink $pHyperlink |
||
| 401 | * @throws \Exception |
||
| 402 | * @return self |
||
| 403 | */ |
||
| 404 | 1 | public function setHyperlink(Hyperlink $pHyperlink = null) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Get hash code |
||
| 412 | * |
||
| 413 | * @return string Hash code |
||
| 414 | */ |
||
| 415 | 70 | public function getHashCode() |
|
| 416 | { |
||
| 417 | 70 | return md5((is_object($this->container) ? $this->container->getHashCode() : '') . $this->offsetX . $this->offsetY . $this->width . $this->height . $this->rotation . (is_null($this->getFill()) ? '' : $this->getFill()->getHashCode()) . (is_null($this->shadow) ? '' : $this->shadow->getHashCode()) . (is_null($this->hyperlink) ? '' : $this->hyperlink->getHashCode()) . __CLASS__); |
|
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Get hash index |
||
| 422 | * |
||
| 423 | * Note that this index may vary during script execution! Only reliable moment is |
||
| 424 | * while doing a write of a workbook and when changes are not allowed. |
||
| 425 | * |
||
| 426 | * @return string Hash index |
||
| 427 | */ |
||
| 428 | 66 | public function getHashIndex() |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Set hash index |
||
| 435 | * |
||
| 436 | * Note that this index may vary during script execution! Only reliable moment is |
||
| 437 | * while doing a write of a workbook and when changes are not allowed. |
||
| 438 | * |
||
| 439 | * @param string $value Hash index |
||
| 440 | */ |
||
| 441 | 66 | public function setHashIndex($value) |
|
| 445 | |||
| 446 | 61 | public function isPlaceholder() |
|
| 447 | { |
||
| 448 | 61 | return !is_null($this->placeholder); |
|
| 449 | } |
||
| 450 | |||
| 451 | 1 | public function getPlaceholder() |
|
| 452 | { |
||
| 453 | 1 | if (!$this->isPlaceholder()) { |
|
| 454 | 1 | return null; |
|
| 455 | } |
||
| 456 | 1 | return $this->placeholder; |
|
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param \PhpOffice\PhpPresentation\Shape\Placeholder $placeholder |
||
| 461 | * @return $this |
||
| 462 | */ |
||
| 463 | 5 | public function setPlaceHolder(Placeholder $placeholder) |
|
| 468 | } |
||
| 469 |