Skip to content

Commit 246557e

Browse files
committed
Reactivate PHPMD and fix some rules for textbox
1 parent 6f0579c commit 246557e

File tree

6 files changed

+40
-23
lines changed

6 files changed

+40
-23
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ script:
5252
## PHP Copy/Paste Detector
5353
- php phpcpd.phar src/ tests/ --verbose
5454
## PHP Mess Detector
55-
#- phpmd src/,tests/ text unusedcode,naming,design,controversial --exclude pclzip.lib.php
55+
- phpmd src/,tests/ text unusedcode,naming,design,controversial --exclude pclzip.lib.php
5656
## PHPLOC
5757
#- php phploc.phar src/
5858
## PHPUnit

samples/Sample_25_TextBox.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,29 @@
66
$phpWord = new \PhpOffice\PhpWord\PhpWord();
77

88
$section = $phpWord->addSection();
9-
$textbox = $section->addTextBox(array('align' => 'left', 'width' => 300, 'borderSize' => 1, 'borderColor' => '#FF0000'));
10-
$textbox->addText('Text box content ');
11-
$textbox->addText('with bold text', array('bold' => true));
12-
$textbox->addText(', ');
13-
$textbox->addLink('http://www.google.com', 'link');
14-
$textbox->addText(', and image ');
15-
$textbox->addImage('resources/_earth.jpg', array('width' => 18, 'height' => 18));
16-
$textbox->addText('.');
9+
10+
// In section
11+
$textbox = $section->addTextBox(array('align' => 'left', 'width' => 400, 'borderSize' => 1, 'borderColor' => '#FF0000'));
12+
$textbox->addText('Text box content in section.');
13+
$textbox->addText('Another line.');
14+
15+
// Inside table
16+
$section->addTextBreak(2);
17+
$cell = $section->addTable()->addRow()->addCell(300);
18+
$textbox = $cell->addTextBox(array('borderSize' => 1, 'borderColor' => '#0000FF', 'innerMargin' => 100));
19+
$textbox->addText('Inside table');
20+
21+
// Inside header with textrun
22+
$header = $section->addHeader();
23+
$textbox = $header->addTextBox(array('align' => 'center', 'width' => 600, 'borderSize' => 1, 'borderColor' => '#00FF00'));
24+
$textrun = $textbox->addTextRun();
25+
$textrun->addText('TextBox in header. TextBox can contain a TextRun ');
26+
$textrun->addText('with bold text', array('bold' => true));
27+
$textrun->addText(', ');
28+
$textrun->addLink('http://www.google.com', 'link');
29+
$textrun->addText(', and image ');
30+
$textrun->addImage('resources/_earth.jpg', array('width' => 18, 'height' => 18));
31+
$textrun->addText('.');
1732

1833
// Save file
1934
echo write($phpWord, basename(__FILE__, '.php'), $writers);

src/PhpWord/Element/AbstractContainer.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function addText($text, $fontStyle = null, $paragraphStyle = null, $eleme
100100
*/
101101
public function addTextRun($paragraphStyle = null)
102102
{
103-
$this->checkValidity('Textrun');
103+
$this->checkValidity('TextRun');
104104

105105
$element = new TextRun($paragraphStyle);
106106
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
@@ -327,10 +327,10 @@ private function checkValidity($method)
327327
'TextBreak' => $allContainers,
328328
'Image' => $allContainers,
329329
'Object' => $allContainers,
330-
'TextRun' => array('section', 'header', 'footer', 'cell'),
331-
'ListItem' => array('section', 'header', 'footer', 'cell'),
330+
'TextRun' => array('section', 'header', 'footer', 'cell', 'textbox'),
331+
'ListItem' => array('section', 'header', 'footer', 'cell', 'textbox'),
332332
'CheckBox' => array('section', 'header', 'footer', 'cell'),
333-
'TextBox' => array('section', 'header', 'footer'),
333+
'TextBox' => array('section', 'header', 'footer', 'cell'),
334334
'Footnote' => array('section', 'textrun', 'cell'),
335335
'Endnote' => array('section', 'textrun', 'cell'),
336336
'PreserveText' => array('header', 'footer', 'cell'),
@@ -346,7 +346,7 @@ private function checkValidity($method)
346346
// Check if a method is valid for current container
347347
if (array_key_exists($method, $validContainers)) {
348348
if (!in_array($this->container, $validContainers[$method])) {
349-
throw new \BadMethodCallException();
349+
throw new \BadMethodCallException("Cannot put $method in $this->container.");
350350
}
351351
}
352352
// Check if a method is valid for current container, located in other container
@@ -356,7 +356,7 @@ private function checkValidity($method)
356356
$allowedDocParts = $rules[1];
357357
foreach ($containers as $container) {
358358
if ($this->container == $container && !in_array($this->getDocPart(), $allowedDocParts)) {
359-
throw new \BadMethodCallException();
359+
throw new \BadMethodCallException("Cannot put $method in $this->container.");
360360
}
361361
}
362362
}
@@ -369,11 +369,12 @@ private function checkValidity($method)
369369
*/
370370
private function checkElementDocPart()
371371
{
372-
$isCellTextrun = in_array($this->container, array('cell', 'textrun', 'textbox'));
373-
$docPart = $isCellTextrun ? $this->getDocPart() : $this->container;
374-
$docPartId = $isCellTextrun ? $this->getDocPartId() : $this->sectionId;
372+
$inOtherPart = in_array($this->container, array('cell', 'textrun', 'textbox'));
373+
$docPart = $inOtherPart ? $this->getDocPart() : $this->container;
374+
$docPartId = $inOtherPart ? $this->getDocPartId() : $this->sectionId;
375375
$inHeaderFooter = ($docPart == 'header' || $docPart == 'footer');
376376
$docPartId = $inHeaderFooter ? $this->getDocPartId() : $docPartId;
377+
377378
return $inHeaderFooter ? $docPart . $docPartId : $docPart;
378379
}
379380

src/PhpWord/Style/TextBox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public function getInnerMargin()
177177
public function hasInnerMargins()
178178
{
179179
$hasInnerMargins = false;
180-
$margins = $this->getInnerMargins();
180+
$margins = $this->getInnerMargin();
181181
for ($i = 0; $i < count($margins); $i++) {
182182
if (!is_null($margins[$i])) {
183183
$hasInnerMargins = true;

src/PhpWord/Writer/Word2007/Element/Container.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public function write()
4242
$container = $this->getElement();
4343

4444
// Loop through subelements
45-
$containerClass = substr(get_class($container), strrpos(get_class($this), '\\') + 1);
45+
$containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
4646
$subelements = $container->getElements();
47-
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote', 'TextBox')) ? true : false;
47+
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false;
4848
if (count($subelements) > 0) {
4949
foreach ($subelements as $subelement) {
5050
$writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, get_class($subelement));

src/PhpWord/Writer/Word2007/Element/TextBox.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@ public function write()
5454
$styleWriter->write();
5555
$xmlWriter->startElement('v:textbox');
5656
$styleWriter->writeInnerMargin();
57+
58+
// TextBox content, serving as a container
5759
$xmlWriter->startElement('w:txbxContent');
58-
$xmlWriter->startElement('w:p');
5960
$containerWriter = new Container($xmlWriter, $element);
6061
$containerWriter->write();
61-
$xmlWriter->endElement(); // w:p
6262
$xmlWriter->endElement(); // w:txbxContent
63+
6364
$xmlWriter->endElement(); // v: textbox
6465
$styleWriter->writeW10Wrap();
6566
$xmlWriter->endElement(); // v:shape

0 commit comments

Comments
 (0)