Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix that style objects cannot be passed to element constructors #2685

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes/1.x/1.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 0 additions & 20 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<int\\|string, int\\|string>\\|false\\|null\\.$#"
count: 1
Expand Down Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions src/PhpWord/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\AbstractStyle;

/**
* Element abstract class.
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/PhpWordTests/Element/CellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Loading