Skip to content

Commit 0c1e47d

Browse files
committed
Type checking
1 parent c4e8fda commit 0c1e47d

29 files changed

+124
-7
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class Footnote extends Element
3838
*/
3939
public function write()
4040
{
41+
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Footnote) {
42+
return;
43+
}
44+
4145
$noteId = count($this->parentWriter->getNotes()) + 1;
4246
$noteMark = $this->noteType . '-' . $this->element->getRelationId();
4347
$this->parentWriter->addNote($noteId, $noteMark);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ class Image extends Element
3434
*/
3535
public function write()
3636
{
37-
$html = '';
38-
if (!$this->element instanceof ImageElement) {
39-
return $html;
37+
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) {
38+
return;
4039
}
40+
41+
$html = '';
4142
if (!$this->parentWriter->isPdf()) {
4243
$imageData = $this->getBase64ImageData($this->element);
4344
if (!is_null($imageData)) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class Link extends Element
3131
*/
3232
public function write()
3333
{
34+
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) {
35+
return;
36+
}
37+
3438
$html = "<a href=\"{$this->element->getTarget()}\">{$this->element->getText()}</a>" . PHP_EOL;
3539
if (!$this->withoutP) {
3640
$html = '<p>' . $html . '</p>' . PHP_EOL;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class ListItem extends Element
3131
*/
3232
public function write()
3333
{
34+
if (!$this->element instanceof \PhpOffice\PhpWord\Element\ListItem) {
35+
return;
36+
}
37+
3438
$text = htmlspecialchars($this->element->getTextObject()->getText());
3539
$html = '<p>' . $text . '</p>' . PHP_EOL;
3640

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class Table extends Element
3131
*/
3232
public function write()
3333
{
34+
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Table) {
35+
return;
36+
}
37+
3438
$html = '';
3539
$rows = $this->element->getRows();
3640
$rowCount = count($rows);

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class Text extends Element
3636
*/
3737
public function write()
3838
{
39+
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) {
40+
return;
41+
}
42+
3943
$html = '';
4044
// Paragraph style
4145
$paragraphStyle = $this->element->getParagraphStyle();
@@ -44,6 +48,8 @@ public function write()
4448
$styleWriter = new ParagraphStyleWriter($paragraphStyle);
4549
$paragraphStyle = $styleWriter->write();
4650
}
51+
$hasParagraphStyle = $paragraphStyle && !$this->withoutP;
52+
4753
// Font style
4854
$fontStyle = $this->element->getFontStyle();
4955
$fontStyleIsObject = ($fontStyle instanceof Font);
@@ -52,7 +58,7 @@ public function write()
5258
$fontStyle = $styleWriter->write();
5359
}
5460

55-
if ($paragraphStyle && !$this->withoutP) {
61+
if ($hasParagraphStyle) {
5662
$attribute = $pStyleIsObject ? 'style' : 'class';
5763
$html .= "<p {$attribute}=\"{$paragraphStyle}\">";
5864
}
@@ -64,7 +70,7 @@ public function write()
6470
if ($fontStyle) {
6571
$html .= '</span>';
6672
}
67-
if ($paragraphStyle && !$this->withoutP) {
73+
if ($hasParagraphStyle) {
6874
$html .= '</p>' . PHP_EOL;
6975
}
7076

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class TextRun extends Element
3434
*/
3535
public function write()
3636
{
37+
if (!$this->element instanceof \PhpOffice\PhpWord\Element\TextRun) {
38+
return;
39+
}
40+
3741
$html = '';
3842
$elements = $this->element->getElements();
3943
if (count($elements) > 0) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class Title extends Element
3131
*/
3232
public function write()
3333
{
34+
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Title) {
35+
return;
36+
}
37+
3438
$tag = 'h' . $this->element->getDepth();
3539
$text = htmlspecialchars($this->element->getText());
3640
$html = "<{$tag}>{$text}</{$tag}>" . PHP_EOL;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class Image extends Element
3131
*/
3232
public function write()
3333
{
34+
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Image) {
35+
return;
36+
}
37+
3438
$mediaIndex = $this->element->getMediaIndex();
3539
$target = 'Pictures/' . $this->element->getTarget();
3640
$style = $this->element->getStyle();

src/PhpWord/Writer/ODText/Element/Link.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class Link extends Element
2929
*/
3030
public function write()
3131
{
32+
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) {
33+
return;
34+
}
35+
3236
if (!$this->withoutP) {
3337
$this->xmlWriter->startElement('text:p'); // text:p
3438
}

0 commit comments

Comments
 (0)