Skip to content

Update phpstan/phpstan requirement from ^0.12.88 || ^1.0.0 to ^0.12.88 || ^1.0.0 || ^2.0.0 #2736

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

Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
"friendsofphp/php-cs-fixer": "^3.3",
"mpdf/mpdf": "^7.0 || ^8.0",
"phpmd/phpmd": "^2.13",
"phpstan/phpstan": "^0.12.88 || ^1.0.0",
"phpstan/phpstan": "^0.12.88 || ^1.0.0 || ^2.0.0",
"phpstan/phpstan-phpunit": "^1.0 || ^2.0",
"phpunit/phpunit": ">=7.0",
"symfony/process": "^4.4 || ^5.0",
Expand Down
2 changes: 2 additions & 0 deletions docs/changes/1.x/1.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

### Miscellaneous

- Update phpstan/phpstan requirement from ^0.12.88 || ^1.0.0 to ^0.12.88 || ^1.0.0 || ^2.0.0 by [@dependabot](https://github.com/dependabot) & [@Progi1984](https://github.com/Progi1984) in [#2736](https://github.com/PHPOffice/PHPWord/pull/2736)

### Deprecations

### BC Breaks
Expand Down
36 changes: 36 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1839,3 +1839,39 @@ parameters:
message: "#^Property PhpOffice\\\\PhpWordTests\\\\XmlDocument\\:\\:\\$xpath \\(DOMXPath\\) does not accept null\\.$#"
count: 1
path: tests/PhpWordTests/XmlDocument.php

# https://github.com/phpstan/phpstan/issues/8770
-
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Writer\\\\HTML\\\\Part\\\\AbstractPart is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:|]+$#"
count: 1
path: src/PhpWord/Writer/HTML.php

# https://github.com/phpstan/phpstan/issues/8770
-
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Element\\\\AbstractElement is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
count: 2
path: tests/PhpWordTests/Element/AbstractElementTest.php

# https://github.com/phpstan/phpstan/issues/8770
-
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Style\\\\AbstractStyle is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
count: 4
path: tests/PhpWordTests/Style/AbstractStyleTest.php

# https://github.com/phpstan/phpstan/issues/8770
-
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Writer\\\\EPub3\\\\Style\\\\AbstractStyle is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
count: 2
path: tests/PhpWordTests/Writer/EPub3/Style/AbstractStyleTest.php

# https://github.com/phpstan/phpstan/issues/8770
-
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Writer\\\\ODText\\\\Part\\\\AbstractPart is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
count: 2
path: tests/PhpWordTests/Writer/ODText/Part/AbstractPartTest.php

# https://github.com/phpstan/phpstan/issues/8770
-
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Writer\\\\Word2007\\\\Part\\\\AbstractPart is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
count: 2
path: tests/PhpWordTests/Writer/Word2007/Part/AbstractPartTest.php
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ parameters:
- tests/bootstrap.php
## <=PHP7.4
reportUnmatchedIgnoredErrors: false
treatPhpDocTypesAsCertain: false
ignoreErrors:
-
identifier: missingType.iterableValue
Expand Down
38 changes: 15 additions & 23 deletions src/PhpWord/Element/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Field extends AbstractElement
/**
* Field text.
*
* @var string|TextRun
* @var null|string|TextRun
*/
protected $text;

Expand Down Expand Up @@ -217,22 +217,18 @@ public function getType()
/**
* Set Field properties.
*
* @param array $properties
*
* @return self
*/
public function setProperties($properties = [])
public function setProperties(array $properties = [])
{
if (is_array($properties)) {
foreach (array_keys($properties) as $propkey) {
if (!(isset($this->fieldsArray[$this->type]['properties'][$propkey]))) {
throw new InvalidArgumentException("Invalid property '$propkey'");
}
foreach (array_keys($properties) as $propkey) {
if (!(isset($this->fieldsArray[$this->type]['properties'][$propkey]))) {
throw new InvalidArgumentException("Invalid property '$propkey'");
}
$this->properties = array_merge($this->properties, $properties);
}
$this->properties = array_merge($this->properties, $properties);

return $this->properties;
return $this;
}

/**
Expand All @@ -248,22 +244,18 @@ public function getProperties()
/**
* Set Field options.
*
* @param array $options
*
* @return self
*/
public function setOptions($options = [])
public function setOptions(array $options = [])
{
if (is_array($options)) {
foreach (array_keys($options) as $optionkey) {
if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey])) && substr($optionkey, 0, 1) !== '\\') {
throw new InvalidArgumentException("Invalid option '$optionkey', possible values are " . implode(', ', $this->fieldsArray[$this->type]['options']));
}
foreach (array_keys($options) as $optionkey) {
if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey])) && substr($optionkey, 0, 1) !== '\\') {
throw new InvalidArgumentException("Invalid option '$optionkey', possible values are " . implode(', ', $this->fieldsArray[$this->type]['options']));
}
$this->options = array_merge($this->options, $options);
}
$this->options = array_merge($this->options, $options);

