Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove static classes #206; Reactivate PHPCPD and PHPMD #220

Merged
merged 7 commits into from
May 4, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More refactoring for PHPMD compliance
  • Loading branch information
ivanlanin committed May 4, 2014
commit c21e28f9744cf25be7d643cdaf3fb28470d917a5
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ before_script:

script:
## PHP_CodeSniffer
- phpcs --standard=PSR2 -n src/ --ignore=src/PhpWord/Shared/PCLZip
- phpcs --standard=PSR2 -n tests/
- phpcs src/ tests/ --standard=PSR2 -n --ignore=src/PhpWord/Shared/PCLZip
## PHP Copy/Paste Detector
- php phpcpd.phar --verbose src/
- phpcpd src/ tests/ --verbose
## PHP Mess Detector
- phpmd src/ text phpmd.xml --exclude pclzip.lib.php
- phpmd src/ text unusedcode,naming,design,controversial --exclude pclzip.lib.php
## PHPLOC
#- php phploc.phar src/
## PHPUnit
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ This release marked heavy refactorings on internal code structure with the creat
- Refactor: Apply composite pattern for Word2007 reader - @ivanlanin
- Refactor: Replace static classes `Footnotes`, `Endnotes`, and `TOC` with `Collections` - @ivanlanin GH-206
- QA: Reactivate `phpcpd` and `phpmd` on Travis - @ivanlanin
- Refactor: PHPMD recommendation: Change all `get...` method that returns `boolean` into `is...` or `has...` - @ivanlanin

## 0.9.1 - 27 Mar 2014

Expand Down
14 changes: 0 additions & 14 deletions phpmd.xml

This file was deleted.

142 changes: 66 additions & 76 deletions src/PhpWord/Element/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@

namespace PhpOffice\PhpWord\Element;

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\Element\CheckBox;
use PhpOffice\PhpWord\Element\Image;
use PhpOffice\PhpWord\Element\Link;
use PhpOffice\PhpWord\Element\ListItem;
use PhpOffice\PhpWord\Element\Object;
use PhpOffice\PhpWord\Element\PreserveText;
use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextBreak;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Exception\InvalidObjectException;

/**
* Container abstract class
Expand Down Expand Up @@ -69,27 +67,29 @@ public function countElements()
}

/**
* Add text element
* Add text/preservetext element
*
* @param string $text
* @param mixed $fontStyle
* @param mixed $paragraphStyle
* @return \PhpOffice\PhpWord\Element\Text
* @param string $elementName Text|PreserveText
* @return \PhpOffice\PhpWord\Element\Text|\PhpOffice\PhpWord\Element\PreserveText
*/
public function addText($text, $fontStyle = null, $paragraphStyle = null)
public function addText($text, $fontStyle = null, $paragraphStyle = null, $elementName = 'Text')
{
$this->checkValidity('Text');
$this->checkValidity($elementName);
$elementClass = 'PhpOffice\\PhpWord\\Element\\' . $elementName;

// Reset paragraph style for footnote and textrun. They have their own
if (in_array($this->container, array('textrun', 'footnote', 'endnote'))) {
$paragraphStyle = null;
}

$textObject = new Text($text, $fontStyle, $paragraphStyle);
$textObject->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($textObject);
$element = new $elementClass($text, $fontStyle, $paragraphStyle);
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($element);

return $textObject;
return $element;
}

/**
Expand All @@ -102,11 +102,11 @@ public function addTextRun($paragraphStyle = null)
{
$this->checkValidity('Textrun');

$textRun = new TextRun($paragraphStyle);
$textRun->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($textRun);
$element = new TextRun($paragraphStyle);
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($element);

return $textRun;
return $element;
}

/**
Expand All @@ -123,13 +123,15 @@ public function addLink($target, $text = null, $fontStyle = null, $paragraphStyl
$this->checkValidity('Link');
$elementDocPart = $this->checkElementDocPart();

$link = new Link($target, $text, $fontStyle, $paragraphStyle);
$link->setDocPart($this->getDocPart(), $this->getDocPartId());
$element = new Link($target, $text, $fontStyle, $paragraphStyle);
$element->setDocPart($this->getDocPart(), $this->getDocPartId());

$rId = Media::addElement($elementDocPart, 'link', $target);
$link->setRelationId($rId);
$this->addElement($link);
$element->setRelationId($rId);

return $link;
$this->addElement($element);

return $element;
}

/**
Expand All @@ -142,13 +144,7 @@ public function addLink($target, $text = null, $fontStyle = null, $paragraphStyl
*/
public function addPreserveText($text, $fontStyle = null, $paragraphStyle = null)
{
$this->checkValidity('PreserveText');

$preserveText = new PreserveText($text, $fontStyle, $paragraphStyle);
$preserveText->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($preserveText);

return $preserveText;
return $this->addText($text, $fontStyle, $paragraphStyle, 'PreserveText');
}

