Skip to content

Commit c4e8fda

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

File tree

4 files changed

+165
-124
lines changed

4 files changed

+165
-124
lines changed

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-
$anchor = null;
141+
$this->anchor = $anchor;
142142
}
143143

144144
/**

src/PhpWord/Style/Table.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,4 +441,40 @@ public function setShading($value = null)
441441

442442
return $this;
443443
}
444+
445+
/**
446+
* Has borders?
447+
*
448+
* @return bool
449+
*/
450+
public function hasBorders()
451+
{
452+
$hasBorders = false;
453+
$borders = $this->getBorderSize();
454+
for ($i = 0; $i < 6; $i++) {
455+
if (!is_null($borders[$i])) {
456+
$hasBorders = true;
457+
}
458+
}
459+
460+
return $hasBorders;
461+
}
462+
463+
/**
464+
* Has margins?
465+
*
466+
* @return bool
467+
*/
468+
public function hasMargins()
469+
{
470+
$hasMargins = false;
471+
$margins = $this->getCellMargin();
472+
for ($i = 0; $i < 4; $i++) {
473+
if (!is_null($margins[$i])) {
474+
$hasMargins = true;
475+
}
476+
}
477+
478+
return $hasMargins;
479+
}
444480
}

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

Lines changed: 120 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -35,125 +35,154 @@ class TOC extends Element
3535
public function write()
3636
{
3737
$titles = $this->element->getTitles();
38+
$writeFieldMark = true;
39+
40+
foreach ($titles as $title) {
41+
$this->writeTitle($title, $writeFieldMark);
42+
if ($writeFieldMark) {
43+
$writeFieldMark = false;
44+
}
45+
}
46+
47+
$this->xmlWriter->startElement('w:p');
48+
$this->xmlWriter->startElement('w:r');
49+
$this->xmlWriter->startElement('w:fldChar');
50+
$this->xmlWriter->writeAttribute('w:fldCharType', 'end');
51+
$this->xmlWriter->endElement();
52+
$this->xmlWriter->endElement();
53+
$this->xmlWriter->endElement();
54+
}
55+
56+
/**
57+
* Write title
58+
*/
59+
private function writeTitle($title, $writeFieldMark)
60+
{
3861
$tocStyle = $this->element->getStyleTOC();
3962
$fontStyle = $this->element->getStyleFont();
4063
$isObject = ($fontStyle instanceof Font) ? true : false;
64+
$anchor = '_Toc' . ($title->getBookmarkId() + 252634154);
65+
$indent = ($title->getDepth() - 1) * $tocStyle->getIndent();
4166

42-
$tocFieldWritten = false;
43-
foreach ($titles as $title) {
44-
$indent = ($title->getDepth() - 1) * $tocStyle->getIndent();
45-
$anchor = '_Toc' . ($title->getBookmarkId() + 252634154);
67+
$this->xmlWriter->startElement('w:p');
4668

47-
$this->xmlWriter->startElement('w:p');
69+
// Write style and field mark
70+
$this->writeStyle($indent);
71+
if ($writeFieldMark) {
72+
$this->writeFieldMark();
73+
}
4874

49-
// Style
50-
$this->xmlWriter->startElement('w:pPr');
75+
// Hyperlink
76+
$this->xmlWriter->startElement('w:hyperlink');
77+
$this->xmlWriter->writeAttribute('w:anchor', $anchor);
78+
$this->xmlWriter->writeAttribute('w:history', '1');
5179

52-
// Paragraph
53-
if ($isObject && !is_null($fontStyle->getParagraphStyle())) {
54-
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $fontStyle->getParagraphStyle());
55-
$styleWriter->write();
56-
}
80+
// Title text
81+
$this->xmlWriter->startElement('w:r');
82+
if ($isObject) {
83+
$styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
84+
$styleWriter->write();
85+
}
86+
$this->xmlWriter->startElement('w:t');
87+
$this->xmlWriter->writeRaw($title->getText());
88+
$this->xmlWriter->endElement();
89+
$this->xmlWriter->endElement(); // w:r
5790

58-
// Font
59-
if (!empty($fontStyle) && !$isObject) {
60-
$this->xmlWriter->startElement('w:rPr');
61-
$this->xmlWriter->startElement('w:rStyle');
62-
$this->xmlWriter->writeAttribute('w:val', $fontStyle);
63-
$this->xmlWriter->endElement();
64-
$this->xmlWriter->endElement(); // w:rPr
65-
}
91+
$this->xmlWriter->startElement('w:r');
92+
$this->xmlWriter->writeElement('w:tab', null);
93+
$this->xmlWriter->endElement();
6694

67-
// Tab
68-
$this->xmlWriter->startElement('w:tabs');
69-
$styleWriter = new TabStyleWriter($this->xmlWriter, $tocStyle);
70-
$styleWriter->write();
71-
$this->xmlWriter->endElement();
95+
$this->xmlWriter->startElement('w:r');
96+
$this->xmlWriter->startElement('w:fldChar');
97+
$this->xmlWriter->writeAttribute('w:fldCharType', 'begin');
98+
$this->xmlWriter->endElement();
99+
$this->xmlWriter->endElement();
72100

73-
// Indent
74-
if ($indent > 0) {
75-
$this->xmlWriter->startElement('w:ind');
76-
$this->xmlWriter->writeAttribute('w:left', $indent);
77-
$this->xmlWriter->endElement();
78-
}
101+
$this->xmlWriter->startElement('w:r');
102+
$this->xmlWriter->startElement('w:instrText');
103+
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
104+
$this->xmlWriter->writeRaw('PAGEREF ' . $anchor . ' \h');
105+
$this->xmlWriter->endElement();
106+
$this->xmlWriter->endElement();
79107

80-
$this->xmlWriter->endElement(); // w:pPr
81-
82-
if ($tocFieldWritten !== true) {
83-
$tocFieldWritten = true;
84-
$minDepth = $this->element->getMinDepth();
85-
$maxDepth = $this->element->getMaxDepth();
86-
87-
$this->xmlWriter->startElement('w:r');
88-
$this->xmlWriter->startElement('w:fldChar');
89-
$this->xmlWriter->writeAttribute('w:fldCharType', 'begin');
90-
$this->xmlWriter->endElement();
91-
$this->xmlWriter->endElement();
92-
93-
$this->xmlWriter->startElement('w:r');
94-
$this->xmlWriter->startElement('w:instrText');
95-
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
96-
$this->xmlWriter->writeRaw("TOC \o {$minDepth}-{$maxDepth} \h \z \u");
97-
$this->xmlWriter->endElement();
98-
$this->xmlWriter->endElement();
99-
100-
$this->xmlWriter->startElement('w:r');
101-
$this->xmlWriter->startElement('w:fldChar');
102-
$this->xmlWriter->writeAttribute('w:fldCharType', 'separate');
103-
$this->xmlWriter->endElement();
104-
$this->xmlWriter->endElement();
105-
}
108+
$this->xmlWriter->startElement('w:r');
109+
$this->xmlWriter->startElement('w:fldChar');
110+
$this->xmlWriter->writeAttribute('w:fldCharType', 'end');
111+
$this->xmlWriter->endElement();
112+
$this->xmlWriter->endElement();
106113

107-
$this->xmlWriter->startElement('w:hyperlink');
108-
$this->xmlWriter->writeAttribute('w:anchor', $anchor);
109-
$this->xmlWriter->writeAttribute('w:history', '1');
114+
$this->xmlWriter->endElement(); // w:hyperlink
110115

111-
$this->xmlWriter->startElement('w:r');
116+
$this->xmlWriter->endElement(); // w:p
117+
}
112118