return $this->options;
return $this;
}

/**
Expand All @@ -279,13 +271,13 @@ public function getOptions()
/**
* Set Field text.
*
* @param null|string|TextRun $text
* @param null|mixed|string|TextRun $text
*
* @return null|string|TextRun
*/
public function setText($text = null)
{
if (isset($text)) {
if (null !== $text) {
if (is_string($text) || $text instanceof TextRun) {
$this->text = $text;
} else {
Expand Down
11 changes: 5 additions & 6 deletions src/PhpWord/Element/OLEObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,16 @@ class OLEObject extends AbstractElement
public function __construct($source, $style = null)
{
$supportedTypes = ['xls', 'doc', 'ppt', 'xlsx', 'docx', 'pptx'];
$pathInfo = pathinfo($source);
$pathInfoExtension = pathinfo($source, PATHINFO_EXTENSION);

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

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

return;
}
Expand Down
6 changes: 2 additions & 4 deletions src/PhpWord/Element/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,10 @@ public function __construct($sectionCount, $style = null)

/**
* Set section style.
*
* @param array $style
*/
public function setStyle($style = null): void
public function setStyle(?array $style = null): void
{
if (null !== $style && is_array($style)) {
if (null !== $style) {
$this->style->setStyleByArray($style);
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/PhpWord/Element/TOC.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,14 @@ class TOC extends AbstractElement
* Create a new Table-of-Contents Element.
*
* @param mixed $fontStyle
* @param array $tocStyle
* @param int $minDepth
* @param int $maxDepth
*/
public function __construct($fontStyle = null, $tocStyle = null, $minDepth = 1, $maxDepth = 9)
public function __construct($fontStyle = null, ?array $tocStyle = null, $minDepth = 1, $maxDepth = 9)
{
$this->tocStyle = new TOCStyle();

if (null !== $tocStyle && is_array($tocStyle)) {
if (null !== $tocStyle) {
$this->tocStyle->setStyleByArray($tocStyle);
}

Expand Down
3 changes: 1 addition & 2 deletions src/PhpWord/Reader/ODText/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public function read(PhpWord $phpWord): void
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);

$trackedChanges = [];

$nodes = $xmlReader->getElements('office:body/office:text/*');
$this->section = null;
$this->processNodes($nodes, $xmlReader, $phpWord);
Expand Down Expand Up @@ -186,6 +184,7 @@ public function processNodes(DOMNodeList $nodes, XMLReader $xmlReader, PhpWord $
case 'text:section': // Section
// $sectionStyleName = $xmlReader->getAttribute('text:style-name', $listItem);
$this->section = $phpWord->addSection();
/** @var DOMNodeList<DOMElement> $children */
$children = $node->childNodes;
$this->processNodes($children, $xmlReader, $phpWord);

Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Reader/RTF/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*
* @since 0.11.0
*
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
* @SuppressWarnings("PHPMD.UnusedPrivateMethod")
*/
class Document
{
Expand Down
9 changes: 8 additions & 1 deletion src/PhpWord/Reader/Word2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,15 @@ private function readRelationships($docFile)
if ($zip->open($docFile) === true) {
for ($i = 0; $i < $zip->numFiles; ++$i) {
$xmlFile = $zip->getNameIndex($i);
if (!is_string($xmlFile)) {
continue;
}
if ((substr($xmlFile, 0, strlen($wordRelsPath))) == $wordRelsPath && (substr($xmlFile, -1)) != '/') {
$docPart = str_replace('.xml.rels', '', str_replace($wordRelsPath, '', $xmlFile));
$docPart = str_replace(
'.xml.rels',
'',
str_replace($wordRelsPath, '', $xmlFile)
);
$relationships[$docPart] = $this->getRels($docFile, $xmlFile, 'word/');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Reader/Word2007/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* @since 0.10.0
*
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) For readWPNode
* @SuppressWarnings("PHPMD.UnusedPrivateMethod") For readWPNode
*/
class Document extends AbstractPart
{
Expand Down
14 changes: 5 additions & 9 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

namespace PhpOffice\PhpWord\Shared;

use DOMAttr;
use DOMDocument;
use DOMNode;
use DOMXPath;
Expand All @@ -36,7 +35,7 @@
/**
* Common Html functions.
*
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) For readWPNode
* @SuppressWarnings("PHPMD.UnusedPrivateMethod") For readWPNode
*/
class Html
{
Expand Down Expand Up @@ -666,14 +665,11 @@ protected static function parseListItem($node, $element, &$styles, $data): void
/**
* Parse style.
*
* @param DOMAttr $attribute
* @param array $styles
*
* @return array
* @param DOMNode $attribute
*/
protected static function parseStyle($attribute, $styles)
protected static function parseStyle($attribute, array $styles): array
{
$properties = explode(';', trim($attribute->value, " \t\n\r\0\x0B;"));
$properties = explode(';', trim($attribute->nodeValue, " \t\n\r\0\x0B;"));

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

protected static function parseStyleDeclarations(array $selectors, array $styles)
protected static function parseStyleDeclarations(array $selectors, array $styles): array
{
$bidi = ($selectors['direction'] ?? '') === 'rtl';
foreach ($selectors as $property => $value) {
Expand Down
33 changes: 18 additions & 15 deletions src/PhpWord/Shared/ZipArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public function extractTo($destination, $entries = null)
*
* @param string $filename Filename for the file in zip archive
*
* @return string $contents File string contents
* @return bool|string $contents File string contents
*/
public function getFromName($filename)
{
Expand Down Expand Up @@ -245,22 +245,24 @@ public function pclzipAddFile($filename, $localname = null)
$filename = $realpathFilename;
}

$filenameParts = pathinfo($filename);
$localnameParts = pathinfo($localname);
$filenamePartsBaseName = pathinfo($filename, PATHINFO_BASENAME);
$filenamePartsDirName = pathinfo($filename, PATHINFO_DIRNAME);
$localnamePartsBaseName = pathinfo($localname, PATHINFO_BASENAME);
$localnamePartsDirName = pathinfo($localname, PATHINFO_DIRNAME);

// To Rename the file while adding it to the zip we
// need to create a temp file with the correct name
$tempFile = false;
if ($filenameParts['basename'] != $localnameParts['basename']) {
if ($filenamePartsBaseName != $localnamePartsBaseName) {
$tempFile = true; // temp file created
$temppath = $this->tempDir . DIRECTORY_SEPARATOR . $localnameParts['basename'];
$temppath = $this->tempDir . DIRECTORY_SEPARATOR . $localnamePartsBaseName;
copy($filename, $temppath);
$filename = $temppath;
$filenameParts = pathinfo($temppath);
$filenamePartsDirName = pathinfo($temppath, PATHINFO_DIRNAME);
}

$pathRemoved = $filenameParts['dirname'];
$pathAdded = $localnameParts['dirname'];
$pathRemoved = $filenamePartsDirName;
$pathAdded = $localnamePartsDirName;

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

if ($tempFile) {
// Remove temp file, if created
unlink($this->tempDir . DIRECTORY_SEPARATOR . $localnameParts['basename']);
unlink($this->tempDir . DIRECTORY_SEPARATOR . $localnamePartsBaseName);
}

return $res != 0;
Expand All @@ -290,24 +292,25 @@ public function pclzipAddFromString($localname, $contents)
{
/** @var PclZip $zip Type hint */
$zip = $this->zip;
$filenameParts = pathinfo($localname);
$filenamePartsBaseName = pathinfo($localname, PATHINFO_BASENAME);
$filenamePartsDirName = pathinfo($localname, PATHINFO_DIRNAME);

// Write $contents to a temp file
$handle = fopen($this->tempDir . DIRECTORY_SEPARATOR . $filenameParts['basename'], 'wb');
$handle = fopen($this->tempDir . DIRECTORY_SEPARATOR . $filenamePartsBaseName, 'wb');
if ($handle) {
fwrite($handle, $contents);
fclose($handle);
}

// Add temp file to zip
$filename = $this->tempDir . DIRECTORY_SEPARATOR . $filenameParts['basename'];
$filename = $this->tempDir . DIRECTORY_SEPARATOR . $filenamePartsBaseName;
$pathRemoved = $this->tempDir;
$pathAdded = $filenameParts['dirname'];
$pathAdded = $filenamePartsDirName;

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

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

return $res != 0;
}
Expand Down Expand Up @@ -370,7 +373,7 @@ public function pclzipGetFromName($filename)
$listIndex = $this->pclzipLocateName($filename);
$extracted = $zip->extractByIndex($listIndex, PCLZIP_OPT_EXTRACT_AS_STRING);
}
if ((is_array($extracted)) && ($extracted != 0)) {
if (is_array($extracted) && count($extracted) != 0) {
$contents = $extracted[0]['content'];
}

Expand Down
Loading