From c243a11e5756c0dfb00c1b4155d8283f60a29e1d Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 11 May 2014 18:57:58 +0700 Subject: [PATCH] Additional type checks in style writers --- src/PhpWord/Element/ListItemRun.php | 1 - src/PhpWord/Shared/XMLWriter.php | 8 ++++---- src/PhpWord/Style/Font.php | 2 +- src/PhpWord/Writer/HTML/Style/AbstractStyle.php | 4 ++-- src/PhpWord/Writer/Word2007/Style/Image.php | 10 ++++------ src/PhpWord/Writer/Word2007/Style/TextBox.php | 12 +++++++----- 6 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/PhpWord/Element/ListItemRun.php b/src/PhpWord/Element/ListItemRun.php index 1a52bd89..a6dbd932 100644 --- a/src/PhpWord/Element/ListItemRun.php +++ b/src/PhpWord/Element/ListItemRun.php @@ -17,7 +17,6 @@ namespace PhpOffice\PhpWord\Element; -use PhpOffice\PhpWord\Shared\String; use PhpOffice\PhpWord\Style\ListItem as ListItemStyle; use PhpOffice\PhpWord\Style\Paragraph; diff --git a/src/PhpWord/Shared/XMLWriter.php b/src/PhpWord/Shared/XMLWriter.php index a16fe0b4..fd1f1f60 100644 --- a/src/PhpWord/Shared/XMLWriter.php +++ b/src/PhpWord/Shared/XMLWriter.php @@ -153,14 +153,14 @@ class XMLWriter /** * Write element if ... * - * @param bool $condition + * @param bool|null $condition * @param string $element * @param string $attribute * @param string $value */ public function writeElementIf($condition, $element, $attribute = null, $value = null) { - if ($condition) { + if ($condition == true) { if (is_null($attribute)) { $this->xmlWriter->writeElement($element, $value); } else { @@ -174,13 +174,13 @@ class XMLWriter /** * Write attribute if ... * - * @param bool $condition + * @param bool|null $condition * @param string $attribute * @param string $value */ public function writeAttributeIf($condition, $attribute, $value) { - if ($condition) { + if ($condition == true) { $this->xmlWriter->writeAttribute($attribute, $value); } } diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 32874b2f..85ad52b9 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -651,7 +651,7 @@ class Font extends AbstractStyle /** * Toggle $target property to false when $source true * - * @param bool $target Target property + * @param bool|null $target Target property * @param bool $sourceValue */ private function toggleFalse(&$target, $sourceValue) diff --git a/src/PhpWord/Writer/HTML/Style/AbstractStyle.php b/src/PhpWord/Writer/HTML/Style/AbstractStyle.php index d25e7b6a..3b6d99c1 100644 --- a/src/PhpWord/Writer/HTML/Style/AbstractStyle.php +++ b/src/PhpWord/Writer/HTML/Style/AbstractStyle.php @@ -87,12 +87,12 @@ abstract class AbstractStyle /** * Get value if ... * - * @param bool $condition + * @param bool|null $condition * @param string $value * @return string */ protected function getValueIf($condition, $value) { - return $condition ? $value : ''; + return $condition == true ? $value : ''; } } diff --git a/src/PhpWord/Writer/Word2007/Style/Image.php b/src/PhpWord/Writer/Word2007/Style/Image.php index a891625b..0b25f4b9 100644 --- a/src/PhpWord/Writer/Word2007/Style/Image.php +++ b/src/PhpWord/Writer/Word2007/Style/Image.php @@ -42,7 +42,7 @@ class Image extends AbstractStyle if (!$style instanceof \PhpOffice\PhpWord\Style\Image) { return; } - $this->writeStyle(); + $this->writeStyle($style); } /** @@ -65,10 +65,9 @@ class Image extends AbstractStyle /** * Write style attribute */ - protected function writeStyle() + protected function writeStyle(ImageStyle $style) { $xmlWriter = $this->getXmlWriter(); - $style = $this->getStyle(); // Default style array $styleArray = array( @@ -77,7 +76,7 @@ class Image extends AbstractStyle 'mso-width-relative' => 'margin', 'mso-height-relative' => 'margin', ); - $styleArray = array_merge($styleArray, $this->getElementStyle()); + $styleArray = array_merge($styleArray, $this->getElementStyle($style)); // Absolute/relative positioning $positioning = $style->getPositioning(); @@ -123,9 +122,8 @@ class Image extends AbstractStyle * * @return array */ - private function getElementStyle() + private function getElementStyle(ImageStyle $style) { - $style = $this->getStyle(); $styles = array(); $styleValues = array( 'width' => $style->getWidth(), diff --git a/src/PhpWord/Writer/Word2007/Style/TextBox.php b/src/PhpWord/Writer/Word2007/Style/TextBox.php index 2bbe40e9..d8456ae8 100644 --- a/src/PhpWord/Writer/Word2007/Style/TextBox.php +++ b/src/PhpWord/Writer/Word2007/Style/TextBox.php @@ -35,8 +35,8 @@ class TextBox extends Image if (!$style instanceof \PhpOffice\PhpWord\Style\TextBox) { return; } - $this->writeStyle(); - $this->writeBorder(); + $this->writeStyle($style); + $this->writeBorder($style); } /** @@ -50,6 +50,9 @@ class TextBox extends Image return; } $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\TextBox) { + return; + } $relativePositions = array( TextBoxStyle::POSITION_RELATIVE_TO_MARGIN => 'margin', @@ -88,7 +91,7 @@ class TextBox extends Image public function writeInnerMargin() { $style = $this->getStyle(); - if (!$style->hasInnerMargins()) { + if (!$style instanceof \PhpOffice\PhpWord\Style\TextBox || !$style->hasInnerMargins()) { return; } @@ -100,10 +103,9 @@ class TextBox extends Image /** * Writer border */ - private function writeBorder() + private function writeBorder(TextBoxStyle $style) { $xmlWriter = $this->getXmlWriter(); - $style = $this->getStyle(); // Border size $borderSize = $style->getBorderSize();