Skip to content

Commit 7ae8c3c

Browse files
committed
Refactoring: Word2007 table and marginborder style writer
1 parent 8745c5e commit 7ae8c3c

File tree

9 files changed

+191
-143
lines changed

9 files changed

+191
-143
lines changed

phpmd.xml.dist

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
<rule ref="rulesets/design.xml/EvalExpression" />
1010
<rule ref="rulesets/design.xml/GotoStatement" />
1111
<rule ref="rulesets/design.xml/DepthOfInheritance" />
12-
<rule ref="rulesets/design.xml/CouplingBetweenObjects" />
12+
<rule ref="rulesets/design.xml/CouplingBetweenObjects">
13+
<!-- AbstractContainer needs more coupling (default: 13) -->
14+
<properties>
15+
<property name="minimum" value="20" />
16+
</properties>
17+
</rule>
1318
<rule ref="rulesets/design.xml/NumberOfChildren">
14-
<!-- AbstractElement and AbstractStyle still needs a lot of children classes -->
19+
<!-- AbstractStyle needs more children (default: 15) -->
1520
<properties>
1621
<property name="minimum" value="20" />
1722
</properties>

src/PhpWord/Settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Settings
9595
/**
9696
* Measurement unit
9797
*
98-
* @var string
98+
* @var int|float
9999
*/
100100
private static $measurementUnit = self::UNIT_TWIP;
101101

src/PhpWord/Style/AbstractStyle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function setStyleByArray($styles = array())
138138
/**
139139
* Set default for null and empty value
140140
*
141-
* @param mixed $value
141+
* @param string $value
142142
* @param mixed $default
143143
* @return mixed
144144
*/
@@ -186,7 +186,7 @@ protected function setNumericVal($value, $default = null)
186186
/**
187187
* Set integer value: Convert string that contains only numeric into integer
188188
*
189-
* @param mixed $value
189+
* @param int|null $value
190190
* @param int|null $default
191191
* @return int|null
192192
*/

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public function write()
4747
$content = '';
4848

4949
$elements = $container->getElements();
50-
$elementClass = '';
5150
foreach ($elements as $element) {
5251
$elementClass = get_class($element);
5352
$writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, $elementClass);

