From eafb11eacbaee70158c89e0c0ea889c32f9ff52b Mon Sep 17 00:00:00 2001 From: Erik Hazigton Date: Sun, 22 Sep 2024 16:14:49 +0200 Subject: [PATCH 1/5] Ensured that mt_rand() is passed as string to md5. Fixed that the setNewStyle() method could not return style objects as value arguments, when $returnObject is true, like in the Cell element. --- src/PhpWord/Element/AbstractElement.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/PhpWord/Element/AbstractElement.php b/src/PhpWord/Element/AbstractElement.php index 385e4d3140..89297152c3 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -23,6 +23,9 @@ use PhpOffice\PhpWord\Media; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Style; +use PhpOffice\PhpWord\Style\AbstractStyle; + +use function is_array; /** * Element abstract class. @@ -256,7 +259,7 @@ public function getElementId() */ public function setElementId(): void { - $this->elementId = substr(md5(mt_rand()), 0, 6); + $this->elementId = substr(md5((string) mt_rand()), 0, 6); } /** @@ -481,22 +484,24 @@ public function isInSection() /** * Set new style value. * - * @param mixed $styleObject Style object - * @param null|array|string|Style $styleValue Style value + * @param AbstractStyle $styleObject Style object + * @param null|string|array|AbstractStyle $styleValue Style value * @param bool $returnObject Always return object * - * @return mixed + * @return null|string|array|AbstractStyle */ protected function setNewStyle($styleObject, $styleValue = null, $returnObject = false) { - if (null !== $styleValue && is_array($styleValue)) { + if ($styleValue instanceof AbstractStyle && get_class($styleValue) === get_class($styleObject)) { + return clone $styleValue; + } + + if (is_array($styleValue)) { $styleObject->setStyleByArray($styleValue); - $style = $styleObject; - } else { - $style = $returnObject ? $styleObject : $styleValue; + return $styleObject; } - return $style; + return $returnObject === true ? $styleObject : $styleValue; } /** From 728e2f04da9193c80783f7e0d4ac6c23e7b3c04b Mon Sep 17 00:00:00 2001 From: Erik Hazigton Date: Sun, 22 Sep 2024 16:39:57 +0200 Subject: [PATCH 2/5] Updated change log, fixed coding style issues and ensured that the style value is returned and not a clone. --- docs/changes/1.x/1.4.0.md | 1 + src/PhpWord/Element/AbstractElement.php | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/docs/changes/1.x/1.4.0.md b/docs/changes/1.x/1.4.0.md index 8db845337d..51b2fa8bc1 100644 --- a/docs/changes/1.x/1.4.0.md +++ b/docs/changes/1.x/1.4.0.md @@ -11,6 +11,7 @@ - Writer ODText: Support for images inside a textRun by [@Progi1984](https://github.com/Progi1984) fixing [#2240](https://github.com/PHPOffice/PHPWord/issues/2240) in [#2668](https://github.com/PHPOffice/PHPWord/pull/2668) - Allow vAlign and vMerge on Style\Cell to be set to null by [@SpraxDev](https://github.com/SpraxDev) fixing [#2673](https://github.com/PHPOffice/PHPWord/issues/2673) in [#2676](https://github.com/PHPOffice/PHPWord/pull/2676) +- Fixed that passed style objects to the constructor of elements were ignored like `\PhpOffice\PhpWord\Style\Cell` for `\PhpOffice\PhpWord\Element\Cell` by [@hazington](https://github.com/hazington) ### Miscellaneous diff --git a/src/PhpWord/Element/AbstractElement.php b/src/PhpWord/Element/AbstractElement.php index 89297152c3..936d1044a7 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -19,13 +19,13 @@ use DateTime; use InvalidArgumentException; +use function is_array; use PhpOffice\PhpWord\Collection\Comments; use PhpOffice\PhpWord\Media; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Style; -use PhpOffice\PhpWord\Style\AbstractStyle; -use function is_array; +use PhpOffice\PhpWord\Style\AbstractStyle; /** * Element abstract class. @@ -485,19 +485,20 @@ public function isInSection() * Set new style value. * * @param AbstractStyle $styleObject Style object - * @param null|string|array|AbstractStyle $styleValue Style value + * @param null|AbstractStyle|array|string $styleValue Style value * @param bool $returnObject Always return object * - * @return null|string|array|AbstractStyle + * @return mixed */ protected function setNewStyle($styleObject, $styleValue = null, $returnObject = false) { if ($styleValue instanceof AbstractStyle && get_class($styleValue) === get_class($styleObject)) { - return clone $styleValue; + return $styleValue; } if (is_array($styleValue)) { $styleObject->setStyleByArray($styleValue); + return $styleObject; } From 74264fac707c4f968bc1491cef9db7386d4bb267 Mon Sep 17 00:00:00 2001 From: Erik Hazigton Date: Sun, 22 Sep 2024 16:46:28 +0200 Subject: [PATCH 3/5] Added unit for passing a cell style object to the cell element. --- src/PhpWord/Element/AbstractElement.php | 2 +- tests/PhpWordTests/Element/CellTest.php | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/PhpWord/Element/AbstractElement.php b/src/PhpWord/Element/AbstractElement.php index 936d1044a7..6e13c705a6 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -485,7 +485,7 @@ public function isInSection() * Set new style value. * * @param AbstractStyle $styleObject Style object - * @param null|AbstractStyle|array|string $styleValue Style value + * @param null|AbstractStyle|Style|array|string $styleValue Style value * @param bool $returnObject Always return object * * @return mixed diff --git a/tests/PhpWordTests/Element/CellTest.php b/tests/PhpWordTests/Element/CellTest.php index 700e16d58e..d9181a9328 100644 --- a/tests/PhpWordTests/Element/CellTest.php +++ b/tests/PhpWordTests/Element/CellTest.php @@ -50,6 +50,17 @@ public function testConstructWithStyleArray(): void self::assertNull($oCell->getWidth()); } + /** + * Test if the style object passed to the constructor is actually used + */ + public function testConstructWithStyleObject(): void + { + $style = new \PhpOffice\PhpWord\Style\Cell(); + $oCell = new Cell(null, $style); + + self::assertSame($style, $oCell->getStyle()); + } + /** * Add text. */ From cb02d7febcd0052e9a026fa87737090070ac8b38 Mon Sep 17 00:00:00 2001 From: Erik Hazigton Date: Sun, 22 Sep 2024 16:48:06 +0200 Subject: [PATCH 4/5] Fixed CS issues --- src/PhpWord/Element/AbstractElement.php | 2 +- tests/PhpWordTests/Element/CellTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/Element/AbstractElement.php b/src/PhpWord/Element/AbstractElement.php index 6e13c705a6..42d55b1e7b 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -485,7 +485,7 @@ public function isInSection() * Set new style value. * * @param AbstractStyle $styleObject Style object - * @param null|AbstractStyle|Style|array|string $styleValue Style value + * @param null|AbstractStyle|array|string|Style $styleValue Style value * @param bool $returnObject Always return object * * @return mixed diff --git a/tests/PhpWordTests/Element/CellTest.php b/tests/PhpWordTests/Element/CellTest.php index d9181a9328..93eba9df93 100644 --- a/tests/PhpWordTests/Element/CellTest.php +++ b/tests/PhpWordTests/Element/CellTest.php @@ -51,7 +51,7 @@ public function testConstructWithStyleArray(): void } /** - * Test if the style object passed to the constructor is actually used + * Test if the style object passed to the constructor is actually used. */ public function testConstructWithStyleObject(): void { From 15e69a6c62cf31a69281b149653ccba806c198da Mon Sep 17 00:00:00 2001 From: Erik Hazigton Date: Sun, 22 Sep 2024 17:30:14 +0200 Subject: [PATCH 5/5] Removed use function import and no longer needed ignored phpstan errors --- phpstan-baseline.neon | 20 -------------------- src/PhpWord/Element/AbstractElement.php | 6 ++---- 2 files changed, 2 insertions(+), 24 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 909718b83b..b89529645b 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -10,16 +10,6 @@ parameters: count: 1 path: src/PhpWord/Element/AbstractContainer.php - - - message: "#^Parameter \\#1 \\$string of function md5 expects string, int\\<0, max\\> given\\.$#" - count: 1 - path: src/PhpWord/Element/AbstractElement.php - - - - message: "#^Parameter \\#2 \\$styleValue of method PhpOffice\\\\PhpWord\\\\Element\\\\AbstractElement\\:\\:setNewStyle\\(\\) expects array\\|PhpOffice\\\\PhpWord\\\\Style\\|string\\|null, array\\|PhpOffice\\\\PhpWord\\\\Style\\\\Cell\\|null given\\.$#" - count: 1 - path: src/PhpWord/Element/Cell.php - - message: "#^Method PhpOffice\\\\PhpWord\\\\Element\\\\Field\\:\\:setOptions\\(\\) should return PhpOffice\\\\PhpWord\\\\Element\\\\Field but returns array\\.$#" count: 1 @@ -40,11 +30,6 @@ parameters: count: 1 path: src/PhpWord/Element/Field.php - - - message: "#^Parameter \\#2 \\$styleValue of method PhpOffice\\\\PhpWord\\\\Element\\\\AbstractElement\\:\\:setNewStyle\\(\\) expects array\\|PhpOffice\\\\PhpWord\\\\Style\\|string\\|null, array\\|PhpOffice\\\\PhpWord\\\\Style\\\\Paragraph\\|string\\|null given\\.$#" - count: 1 - path: src/PhpWord/Element/Footnote.php - - message: "#^Method PhpOffice\\\\PhpWord\\\\Element\\\\Image\\:\\:getArchiveImageSize\\(\\) should return array\\|null but returns array\\|false\\|null\\.$#" count: 1 @@ -85,11 +70,6 @@ parameters: count: 1 path: src/PhpWord/Element/Section.php - - - message: "#^Parameter \\#2 \\$styleValue of method PhpOffice\\\\PhpWord\\\\Element\\\\AbstractElement\\:\\:setNewStyle\\(\\) expects array\\|PhpOffice\\\\PhpWord\\\\Style\\|string\\|null, array\\|PhpOffice\\\\PhpWord\\\\Style\\|PhpOffice\\\\PhpWord\\\\Style\\\\Section\\|string given\\.$#" - count: 1 - path: src/PhpWord/Element/Section.php - - message: "#^Parameter \\#3 \\$length of function substr expects int\\|null, int\\|false given\\.$#" count: 1 diff --git a/src/PhpWord/Element/AbstractElement.php b/src/PhpWord/Element/AbstractElement.php index 42d55b1e7b..92d08d8b00 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -19,12 +19,10 @@ use DateTime; use InvalidArgumentException; -use function is_array; use PhpOffice\PhpWord\Collection\Comments; use PhpOffice\PhpWord\Media; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Style; - use PhpOffice\PhpWord\Style\AbstractStyle; /** @@ -484,13 +482,13 @@ public function isInSection() /** * Set new style value. * - * @param AbstractStyle $styleObject Style object + * @param mixed $styleObject Style object * @param null|AbstractStyle|array|string|Style $styleValue Style value * @param bool $returnObject Always return object * * @return mixed */ - protected function setNewStyle($styleObject, $styleValue = null, $returnObject = false) + protected function setNewStyle($styleObject, $styleValue = null, bool $returnObject = false) { if ($styleValue instanceof AbstractStyle && get_class($styleValue) === get_class($styleObject)) { return $styleValue;