Reactivate PHPMD and fix some rules for textbox

This commit is contained in:
Ivan Lanin 2014-05-09 02:09:20 +07:00
parent 6f0579c78f
commit 246557e3be
6 changed files with 40 additions and 23 deletions

View File

@ -52,7 +52,7 @@ script:
## PHP Copy/Paste Detector ## PHP Copy/Paste Detector
- php phpcpd.phar src/ tests/ --verbose - php phpcpd.phar src/ tests/ --verbose
## PHP Mess Detector ## PHP Mess Detector
#- phpmd src/,tests/ text unusedcode,naming,design,controversial --exclude pclzip.lib.php - phpmd src/,tests/ text unusedcode,naming,design,controversial --exclude pclzip.lib.php
## PHPLOC ## PHPLOC
#- php phploc.phar src/ #- php phploc.phar src/
## PHPUnit ## PHPUnit

View File

@ -6,14 +6,29 @@ echo date('H:i:s') , ' Create new PhpWord object' , EOL;
$phpWord = new \PhpOffice\PhpWord\PhpWord(); $phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection(); $section = $phpWord->addSection();
$textbox = $section->addTextBox(array('align' => 'left', 'width' => 300, 'borderSize' => 1, 'borderColor' => '#FF0000'));
$textbox->addText('Text box content '); // In section
$textbox->addText('with bold text', array('bold' => true)); $textbox = $section->addTextBox(array('align' => 'left', 'width' => 400, 'borderSize' => 1, 'borderColor' => '#FF0000'));
$textbox->addText(', '); $textbox->addText('Text box content in section.');
$textbox->addLink('http://www.google.com', 'link'); $textbox->addText('Another line.');
$textbox->addText(', and image ');
$textbox->addImage('resources/_earth.jpg', array('width' => 18, 'height' => 18)); // Inside table
$textbox->addText('.'); $section->addTextBreak(2);
$cell = $section->addTable()->addRow()->addCell(300);
$textbox = $cell->addTextBox(array('borderSize' => 1, 'borderColor' => '#0000FF', 'innerMargin' => 100));
$textbox->addText('Inside table');
// Inside header with textrun
$header = $section->addHeader();
$textbox = $header->addTextBox(array('align' => 'center', 'width' => 600, 'borderSize' => 1, 'borderColor' => '#00FF00'));
$textrun = $textbox->addTextRun();
$textrun->addText('TextBox in header. TextBox can contain a TextRun ');
$textrun->addText('with bold text', array('bold' => true));
$textrun->addText(', ');
$textrun->addLink('http://www.google.com', 'link');
$textrun->addText(', and image ');
$textrun->addImage('resources/_earth.jpg', array('width' => 18, 'height' => 18));
$textrun->addText('.');
// Save file // Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers); echo write($phpWord, basename(__FILE__, '.php'), $writers);

View File