/**
Expand All @@ -163,9 +159,9 @@ public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = nu
$this->checkValidity('TextBreak');

for ($i = 1; $i <= $count; $i++) {
$textBreak = new TextBreak($fontStyle, $paragraphStyle);
$textBreak->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($textBreak);
$element = new TextBreak($fontStyle, $paragraphStyle);
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($element);
}
}

Expand All @@ -183,69 +179,63 @@ public function addListItem($text, $depth = 0, $fontStyle = null, $listStyle = n
{
$this->checkValidity('ListItem');

$listItem = new ListItem($text, $depth, $fontStyle, $listStyle, $paragraphStyle);
$listItem->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($listItem);
$element = new ListItem($text, $depth, $fontStyle, $listStyle, $paragraphStyle);
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($element);

return $listItem;
return $element;
}

/**
* Add image element
*
* @param string $src
* @param string $source
* @param mixed $style Image style
* @param boolean $isWatermark
* @return \PhpOffice\PhpWord\Element\Image
*/
public function addImage($src, $style = null, $isWatermark = false)
public function addImage($source, $style = null, $isWatermark = false)
{
$this->checkValidity('Image');
$elementDocPart = $this->checkElementDocPart();

$image = new Image($src, $style, $isWatermark);
$image->setDocPart($this->getDocPart(), $this->getDocPartId());
$rId = Media::addElement($elementDocPart, 'image', $src, $image);
$image->setRelationId($rId);
$this->addElement($image);
$element = new Image($source, $style, $isWatermark);
$element->setDocPart($this->getDocPart(), $this->getDocPartId());

$rId = Media::addElement($elementDocPart, 'image', $source, $element);
$element->setRelationId($rId);

$this->addElement($element);

return $image;
return $element;
}

/**
* Add OLE-object element
*
* All exceptions should be handled by \PhpOffice\PhpWord\Element\Object
*
* @param string $src
* @param string $source
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\Object
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
public function addObject($src, $style = null)
public function addObject($source, $style = null)
{
$this->checkValidity('Object');
$elementDocPart = $this->checkElementDocPart();

$object = new Object($src, $style);
$object->setDocPart($this->getDocPart(), $this->getDocPartId());
if (!is_null($object->getSource())) {
$inf = pathinfo($src);
$ext = $inf['extension'];
if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') {
$ext = substr($ext, 0, -1);
}
$icon = realpath(__DIR__ . "/../resources/{$ext}.png");
$rId = Media::addElement($elementDocPart, 'object', $src);
$object->setRelationId($rId);
$rIdimg = Media::addElement($elementDocPart, 'image', $icon, new Image($icon));
$object->setImageRelationId($rIdimg);
$this->addElement($object);

return $object;
} else {
throw new InvalidObjectException();
}
$element = new Object($source, $style);
$element->setDocPart($this->getDocPart(), $this->getDocPartId());

$rId = Media::addElement($elementDocPart, 'object', $source);
$element->setRelationId($rId);
$rIdIcon = Media::addElement($elementDocPart, 'image', $element->getIcon(), new Image($element->getIcon()));
$element->setImageRelationId($rIdIcon);

$this->addElement($element);

return $element;
}

/**
Expand All @@ -258,19 +248,19 @@ public function addObject($src, $style = null)
public function addFootnote($paragraphStyle = null, $elementName = 'Footnote')
{
$this->checkValidity($elementName);
$elementClass = 'PhpOffice\\PhpWord\\Element\\' . $elementName;
$docPart = strtolower($elementName);
$addMethod = "add{$elementName}";
$elementClass = 'PhpOffice\\PhpWord\\Element\\' . $elementName;

$note = new $elementClass($paragraphStyle);
// if ($this->phpWord instanceof PhpWord) {
$rId = $this->phpWord->$addMethod($note);
// }
$note->setDocPart($docPart, $this->getDocPartId());
$note->setRelationId($rId);
$this->addElement($note);
$element = new $elementClass($paragraphStyle);
if ($this->phpWord instanceof PhpWord) {
$rId = $this->phpWord->$addMethod($element);
}
$element->setDocPart($docPart, $this->getDocPartId());
$element->setRelationId($rId);
$this->addElement($element);

return $note;
return $element;
}

/**
Expand All @@ -297,11 +287,11 @@ public function addCheckBox($name, $text, $fontStyle = null, $paragraphStyle = n
{
$this->checkValidity('CheckBox');

$checkBox = new CheckBox($name, $text, $fontStyle, $paragraphStyle);
$checkBox->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($checkBox);
$element = new CheckBox($name, $text, $fontStyle, $paragraphStyle);
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($element);

return $checkBox;
return $element;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ public function getPhpWord()
}

/**
* Set PhpWord
* Set PhpWord as reference
*
* @param \PhpOffice\PhpWord\PhpWord
*/
public function setPhpWord(PhpWord &$phpWord = null)
{
$this->phpWord = $phpWord;
$this->phpWord = &$phpWord;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Element/Footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Footer extends AbstractContainer
* Create new instance
*
* @param int $sectionId
* @param int $footerId
* @param int $containerId
* @param string $type
*/
public function __construct($sectionId, $containerId = 1, $type = self::AUTO)
Expand Down
Loading