From 51d69a44c6987d8c87e4a83a581da8dd5a730c64 Mon Sep 17 00:00:00 2001
From: Ivan Lanin
Date: Wed, 7 May 2014 01:11:56 +0700
Subject: [PATCH] Refactoring based on Scrutinizer recommendation
---
src/PhpWord/Writer/HTML/Element/Image.php | 2 +-
src/PhpWord/Writer/HTML/Element/Text.php | 17 +++----
src/PhpWord/Writer/HTML/Element/TextRun.php | 4 +-
src/PhpWord/Writer/HTML/Style/Font.php | 56 ++++++++++++---------
src/PhpWord/Writer/RTF/Element/Text.php | 33 ++++++++----
src/PhpWord/Writer/Word2007/Element/TOC.php | 2 +
6 files changed, 69 insertions(+), 45 deletions(-)
diff --git a/src/PhpWord/Writer/HTML/Element/Image.php b/src/PhpWord/Writer/HTML/Element/Image.php
index 3813146d..93c10826 100644
--- a/src/PhpWord/Writer/HTML/Element/Image.php
+++ b/src/PhpWord/Writer/HTML/Element/Image.php
@@ -34,7 +34,7 @@ class Image extends Element
*/
public function write()
{
- if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) {
+ if (!$this->element instanceof \PhpOffice\PhpWord\Element\Image) {
return;
}
diff --git a/src/PhpWord/Writer/HTML/Element/Text.php b/src/PhpWord/Writer/HTML/Element/Text.php
index dceb7a4d..7130ea21 100644
--- a/src/PhpWord/Writer/HTML/Element/Text.php
+++ b/src/PhpWord/Writer/HTML/Element/Text.php
@@ -58,22 +58,21 @@ class Text extends Element
$fontStyle = $styleWriter->write();
}
+ $openingTags = '';
+ $endingTags = '';
if ($hasParagraphStyle) {
$attribute = $pStyleIsObject ? 'style' : 'class';
- $html .= "";
+ $openingTags = "
";
+ $endingTags = '
' . PHP_EOL;
}
if ($fontStyle) {
$attribute = $fontStyleIsObject ? 'style' : 'class';
- $html .= "";
- }
- $html .= htmlspecialchars($this->element->getText());
- if ($fontStyle) {
- $html .= '';
- }
- if ($hasParagraphStyle) {
- $html .= '
' . PHP_EOL;
+ $openingTags = $openingTags . "";
+ $endingTags = '' . $endingTags;
}
+ $html = $openingTags . htmlspecialchars($this->element->getText()) . $endingTags;
+
return $html;
}
}
diff --git a/src/PhpWord/Writer/HTML/Element/TextRun.php b/src/PhpWord/Writer/HTML/Element/TextRun.php
index b6848d8f..60415043 100644
--- a/src/PhpWord/Writer/HTML/Element/TextRun.php
+++ b/src/PhpWord/Writer/HTML/Element/TextRun.php
@@ -17,6 +17,8 @@
namespace PhpOffice\PhpWord\Writer\HTML\Element;
+use PhpOffice\PhpWord\Element\Footnote as FootnoteElement;
+use PhpOffice\PhpWord\Element\TextRun as TextRunElement;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
@@ -34,7 +36,7 @@ class TextRun extends Element
*/
public function write()
{
- if (!$this->element instanceof \PhpOffice\PhpWord\Element\TextRun) {
+ if (!($this->element instanceof TextRunElement || $this->element instanceof FootnoteElement)) {
return;
}
diff --git a/src/PhpWord/Writer/HTML/Style/Font.php b/src/PhpWord/Writer/HTML/Style/Font.php
index 7c8111f3..7ab4dc40 100644
--- a/src/PhpWord/Writer/HTML/Style/Font.php
+++ b/src/PhpWord/Writer/HTML/Style/Font.php
@@ -34,40 +34,48 @@ class Font extends AbstractStyle
*/
public function write()
{
- if (!($this->style instanceof \PhpOffice\PhpWord\Style\Font)) {
+ if (!$this->style instanceof \PhpOffice\PhpWord\Style\Font) {
return;
}
+ $font = $this->style->getName();
+ $size = $this->style->getSize();
+ $color = $this->style->getColor();
+ $fgColor = $this->style->getFgColor();
+ $underline = $this->style->getUnderline() != FontStyle::UNDERLINE_NONE;
+ $lineThrough = $this->style->isStrikethrough() || $this->style->isDoubleStrikethrough();
+
$css = array();
- if (PhpWord::DEFAULT_FONT_NAME != $this->style->getName()) {
- $css['font-family'] = "'" . $this->style->getName() . "'";
- }
- if (PhpWord::DEFAULT_FONT_SIZE != $this->style->getSize()) {
- $css['font-size'] = $this->style->getSize() . 'pt';
- }
- if (PhpWord::DEFAULT_FONT_COLOR != $this->style->getColor()) {
- $css['color'] = '#' . $this->style->getColor();
- }
- $css['background'] = $this->style->getFgColor();
- if ($this->style->isBold()) {
- $css['font-weight'] = 'bold';
- }
- if ($this->style->isItalic()) {
- $css['font-style'] = 'italic';
- }
+
+ $css['font-family'] = $this->getValueIf($font != PhpWord::DEFAULT_FONT_NAME, "'{$font}'");
+ $css['font-size'] = $this->getValueIf($size != PhpWord::DEFAULT_FONT_SIZE, "{$size}pt");
+ $css['color'] = $this->getValueIf($color != PhpWord::DEFAULT_FONT_COLOR, "#{$color}");
+ $css['background'] = $this->getValueIf($fgColor != '', $fgColor);
+ $css['font-weight'] = $this->getValueIf($this->style->isBold(), 'bold');
+ $css['font-style'] = $this->getValueIf($this->style->isItalic(), 'italic');
+
+ $css['text-decoration'] = '';
+ $css['text-decoration'] .= $this->getValueIf($underline, 'underline ');
+ $css['text-decoration'] .= $this->getValueIf($lineThrough, 'line-through ');
+
if ($this->style->isSuperScript()) {
$css['vertical-align'] = 'super';
} elseif ($this->style->isSubScript()) {
$css['vertical-align'] = 'sub';
}
- $css['text-decoration'] = '';
- if ($this->style->getUnderline() != FontStyle::UNDERLINE_NONE) {
- $css['text-decoration'] .= 'underline ';
- }
- if ($this->style->isStrikethrough()) {
- $css['text-decoration'] .= 'line-through ';
- }
return $this->assembleCss($css);
}
+
+ /**
+ * Get value if ...
+ *
+ * @param bool $condition
+ * @param string $value
+ * @return string
+ */
+ private function getValueIf($condition, $value)
+ {
+ return $condition ? $value : '';
+ }
}
diff --git a/src/PhpWord/Writer/RTF/Element/Text.php b/src/PhpWord/Writer/RTF/Element/Text.php
index 8b71b390..f4225ead 100644
--- a/src/PhpWord/Writer/RTF/Element/Text.php
+++ b/src/PhpWord/Writer/RTF/Element/Text.php
@@ -18,8 +18,9 @@
namespace PhpOffice\PhpWord\Writer\RTF\Element;
use PhpOffice\PhpWord\PhpWord;
-use PhpOffice\PhpWord\Style\Font as FontStyle;
use PhpOffice\PhpWord\Style;
+use PhpOffice\PhpWord\Style\Font as FontStyle;
+use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
/**
* Text element RTF writer
@@ -51,15 +52,7 @@ class Text extends Element
if ($paragraphStyle && !$this->withoutP) {
if ($this->parentWriter->getLastParagraphStyle() != $this->element->getParagraphStyle()) {
- $rtfText .= '\pard\nowidctlpar';
- if ($paragraphStyle->getSpaceAfter() != null) {
- $rtfText .= '\sa' . $paragraphStyle->getSpaceAfter();
- }
- if ($paragraphStyle->getAlign() != null) {
- if ($paragraphStyle->getAlign() == 'center') {
- $rtfText .= '\qc';
- }
- }
+ $rtfText .= $this->writeParagraphStyle($paragraphStyle);
$this->parentWriter->setLastParagraphStyle($this->element->getParagraphStyle());
} else {
$this->parentWriter->setLastParagraphStyle();
@@ -85,6 +78,26 @@ class Text extends Element
return $rtfText;
}
+ /**
+ * Write paragraph style
+ *
+ * @return string
+ */
+ private function writeParagraphStyle(ParagraphStyle $paragraphStyle)
+ {
+ $rtfText = '\pard\nowidctlpar';
+ if ($paragraphStyle->getSpaceAfter() != null) {
+ $rtfText .= '\sa' . $paragraphStyle->getSpaceAfter();
+ }
+ if ($paragraphStyle->getAlign() != null) {
+ if ($paragraphStyle->getAlign() == 'center') {
+ $rtfText .= '\qc';
+ }
+ }
+
+ return $rtfText;
+ }
+
/**
* Write font style beginning
*
diff --git a/src/PhpWord/Writer/Word2007/Element/TOC.php b/src/PhpWord/Writer/Word2007/Element/TOC.php
index e4542bcb..6f1e2cdf 100644
--- a/src/PhpWord/Writer/Word2007/Element/TOC.php
+++ b/src/PhpWord/Writer/Word2007/Element/TOC.php
@@ -125,6 +125,8 @@ class TOC extends Element
/**
* Write style
+ *
+ * @param int $indent
*/
private function writeStyle($indent)
{