@ -100,7 +100,7 @@ abstract class AbstractContainer extends AbstractElement
*/ */
public function addTextRun($paragraphStyle = null) public function addTextRun($paragraphStyle = null)
{ {
$this->checkValidity('Textrun'); $this->checkValidity('TextRun');
$element = new TextRun($paragraphStyle); $element = new TextRun($paragraphStyle);
$element->setDocPart($this->getDocPart(), $this->getDocPartId()); $element->setDocPart($this->getDocPart(), $this->getDocPartId());
@ -327,10 +327,10 @@ abstract class AbstractContainer extends AbstractElement
'TextBreak' => $allContainers, 'TextBreak' => $allContainers,
'Image' => $allContainers, 'Image' => $allContainers,
'Object' => $allContainers, 'Object' => $allContainers,
'TextRun' => array('section', 'header', 'footer', 'cell'), 'TextRun' => array('section', 'header', 'footer', 'cell', 'textbox'),
'ListItem' => array('section', 'header', 'footer', 'cell'), 'ListItem' => array('section', 'header', 'footer', 'cell', 'textbox'),
'CheckBox' => array('section', 'header', 'footer', 'cell'), 'CheckBox' => array('section', 'header', 'footer', 'cell'),
'TextBox' => array('section', 'header', 'footer'), 'TextBox' => array('section', 'header', 'footer', 'cell'),
'Footnote' => array('section', 'textrun', 'cell'), 'Footnote' => array('section', 'textrun', 'cell'),
'Endnote' => array('section', 'textrun', 'cell'), 'Endnote' => array('section', 'textrun', 'cell'),
'PreserveText' => array('header', 'footer', 'cell'), 'PreserveText' => array('header', 'footer', 'cell'),
@ -346,7 +346,7 @@ abstract class AbstractContainer extends AbstractElement
// Check if a method is valid for current container // Check if a method is valid for current container
if (array_key_exists($method, $validContainers)) { if (array_key_exists($method, $validContainers)) {
if (!in_array($this->container, $validContainers[$method])) { if (!in_array($this->container, $validContainers[$method])) {
throw new \BadMethodCallException(); throw new \BadMethodCallException("Cannot put $method in $this->container.");
} }
} }
// Check if a method is valid for current container, located in other container // Check if a method is valid for current container, located in other container
@ -356,7 +356,7 @@ abstract class AbstractContainer extends AbstractElement
$allowedDocParts = $rules[1]; $allowedDocParts = $rules[1];
foreach ($containers as $container) { foreach ($containers as $container) {
if ($this->container == $container && !in_array($this->getDocPart(), $allowedDocParts)) { if ($this->container == $container && !in_array($this->getDocPart(), $allowedDocParts)) {
throw new \BadMethodCallException(); throw new \BadMethodCallException("Cannot put $method in $this->container.");
} }
} }
} }
@ -369,11 +369,12 @@ abstract class AbstractContainer extends AbstractElement
*/ */
private function checkElementDocPart() private function checkElementDocPart()
{ {
$isCellTextrun = in_array($this->container, array('cell', 'textrun', 'textbox')); $inOtherPart = in_array($this->container, array('cell', 'textrun', 'textbox'));
$docPart = $isCellTextrun ? $this->getDocPart() : $this->container; $docPart = $inOtherPart ? $this->getDocPart() : $this->container;
$docPartId = $isCellTextrun ? $this->getDocPartId() : $this->sectionId; $docPartId = $inOtherPart ? $this->getDocPartId() : $this->sectionId;
$inHeaderFooter = ($docPart == 'header' || $docPart == 'footer'); $inHeaderFooter = ($docPart == 'header' || $docPart == 'footer');
$docPartId = $inHeaderFooter ? $this->getDocPartId() : $docPartId; $docPartId = $inHeaderFooter ? $this->getDocPartId() : $docPartId;
return $inHeaderFooter ? $docPart . $docPartId : $docPart; return $inHeaderFooter ? $docPart . $docPartId : $docPart;
} }

View File

@ -177,7 +177,7 @@ class TextBox extends Image
public function hasInnerMargins() public function hasInnerMargins()
{ {
$hasInnerMargins = false; $hasInnerMargins = false;
$margins = $this->getInnerMargins(); $margins = $this->getInnerMargin();
for ($i = 0; $i < count($margins); $i++) { for ($i = 0; $i < count($margins); $i++) {
if (!is_null($margins[$i])) { if (!is_null($margins[$i])) {
$hasInnerMargins = true; $hasInnerMargins = true;

View File

@ -42,9 +42,9 @@ class Container extends AbstractElement
$container = $this->getElement(); $container = $this->getElement();
// Loop through subelements // Loop through subelements
$containerClass = substr(get_class($container), strrpos(get_class($this), '\\') + 1); $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
$subelements = $container->getElements(); $subelements = $container->getElements();
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote', 'TextBox')) ? true : false; $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false;
if (count($subelements) > 0) { if (count($subelements) > 0) {
foreach ($subelements as $subelement) { foreach ($subelements as $subelement) {
$writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, get_class($subelement)); $writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, get_class($subelement));

View File

@ -54,12 +54,13 @@ class TextBox extends AbstractElement
$styleWriter->write(); $styleWriter->write();
$xmlWriter->startElement('v:textbox'); $xmlWriter->startElement('v:textbox');
$styleWriter->writeInnerMargin(); $styleWriter->writeInnerMargin();
// TextBox content, serving as a container
$xmlWriter->startElement('w:txbxContent'); $xmlWriter->startElement('w:txbxContent');
$xmlWriter->startElement('w:p');
$containerWriter = new Container($xmlWriter, $element); $containerWriter = new Container($xmlWriter, $element);
$containerWriter->write(); $containerWriter->write();
$xmlWriter->endElement(); // w:p
$xmlWriter->endElement(); // w:txbxContent $xmlWriter->endElement(); // w:txbxContent
$xmlWriter->endElement(); // v: textbox $xmlWriter->endElement(); // v: textbox
$styleWriter->writeW10Wrap(); $styleWriter->writeW10Wrap();
$xmlWriter->endElement(); // v:shape $xmlWriter->endElement(); // v:shape