113-
if ($isObject) {
114-
$styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
115-
$styleWriter->write();
116-
}
119+
/**
120+
* Write style
121+
*/
122+
private function writeStyle($indent)
123+
{
124+
$tocStyle = $this->element->getStyleTOC();
125+
$fontStyle = $this->element->getStyleFont();
126+
$isObject = ($fontStyle instanceof Font) ? true : false;
117127

118-
$this->xmlWriter->startElement('w:t');
119-
$this->xmlWriter->writeRaw($title->getText());
120-
$this->xmlWriter->endElement();
121-
$this->xmlWriter->endElement();
128+
$this->xmlWriter->startElement('w:pPr');
122129

123-
$this->xmlWriter->startElement('w:r');
124-
$this->xmlWriter->writeElement('w:tab', null);
125-
$this->xmlWriter->endElement();
130+
// Paragraph
131+
if ($isObject && !is_null($fontStyle->getParagraphStyle())) {
132+
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $fontStyle->getParagraphStyle());
133+
$styleWriter->write();
134+
}
126135

127-
$this->xmlWriter->startElement('w:r');
128-
$this->xmlWriter->startElement('w:fldChar');
129-
$this->xmlWriter->writeAttribute('w:fldCharType', 'begin');
130-
$this->xmlWriter->endElement();
136+
// Font
137+
if (!empty($fontStyle) && !$isObject) {
138+
$this->xmlWriter->startElement('w:rPr');
139+
$this->xmlWriter->startElement('w:rStyle');
140+
$this->xmlWriter->writeAttribute('w:val', $fontStyle);
131141
$this->xmlWriter->endElement();
142+
$this->xmlWriter->endElement(); // w:rPr
143+
}
132144

133-
$this->xmlWriter->startElement('w:r');
134-
$this->xmlWriter->startElement('w:instrText');
135-
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
136-
$this->xmlWriter->writeRaw('PAGEREF ' . $anchor . ' \h');
137-
$this->xmlWriter->endElement();
138-
$this->xmlWriter->endElement();
145+
// Tab
146+
$this->xmlWriter->startElement('w:tabs');
147+
$styleWriter = new TabStyleWriter($this->xmlWriter, $tocStyle);
148+
$styleWriter->write();
149+
$this->xmlWriter->endElement();
139150

