PHPMD fixes and remove Style\Tabs (not needed)

This commit is contained in:
Ivan Lanin 2014-04-22 17:25:10 +07:00
parent f829559f65
commit 0060e4316c
21 changed files with 159 additions and 168 deletions

View File

@ -72,6 +72,7 @@ This release marked heavy refactorings on internal code structure with the creat
- General: Rename `Footnote` to `Footnotes` to reflect the nature of collection - @ivanlanin
- General: Add some unit tests for Shared & Element (100%!) - @Progi1984
- Test: Add some samples and tests for image wrapping style - @brunocasado GH-59
- Refactor: Remove Style\Tabs
## 0.9.1 - 27 Mar 2014

19
phpmd.xml Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0"?>
<ruleset name="PHPPowerPoint mess detector ruleset"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation=" http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>PHPPowerPoint ruleset</description>
<rule ref="rulesets/unusedcode.xml" />
<rule ref="rulesets/design.xml" />
<rule ref="rulesets/naming.xml/LongVariable">
<properties>
<property name="maximum" value="30" />
</properties>
</rule>
<rule ref="rulesets/naming.xml/ShortVariable" />
<rule ref="rulesets/naming.xml/ShortMethodName" />
<rule ref="rulesets/naming.xml/ConstructorWithNameAsEnclosingClass" />
<rule ref="rulesets/naming.xml/ConstantNamingConventions" />
</ruleset>

View File

@ -73,8 +73,8 @@ class Table extends AbstractElement
*/
public function addCell($width = null, $style = null)
{
$i = count($this->rows) - 1;
$cell = $this->rows[$i]->addCell($width, $style);
$index = count($this->rows) - 1;
$cell = $this->rows[$index]->addCell($width, $style);
return $cell;
}

View File

