From 17e2f0281796fc551580c8af2e31f89308ee648e Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Sun, 11 May 2014 11:23:46 +0700 Subject: [PATCH] Type checks on style writers --- src/PhpWord/Writer/HTML/Style/Font.php | 3 ++ src/PhpWord/Writer/HTML/Style/Image.php | 3 ++ src/PhpWord/Writer/HTML/Style/Paragraph.php | 3 ++ src/PhpWord/Writer/ODText/Style/Font.php | 3 +- src/PhpWord/Writer/ODText/Style/Paragraph.php | 3 +- src/PhpWord/Writer/Word2007/Style/Cell.php | 3 +- src/PhpWord/Writer/Word2007/Style/Font.php | 3 +- src/PhpWord/Writer/Word2007/Style/Image.php | 6 ++- .../Writer/Word2007/Style/Indentation.php | 3 +- .../Writer/Word2007/Style/LineNumbering.php | 3 +- .../Writer/Word2007/Style/MarginBorder.php | 4 +- .../Writer/Word2007/Style/Paragraph.php | 3 +- src/PhpWord/Writer/Word2007/Style/Section.php | 3 +- src/PhpWord/Writer/Word2007/Style/Shading.php | 3 +- src/PhpWord/Writer/Word2007/Style/Spacing.php | 3 +- src/PhpWord/Writer/Word2007/Style/Tab.php | 3 +- src/PhpWord/Writer/Word2007/Style/Table.php | 3 +- src/PhpWord/Writer/Word2007/Style/TextBox.php | 8 ++-- tests/PhpWord/Tests/Writer/HTML/StyleTest.php | 39 ++++++++++++++++ .../PhpWord/Tests/Writer/ODText/StyleTest.php | 41 +++++++++++++++++ tests/PhpWord/Tests/Writer/RTF/StyleTest.php | 39 ++++++++++++++++ .../Tests/Writer/Word2007/StyleTest.php | 44 +++++++++++++++++++ 22 files changed, 207 insertions(+), 19 deletions(-) create mode 100644 tests/PhpWord/Tests/Writer/HTML/StyleTest.php create mode 100644 tests/PhpWord/Tests/Writer/ODText/StyleTest.php create mode 100644 tests/PhpWord/Tests/Writer/RTF/StyleTest.php create mode 100644 tests/PhpWord/Tests/Writer/Word2007/StyleTest.php diff --git a/src/PhpWord/Writer/HTML/Style/Font.php b/src/PhpWord/Writer/HTML/Style/Font.php index 0c416e8f..33c23f7a 100644 --- a/src/PhpWord/Writer/HTML/Style/Font.php +++ b/src/PhpWord/Writer/HTML/Style/Font.php @@ -35,6 +35,9 @@ class Font extends AbstractStyle public function write() { $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Font) { + return; + } $css = array(); $font = $style->getName(); diff --git a/src/PhpWord/Writer/HTML/Style/Image.php b/src/PhpWord/Writer/HTML/Style/Image.php index cdb4f8a2..fcf18395 100644 --- a/src/PhpWord/Writer/HTML/Style/Image.php +++ b/src/PhpWord/Writer/HTML/Style/Image.php @@ -32,6 +32,9 @@ class Image extends AbstractStyle public function write() { $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Image) { + return; + } $css = array(); $css['width'] = $this->getValueIf($style->getWidth(), $style->getWidth() . 'px'); diff --git a/src/PhpWord/Writer/HTML/Style/Paragraph.php b/src/PhpWord/Writer/HTML/Style/Paragraph.php index 27618061..cc3ad785 100644 --- a/src/PhpWord/Writer/HTML/Style/Paragraph.php +++ b/src/PhpWord/Writer/HTML/Style/Paragraph.php @@ -34,6 +34,9 @@ class Paragraph extends AbstractStyle public function write() { $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Paragraph) { + return; + } $css = array(); // Alignment diff --git a/src/PhpWord/Writer/ODText/Style/Font.php b/src/PhpWord/Writer/ODText/Style/Font.php index 1a6d4fc0..eb96946c 100644 --- a/src/PhpWord/Writer/ODText/Style/Font.php +++ b/src/PhpWord/Writer/ODText/Style/Font.php @@ -34,7 +34,8 @@ class Font extends AbstractStyle */ public function write() { - if (is_null($style = $this->getStyle())) { + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Font) { return; } $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/ODText/Style/Paragraph.php b/src/PhpWord/Writer/ODText/Style/Paragraph.php index 143627cd..e5f52cfe 100644 --- a/src/PhpWord/Writer/ODText/Style/Paragraph.php +++ b/src/PhpWord/Writer/ODText/Style/Paragraph.php @@ -34,7 +34,8 @@ class Paragraph extends AbstractStyle */ public function write() { - if (is_null($style = $this->getStyle())) { + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Paragraph) { return; } $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/Style/Cell.php b/src/PhpWord/Writer/Word2007/Style/Cell.php index 76c347f0..c6e21625 100644 --- a/src/PhpWord/Writer/Word2007/Style/Cell.php +++ b/src/PhpWord/Writer/Word2007/Style/Cell.php @@ -31,7 +31,8 @@ class Cell extends AbstractStyle */ public function write() { - if (is_null($style = $this->getStyle())) { + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Cell) { return; } $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/Style/Font.php b/src/PhpWord/Writer/Word2007/Style/Font.php index 5965d5a4..851bca22 100644 --- a/src/PhpWord/Writer/Word2007/Style/Font.php +++ b/src/PhpWord/Writer/Word2007/Style/Font.php @@ -57,7 +57,8 @@ class Font extends AbstractStyle */ private function writeStyle() { - if (is_null($style = $this->getStyle())) { + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Font) { return; } $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/Style/Image.php b/src/PhpWord/Writer/Word2007/Style/Image.php index f9338759..a891625b 100644 --- a/src/PhpWord/Writer/Word2007/Style/Image.php +++ b/src/PhpWord/Writer/Word2007/Style/Image.php @@ -38,9 +38,11 @@ class Image extends AbstractStyle */ public function write() { - if (!is_null($this->getStyle())) { - $this->writeStyle(); + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Image) { + return; } + $this->writeStyle(); } /** diff --git a/src/PhpWord/Writer/Word2007/Style/Indentation.php b/src/PhpWord/Writer/Word2007/Style/Indentation.php index 797179b7..f9bb696f 100644 --- a/src/PhpWord/Writer/Word2007/Style/Indentation.php +++ b/src/PhpWord/Writer/Word2007/Style/Indentation.php @@ -29,7 +29,8 @@ class Indentation extends AbstractStyle */ public function write() { - if (is_null($style = $this->getStyle())) { + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Indentation) { return; } $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/Style/LineNumbering.php b/src/PhpWord/Writer/Word2007/Style/LineNumbering.php index c4c1216b..b3c103b6 100644 --- a/src/PhpWord/Writer/Word2007/Style/LineNumbering.php +++ b/src/PhpWord/Writer/Word2007/Style/LineNumbering.php @@ -31,7 +31,8 @@ class LineNumbering extends AbstractStyle */ public function write() { - if (is_null($style = $this->getStyle())) { + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\LineNumbering) { return; } $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/Style/MarginBorder.php b/src/PhpWord/Writer/Word2007/Style/MarginBorder.php index 8f7c1c00..28cf1224 100644 --- a/src/PhpWord/Writer/Word2007/Style/MarginBorder.php +++ b/src/PhpWord/Writer/Word2007/Style/MarginBorder.php @@ -29,14 +29,14 @@ class MarginBorder extends AbstractStyle * * @var int[] */ - private $sizes; + private $sizes = array(); /** * Colors * * @var string[] */ - private $colors; + private $colors = array(); /** * Other attributes diff --git a/src/PhpWord/Writer/Word2007/Style/Paragraph.php b/src/PhpWord/Writer/Word2007/Style/Paragraph.php index 7056028a..88a75845 100644 --- a/src/PhpWord/Writer/Word2007/Style/Paragraph.php +++ b/src/PhpWord/Writer/Word2007/Style/Paragraph.php @@ -66,7 +66,8 @@ class Paragraph extends AbstractStyle */ private function writeStyle() { - if (is_null($style = $this->getStyle())) { + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Paragraph) { return; } $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/Style/Section.php b/src/PhpWord/Writer/Word2007/Style/Section.php index b773564c..61658e03 100644 --- a/src/PhpWord/Writer/Word2007/Style/Section.php +++ b/src/PhpWord/Writer/Word2007/Style/Section.php @@ -31,7 +31,8 @@ class Section extends AbstractStyle */ public function write() { - if (is_null($style = $this->getStyle())) { + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Section) { return; } $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/Style/Shading.php b/src/PhpWord/Writer/Word2007/Style/Shading.php index cc7f9364..72054c20 100644 --- a/src/PhpWord/Writer/Word2007/Style/Shading.php +++ b/src/PhpWord/Writer/Word2007/Style/Shading.php @@ -29,7 +29,8 @@ class Shading extends AbstractStyle */ public function write() { - if (is_null($style = $this->getStyle())) { + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Shading) { return; } $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/Style/Spacing.php b/src/PhpWord/Writer/Word2007/Style/Spacing.php index b594d9c0..5dfb5c43 100644 --- a/src/PhpWord/Writer/Word2007/Style/Spacing.php +++ b/src/PhpWord/Writer/Word2007/Style/Spacing.php @@ -29,7 +29,8 @@ class Spacing extends AbstractStyle */ public function write() { - if (is_null($style = $this->getStyle())) { + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Spacing) { return; } $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/Style/Tab.php b/src/PhpWord/Writer/Word2007/Style/Tab.php index c15d1364..865a5a9c 100644 --- a/src/PhpWord/Writer/Word2007/Style/Tab.php +++ b/src/PhpWord/Writer/Word2007/Style/Tab.php @@ -29,7 +29,8 @@ class Tab extends AbstractStyle */ public function write() { - if (is_null($style = $this->getStyle())) { + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Tab) { return; } $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/Style/Table.php b/src/PhpWord/Writer/Word2007/Style/Table.php index 7c59fd03..0a0241c3 100644 --- a/src/PhpWord/Writer/Word2007/Style/Table.php +++ b/src/PhpWord/Writer/Word2007/Style/Table.php @@ -36,7 +36,8 @@ class Table extends AbstractStyle */ public function write() { - if (is_null($style = $this->getStyle())) { + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\Table) { return; } $xmlWriter = $this->getXmlWriter(); diff --git a/src/PhpWord/Writer/Word2007/Style/TextBox.php b/src/PhpWord/Writer/Word2007/Style/TextBox.php index bb9b3a24..2bbe40e9 100644 --- a/src/PhpWord/Writer/Word2007/Style/TextBox.php +++ b/src/PhpWord/Writer/Word2007/Style/TextBox.php @@ -31,10 +31,12 @@ class TextBox extends Image */ public function write() { - if (!is_null($this->getStyle())) { - $this->writeStyle(); - $this->writeBorder(); + $style = $this->getStyle(); + if (!$style instanceof \PhpOffice\PhpWord\Style\TextBox) { + return; } + $this->writeStyle(); + $this->writeBorder(); } /** diff --git a/tests/PhpWord/Tests/Writer/HTML/StyleTest.php b/tests/PhpWord/Tests/Writer/HTML/StyleTest.php new file mode 100644 index 00000000..8af1e479 --- /dev/null +++ b/tests/PhpWord/Tests/Writer/HTML/StyleTest.php @@ -0,0 +1,39 @@ +assertEquals('', $object->write()); + } + } +} diff --git a/tests/PhpWord/Tests/Writer/ODText/StyleTest.php b/tests/PhpWord/Tests/Writer/ODText/StyleTest.php new file mode 100644 index 00000000..387accfc --- /dev/null +++ b/tests/PhpWord/Tests/Writer/ODText/StyleTest.php @@ -0,0 +1,41 @@ +write(); + + $this->assertEquals('', $xmlWriter->getData()); + } + } +} diff --git a/tests/PhpWord/Tests/Writer/RTF/StyleTest.php b/tests/PhpWord/Tests/Writer/RTF/StyleTest.php new file mode 100644 index 00000000..8b4a4441 --- /dev/null +++ b/tests/PhpWord/Tests/Writer/RTF/StyleTest.php @@ -0,0 +1,39 @@ +assertEquals('', $object->write()); + } + } +} diff --git a/tests/PhpWord/Tests/Writer/Word2007/StyleTest.php b/tests/PhpWord/Tests/Writer/Word2007/StyleTest.php new file mode 100644 index 00000000..97a6c6bd --- /dev/null +++ b/tests/PhpWord/Tests/Writer/Word2007/StyleTest.php @@ -0,0 +1,44 @@ +write(); + + $this->assertEquals('', $xmlWriter->getData()); + } + } +}