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/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 385e4d3140..92d08d8b00 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -23,6 +23,7 @@ use PhpOffice\PhpWord\Media; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Style; +use PhpOffice\PhpWord\Style\AbstractStyle; /** * Element abstract class. @@ -256,7 +257,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); } /** @@ -482,21 +483,24 @@ public function isInSection() * Set new style value. * * @param mixed $styleObject Style object - * @param null|array|string|Style $styleValue Style value + * @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 (null !== $styleValue && is_array($styleValue)) { + if ($styleValue instanceof AbstractStyle && get_class($styleValue) === get_class($styleObject)) { + return $styleValue; + } + + if (is_array($styleValue)) { $styleObject->setStyleByArray($styleValue); - $style = $styleObject; - } else { - $style = $returnObject ? $styleObject : $styleValue; + + return $styleObject; } - return $style; + return $returnObject === true ? $styleObject : $styleValue; } /** diff --git a/tests/PhpWordTests/Element/CellTest.php b/tests/PhpWordTests/Element/CellTest.php index 700e16d58e..93eba9df93 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. */