Skip to content

Commit c28f28e

Browse files
committed
Refactoring for code quality improvement (based on Scrutinizer)
1 parent b7374f8 commit c28f28e

File tree

12 files changed

+278
-261
lines changed

12 files changed

+278
-261
lines changed

src/PhpWord/Element/AbstractContainer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ abstract class AbstractContainer extends AbstractElement
3939
*/
4040
protected function addElement(AbstractElement $element)
4141
{
42-
$type = get_class($element);
43-
$type = str_replace('PhpOffice\\PhpWord\\Element\\', '', $type);
42+
// $type = get_class($element);
43+
// $type = str_replace('PhpOffice\\PhpWord\\Element\\', '', $type);
4444
$element->setElementIndex($this->countElements() + 1);
4545
$element->setElementId();
4646
$element->setPhpWord($this->phpWord);
@@ -254,11 +254,11 @@ public function addFootnote($paragraphStyle = null, $elementName = 'Footnote')
254254
$addMethod = "add{$elementName}";
255255

256256
$element = new $elementClass($paragraphStyle);
257+
$element->setDocPart($docPart, $this->getDocPartId());
257258
if ($this->phpWord instanceof PhpWord) {
258259
$rId = $this->phpWord->$addMethod($element);
260+
$element->setRelationId($rId);
259261
}
260-
$element->setDocPart($docPart, $this->getDocPartId());
261-
$element->setRelationId($rId);
262262
$this->addElement($element);
263263

264264
return $element;

src/PhpWord/Element/Title.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function getStyle()
138138
*/
139139
public function setAnchor($anchor)
140140
{
141-
$this->anchor = $anchor;
141+
$anchor = null;
142142
}
143143

144144
/**

src/PhpWord/Style/TOC.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class TOC extends Tab
4545
*/
4646
public function __construct()
4747
{
48-
parent::__construct(self::TAB_STOP_RIGHT, 9062, self::TABLEADER_DOT);
48+
parent::__construct(self::TAB_STOP_RIGHT, 9062, self::TAB_LEADER_DOT);
4949
}
5050

5151
/**
@@ -83,7 +83,7 @@ public function getTabLeader()
8383
*
8484
* @param string $value
8585
*/
86-
public function setTabLeader($value = self::TABLEADER_DOT)
86+
public function setTabLeader($value = self::TAB_LEADER_DOT)
8787
{
8888
$this->setLeader($value);
8989
}

src/PhpWord/Template.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function cloneRow($search, $numberOfClones)
192192

193193
// Check if there's a cell spanning multiple rows.
194194
if (preg_match('#<w:vMerge w:val="restart"/>#', $xmlRow)) {
195-
$extraRowStart = $rowEnd;
195+
// $extraRowStart = $rowEnd;
196196
$extraRowEnd = $rowEnd;
197197
while (true) {
198198
$extraRowStart = $this->findRowStart($extraRowEnd + 1);

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

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
namespace PhpOffice\PhpWord\Writer\RTF\Element;
1919

2020
use PhpOffice\PhpWord\PhpWord;
21-
use PhpOffice\PhpWord\Style\Font;
21+
use PhpOffice\PhpWord\Style\Font as FontStyle;
2222
use PhpOffice\PhpWord\Style;
2323

2424
/**
@@ -64,56 +64,81 @@ public function write()
6464
$this->parentWriter->setLastParagraphStyle();
6565
}
6666

67-
if ($fontStyle instanceof Font) {
68-
if ($fontStyle->getColor() != null) {
69-
$idxColor = array_search($fontStyle->getColor(), $this->parentWriter->getColorTable());
70-
if ($idxColor !== false) {
71-
$rtfText .= '\cf' . ($idxColor + 1);
72-
}
73-
} else {
74-
$rtfText .= '\cf0';
75-
}
76-
if ($fontStyle->getName() != null) {
77-
$idxFont = array_search($fontStyle->getName(), $this->parentWriter->getFontTable());
78-
if ($idxFont !== false) {
79-
$rtfText .= '\f' . $idxFont;
80-
}
81-
} else {
82-
$rtfText .= '\f0';
83-
}
84-
if ($fontStyle->isBold()) {
85-
$rtfText .= '\b';
86-
}
87-
if ($fontStyle->isItalic()) {
88-
$rtfText .= '\i';
89-
}
90-
if ($fontStyle->getSize()) {
91-
$rtfText .= '\fs' . ($fontStyle->getSize() * 2);
92-
}
67+
if ($fontStyle instanceof FontStyle) {
68+
$rtfText .= $this->writeFontStyleBegin($fontStyle);
9369
}
9470
if ($this->parentWriter->getLastParagraphStyle() != '' || $fontStyle) {
9571
$rtfText .= ' ';
9672
}
9773
$rtfText .= $this->element->getText();
74+
if ($fontStyle instanceof FontStyle) {
75+
$rtfText .= $this->writeFontStyleEnd($fontStyle);
76+
}
77+
if (!$this->withoutP) {
78+
$rtfText .= '\par' . PHP_EOL;
79+
}
9880

99-
if ($fontStyle instanceof Font) {
100-
$rtfText .= '\cf0';
101-
$rtfText .= '\f0';
81+
return $rtfText;
82+
}
10283

103-
if ($fontStyle->isBold()) {
104-
$rtfText .= '\b0';
105-
}
106-
if ($fontStyle->isItalic()) {
107-
$rtfText .= '\i0';
84+
/**
85+
* Write font style beginning
86+
*
87+
* @return string
88+
*/
89+
private function writeFontStyleBegin(FontStyle $style)
90+
{
91+
$rtfText = '';
92+
if ($style->getColor() != null) {
93+
$idxColor = array_search($style->getColor(), $this->parentWriter->getColorTable());
94+
if ($idxColor !== false) {
95+
$rtfText .= '\cf' . ($idxColor + 1);
10896
}
109-
if ($fontStyle->getSize()) {
110-
$rtfText .= '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2);
97+
} else {
98+
$rtfText .= '\cf0';
99+
}
100+
if ($style->getName() != null) {
101+
$idxFont = array_search($style->getName(), $this->parentWriter->getFontTable());
102+
if ($idxFont !== false) {
103+
$rtfText .= '\f' . $idxFont;
111104
}
105+
} else {
106+
$rtfText .= '\f0';
107+
}
108+
if ($style->isBold()) {
109+
$rtfText .= '\b';
110+
}
111+
if ($style->isItalic()) {
112+
$rtfText .= '\i';
113+
}
114+
if ($style->getSize()) {
115+
$rtfText .= '\fs' . ($style->getSize() * 2);
112116
}
113117

114-
if (!$this->withoutP) {
115-
$rtfText .= '\par' . PHP_EOL;
118+
return $rtfText;
119+
}
120+
121+
/**
122+
* Write font style ending
123+
*
124+
* @return string
125+
*/
126+
private function writeFontStyleEnd(FontStyle $style)
127+
{
128+
$rtfText = '';
129+
$rtfText .= '\cf0';
130+
$rtfText .= '\f0';
131+
132+
if ($style->isBold()) {
133+
$rtfText .= '\b0';
134+
}
135+
if ($style->isItalic()) {
136+
$rtfText .= '\i0';
116137
}
138+
if ($style->getSize()) {
139+
$rtfText .= '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2);
140+
}
141+
117142
return $rtfText;
118143
}
119144
}

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

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

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

20-
use PhpOffice\PhpWord\Style\Cell;
20+
use PhpOffice\PhpWord\Element\Cell as CellElement;
21+
use PhpOffice\PhpWord\Element\Row as RowElement;
22+
use PhpOffice\PhpWord\Style\Cell as CellStyle;
2123
use PhpOffice\PhpWord\Style\Table as TableStyle;
2224
use PhpOffice\PhpWord\Writer\Word2007\Style\Cell as CellStyleWriter;
2325
use PhpOffice\PhpWord\Writer\Word2007\Style\Table as TableStyleWriter;
@@ -89,51 +91,66 @@ public function write()
8991

9092
// Table rows
9193
for ($i = 0; $i < $rowCount; $i++) {
92-
$row = $rows[$i];
93-
$height = $row->getHeight();
94-
$rowStyle = $row->getStyle();
94+
$this->writeRow($rows[$i]);
95+
}
96+
$this->xmlWriter->endElement();
97+
}
98+
}
9599

96-
$this->xmlWriter->startElement('w:tr');
97-
if (!is_null($height) || $rowStyle->isTblHeader() || $rowStyle->isCantSplit()) {
98-
$this->xmlWriter->startElement('w:trPr');
99-
if (!is_null($height)) {
100-
$this->xmlWriter->startElement('w:trHeight');
101-
$this->xmlWriter->writeAttribute('w:val', $height);
102-
$this->xmlWriter->writeAttribute('w:hRule', ($rowStyle->isExactHeight() ? 'exact' : 'atLeast'));
103-
$this->xmlWriter->endElement();
104-
}
105-
if ($rowStyle->isTblHeader()) {
106-
$this->xmlWriter->startElement('w:tblHeader');
107-
$this->xmlWriter->writeAttribute('w:val', '1');
108-
$this->xmlWriter->endElement();
109-
}
110-
if ($rowStyle->isCantSplit()) {
111-
$this->xmlWriter->startElement('w:cantSplit');
112-
$this->xmlWriter->writeAttribute('w:val', '1');
113-
$this->xmlWriter->endElement();
114-
}
115-
$this->xmlWriter->endElement();
116-
}
117-
foreach ($row->getCells() as $cell) {
118-
$cellStyle = $cell->getStyle();
119-
$width = $cell->getWidth();
120-
$this->xmlWriter->startElement('w:tc');
121-
$this->xmlWriter->startElement('w:tcPr');
122-
$this->xmlWriter->startElement('w:tcW');
123-
$this->xmlWriter->writeAttribute('w:w', $width);
124-
$this->xmlWriter->writeAttribute('w:type', 'dxa');
125-
$this->xmlWriter->endElement(); // w:tcW
126-
if ($cellStyle instanceof Cell) {
127-
$styleWriter = new CellStyleWriter($this->xmlWriter, $cellStyle);
128-
$styleWriter->write();
129-
}
130-
$this->xmlWriter->endElement(); // w:tcPr
131-
$this->parentWriter->writeContainerElements($this->xmlWriter, $cell);
132-
$this->xmlWriter->endElement(); // w:tc
133-
}
134-
$this->xmlWriter->endElement(); // w:tr
100+
/**
101+
* Write row
102+
*/
103+
private function writeRow(RowElement $row)
104+
{
105+
$height = $row->getHeight();
106+
$rowStyle = $row->getStyle();
107+
108+
$this->xmlWriter->startElement('w:tr');
109+
if (!is_null($height) || $rowStyle->isTblHeader() || $rowStyle->isCantSplit()) {
110+
$this->xmlWriter->startElement('w:trPr');
111+
if (!is_null($height)) {
112+
$this->xmlWriter->startElement('w:trHeight');
113+
$this->xmlWriter->writeAttribute('w:val', $height);
114+
$this->xmlWriter->writeAttribute('w:hRule', ($rowStyle->isExactHeight() ? 'exact' : 'atLeast'));
115+
$this->xmlWriter->endElement();
116+
}
117+
if ($rowStyle->isTblHeader()) {
118+
$this->xmlWriter->startElement('w:tblHeader');
119+
$this->xmlWriter->writeAttribute('w:val', '1');
120+
$this->xmlWriter->endElement();
121+
}
122+
if ($rowStyle->isCantSplit()) {
123+
$this->xmlWriter->startElement('w:cantSplit');
124+
$this->xmlWriter->writeAttribute('w:val', '1');
125+
$this->xmlWriter->endElement();
135126
}
136127
$this->xmlWriter->endElement();
137128
}
129+
foreach ($row->getCells() as $cell) {
130+
$this->writeCell($cell);
131+
}
132+
$this->xmlWriter->endElement(); // w:tr
133+
}
134+
135+
/**
136+
* Write cell
137+
*/
138+
private function writeCell(CellElement $cell)
139+
{
140+
$cellStyle = $cell->getStyle();
141+
142+
$this->xmlWriter->startElement('w:tc');
143+
$this->xmlWriter->startElement('w:tcPr');
144+
$this->xmlWriter->startElement('w:tcW');
145+
$this->xmlWriter->writeAttribute('w:w', $cell->getWidth());
146+
$this->xmlWriter->writeAttribute('w:type', 'dxa');
147+
$this->xmlWriter->endElement(); // w:tcW
148+
if ($cellStyle instanceof CellStyle) {
149+
$styleWriter = new CellStyleWriter($this->xmlWriter, $cellStyle);
150+
$styleWriter->write();
151+
}
152+
$this->xmlWriter->endElement(); // w:tcPr
153+
$this->parentWriter->writeContainerElements($this->xmlWriter, $cell);
154+
$this->xmlWriter->endElement(); // w:tc
138155
}
139156
}

0 commit comments

Comments
 (0)