Skip to content

Commit eafb11e

Browse files
committed
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.
1 parent 43785fe commit eafb11e

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/PhpWord/Element/AbstractElement.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
use PhpOffice\PhpWord\Media;
2424
use PhpOffice\PhpWord\PhpWord;
2525
use PhpOffice\PhpWord\Style;
26+
use PhpOffice\PhpWord\Style\AbstractStyle;
27+
28+
use function is_array;
2629

2730
/**
2831
* Element abstract class.
@@ -256,7 +259,7 @@ public function getElementId()
256259
*/
257260
public function setElementId(): void
258261
{
259-
$this->elementId = substr(md5(mt_rand()), 0, 6);
262+
$this->elementId = substr(md5((string) mt_rand()), 0, 6);
260263
}
261264

262265
/**
@@ -481,22 +484,24 @@ public function isInSection()
481484
/**
482485
* Set new style value.
483486
*
484-
* @param mixed $styleObject Style object
485-
* @param null|array|string|Style $styleValue Style value
487+
* @param AbstractStyle $styleObject Style object
488+
* @param null|string|array|AbstractStyle $styleValue Style value
486489
* @param bool $returnObject Always return object
487490
*
488-
* @return mixed
491+
* @return null|string|array|AbstractStyle
489492
*/
490493
protected function setNewStyle($styleObject, $styleValue = null, $returnObject = false)
491494
{
492-
if (null !== $styleValue && is_array($styleValue)) {
495+
if ($styleValue instanceof AbstractStyle && get_class($styleValue) === get_class($styleObject)) {
496+
return clone $styleValue;
497+
}
498+
499+
if (is_array($styleValue)) {
493500
$styleObject->setStyleByArray($styleValue);
494-
$style = $styleObject;
495-
} else {
496-
$style = $returnObject ? $styleObject : $styleValue;
501+
return $styleObject;
497502
}
498503

499-
return $style;
504+
return $returnObject === true ? $styleObject : $styleValue;
500505
}
501506

502507
/**

0 commit comments

Comments
 (0)