Skip to content

Commit 0c8483e

Browse files
committed
Fixed PHPStan errors
1 parent fe03ca5 commit 0c8483e

File tree

93 files changed

+536
-479
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+536
-479
lines changed

phpstan-baseline.neon

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,3 +1839,39 @@ parameters:
18391839
message: "#^Property PhpOffice\\\\PhpWordTests\\\\XmlDocument\\:\\:\\$xpath \\(DOMXPath\\) does not accept null\\.$#"
18401840
count: 1
18411841
path: tests/PhpWordTests/XmlDocument.php
1842+
1843+
# https://github.com/phpstan/phpstan/issues/8770
1844+
-
1845+
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Writer\\\\HTML\\\\Part\\\\AbstractPart is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:|]+$#"
1846+
count: 1
1847+
path: src/PhpWord/Writer/HTML.php
1848+
1849+
# https://github.com/phpstan/phpstan/issues/8770
1850+
-
1851+
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Element\\\\AbstractElement is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
1852+
count: 2
1853+
path: tests/PhpWordTests/Element/AbstractElementTest.php
1854+
1855+
# https://github.com/phpstan/phpstan/issues/8770
1856+
-
1857+
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Style\\\\AbstractStyle is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
1858+
count: 4
1859+
path: tests/PhpWordTests/Style/AbstractStyleTest.php
1860+
1861+
# https://github.com/phpstan/phpstan/issues/8770
1862+
-
1863+
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Writer\\\\EPub3\\\\Style\\\\AbstractStyle is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
1864+
count: 2
1865+
path: tests/PhpWordTests/Writer/EPub3/Style/AbstractStyleTest.php
1866+
1867+
# https://github.com/phpstan/phpstan/issues/8770
1868+
-
1869+
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Writer\\\\ODText\\\\Part\\\\AbstractPart is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
1870+
count: 2
1871+
path: tests/PhpWordTests/Writer/ODText/Part/AbstractPartTest.php
1872+
1873+
# https://github.com/phpstan/phpstan/issues/8770
1874+
-
1875+
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Writer\\\\Word2007\\\\Part\\\\AbstractPart is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
1876+
count: 2
1877+
path: tests/PhpWordTests/Writer/Word2007/Part/AbstractPartTest.php

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ parameters:
1616
- tests/bootstrap.php
1717
## <=PHP7.4
1818
reportUnmatchedIgnoredErrors: false
19+
treatPhpDocTypesAsCertain: false
1920
ignoreErrors:
2021
-
2122
identifier: missingType.iterableValue

src/PhpWord/Element/Field.php

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class Field extends AbstractElement
108108
/**
109109
* Field text.
110110
*
111-
* @var string|TextRun
111+
* @var null|string|TextRun
112112
*/
113113
protected $text;
114114

@@ -217,22 +217,18 @@ public function getType()
217217
/**
218218
* Set Field properties.
219219
*
220-
* @param array $properties
221-
*
222220
* @return self
223221
*/
224-
public function setProperties($properties = [])
222+
public function setProperties(array $properties = [])
225223
{
226-
if (is_array($properties)) {
227-
foreach (array_keys($properties) as $propkey) {
228-
if (!(isset($this->fieldsArray[$this->type]['properties'][$propkey]))) {
229-
throw new InvalidArgumentException("Invalid property '$propkey'");
230-
}
224+
foreach (array_keys($properties) as $propkey) {
225+
if (!(isset($this->fieldsArray[$this->type]['properties'][$propkey]))) {
226+
throw new InvalidArgumentException("Invalid property '$propkey'");
231227
}
232-
$this->properties = array_merge($this->properties, $properties);
233228
}
229+
$this->properties = array_merge($this->properties, $properties);
234230

235-
return $this->properties;
231+
return $this;
236232
}
237233

238234
/**
@@ -248,22 +244,18 @@ public function getProperties()
248244
/**
249245
* Set Field options.
250246
*
251-
* @param array $options
252-
*
253247
* @return self
254248
*/
255-
public function setOptions($options = [])
249+
public function setOptions(array $options = [])
256250
{
257-
if (is_array($options)) {
258-
foreach (array_keys($options) as $optionkey) {
259-
if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey])) && substr($optionkey, 0, 1) !== '\\') {
260-
throw new InvalidArgumentException("Invalid option '$optionkey', possible values are " . implode(', ', $this->fieldsArray[$this->type]['options']));
261-
}
251+
foreach (array_keys($options) as $optionkey) {
252+
if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey])) && substr($optionkey, 0, 1) !== '\\') {
253+
throw new InvalidArgumentException("Invalid option '$optionkey', possible values are " . implode(', ', $this->fieldsArray[$this->type]['options']));
262254
}
263-
$this->options = array_merge($this->options, $options);
264255
}
256+
$this->options = array_merge($this->options, $options);
265257

