Skip to content

Commit 1c8408f

Browse files
committed
Fix scrutinizer issues
1 parent 34c7045 commit 1c8408f

File tree

11 files changed

+37
-15
lines changed

11 files changed

+37
-15
lines changed

src/PhpWord/Metadata/Settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class Settings
148148

149149
/**
150150
* The allowed amount of whitespace before hyphenation is applied
151-
* @var float|null
151+
* @var Absolute
152152
*/
153153
private $hyphenationZone;
154154

src/PhpWord/Shared/Html.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use PhpOffice\PhpWord\Element\AbstractContainer;
2323
use PhpOffice\PhpWord\Element\Row;
2424
use PhpOffice\PhpWord\Element\Table;
25+
use PhpOffice\PhpWord\Exception\Exception;
2526
use PhpOffice\PhpWord\Settings;
2627
use PhpOffice\PhpWord\Shared\HtmlDpi as Dpi;
2728
use PhpOffice\PhpWord\SimpleType\Jc;
@@ -953,7 +954,7 @@ protected static function mapBorderWidth(DOMNode $node, &$styles, string $cssSiz
953954
}
954955
}
955956

956-
protected static function expandBorderSides(DOMNode $node, string $values)
957+
protected static function expandBorderSides(DOMNode $node, string $valuesString)
957958
{
958959
$side_mapping = array(
959960
'table' => array('top', 'end', 'bottom', 'start'),
@@ -962,12 +963,19 @@ protected static function expandBorderSides(DOMNode $node, string $values)
962963
'p' => array('top', 'right', 'bottom', 'left'),
963964
);
964965
if (!array_key_exists($node->nodeName, $side_mapping)) {
965-
trigger_error(sprintf('Node ``%s` does not support borders', $node->nodeName), E_USER_WARNING);
966+
trigger_error(sprintf('Node `%s` does not support borders', $node->nodeName), E_USER_WARNING);
966967
}
967968

968969
$sides = $side_mapping[$node->nodeName];
969-
$values = explode(' ', $values);
970+
$values = explode(' ', $valuesString);
971+
if (count($values) > count($sides)) {
972+
trigger_error(sprintf('Provided `%s` style `%s` had more than %d values', $node->nodeName, $valuesString, count($sides)), E_USER_WARNING);
973+
$values = array_slice($values, 0, count($sides));
974+
}
970975
$values = array_combine(array_slice($sides, 0, count($values)), $values);
976+
if ($values === false) {
977+
throw new Exception(sprintf('Mismatch between number of items in `$sides` and `$values`. This should never happen. Provided values: `%s`, Sides: `%s`, Values: `%s`', $valuesString, serialize($sides), serialize($values)));
978+
}
971979

972980
if (count($values) === 1) {
973981
$values[$sides[1]] = $values[$sides[0]];

src/PhpWord/Style/LineNumbering.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class LineNumbering extends AbstractStyle
5050
/**
5151
* Distance between text and line numbering in twip
5252
*
53-
* @var int|float
53+
* @var Absolute
5454
*/
5555
private $distance;
5656

@@ -69,7 +69,6 @@ class LineNumbering extends AbstractStyle
6969
*/
7070
public function __construct($style = array())
7171
{
72-
$this->setDistance(new Absolute(null));
7372
$this->setStyleByArray($style);
7473
}
7574

@@ -124,6 +123,10 @@ public function setIncrement($value = null)
124123
*/
125124
public function getDistance(): Absolute
126125
{
126+
if ($this->distance === null) {
127+
$this->distance = new Absolute(null);
128+
}
129+
127130
return $this->distance;
128131
}
129132

src/PhpWord/Style/NumberingLevel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ class NumberingLevel extends AbstractStyle
9595
/**
9696
* Left
9797
*
98-
* @var int
98+
* @var Absolute
9999
*/
100100
private $left;
101101

102102
/**
103103
* Hanging
104104
*
105-
* @var int
105+
* @var Absolute
106106
*/
107107
private $hanging;
108108

src/PhpWord/Style/Spacing.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Spacing extends AbstractStyle
4646
/**
4747
* Spacing between lines in paragraph
4848
*
49-
* @var int|float
49+
* @var Absolute
5050
*/
5151
private $line;
5252

src/PhpWord/Writer/HTML/Element/Table.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
namespace PhpOffice\PhpWord\Writer\HTML\Element;
2020

21+
use PhpOffice\PhpWord\Style\Colors\StaticColorInterface;
22+
2123
/**
2224
* Table element HTML writer
2325
*
@@ -54,9 +56,11 @@ public function write()
5456
$cellStyle = $rowCells[$j]->getStyle();
5557
$cellBgColor = $cellStyle->getBgColor();
5658
$cellFgColor = null;
57-
if ($cellBgColor->toRgb()) {
59+
if ($cellBgColor instanceof StaticColorInterface) {
5860
list($red, $green, $blue) = $cellBgColor->toRgb();
59-
$cellFgColor = (($red * 0.299 + $green * 0.587 + $blue * 0.114) > 186) ? null : 'FFFFFF';
61+
if (($red * 0.299 + $green * 0.587 + $blue * 0.114) <= 186) {
62+
$cellFgColor = 'FFFFFF';
63+
}
6064
}
6165
$cellColSpan = $cellStyle->getGridSpan();
6266
$cellRowSpan = 1;

src/PhpWord/Writer/HTML/Style/Font.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function write()
4242

4343
$font = $style->getName();
4444
$size = $style->getSize()->toInt('pt');
45-
$color = $style->getColor()->toHex(true);
45+
$color = $style->getColor()->toHexOrName(true);
4646
$fgColor = $style->getFgColor()->getName();
4747
$underline = $style->getUnderline() != FontStyle::UNDERLINE_NONE;
4848
$lineThrough = $style->isStrikethrough() || $style->isDoubleStrikethrough();

src/PhpWord/Writer/ODText/Style/Font.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function write()
5353
$xmlWriter->writeAttributeIf($size !== null, 'style:font-size-complex', $size . 'pt');
5454

5555
// Color
56-
$color = $style->getColor()->toHex(true);
56+
$color = $style->getColor()->toHexOrName(true);
5757
$xmlWriter->writeAttributeIf($color != '', 'fo:color', $color);
5858

5959
// Bold & italic

src/PhpWord/Writer/ODText/Style/Table.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function write()
3939
$style = $this->getStyle();
4040
if ($style === null) {
4141
return;
42+
} elseif (is_string($style)) {
43+
throw new Exception(sprintf('Incorrect value provided for style. `%s` expected, `string(%s)` provided', TableStyle::class, $style));
4244
} elseif (!$style instanceof TableStyle) {
4345
throw new Exception(sprintf('Incorrect value provided for style. %s expected, %s provided', TableStyle::class, get_class($style)));
4446
}

src/PhpWord/Writer/RTF/Element/Table.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PhpOffice\PhpWord\Element\Cell as CellElement;
2222
use PhpOffice\PhpWord\Element\Row as RowElement;
2323
use PhpOffice\PhpWord\Element\Table as TableElement;
24+
use PhpOffice\PhpWord\Style\Lengths\Absolute;
2425

2526
/**
2627
* Table element RTF writer
@@ -67,6 +68,7 @@ public function write()
6768
/**
6869
* Write column
6970
*
71+
* @todo Add support for percentage widths and auto (https://stackoverflow.com/a/5531449/526741)
7072
* @return string
7173
*/
7274
private function writeRowDef(RowElement $row)
@@ -76,7 +78,10 @@ private function writeRowDef(RowElement $row)
7678
$rightMargin = 0;
7779
foreach ($row->getCells() as $cell) {
7880
// Arbitrary default width
79-
$width = $cell->getWidth()->toInt('twip') ?? 720;
81+
$width = 720;
82+
if ($width instanceof Absolute && $width->isSpecified()) {
83+
$width = $width->toInt('twip');
84+
}
8085

8186
$vMerge = $this->getVMerge($cell->getStyle()->getVMerge());
8287
$rightMargin += $width;

src/PhpWord/Writer/RTF/Part/Header.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ private function registerBorderColor(SectionStyle $style): self
220220
foreach ($borders as $border) {
221221
$color = $border->getColor();
222222
if ($color->isSpecified()) {
223-
$this->registerTableItem($this->colorTable, $color);
223+
$this->registerTableItem($this->colorTable, $color, new Hex('000000'));
224224
}
225225
}
226226

0 commit comments

Comments
 (0)