Complex classes like ODPresentation 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 ODPresentation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class ODPresentation implements ReaderInterface |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * Output Object |
||
| 42 | * @var PhpPresentation |
||
| 43 | */ |
||
| 44 | protected $oPhpPresentation; |
||
| 45 | /** |
||
| 46 | * Output Object |
||
| 47 | * @var \ZipArchive |
||
| 48 | */ |
||
| 49 | protected $oZip; |
||
| 50 | /** |
||
| 51 | * @var array[] |
||
| 52 | */ |
||
| 53 | protected $arrayStyles = array(); |
||
| 54 | /** |
||
| 55 | * @var array[] |
||
| 56 | */ |
||
| 57 | protected $arrayCommonStyles = array(); |
||
| 58 | /** |
||
| 59 | * @var \PhpOffice\Common\XMLReader |
||
| 60 | */ |
||
| 61 | protected $oXMLReader; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file? |
||
| 65 | * |
||
| 66 | * @param string $pFilename |
||
| 67 | * @throws \Exception |
||
| 68 | * @return boolean |
||
| 69 | */ |
||
| 70 | 2 | public function canRead($pFilename) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Does a file support UnserializePhpPresentation ? |
||
| 77 | * |
||
| 78 | * @param string $pFilename |
||
| 79 | * @throws \Exception |
||
| 80 | * @return boolean |
||
| 81 | */ |
||
| 82 | 5 | public function fileSupportsUnserializePhpPresentation($pFilename = '') |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Loads PhpPresentation Serialized file |
||
| 103 | * |
||
| 104 | * @param string $pFilename |
||
| 105 | * @return \PhpOffice\PhpPresentation\PhpPresentation |
||
| 106 | * @throws \Exception |
||
| 107 | */ |
||
| 108 | 2 | public function load($pFilename) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Load PhpPresentation Serialized file |
||
| 120 | * |
||
| 121 | * @param string $pFilename |
||
| 122 | * @return \PhpOffice\PhpPresentation\PhpPresentation |
||
| 123 | * @throws \Exception |
||
| 124 | */ |
||
| 125 | protected function loadFile($pFilename) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Read Document Properties |
||
| 151 | */ |
||
| 152 | protected function loadDocumentProperties() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Extract all slides |
||
| 180 | */ |
||
| 181 | protected function loadSlides() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Extract style |
||
| 197 | * @param \DOMElement $nodeStyle |
||
| 198 | * @return bool |
||
| 199 | * @throws \Exception |
||
| 200 | */ |
||
| 201 | protected function loadStyle(\DOMElement $nodeStyle) |
||
| 202 | { |
||
| 203 | $keyStyle = $nodeStyle->getAttribute('style:name'); |
||
| 204 | |||
| 205 | $nodeDrawingPageProps = $this->oXMLReader->getElement('style:drawing-page-properties', $nodeStyle); |
||
| 206 | if ($nodeDrawingPageProps instanceof \DOMElement) { |
||
| 207 | // Read Background Color |
||
| 208 | if ($nodeDrawingPageProps->hasAttribute('draw:fill-color') && $nodeDrawingPageProps->getAttribute('draw:fill') == 'solid') { |
||
| 209 | $oBackground = new \PhpOffice\PhpPresentation\Slide\Background\Color(); |
||
| 210 | $oColor = new Color(); |
||
| 211 | $oColor->setRGB(substr($nodeDrawingPageProps->getAttribute('draw:fill-color'), -6)); |
||
| 212 | $oBackground->setColor($oColor); |
||
| 213 | } |
||
| 214 | // Read Background Image |
||
| 215 | if ($nodeDrawingPageProps->getAttribute('draw:fill') == 'bitmap' && $nodeDrawingPageProps->hasAttribute('draw:fill-image-name')) { |
||
| 216 | $nameStyle = $nodeDrawingPageProps->getAttribute('draw:fill-image-name'); |
||
| 217 | if (!empty($this->arrayCommonStyles[$nameStyle]) && $this->arrayCommonStyles[$nameStyle]['type'] == 'image' && !empty($this->arrayCommonStyles[$nameStyle]['path'])) { |
||
| 218 | $tmpBkgImg = tempnam(sys_get_temp_dir(), 'PhpPresentationReaderODPBkg'); |
||
| 219 | $contentImg = $this->oZip->getFromName($this->arrayCommonStyles[$nameStyle]['path']); |
||
| 220 | file_put_contents($tmpBkgImg, $contentImg); |
||
| 221 | |||
| 222 | $oBackground = new Image(); |
||
| 223 | $oBackground->setPath($tmpBkgImg); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | $nodeGraphicProps = $this->oXMLReader->getElement('style:graphic-properties', $nodeStyle); |
||
| 229 | if ($nodeGraphicProps instanceof \DOMElement) { |
||
| 230 | // Read Shadow |
||
| 231 | if ($nodeGraphicProps->hasAttribute('draw:shadow') && $nodeGraphicProps->getAttribute('draw:shadow') == 'visible') { |
||
| 232 | $oShadow = new Shadow(); |
||
| 233 | $oShadow->setVisible(true); |
||
| 234 | if ($nodeGraphicProps->hasAttribute('draw:shadow-color')) { |
||
| 235 | $oShadow->getColor()->setRGB(substr($nodeGraphicProps->getAttribute('draw:shadow-color'), -6)); |
||
| 236 | } |
||
| 237 | if ($nodeGraphicProps->hasAttribute('draw:shadow-opacity')) { |
||
| 238 | $oShadow->setAlpha(100 - (int)substr($nodeGraphicProps->getAttribute('draw:shadow-opacity'), 0, -1)); |
||
| 239 | } |
||
| 240 | if ($nodeGraphicProps->hasAttribute('draw:shadow-offset-x') && $nodeGraphicProps->hasAttribute('draw:shadow-offset-y')) { |
||
| 241 | $offsetX = substr($nodeGraphicProps->getAttribute('draw:shadow-offset-x'), 0, -2); |
||
| 242 | $offsetY = substr($nodeGraphicProps->getAttribute('draw:shadow-offset-y'), 0, -2); |
||
| 243 | $distance = 0; |
||
| 244 | if ($offsetX != 0) { |
||
| 245 | $distance = ($offsetX < 0 ? $offsetX * -1 : $offsetX); |
||
| 246 | } elseif ($offsetY != 0) { |
||
| 247 | $distance = ($offsetY < 0 ? $offsetY * -1 : $offsetY); |
||
| 248 | } |
||
| 249 | $oShadow->setDirection(rad2deg(atan2($offsetY, $offsetX))); |
||
| 250 | $oShadow->setDistance(CommonDrawing::centimetersToPixels($distance)); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | // Read Fill |
||
| 254 | if ($nodeGraphicProps->hasAttribute('draw:fill')) { |
||
| 255 | $value = $nodeGraphicProps->getAttribute('draw:fill'); |
||
| 256 | |||
| 257 | switch ($value) { |
||
| 258 | case 'none': |
||
| 259 | $oFill = new Fill(); |
||
| 260 | $oFill->setFillType(Fill::FILL_NONE); |
||
| 261 | break; |
||
| 262 | case 'solid': |
||
| 263 | $oFill = new Fill(); |
||
| 264 | $oFill->setFillType(Fill::FILL_SOLID); |
||
| 265 | if ($nodeGraphicProps->hasAttribute('draw:fill-color')) { |
||
| 266 | $oColor = new Color(); |
||
| 267 | $oColor->setRGB(substr($nodeGraphicProps->getAttribute('draw:fill-color'), 1)); |
||
| 268 | $oFill->setStartColor($oColor); |
||
| 269 | } |
||
| 270 | break; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | $nodeTextProperties = $this->oXMLReader->getElement('style:text-properties', $nodeStyle); |
||
| 276 | if ($nodeTextProperties instanceof \DOMElement) { |
||
| 277 | $oFont = new Font(); |
||
| 278 | if ($nodeTextProperties->hasAttribute('fo:color')) { |
||
| 279 | $oFont->getColor()->setRGB(substr($nodeTextProperties->getAttribute('fo:color'), -6)); |
||
| 280 | } |
||
| 281 | if ($nodeTextProperties->hasAttribute('fo:font-family')) { |
||
| 282 | $oFont->setName($nodeTextProperties->getAttribute('fo:font-family')); |
||
| 283 | } |
||
| 284 | if ($nodeTextProperties->hasAttribute('fo:font-weight') && $nodeTextProperties->getAttribute('fo:font-weight') == 'bold') { |
||
| 285 | $oFont->setBold(true); |
||
| 286 | } |
||
| 287 | if ($nodeTextProperties->hasAttribute('fo:font-size')) { |
||
| 288 | $oFont->setSize(substr($nodeTextProperties->getAttribute('fo:font-size'), 0, -2)); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | $nodeParagraphProps = $this->oXMLReader->getElement('style:paragraph-properties', $nodeStyle); |
||
| 293 | if ($nodeParagraphProps instanceof \DOMElement) { |
||
| 294 | $oAlignment = new Alignment(); |
||
| 295 | if ($nodeParagraphProps->hasAttribute('fo:text-align')) { |
||
| 296 | $oAlignment->setHorizontal($nodeParagraphProps->getAttribute('fo:text-align')); |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | if ($nodeStyle->nodeName == 'text:list-style') { |
||
| 301 | $arrayListStyle = array(); |
||
| 302 | foreach ($this->oXMLReader->getElements('text:list-level-style-bullet', $nodeStyle) as $oNodeListLevel) { |
||
| 303 | $oAlignment = new Alignment(); |
||
| 304 | $oBullet = new Bullet(); |
||
| 305 | $oBullet->setBulletType(Bullet::TYPE_NONE); |
||
| 306 | if ($oNodeListLevel->hasAttribute('text:level')) { |
||
| 307 | $oAlignment->setLevel((int) $oNodeListLevel->getAttribute('text:level') - 1); |
||
| 308 | } |
||
| 309 | if ($oNodeListLevel->hasAttribute('text:bullet-char')) { |
||
| 310 | $oBullet->setBulletChar($oNodeListLevel->getAttribute('text:bullet-char')); |
||
| 311 | $oBullet->setBulletType(Bullet::TYPE_BULLET); |
||
| 312 | } |
||
| 313 | |||
| 314 | $oNodeListProperties = $this->oXMLReader->getElement('style:list-level-properties', $oNodeListLevel); |
||
| 315 | if ($oNodeListProperties instanceof \DOMElement) { |
||
| 316 | if ($oNodeListProperties->hasAttribute('text:min-label-width')) { |
||
| 317 | $oAlignment->setIndent((int)round(CommonDrawing::centimetersToPixels(substr($oNodeListProperties->getAttribute('text:min-label-width'), 0, -2)))); |
||
| 318 | } |
||
| 319 | if ($oNodeListProperties->hasAttribute('text:space-before')) { |
||
| 320 | $iSpaceBefore = CommonDrawing::centimetersToPixels(substr($oNodeListProperties->getAttribute('text:space-before'), 0, -2)); |
||
| 321 | $iMarginLeft = $iSpaceBefore + $oAlignment->getIndent(); |
||
| 322 | $oAlignment->setMarginLeft($iMarginLeft); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | $oNodeTextProperties = $this->oXMLReader->getElement('style:text-properties', $oNodeListLevel); |
||
| 326 | if ($oNodeTextProperties instanceof \DOMElement) { |
||
| 327 | if ($oNodeTextProperties->hasAttribute('fo:font-family')) { |
||
| 328 | $oBullet->setBulletFont($oNodeTextProperties->getAttribute('fo:font-family')); |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | $arrayListStyle[$oAlignment->getLevel()] = array( |
||
| 333 | 'alignment' => $oAlignment, |
||
| 334 | 'bullet' => $oBullet, |
||
| 335 | ); |
||
| 336 | } |
||
| 337 | } |
||
| 338 | |||
| 339 | $this->arrayStyles[$keyStyle] = array( |
||
| 340 | 'alignment' => isset($oAlignment) ? $oAlignment : null, |
||
| 341 | 'background' => isset($oBackground) ? $oBackground : null, |
||
| 342 | 'fill' => isset($oFill) ? $oFill : null, |
||
| 343 | 'font' => isset($oFont) ? $oFont : null, |
||
| 344 | 'shadow' => isset($oShadow) ? $oShadow : null, |
||
| 345 | 'listStyle' => isset($arrayListStyle) ? $arrayListStyle : null, |
||
| 346 | ); |
||
| 347 | |||
| 348 | return true; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Read Slide |
||
| 353 | * |
||
| 354 | * @param \DOMElement $nodeSlide |
||
| 355 | * @return bool |
||
| 356 | * @throws \Exception |
||
| 357 | */ |
||
| 358 | protected function loadSlide(\DOMElement $nodeSlide) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Read Shape Drawing |
||
| 387 | * |
||
| 388 | * @param \DOMElement $oNodeFrame |
||
| 389 | * @throws \Exception |
||
| 390 | */ |
||
| 391 | protected function loadShapeDrawing(\DOMElement $oNodeFrame) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Read Shape RichText |
||
| 434 | * |
||
| 435 | * @param \DOMElement $oNodeFrame |
||
| 436 | * @throws \Exception |
||
| 437 | */ |
||
| 438 | protected function loadShapeRichText(\DOMElement $oNodeFrame) |
||
| 463 | |||
| 464 | protected $levelParagraph = 0; |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Read Paragraph |
||
| 468 | * @param RichText $oShape |
||
| 469 | * @param \DOMElement $oNodeParent |
||
| 470 | * @throws \Exception |
||
| 471 | */ |
||
| 472 | protected function readParagraph(RichText $oShape, \DOMElement $oNodeParent) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Read Paragraph Item |
||
| 490 | * @param Paragraph $oParagraph |
||
| 491 | * @param \DOMElement $oNodeParent |
||
| 492 | * @throws \Exception |
||
| 493 | */ |
||
| 494 | protected function readParagraphItem(Paragraph $oParagraph, \DOMElement $oNodeParent) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Read List |
||
| 520 | * |
||
| 521 | * @param RichText $oShape |
||
| 522 | * @param \DOMElement $oNodeParent |
||
| 523 | * @throws \Exception |
||
| 524 | */ |
||
| 525 | protected function readList(RichText $oShape, \DOMElement $oNodeParent) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Read List Item |
||
| 541 | * @param RichText $oShape |
||
| 542 | * @param \DOMElement $oNodeParent |
||
| 543 | * @param \DOMElement $oNodeParagraph |
||
| 544 | * @throws \Exception |
||
| 545 | */ |
||
| 546 | protected function readListItem(RichText $oShape, \DOMElement $oNodeParent, \DOMElement $oNodeParagraph) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Load file 'styles.xml' |
||
| 563 | */ |
||
| 564 | protected function loadStylesFile() |
||
| 575 | } |
||
| 576 |