src/PhpWord/Writer/RTF/Element/Text.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private function writeParagraphStyle(TextElement $element)
9999
/**
100100
* Write font style beginning
101101
*
102-
* @param mixed $style
102+
* @param \PhpOffice\PhpWord\Style\Font $style
103103
* @return string
104104
*/
105105
private function writeFontStyle($style)

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

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use PhpOffice\PhpWord\Element\Cell as CellElement;
2121
use PhpOffice\PhpWord\Element\Row as RowElement;
22+
use PhpOffice\PhpWord\Element\Table as TableElement;
2223
use PhpOffice\PhpWord\Shared\XMLWriter;
2324
use PhpOffice\PhpWord\Style\Cell as CellStyle;
2425
use PhpOffice\PhpWord\Style\Row as RowStyle;
@@ -51,59 +52,54 @@ public function write()
5152
if ($rowCount > 0) {
5253
$xmlWriter->startElement('w:tbl');
5354

54-
// Table grid
55-
$cellWidths = array();
55+
// Write columns
56+
$this->writeColumns($xmlWriter, $element);
57+
58+
// Write style
59+
$styleWriter = new TableStyleWriter($xmlWriter, $element->getStyle());
60+
$styleWriter->setWidth($element->getWidth());
61+
$styleWriter->write();
62+
63+
// Write rows
5664
for ($i = 0; $i < $rowCount; $i++) {
57-
$row = $rows[$i];
58-
$cells = $row->getCells();
59-
if (count($cells) <= count($cellWidths)) {
60-
continue;
61-
}
62-
$cellWidths = array();
63-
foreach ($cells as $cell) {
64-
$cellWidths[] = $cell->getWidth();
65-
}
65+
$this->writeRow($xmlWriter, $rows[$i]);
6666
}
67-
$xmlWriter->startElement('w:tblGrid');
68-
foreach ($cellWidths as $width) {
69-
$xmlWriter->startElement('w:gridCol');
70-
if (!is_null($width)) {
71-
$xmlWriter->writeAttribute('w:w', $width);
72-
$xmlWriter->writeAttribute('w:type', 'dxa');
73-
}
74-
$xmlWriter->endElement();
67+
68+
$xmlWriter->endElement(); // w:tbl
69+
}
70+
}
71+
72+
/**
73+
* Write column
74+
*/
75+
private function writeColumns(XMLWriter $xmlWriter, TableElement $element)
76+
{
77+
$rows = $element->getRows();
78+
$rowCount = count($rows);
79+
80+
$cellWidths = array();
81+
for ($i = 0; $i < $rowCount; $i++) {
82+
$row = $rows[$i];
83+
$cells = $row->getCells();
84+
if (count($cells) <= count($cellWidths)) {
85+
continue;
7586
}
76-
$xmlWriter->endElement(); // w:tblGrid
77-
78-
// Table style
79-
$tblStyle = $element->getStyle();
80-
$tblWidth = $element->getWidth();
81-
if ($tblStyle instanceof TableStyle) {
82-
$styleWriter = new TableStyleWriter($xmlWriter, $tblStyle);
83-
$styleWriter->setIsFullStyle(false);
84-
$styleWriter->write();
85-
} else {
86-
if (!empty($tblStyle)) {
87-
$xmlWriter->startElement('w:tblPr');
88-
$xmlWriter->startElement('w:tblStyle');
89-
$xmlWriter->writeAttribute('w:val', $tblStyle);
90-
$xmlWriter->endElement();
91-
if (!is_null($tblWidth)) {
92-
$xmlWriter->startElement('w:tblW');
93-
$xmlWriter->writeAttribute('w:w', $tblWidth);
94-
$xmlWriter->writeAttribute('w:type', 'pct');
95-
$xmlWriter->endElement();
96-
}
97-
$xmlWriter->endElement();
98-
}
87+
$cellWidths = array();
88+
foreach ($cells as $cell) {
89+
$cellWidths[] = $cell->getWidth();
9990
}
91+
}
10092

101-
// Table rows
102-
for ($i = 0; $i < $rowCount; $i++) {
103-
$this->writeRow($xmlWriter, $rows[$i]);
93+
$xmlWriter->startElement('w:tblGrid');
94+
foreach ($cellWidths as $width) {
95+
$xmlWriter->startElement('w:gridCol');
96+
if ($width !== null) {
97+
$xmlWriter->writeAttribute('w:w', $width);
98+
$xmlWriter->writeAttribute('w:type', 'dxa');
10499
}
105100
$xmlWriter->endElement();
106101
}
102+
$xmlWriter->endElement(); // w:tblGrid
107103
}
108104

109105
/**

src/PhpWord/Writer/Word2007/Style/MarginBorder.php

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
1919

20+
use PhpOffice\PhpWord\Shared\XMLWriter;
21+
2022
/**
2123
* Margin border style writer
2224
*
@@ -56,29 +58,45 @@ public function write()
5658
$sizeCount = count($this->sizes) - 1;
5759

5860
for ($i = 0; $i < $sizeCount; $i++) {
59-
if (!is_null($this->sizes[$i])) {
60-
$xmlWriter->startElement('w:' . $sides[$i]);
61-
if (!empty($this->colors)) {
62-
if (is_null($this->colors[$i]) && !empty($this->attributes)) {
63-
if (array_key_exists('defaultColor', $this->attributes)) {
64-
$this->colors[$i] = $this->attributes['defaultColor'];
65-
}
66-
}
67-
$xmlWriter->writeAttribute('w:val', 'single');
68-
$xmlWriter->writeAttribute('w:sz', $this->sizes[$i]);
69-
$xmlWriter->writeAttribute('w:color', $this->colors[$i]);
70-
if (!empty($this->attributes)) {
71-
if (array_key_exists('space', $this->attributes)) {
72-
$xmlWriter->writeAttribute('w:space', $this->attributes['space']);
73-
}
74-
}
75-
} else {
76-
$xmlWriter->writeAttribute('w:w', $this->sizes[$i]);
77-
$xmlWriter->writeAttribute('w:type', 'dxa');
61+
if ($this->sizes[$i] !== null) {
62+
$color = null;
63+
if (isset($this->colors[$i])) {
64+
$color = $this->colors[$i];
65+
}
66+
$this->writeSide($xmlWriter, $sides[$i], $this->sizes[$i], $color);
67+
}
68+
}
69+
}
70+
71+
/**
72+
* Write side
73+
*
74+
* @param string $side
75+
* @param int $width
76+
* @param string $color
77+
*/
78+
private function writeSide(XMLWriter $xmlWriter, $side, $width, $color = null)
79+
{
80+
$xmlWriter->startElement('w:' . $side);
81+
if (!empty($this->colors)) {
82+
if ($color === null && !empty($this->attributes)) {
83+
if (array_key_exists('defaultColor', $this->attributes)) {
84+
$color = $this->attributes['defaultColor'];
85+
}
86+
}
87+
$xmlWriter->writeAttribute('w:val', 'single');
88+
$xmlWriter->writeAttribute('w:sz', $width);
89+
$xmlWriter->writeAttribute('w:color', $color);
90+
if (!empty($this->attributes)) {
91+
if (array_key_exists('space', $this->attributes)) {
92+
$xmlWriter->writeAttribute('w:space', $this->attributes['space']);
7893
}
79-
$xmlWriter->endElement();
8094
}
95+
} else {
96+
$xmlWriter->writeAttribute('w:w', $width);
97+
$xmlWriter->writeAttribute('w:type', 'dxa');
8198
}
99+
$xmlWriter->endElement();
82100
}
83101

84102
/**

src/PhpWord/Writer/Word2007/Style/Row.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
namespace PhpOffice\PhpWord\Writer\Word2007\Style;
1919

20-
use PhpOffice\PhpWord\Style\Row as RowStyle;
21-
2220
/**
2321
* Row style writer
2422
*

0 commit comments

Comments
 (0)