Skip to content

Commit 51d69a4

Browse files
committed
Refactoring based on Scrutinizer recommendation
1 parent 0c1e47d commit 51d69a4

File tree

6 files changed

+69
-45
lines changed

6 files changed

+69
-45
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Image extends Element
3434
*/
3535
public function write()
3636
{
37-
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) {
37+
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Image) {
3838
return;
3939
}
4040

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,21 @@ public function write()
5858
$fontStyle = $styleWriter->write();
5959
}
6060

61+
$openingTags = '';
62+
$endingTags = '';
6163
if ($hasParagraphStyle) {
6264
$attribute = $pStyleIsObject ? 'style' : 'class';
63-
$html .= "<p {$attribute}=\"{$paragraphStyle}\">";
65+
$openingTags = "<p {$attribute}=\"{$paragraphStyle}\">";
66+
$endingTags = '</p>' . PHP_EOL;
6467
}
6568
if ($fontStyle) {
6669
$attribute = $fontStyleIsObject ? 'style' : 'class';
67-
$html .= "<span {$attribute}=\"{$fontStyle}\">";
68-
}
69-
$html .= htmlspecialchars($this->element->getText());
70-
if ($fontStyle) {
71-
$html .= '</span>';
72-
}
73-
if ($hasParagraphStyle) {
74-
$html .= '</p>' . PHP_EOL;
70+
$openingTags = $openingTags . "<span {$attribute}=\"{$fontStyle}\">";
71+
$endingTags = '</span>' . $endingTags;
7572
}
7673

74+
$html = $openingTags . htmlspecialchars($this->element->getText()) . $endingTags;
75+
7776
return $html;
7877
}
7978
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

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

20+
use PhpOffice\PhpWord\Element\Footnote as FootnoteElement;
21+
use PhpOffice\PhpWord\Element\TextRun as TextRunElement;
2022
use PhpOffice\PhpWord\Style\Paragraph;
2123
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
2224

@@ -34,7 +36,7 @@ class TextRun extends Element
3436
*/
3537
public function write()
3638
{
37-
if (!$this->element instanceof \PhpOffice\PhpWord\Element\TextRun) {
39+
if (!($this->element instanceof TextRunElement || $this->element instanceof FootnoteElement)) {
3840
return;
3941
}
4042

src/PhpWord/Writer/HTML/Style/Font.php

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,48 @@ class Font extends AbstractStyle
3434
*/
3535
public function write()
3636
{
37-
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Font)) {
37+
if (!$this->style instanceof \PhpOffice\PhpWord\Style\Font) {
3838
return;
3939
}
4040

41+
$font = $this->style->getName();
42+
$size = $this->style->getSize();
43+
$color = $this->style->getColor();
44+
$fgColor = $this->style->getFgColor();
45+
$underline = $this->style->getUnderline() != FontStyle::UNDERLINE_NONE;
46+
$lineThrough = $this->style->isStrikethrough() || $this->style->isDoubleStrikethrough();
47+
4148
$css = array();
42-
if (PhpWord::DEFAULT_FONT_NAME != $this->style->getName()) {
43-
$css['font-family'] = "'" . $this->style->getName() . "'";
44-
}
45-
if (PhpWord::DEFAULT_FONT_SIZE != $this->style->getSize()) {
46-
$css['font-size'] = $this->style->getSize() . 'pt';
47-
}
48-
if (PhpWord::DEFAULT_FONT_COLOR != $this->style->getColor()) {
49-
$css['color'] = '#' . $this->style->getColor();
50-
}
51-
$css['background'] = $this->style->getFgColor();
52-
if ($this->style->isBold()) {
53-
$css['font-weight'] = 'bold';
54-
}
55-
if ($this->style->isItalic()) {
56-
$css['font-style'] = 'italic';
57-
}
49+
50+
$css['font-family'] = $this->getValueIf($font != PhpWord::DEFAULT_FONT_NAME, "'{$font}'");
51+
$css['font-size'] = $this->getValueIf($size != PhpWord::DEFAULT_FONT_SIZE, "{$size}pt");
52+
$css['color'] = $this->getValueIf($color != PhpWord::DEFAULT_FONT_COLOR, "#{$color}");
53+
$css['background'] = $this->getValueIf($fgColor != '', $fgColor);
54+
$css['font-weight'] = $this->getValueIf($this->style->isBold(), 'bold');
55+
$css['font-style'] = $this->getValueIf($this->style->isItalic(), 'italic');
56+
57+
$css['text-decoration'] = '';
58+
$css['text-decoration'] .= $this->getValueIf($underline, 'underline ');
59+
$css['text-decoration'] .= $this->getValueIf($lineThrough, 'line-through ');
60+
5861
if ($this->style->isSuperScript()) {
5962
$css['vertical-align'] = 'super';
6063
} elseif ($this->style->isSubScript()) {
6164
$css['vertical-align'] = 'sub';
6265
}
63-
$css['text-decoration'] = '';
64-
if ($this->style->getUnderline() != FontStyle::UNDERLINE_NONE) {
65-
$css['text-decoration'] .= 'underline ';
66-
}
67-
if ($this->style->isStrikethrough()) {
68-
$css['text-decoration'] .= 'line-through ';
69-
}
7066

7167
return $this->assembleCss($css);
7268
}
69+
70+
/**
71+
* Get value if ...
72+
*
73+
* @param bool $condition
74+
* @param string $value
75+
* @return string
76+
*/
77+
private function getValueIf($condition, $value)
78+
{
79+
return $condition ? $value : '';
80+
}
7381
}

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
namespace PhpOffice\PhpWord\Writer\RTF\Element;
1919