266-
return $this->options;
258+
return $this;
267259
}
268260

269261
/**
@@ -279,13 +271,13 @@ public function getOptions()
279271
/**
280272
* Set Field text.
281273
*
282-
* @param null|string|TextRun $text
274+
* @param null|mixed|string|TextRun $text
283275
*
284276
* @return null|string|TextRun
285277
*/
286278
public function setText($text = null)
287279
{
288-
if (isset($text)) {
280+
if (null !== $text) {
289281
if (is_string($text) || $text instanceof TextRun) {
290282
$this->text = $text;
291283
} else {

src/PhpWord/Element/OLEObject.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,16 @@ class OLEObject extends AbstractElement
7070
public function __construct($source, $style = null)
7171
{
7272
$supportedTypes = ['xls', 'doc', 'ppt', 'xlsx', 'docx', 'pptx'];
73-
$pathInfo = pathinfo($source);
73+
$pathInfoExtension = pathinfo($source, PATHINFO_EXTENSION);
7474

75-
if (file_exists($source) && in_array($pathInfo['extension'], $supportedTypes)) {
76-
$ext = $pathInfo['extension'];
77-
if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') {
78-
$ext = substr($ext, 0, -1);
75+
if (file_exists($source) && in_array($pathInfoExtension, $supportedTypes)) {
76+
if (strlen($pathInfoExtension) == 4 && strtolower(substr($pathInfoExtension, -1)) == 'x') {
77+
$pathInfoExtension = substr($pathInfoExtension, 0, -1);
7978
}
8079

8180
$this->source = $source;
8281
$this->style = $this->setNewStyle(new ImageStyle(), $style, true);
83-
$this->icon = realpath(__DIR__ . "/../resources/{$ext}.png");
82+
$this->icon = realpath(__DIR__ . "/../resources/{$pathInfoExtension}.png");
8483

8584
return;
8685
}

src/PhpWord/Element/Section.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,10 @@ public function __construct($sectionCount, $style = null)
7575

7676
/**
7777
* Set section style.
78-
*
79-
* @param array $style
8078
*/
81-
public function setStyle($style = null): void
79+
public function setStyle(?array $style = null): void
8280
{
83-
if (null !== $style && is_array($style)) {
81+
if (null !== $style) {
8482
$this->style->setStyleByArray($style);
8583
}
8684
}

src/PhpWord/Element/TOC.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,14 @@ class TOC extends AbstractElement
5959
* Create a new Table-of-Contents Element.
6060
*
6161
* @param mixed $fontStyle
62-
* @param array $tocStyle
6362
* @param int $minDepth
6463
* @param int $maxDepth
6564
*/
66-
public function __construct($fontStyle = null, $tocStyle = null, $minDepth = 1, $maxDepth = 9)
65+
public function __construct($fontStyle = null, ?array $tocStyle = null, $minDepth = 1, $maxDepth = 9)
6766
{
6867
$this->tocStyle = new TOCStyle();
6968

70-
if (null !== $tocStyle && is_array($tocStyle)) {
69+
if (null !== $tocStyle) {
7170
$this->tocStyle->setStyleByArray($tocStyle);
7271
}
7372

src/PhpWord/Reader/ODText/Content.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ public function read(PhpWord $phpWord): void
4545
$xmlReader = new XMLReader();
4646
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
4747

48-
$trackedChanges = [];
49-
5048
$nodes = $xmlReader->getElements('office:body/office:text/*');
5149
$this->section = null;
5250
$this->processNodes($nodes, $xmlReader, $phpWord);
@@ -186,6 +184,7 @@ public function processNodes(DOMNodeList $nodes, XMLReader $xmlReader, PhpWord $
186184
case 'text:section': // Section
187185
// $sectionStyleName = $xmlReader->getAttribute('text:style-name', $listItem);
188186
$this->section = $phpWord->addSection();
187+
/** @var DOMNodeList<DOMElement> $children */
189188
$children = $node->childNodes;
190189
$this->processNodes($children, $xmlReader, $phpWord);
191190

src/PhpWord/Reader/RTF/Document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*
3232
* @since 0.11.0
3333
*
34-
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
34+
* @SuppressWarnings("PHPMD.UnusedPrivateMethod")
3535
*/
3636
class Document
3737
{

src/PhpWord/Reader/Word2007.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,15 @@ private function readRelationships($docFile)
138138
if ($zip->open($docFile) === true) {
139139
for ($i = 0; $i < $zip->numFiles; ++$i) {
140140
$xmlFile = $zip->getNameIndex($i);
141+
if (!is_string($xmlFile)) {
142+
continue;
143+
}
141144
if ((substr($xmlFile, 0, strlen($wordRelsPath))) == $wordRelsPath && (substr($xmlFile, -1)) != '/') {
142-
$docPart = str_replace('.xml.rels', '', str_replace($wordRelsPath, '', $xmlFile));
145+
$docPart = str_replace(
146+
'.xml.rels',
147+
'',
148+
str_replace($wordRelsPath, '', $xmlFile)
149+
);
143150
$relationships[$docPart] = $this->getRels($docFile, $xmlFile, 'word/');
144151
}
145152
}

src/PhpWord/Reader/Word2007/Document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*
2929
* @since 0.10.0
3030
*
31-
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) For readWPNode
31+
* @SuppressWarnings("PHPMD.UnusedPrivateMethod") For readWPNode
3232
*/
3333
class Document extends AbstractPart
3434
{

src/PhpWord/Shared/Html.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
namespace PhpOffice\PhpWord\Shared;
2020

21-
use DOMAttr;
2221
use DOMDocument;
2322
use DOMNode;
2423
use DOMXPath;
@@ -36,7 +35,7 @@
3635
/**
3736
* Common Html functions.
3837
*
39-
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) For readWPNode
38+
* @SuppressWarnings("PHPMD.UnusedPrivateMethod") For readWPNode
4039
*/
4140
class Html
4241
{
@@ -666,14 +665,11 @@ protected static function parseListItem($node, $element, &$styles, $data): void
666665
/**
667666
* Parse style.
668667
*
669-
* @param DOMAttr $attribute
670-
* @param array $styles
671-
*
672-
* @return array
668+
* @param DOMNode $attribute
673669
*/
674-
protected static function parseStyle($attribute, $styles)
670+
protected static function parseStyle($attribute, array $styles): array
675671
{
676-
$properties = explode(';', trim($attribute->value, " \t\n\r\0\x0B;"));
672+
$properties = explode(';', trim($attribute->nodeValue, " \t\n\r\0\x0B;"));
677673

678674
$selectors = [];
679675
foreach ($properties as $property) {
@@ -684,7 +680,7 @@ protected static function parseStyle($attribute, $styles)
684680
return self::parseStyleDeclarations($selectors, $styles);
685681
}
686682

687-
protected static function parseStyleDeclarations(array $selectors, array $styles)
683+
protected static function parseStyleDeclarations(array $selectors, array $styles): array
688684
{
689685
$bidi = ($selectors['direction'] ?? '') === 'rtl';
690686
foreach ($selectors as $property => $value) {

src/PhpWord/Shared/ZipArchive.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public function extractTo($destination, $entries = null)
209209
*
210210
* @param string $filename Filename for the file in zip archive
211211
*
212-
* @return string $contents File string contents
212+
* @return bool|string $contents File string contents
213213
*/
214214
public function getFromName($filename)
215215
{
@@ -245,22 +245,24 @@ public function pclzipAddFile($filename, $localname = null)
245245
$filename = $realpathFilename;
246246
}
247247

248-
$filenameParts = pathinfo($filename);
249-
$localnameParts = pathinfo($localname);
248+
$filenamePartsBaseName = pathinfo($filename, PATHINFO_BASENAME);
249+
$filenamePartsDirName = pathinfo($filename, PATHINFO_DIRNAME);
250+
$localnamePartsBaseName = pathinfo($localname, PATHINFO_BASENAME);
251+
$localnamePartsDirName = pathinfo($localname, PATHINFO_DIRNAME);
250252

251253
// To Rename the file while adding it to the zip we
252254
// need to create a temp file with the correct name
253255
$tempFile = false;
254-
if ($filenameParts['basename'] != $localnameParts['basename']) {
256+
if ($filenamePartsBaseName != $localnamePartsBaseName) {
255257
$tempFile = true; // temp file created
256-
$temppath = $this->tempDir . DIRECTORY_SEPARATOR . $localnameParts['basename'];
258+
$temppath = $this->tempDir . DIRECTORY_SEPARATOR . $localnamePartsBaseName;
257259
copy($filename, $temppath);
258260
$filename = $temppath;
259-
$filenameParts = pathinfo($temppath);
261+
$filenamePartsDirName = pathinfo($temppath, PATHINFO_DIRNAME);
260262
}
261263

262-
$pathRemoved = $filenameParts['dirname'];
263-
$pathAdded = $localnameParts['dirname'];
264+
$pathRemoved = $filenamePartsDirName;
265+
$pathAdded = $localnamePartsDirName;
264266

265267
if (!$this->usePclzip) {
266268
$pathAdded = $pathAdded . '/' . ltrim(str_replace('\\', '/', substr($filename, strlen($pathRemoved))), '/');
@@ -272,7 +274,7 @@ public function pclzipAddFile($filename, $localname = null)
272274

273275
if ($tempFile) {
274276
// Remove temp file, if created
275-
unlink($this->tempDir . DIRECTORY_SEPARATOR . $localnameParts['basename']);
277+
unlink($this->tempDir . DIRECTORY_SEPARATOR . $localnamePartsBaseName);
276278
}
277279

278280
return $res != 0;
@@ -290,24 +292,25 @@ public function pclzipAddFromString($localname, $contents)
290292
{
291293
/** @var PclZip $zip Type hint */
292294
$zip = $this->zip;
293-
$filenameParts = pathinfo($localname);
295+
$filenamePartsBaseName = pathinfo($localname, PATHINFO_BASENAME);
296+
$filenamePartsDirName = pathinfo($localname, PATHINFO_DIRNAME);
294297

295298
// Write $contents to a temp file
296-
$handle = fopen($this->tempDir . DIRECTORY_SEPARATOR . $filenameParts['basename'], 'wb');
299+
$handle = fopen($this->tempDir . DIRECTORY_SEPARATOR . $filenamePartsBaseName, 'wb');
297300
if ($handle) {
298301
fwrite($handle, $contents);
299302
fclose($handle);
300303
}
301304

302305
// Add temp file to zip
303-
$filename = $this->tempDir . DIRECTORY_SEPARATOR . $filenameParts['basename'];
306+
$filename = $this->tempDir . DIRECTORY_SEPARATOR . $filenamePartsBaseName;
304307
$pathRemoved = $this->tempDir;
305-
$pathAdded = $filenameParts['dirname'];
308+
$pathAdded = $filenamePartsDirName;
306309

307310
$res = $zip->add($filename, PCLZIP_OPT_REMOVE_PATH, $pathRemoved, PCLZIP_OPT_ADD_PATH, $pathAdded);
308311

309312
// Remove temp file
310-
@unlink($this->tempDir . DIRECTORY_SEPARATOR . $filenameParts['basename']);
313+
@unlink($this->tempDir . DIRECTORY_SEPARATOR . $filenamePartsBaseName);
311314

312315
return $res != 0;
313316
}
@@ -370,7 +373,7 @@ public function pclzipGetFromName($filename)
370373
$listIndex = $this->pclzipLocateName($filename);
371374
$extracted = $zip->extractByIndex($listIndex, PCLZIP_OPT_EXTRACT_AS_STRING);
372375
}
373-
if ((is_array($extracted)) && ($extracted != 0)) {
376+
if (is_array($extracted) && count($extracted) != 0) {
374377
$contents = $extracted[0]['content'];
375378
}
376379

src/PhpWord/Style/Chart.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,11 +456,8 @@ public function getValueLabelPosition()
456456
* "nextTo" - sets labels next to the value
457457
* "low" - sets labels are below the graph
458458
* "high" - sets labels above the graph.
459-
*
460-
* @param string
461-
* @param mixed $labelPosition
462459
*/
463-
public function setValueLabelPosition($labelPosition)
460+
public function setValueLabelPosition(string $labelPosition)
464461
{
465462
$enum = ['nextTo', 'low', 'high'];
466463
$this->valueLabelPosition = $this->setEnumVal($labelPosition, $enum, $this->valueLabelPosition);

0 commit comments

Comments
 (0)