From 46c41c5ac182f8eadc9493f4089ca740d034c6e4 Mon Sep 17 00:00:00 2001 From: Owen Leibman Date: Sun, 5 Jan 2020 15:05:00 -0800 Subject: [PATCH] More Scrutinizer Changes Still one report that I don't understand at all, and one I'm not sure of. --- src/PhpWord/Writer/ODText/Element/TextRun.php | 1 + src/PhpWord/Writer/ODText/Part/Content.php | 35 +++++++++++++++++-- src/PhpWord/Writer/ODText/Style/Paragraph.php | 3 +- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/PhpWord/Writer/ODText/Element/TextRun.php b/src/PhpWord/Writer/ODText/Element/TextRun.php index cde996f6..9a005773 100644 --- a/src/PhpWord/Writer/ODText/Element/TextRun.php +++ b/src/PhpWord/Writer/ODText/Element/TextRun.php @@ -33,6 +33,7 @@ class TextRun extends Text $element = $this->getElement(); $xmlWriter->startElement('text:p'); + /** @scrutinizer ignore-call */ $pStyle = $element->getParagraphStyle(); if (!is_string($pStyle)) { $pStyle = 'Normal'; diff --git a/src/PhpWord/Writer/ODText/Part/Content.php b/src/PhpWord/Writer/ODText/Part/Content.php index ea4c87d2..9d6a50c4 100644 --- a/src/PhpWord/Writer/ODText/Part/Content.php +++ b/src/PhpWord/Writer/ODText/Part/Content.php @@ -266,7 +266,7 @@ class Content extends AbstractPart $elements = $container->getElements(); foreach ($elements as $element) { if ($element instanceof TextRun) { - $this->getElementStyle($element, $paragraphStyleCount, $fontStyleCount); + $this->getElementStyleTextRun($element, $paragraphStyleCount); $this->getContainerStyle($element, $paragraphStyleCount, $fontStyleCount); } elseif ($element instanceof Text) { $this->getElementStyle($element, $paragraphStyleCount, $fontStyleCount); @@ -298,7 +298,7 @@ class Content extends AbstractPart /** * Get style of individual element * - * @param \PhpOffice\PhpWord\Element\Text|\PhpOffice\PhpWord\Element\TextRun $element + * @param \PhpOffice\PhpWord\Element\Text $element * @param int $paragraphStyleCount * @param int $fontStyleCount */ @@ -341,6 +341,37 @@ class Content extends AbstractPart } } + /** + * Get style of individual element + * + * @param \PhpOffice\PhpWord\Element\TextRun $element + * @param int $paragraphStyleCount + */ + private function getElementStyleTextRun(&$element, &$paragraphStyleCount) + { + $paragraphStyle = $element->getParagraphStyle(); + $phpWord = $this->getParentWriter()->getPhpWord(); + + if ($paragraphStyle instanceof Paragraph) { + // Paragraph + $name = $paragraphStyle->getStyleName(); + if (!$name) { + $paragraphStyleCount++; + $style = $phpWord->addParagraphStyle("P{$paragraphStyleCount}", $paragraphStyle); + $style->setAuto(); + $element->setParagraphStyle("P{$paragraphStyleCount}"); + } else { + $element->setParagraphStyle($name); + } + } else { + $paragraphStyleCount++; + $parstylename = "P$paragraphStyleCount" . "_$paragraphStyle"; + $style = $phpWord->addParagraphStyle($parstylename, $paragraphStyle); + $style->setAuto(); + $element->setParagraphStyle($parstylename); + } + } + /** * Finds all tracked changes * diff --git a/src/PhpWord/Writer/ODText/Style/Paragraph.php b/src/PhpWord/Writer/ODText/Style/Paragraph.php index be974e72..9d38de86 100644 --- a/src/PhpWord/Writer/ODText/Style/Paragraph.php +++ b/src/PhpWord/Writer/ODText/Style/Paragraph.php @@ -134,7 +134,8 @@ class Paragraph extends AbstractStyle //Indentation $indent = $style->getIndentation(); - if ($indent instanceof \PhpOffice\PhpWord\Style\Indentation) { + //if ($indent instanceof \PhpOffice\PhpWord\Style\Indentation) { + if (!empty($indent)) { $marg = $indent->getLeft(); $xmlWriter->writeAttributeIf($marg !== null, 'fo:margin-left', (string) ($marg / Converter::INCH_TO_TWIP) . 'in'); $marg = $indent->getRight();