2020
use PhpOffice\PhpWord\PhpWord;
21-
use PhpOffice\PhpWord\Style\Font as FontStyle;
2221
use PhpOffice\PhpWord\Style;
22+
use PhpOffice\PhpWord\Style\Font as FontStyle;
23+
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
2324

2425
/**
2526
* Text element RTF writer
@@ -51,15 +52,7 @@ public function write()
5152

5253
if ($paragraphStyle && !$this->withoutP) {
5354
if ($this->parentWriter->getLastParagraphStyle() != $this->element->getParagraphStyle()) {
54-
$rtfText .= '\pard\nowidctlpar';
55-
if ($paragraphStyle->getSpaceAfter() != null) {
56-
$rtfText .= '\sa' . $paragraphStyle->getSpaceAfter();
57-
}
58-
if ($paragraphStyle->getAlign() != null) {
59-
if ($paragraphStyle->getAlign() == 'center') {
60-
$rtfText .= '\qc';
61-
}
62-
}
55+
$rtfText .= $this->writeParagraphStyle($paragraphStyle);
6356
$this->parentWriter->setLastParagraphStyle($this->element->getParagraphStyle());
6457
} else {
6558
$this->parentWriter->setLastParagraphStyle();
@@ -85,6 +78,26 @@ public function write()
8578
return $rtfText;
8679
}
8780

81+
/**
82+
* Write paragraph style
83+
*
84+
* @return string
85+
*/
86+
private function writeParagraphStyle(ParagraphStyle $paragraphStyle)
87+
{
88+
$rtfText = '\pard\nowidctlpar';
89+
if ($paragraphStyle->getSpaceAfter() != null) {
90+
$rtfText .= '\sa' . $paragraphStyle->getSpaceAfter();
91+
}
92+
if ($paragraphStyle->getAlign() != null) {
93+
if ($paragraphStyle->getAlign() == 'center') {
94+
$rtfText .= '\qc';
95+
}
96+
}
97+
98+
return $rtfText;
99+
}
100+
88101
/**
89102
* Write font style beginning
90103
*

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ private function writeTitle($title, $writeFieldMark)
125125

126126
/**
127127
* Write style
128+
*
129+
* @param int $indent
128130
*/
129131
private function writeStyle($indent)
130132
{

0 commit comments

Comments
 (0)