@ -113,7 +113,7 @@ class Media
$mediaCount = 0;
if (array_key_exists($container, self::$elements)) {
foreach (self::$elements[$container] as $mediaKey => $mediaData) {
foreach (self::$elements[$container] as $mediaData) {
if (!is_null($mediaType)) {
if ($mediaType == $mediaData['type']) {
$mediaCount++;

View File

@ -229,12 +229,12 @@ class Cell extends AbstractStyle
*/
public function getBorderSize()
{
$t = $this->getBorderTopSize();
$l = $this->getBorderLeftSize();
$r = $this->getBorderRightSize();
$b = $this->getBorderBottomSize();
$top = $this->getBorderTopSize();
$left = $this->getBorderLeftSize();
$right = $this->getBorderRightSize();
$bottom = $this->getBorderBottomSize();
return array($t, $l, $r, $b);
return array($top, $left, $right, $bottom);
}
/**
@ -255,12 +255,12 @@ class Cell extends AbstractStyle
*/
public function getBorderColor()
{
$t = $this->getBorderTopColor();
$l = $this->getBorderLeftColor();
$r = $this->getBorderRightColor();
$b = $this->getBorderBottomColor();
$top = $this->getBorderTopColor();
$left = $this->getBorderLeftColor();
$right = $this->getBorderRightColor();
$bottom = $this->getBorderBottomColor();
return array($t, $l, $r, $b);
return array($top, $left, $right, $bottom);
}
/**

View File

@ -57,9 +57,9 @@ class Paragraph extends AbstractStyle
/**
* Set of Custom Tab Stops
*
* @var array
* @var \PhpOffice\PhpWord\Style\Tab[]
*/
private $tabs;
private $tabs = array();
/**
* Indent by how much
@ -172,7 +172,7 @@ class Paragraph extends AbstractStyle
* Set Paragraph Alignment
*
* @param string $pValue
* @return \PhpOffice\PhpWord\Style\Paragraph
* @return self
*/
public function setAlign($pValue = null)
{
@ -198,7 +198,7 @@ class Paragraph extends AbstractStyle
* Set Space before Paragraph
*
* @param int $pValue
* @return \PhpOffice\PhpWord\Style\Paragraph
* @return self
*/
public function setSpaceBefore($pValue = null)
{
@ -220,7 +220,7 @@ class Paragraph extends AbstractStyle
* Set Space after Paragraph
*
* @param int $pValue
* @return \PhpOffice\PhpWord\Style\Paragraph
* @return self
*/
public function setSpaceAfter($pValue = null)
{
@ -242,7 +242,7 @@ class Paragraph extends AbstractStyle
* Set Spacing between breaks
*
* @param int $pValue
* @return \PhpOffice\PhpWord\Style\Paragraph
* @return self
*/
public function setSpacing($pValue = null)
{
@ -264,7 +264,7 @@ class Paragraph extends AbstractStyle
* Set indentation
*
* @param int $pValue
* @return \PhpOffice\PhpWord\Style\Paragraph
* @return self
*/
public function setIndent($pValue = null)
{
@ -286,7 +286,7 @@ class Paragraph extends AbstractStyle
* Set hanging
*
* @param int $pValue
* @return \PhpOffice\PhpWord\Style\Paragraph
* @return self
*/
public function setHanging($pValue = null)
{
@ -297,7 +297,7 @@ class Paragraph extends AbstractStyle
/**
* Get tabs
*
* @return \PhpOffice\PhpWord\Style\Tabs
* @return \PhpOffice\PhpWord\Style\Tab[]
*/
public function getTabs()
{
@ -308,13 +308,14 @@ class Paragraph extends AbstractStyle
* Set tabs
*
* @param array $pValue
* @return \PhpOffice\PhpWord\Style\Paragraph
* @return self
*/
public function setTabs($pValue = null)
{
if (is_array($pValue)) {
$this->tabs = new Tabs($pValue);
$this->tabs = $pValue;
}
return $this;
}
@ -332,7 +333,7 @@ class Paragraph extends AbstractStyle
* Set parent style ID
*
* @param string $pValue
* @return \PhpOffice\PhpWord\Style\Paragraph
* @return self
*/
public function setBasedOn($pValue = 'Normal')
{
@ -354,7 +355,7 @@ class Paragraph extends AbstractStyle
* Set style for next paragraph
*
* @param string $pValue
* @return \PhpOffice\PhpWord\Style\Paragraph
* @return self
*/
public function setNext($pValue = null)
{
@ -376,7 +377,7 @@ class Paragraph extends AbstractStyle
* Set keep paragraph with next paragraph setting
*
* @param bool $pValue
* @return \PhpOffice\PhpWord\Style\Paragraph
* @return self
*/
public function setWidowControl($pValue = true)
{
@ -401,7 +402,7 @@ class Paragraph extends AbstractStyle
* Set keep paragraph with next paragraph setting
*
* @param bool $pValue
* @return \PhpOffice\PhpWord\Style\Paragraph
* @return self
*/
public function setKeepNext($pValue = false)
{
@ -426,7 +427,7 @@ class Paragraph extends AbstractStyle
* Set keep all lines on one page setting
*
* @param bool $pValue
* @return \PhpOffice\PhpWord\Style\Paragraph
* @return self
*/
public function setKeepLines($pValue = false)
{
@ -451,7 +452,7 @@ class Paragraph extends AbstractStyle
* Set start paragraph on next page setting
*
* @param bool $pValue
* @return \PhpOffice\PhpWord\Style\Paragraph
* @return self
*/
public function setPageBreakBefore($pValue = false)
{

View File

@ -390,12 +390,12 @@ class Section extends AbstractStyle
*/
public function getBorderSize()
{
$t = $this->getBorderTopSize();
$l = $this->getBorderLeftSize();
$r = $this->getBorderRightSize();
$b = $this->getBorderBottomSize();
$top = $this->getBorderTopSize();
$left = $this->getBorderLeftSize();
$right = $this->getBorderRightSize();
$bottom = $this->getBorderBottomSize();
return array($t, $l, $r, $b);
return array($top, $left, $right, $bottom);
}
/**
@ -418,12 +418,12 @@ class Section extends AbstractStyle
*/
public function getBorderColor()
{
$t = $this->getBorderTopColor();
$l = $this->getBorderLeftColor();
$r = $this->getBorderRightColor();
$b = $this->getBorderBottomColor();
$top = $this->getBorderTopColor();
$left = $this->getBorderLeftColor();
$right = $this->getBorderRightColor();
$bottom = $this->getBorderBottomColor();
return array($t, $l, $r, $b);
return array($top, $left, $right, $bottom);
}
/**

View File

@ -90,21 +90,33 @@ class Tab extends AbstractStyle
}
/**
* Creates the XML DOM for the instance of Tab.
* Get stop type
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter &$xmlWriter
* @return string
*/
public function toXml(XMLWriter &$xmlWriter = null)
public function getStopType()
{
if (isset($xmlWriter)) {
$xmlWriter->startElement("w:tab");
$xmlWriter->writeAttribute("w:val", $this->val);
if (!is_null($this->leader)) {
$xmlWriter->writeAttribute("w:leader", $this->leader);
return $this->val;
}
$xmlWriter->writeAttribute("w:pos", $this->position);
$xmlWriter->endElement();
/**
* Get leader
*
* @return string
*/
public function getLeader()
{
return $this->leader;
}
/**
* Get position
*
* @return integer
*/
public function getPosition()
{
return $this->position;
}
/**

View File

@ -247,14 +247,14 @@ class Table extends AbstractStyle
*/
public function getBorderSize()
{
$t = $this->getBorderTopSize();
$l = $this->getBorderLeftSize();
$r = $this->getBorderRightSize();
$b = $this->getBorderBottomSize();
$h = $this->getBorderInsideHSize();
$v = $this->getBorderInsideVSize();
$top = $this->getBorderTopSize();
$left = $this->getBorderLeftSize();
$right = $this->getBorderRightSize();
$bottom = $this->getBorderBottomSize();
$insideH = $this->getBorderInsideHSize();
$insideV = $this->getBorderInsideVSize();
return array($t, $l, $r, $b, $h, $v);
return array($top, $left, $right, $bottom, $insideH, $insideV);
}
/**
@ -278,14 +278,14 @@ class Table extends AbstractStyle
*/
public function getBorderColor()
{
$t = $this->getBorderTopColor();
$l = $this->getBorderLeftColor();
$r = $this->getBorderRightColor();
$b = $this->getBorderBottomColor();
$h = $this->getBorderInsideHColor();
$v = $this->getBorderInsideVColor();
$top = $this->getBorderTopColor();
$left = $this->getBorderLeftColor();
$right = $this->getBorderRightColor();
$bottom = $this->getBorderBottomColor();
$insideH = $this->getBorderInsideHColor();
$insideV = $this->getBorderInsideVColor();
return array($t, $l, $r, $b, $h, $v);
return array($top, $left, $right, $bottom, $insideH, $insideV);
}
/**

View File

@ -1,51 +0,0 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Shared\XMLWriter;
/**
* Tabs style
*/
class Tabs extends AbstractStyle
{
/**
* Tabs
*
* @var array
*/
private $tabs;
/**
* Create new tab collection style
*
* @param array $tabs
*/
public function __construct(array $tabs)
{
$this->tabs = $tabs;
}
/**
* Return XML
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter &$xmlWriter
*/
public function toXml(XMLWriter &$xmlWriter = null)
{
if (isset($xmlWriter)) {
$xmlWriter->startElement("w:tabs");
foreach ($this->tabs as &$tab) {
$tab->toXml($xmlWriter);
}
$xmlWriter->endElement();
}
}
}

View File

@ -75,16 +75,16 @@ class Template
$this->zipClass->open($this->tempFileName);
// Find and load headers and footers
$i = 1;
while ($this->zipClass->locateName($this->getHeaderName($i)) !== false) {
$this->headerXMLs[$i] = $this->zipClass->getFromName($this->getHeaderName($i));
$i++;
$index = 1;
while ($this->zipClass->locateName($this->getHeaderName($index)) !== false) {
$this->headerXMLs[$index] = $this->zipClass->getFromName($this->getHeaderName($index));
$index++;
}
$i = 1;
while ($this->zipClass->locateName($this->getFooterName($i)) !== false) {
$this->footerXMLs[$i] = $this->zipClass->getFromName($this->getFooterName($i));
$i++;
$index = 1;
while ($this->zipClass->locateName($this->getFooterName($index)) !== false) {
$this->footerXMLs[$index] = $this->zipClass->getFromName($this->getFooterName($index));
$index++;
}
$this->documentXML = $this->zipClass->getFromName('word/document.xml');

View File

@ -251,9 +251,9 @@ abstract class AbstractWriter implements WriterInterface
// Retrieve OVERWRITE and CREATE constants from the instantiated zip class
// This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP
$ro = new \ReflectionObject($objZip);
$zipOverWrite = $ro->getConstant('OVERWRITE');
$zipCreate = $ro->getConstant('CREATE');
$reflection = new \ReflectionObject($objZip);
$zipOverWrite = $reflection->getConstant('OVERWRITE');
$zipCreate = $reflection->getConstant('CREATE');
// Remove any existing file
if (file_exists($filename)) {

View File

@ -69,9 +69,13 @@ class HTML extends AbstractWriter implements WriterInterface
{
if (!is_null($this->getPhpWord())) {
$this->setTempDir(sys_get_temp_dir() . '/PHPWordWriter/');
$hFile = fopen($filename, 'w') or die("can't open file");
$hFile = fopen($filename, 'w');
if ($hFile !== false) {
fwrite($hFile, $this->writeDocument());
fclose($hFile);
} else {
throw new Exception("Can't open file");
}
$this->clearTempDir();
} else {
throw new Exception("No PHPWord assigned.");
@ -739,9 +743,9 @@ class HTML extends AbstractWriter implements WriterInterface
$imageBinary = ob_get_contents();
ob_end_clean();
} else {
if ($fp = fopen($actualSource, 'rb', false)) {
$imageBinary = fread($fp, filesize($actualSource));
fclose($fp);
if ($fileHandle = fopen($actualSource, 'rb', false)) {
$imageBinary = fread($fileHandle, filesize($actualSource));
fclose($fileHandle);
}
}
if (!is_null($imageBinary)) {

View File

@ -65,7 +65,7 @@ class Base extends AbstractWriterPart
$styles = Style::getStyles();
$numFonts = 0;
if (count($styles) > 0) {
foreach ($styles as $styleName => $style) {
foreach ($styles as $style) {
// Font
if ($style instanceof Font) {
$numFonts++;

View File

@ -187,7 +187,7 @@ class Content extends Base
if ($SfIsObject) {
// Don't never be the case, because I browse all sections for cleaning all styles not declared
die('PhpWord : $SfIsObject wouldn\'t be an object');
throw new Exception('PhpWord : $SfIsObject wouldn\'t be an object');
} else {
if (!$withoutP) {
$xmlWriter->startElement('text:p'); // text:p
@ -457,8 +457,8 @@ class Content extends Base
}
// Images
$imageData = Media::getElements('section');
foreach ($imageData as $imageId => $image) {
$images = Media::getElements('section');
foreach ($images as $image) {
if ($image['type'] == 'image') {
$xmlWriter->startElement('style:style');
$xmlWriter->writeAttribute('style:name', 'fr' . $image['rID']);

View File

@ -45,13 +45,6 @@ abstract class AbstractRenderer extends \PhpOffice\PhpWord\Writer\HTML
*/
protected $orientation = null;
/**
* Temporary storage for Save Array Return type
*
* @var string
*/
private $saveArrayReturnType;
/**
* Paper Sizes xRef List
*

View File

@ -9,6 +9,8 @@
namespace PhpOffice\PhpWord\Writer;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\TOC;
use PhpOffice\PhpWord\Element\Image;
use PhpOffice\PhpWord\Element\Link;
use PhpOffice\PhpWord\Element\ListItem;
@ -20,11 +22,9 @@ use PhpOffice\PhpWord\Element\TextBreak;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Element\Title;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Drawing;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\TOC;
use PhpOffice\PhpWord\Style\Font;
/**
* RTF writer
@ -75,10 +75,13 @@ class RTF extends AbstractWriter implements WriterInterface
if (!is_null($this->phpWord)) {
$pFilename = $this->getTempFile($pFilename);
$hFile = fopen($pFilename, 'w') or die("can't open file");
$hFile = fopen($pFilename, 'w');
if ($hFile !== false) {
fwrite($hFile, $this->getData());
fclose($hFile);
} else {
throw new Exception("Can't open file");
}
$this->cleanupTempFile();
} else {
throw new Exception("PhpWord object unassigned.");
@ -160,7 +163,7 @@ class RTF extends AbstractWriter implements WriterInterface
// Browse styles
$styles = Style::getStyles();
if (count($styles) > 0) {
foreach ($styles as $styleName => $style) {
foreach ($styles as $style) {
// Font
if ($style instanceof Font) {
if (in_array($style->getName(), $arrFonts) == false) {
@ -212,7 +215,7 @@ class RTF extends AbstractWriter implements WriterInterface
// Browse styles
$styles = Style::getStyles();
if (count($styles) > 0) {
foreach ($styles as $styleName => $style) {
foreach ($styles as $style) {
// Font
if ($style instanceof Font) {
$color = $style->getColor();

View File

@ -180,7 +180,7 @@ class Word2007 extends AbstractWriter implements WriterInterface
$writeFunction = $elmType == 'header' ? 'writeHeader' : 'writeFooter';
$elmCount = ($section->getSectionId() - 1) * 3;
$elmObjects = $section->$getFunction();
foreach ($elmObjects as $index => &$elmObject) {
foreach ($elmObjects as &$elmObject) {
$elmCount++;
$elmObject->setRelationId(++$rId);
$elmFile = "{$elmType}{$elmCount}.xml";

View File

@ -814,8 +814,18 @@ class Base extends AbstractWriterPart
}
// Tabs
if (!is_null($tabs)) {
$tabs->toXml($xmlWriter);
if (!empty($tabs)) {
$xmlWriter->startElement("w:tabs");
foreach ($tabs as $tab) {
$xmlWriter->startElement("w:tab");
$xmlWriter->writeAttribute("w:val", $tab->getStopType());
if (!is_null($tab->getLeader())) {
$xmlWriter->writeAttribute("w:leader", $tab->getLeader());
}
$xmlWriter->writeAttribute("w:pos", $tab->getPosition());
$xmlWriter->endElement();
}
$xmlWriter->endElement();
}
if (!$withoutPPR) {

View File

@ -80,9 +80,9 @@ class Rels extends AbstractWriterPart
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param null|array $xmlRels
* @param null|array $mediaRels
* @param integer $id
* @param integer $relId
*/
private function writeRels(XMLWriter $xmlWriter, $xmlRels = null, $mediaRels = null, $id = 1)
private function writeRels(XMLWriter $xmlWriter, $xmlRels = null, $mediaRels = null, $relId = 1)
{
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
$xmlWriter->startElement('Relationships');
@ -91,7 +91,7 @@ class Rels extends AbstractWriterPart
// XML files relationships
if (is_array($xmlRels)) {
foreach ($xmlRels as $target => $type) {
$this->writeRel($xmlWriter, $id++, $type, $target);
$this->writeRel($xmlWriter, $relId++, $type, $target);
}
}
@ -105,7 +105,7 @@ class Rels extends AbstractWriterPart
$target = array_key_exists($mediaType, $targetPaths) ? $targetPaths[$mediaType] : '';
$target .= $mediaRel['target'];
$targetMode = ($type == 'hyperlink') ? 'External' : '';
$this->writeRel($xmlWriter, $id++, "officeDocument/2006/relationships/{$type}", $target, $targetMode);
$this->writeRel($xmlWriter, $relId++, "officeDocument/2006/relationships/{$type}", $target, $targetMode);
}
}
@ -119,19 +119,19 @@ class Rels extends AbstractWriterPart
* <Relationship Id="rId..." Type="http://..." Target="....xml" TargetMode="..." />
*
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param int $id Relationship ID
* @param int $relId Relationship ID
* @param string $type Relationship type
* @param string $target Relationship target
* @param string $targetMode Relationship target mode
*/
private function writeRel(XMLWriter $xmlWriter, $id, $type, $target, $targetMode = '')
private function writeRel(XMLWriter $xmlWriter, $relId, $type, $target, $targetMode = '')
{
if ($type != '' && $target != '') {
if (strpos($id, 'rId') === false) {
$id = 'rId' . $id;
if (strpos($relId, 'rId') === false) {
$relId = 'rId' . $relId;
}
$xmlWriter->startElement('Relationship');
$xmlWriter->writeAttribute('Id', $id);
$xmlWriter->writeAttribute('Id', $relId);
$xmlWriter->writeAttribute('Type', self::RELS_BASE . $type);
$xmlWriter->writeAttribute('Target', $target);
if ($targetMode != '') {

View File

@ -37,7 +37,6 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase
$object = new Paragraph();
$attributes = array(
'tabs' => null,
'widowControl' => true,
'keepNext' => false,
'keepLines' => false,
@ -96,7 +95,7 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase
{
$object = new Paragraph();
$object->setTabs(array(new Tab('left', 1550), new Tab('right', 5300)));
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Tabs', $object->getTabs());
$this->assertEquals(2, count($object->getTabs()));
}
/**