140-
$this->xmlWriter->startElement('w:r');
141-
$this->xmlWriter->startElement('w:fldChar');
142-
$this->xmlWriter->writeAttribute('w:fldCharType', 'end');
143-
$this->xmlWriter->endElement();
151+
// Indent
152+
if ($indent > 0) {
153+
$this->xmlWriter->startElement('w:ind');
154+
$this->xmlWriter->writeAttribute('w:left', $indent);
144155
$this->xmlWriter->endElement();
156+
}
145157

146-
$this->xmlWriter->endElement(); // w:hyperlink
158+
$this->xmlWriter->endElement(); // w:pPr
159+
}
147160

148-
$this->xmlWriter->endElement(); // w:p
149-
}
161+
/**
162+
* Write TOC Field
163+
*/
164+
private function writeFieldMark()
165+
{
166+
$minDepth = $this->element->getMinDepth();
167+
$maxDepth = $this->element->getMaxDepth();
150168

151-
$this->xmlWriter->startElement('w:p');
152169
$this->xmlWriter->startElement('w:r');
153170
$this->xmlWriter->startElement('w:fldChar');
154-
$this->xmlWriter->writeAttribute('w:fldCharType', 'end');
171+
$this->xmlWriter->writeAttribute('w:fldCharType', 'begin');
172+
$this->xmlWriter->endElement();
173+
$this->xmlWriter->endElement();
174+
175+
$this->xmlWriter->startElement('w:r');
176+
$this->xmlWriter->startElement('w:instrText');
177+
$this->xmlWriter->writeAttribute('xml:space', 'preserve');
178+
$this->xmlWriter->writeRaw("TOC \o {$minDepth}-{$maxDepth} \h \z \u");
155179
$this->xmlWriter->endElement();
156180
$this->xmlWriter->endElement();
181+
182+
$this->xmlWriter->startElement('w:r');
183+
$this->xmlWriter->startElement('w:fldChar');
184+
$this->xmlWriter->writeAttribute('w:fldCharType', 'separate');
185+
$this->xmlWriter->endElement();
157186
$this->xmlWriter->endElement();
158187
}
159188
}

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

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,23 @@ public function write()
4040
return;
4141
}
4242

43-
$brdCol = $this->style->getBorderColor();
44-
$brdSz = $this->style->getBorderSize();
45-
$cellMargin = $this->style->getCellMargin();
43+
$hasBorders = $this->style->hasBorders();
44+
$hasMargins = $this->style->hasMargins();
4645

47-
// If any of the borders/margins is set, process them
48-
$hasBorders = false;
49-
for ($i = 0; $i < 6; $i++) {
50-
if (!is_null($brdSz[$i])) {
51-
$hasBorders = true;
52-
break;
53-
}
54-
}
55-
$hasMargins = false;
56-
for ($i = 0; $i < 4; $i++) {
57-
if (!is_null($cellMargin[$i])) {
58-
$hasMargins = true;
59-
break;
60-
}
61-
}
6246
if ($hasMargins || $hasBorders) {
6347
$this->xmlWriter->startElement('w:tblPr');
6448
if ($hasMargins) {
6549
$mbWriter = new MarginBorder($this->xmlWriter);
66-
$mbWriter->setSizes($cellMargin);
50+
$mbWriter->setSizes($this->style->getCellMargin());
6751

6852
$this->xmlWriter->startElement('w:tblCellMar');
6953
$mbWriter->write();
7054
$this->xmlWriter->endElement(); // w:tblCellMar
7155
}
7256
if ($hasBorders) {
7357
$mbWriter = new MarginBorder($this->xmlWriter);
74-
$mbWriter->setSizes($brdSz);
75-
$mbWriter->setColors($brdCol);
58+
$mbWriter->setSizes($this->style->getBorderSize());
59+
$mbWriter->setColors($this->style->getBorderColor());
7660

7761
$this->xmlWriter->startElement('w:tblBorders');
7862
$mbWriter->write();
@@ -123,18 +107,10 @@ private function writeFirstRow(\PhpOffice\PhpWord\Style\Table $style, $type)
123107
}
124108

125109
// Borders
126-
$brdSz = $style->getBorderSize();
127-
$brdCol = $style->getBorderColor();
128-
$hasBorders = false;
129-
for ($i = 0; $i < 6; $i++) {
130-
if (!is_null($brdSz[$i])) {
131-
$hasBorders = true;
132-
}
133-
}
134-
if ($hasBorders) {
110+
if ($style->hasBorders()) {
135111
$mbWriter = new MarginBorder($this->xmlWriter);
136-
$mbWriter->setSizes($brdSz);
137-
$mbWriter->setColors($brdCol);
112+
$mbWriter->setSizes($style->getBorderSize());
113+
$mbWriter->setColors($style->getBorderColor());
138114

139115
$this->xmlWriter->startElement('w:tcBorders');
140116
$mbWriter->write();

0 commit comments

Comments
 (0)