diff --git a/composer.json b/composer.json index 90b1c2e571..08ef4ec927 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/docs/changes/1.x/1.5.0.md b/docs/changes/1.x/1.5.0.md index bbe131d32f..b96865bada 100644 --- a/docs/changes/1.x/1.5.0.md +++ b/docs/changes/1.x/1.5.0.md @@ -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 diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 86a542769a..ea75a4bd91 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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 diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 0cdf25ef21..cf26b6956b 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -16,6 +16,7 @@ parameters: - tests/bootstrap.php ## <=PHP7.4 reportUnmatchedIgnoredErrors: false + treatPhpDocTypesAsCertain: false ignoreErrors: - identifier: missingType.iterableValue diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php index cb6ac070f0..592d6e421c 100644 --- a/src/PhpWord/Element/Field.php +++ b/src/PhpWord/Element/Field.php @@ -108,7 +108,7 @@ class Field extends AbstractElement /** * Field text. * - * @var string|TextRun + * @var null|string|TextRun */ protected $text; @@ -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; } /** @@ -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; } /** @@ -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 { diff --git a/src/PhpWord/Element/OLEObject.php b/src/PhpWord/Element/OLEObject.php index 74c31a79ed..8e447873a7 100644 --- a/src/PhpWord/Element/OLEObject.php +++ b/src/PhpWord/Element/OLEObject.php @@ -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; } diff --git a/src/PhpWord/Element/Section.php b/src/PhpWord/Element/Section.php index cf765792dc..0ae00aa9f6 100644 --- a/src/PhpWord/Element/Section.php +++ b/src/PhpWord/Element/Section.php @@ -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); } } diff --git a/src/PhpWord/Element/TOC.php b/src/PhpWord/Element/TOC.php index 8f8e71f65a..9e784c75df 100644 --- a/src/PhpWord/Element/TOC.php +++ b/src/PhpWord/Element/TOC.php @@ -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); } diff --git a/src/PhpWord/Reader/ODText/Content.php b/src/PhpWord/Reader/ODText/Content.php index f555d00654..fff93618d9 100644 --- a/src/PhpWord/Reader/ODText/Content.php +++ b/src/PhpWord/Reader/ODText/Content.php @@ -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); @@ -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 $children */ $children = $node->childNodes; $this->processNodes($children, $xmlReader, $phpWord); diff --git a/src/PhpWord/Reader/RTF/Document.php b/src/PhpWord/Reader/RTF/Document.php index 358464a61d..59f0a7dd59 100644 --- a/src/PhpWord/Reader/RTF/Document.php +++ b/src/PhpWord/Reader/RTF/Document.php @@ -31,7 +31,7 @@ * * @since 0.11.0 * - * @SuppressWarnings(PHPMD.UnusedPrivateMethod) + * @SuppressWarnings("PHPMD.UnusedPrivateMethod") */ class Document { diff --git a/src/PhpWord/Reader/Word2007.php b/src/PhpWord/Reader/Word2007.php index a67cd612ed..1b9055754c 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -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/'); } } diff --git a/src/PhpWord/Reader/Word2007/Document.php b/src/PhpWord/Reader/Word2007/Document.php index 11e6089dba..89e479ef36 100644 --- a/src/PhpWord/Reader/Word2007/Document.php +++ b/src/PhpWord/Reader/Word2007/Document.php @@ -28,7 +28,7 @@ * * @since 0.10.0 * - * @SuppressWarnings(PHPMD.UnusedPrivateMethod) For readWPNode + * @SuppressWarnings("PHPMD.UnusedPrivateMethod") For readWPNode */ class Document extends AbstractPart { diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 170dc5dff3..6ee6399fd6 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -18,7 +18,6 @@ namespace PhpOffice\PhpWord\Shared; -use DOMAttr; use DOMDocument; use DOMNode; use DOMXPath; @@ -36,7 +35,7 @@ /** * Common Html functions. * - * @SuppressWarnings(PHPMD.UnusedPrivateMethod) For readWPNode + * @SuppressWarnings("PHPMD.UnusedPrivateMethod") For readWPNode */ class Html { @@ -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) { @@ -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) { diff --git a/src/PhpWord/Shared/ZipArchive.php b/src/PhpWord/Shared/ZipArchive.php index bce7f18e0f..462206c7f5 100644 --- a/src/PhpWord/Shared/ZipArchive.php +++ b/src/PhpWord/Shared/ZipArchive.php @@ -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) { @@ -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))), '/'); @@ -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; @@ -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; } @@ -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']; } diff --git a/src/PhpWord/Style/Chart.php b/src/PhpWord/Style/Chart.php index 6cffc4d5b0..3773565ce9 100644 --- a/src/PhpWord/Style/Chart.php +++ b/src/PhpWord/Style/Chart.php @@ -456,11 +456,8 @@ public function getValueLabelPosition() * "nextTo" - sets labels next to the value * "low" - sets labels are below the graph * "high" - sets labels above the graph. - * - * @param string - * @param mixed $labelPosition */ - public function setValueLabelPosition($labelPosition) + public function setValueLabelPosition(string $labelPosition) { $enum = ['nextTo', 'low', 'high']; $this->valueLabelPosition = $this->setEnumVal($labelPosition, $enum, $this->valueLabelPosition); diff --git a/src/PhpWord/Style/Language.php b/src/PhpWord/Style/Language.php index 54e4376562..641ed7b41e 100644 --- a/src/PhpWord/Style/Language.php +++ b/src/PhpWord/Style/Language.php @@ -101,32 +101,28 @@ final class Language extends AbstractStyle /** * Latin Language. * - * @var string + * @var null|string */ private $latin; /** * East Asian Language. * - * @var string + * @var null|string */ private $eastAsia; /** * Complex Script Language. * - * @var string + * @var null|string */ private $bidirectional; /** * Constructor. - * - * @param null|string $latin - * @param null|string $eastAsia - * @param null|string $bidirectional */ - public function __construct($latin = null, $eastAsia = null, $bidirectional = null) + public function __construct(?string $latin = null, ?string $eastAsia = null, ?string $bidirectional = null) { if (!empty($latin)) { $this->setLatin($latin); @@ -144,10 +140,8 @@ public function __construct($latin = null, $eastAsia = null, $bidirectional = nu * * @param string $latin * The value for the latin language - * - * @return self */ - public function setLatin($latin) + public function setLatin(?string $latin): self { $this->latin = $this->validateLocale($latin); @@ -156,10 +150,8 @@ public function setLatin($latin) /** * Get the Latin Language. - * - * @return null|string */ - public function getLatin() + public function getLatin(): ?string { return $this->latin; } diff --git a/src/PhpWord/Style/Numbering.php b/src/PhpWord/Style/Numbering.php index 1ecbe68c83..2b34f2a9bb 100644 --- a/src/PhpWord/Style/Numbering.php +++ b/src/PhpWord/Style/Numbering.php @@ -105,15 +105,13 @@ public function getLevels(): array */ public function setLevels(array $values): self { - if (is_array($values)) { - foreach ($values as $key => $value) { - $numberingLevel = new NumberingLevel(); - if (is_array($value)) { - $numberingLevel->setStyleByArray($value); - $numberingLevel->setLevel($key); - } - $this->levels[$key] = $numberingLevel; + foreach ($values as $key => $value) { + $numberingLevel = new NumberingLevel(); + if (is_array($value)) { + $numberingLevel->setStyleByArray($value); + $numberingLevel->setLevel($key); } + $this->levels[$key] = $numberingLevel; } return $this; diff --git a/src/PhpWord/Style/NumberingLevel.php b/src/PhpWord/Style/NumberingLevel.php index 31ec3738c8..f2505a330d 100644 --- a/src/PhpWord/Style/NumberingLevel.php +++ b/src/PhpWord/Style/NumberingLevel.php @@ -93,7 +93,7 @@ class NumberingLevel extends AbstractStyle /** * Justification, w:lvlJc. * - * @var string, one of PhpOffice\PhpWord\SimpleType\Jc + * @var string one of PhpOffice\PhpWord\SimpleType\Jc */ private $alignment = ''; diff --git a/src/PhpWord/Style/Row.php b/src/PhpWord/Style/Row.php index 31ae3dedc1..749839c4e2 100644 --- a/src/PhpWord/Style/Row.php +++ b/src/PhpWord/Style/Row.php @@ -55,22 +55,16 @@ public function __construct() /** * Is tblHeader. - * - * @return bool */ - public function isTblHeader() + public function isTblHeader(): bool { return $this->tblHeader; } /** * Is tblHeader. - * - * @param bool $value - * - * @return self */ - public function setTblHeader($value = true) + public function setTblHeader(bool $value = true): self { $this->tblHeader = $this->setBoolVal($value, $this->tblHeader); @@ -79,22 +73,16 @@ public function setTblHeader($value = true) /** * Is cantSplit. - * - * @return bool */ - public function isCantSplit() + public function isCantSplit(): bool { return $this->cantSplit; } /** * Is cantSplit. - * - * @param bool $value - * - * @return self */ - public function setCantSplit($value = true) + public function setCantSplit(bool $value = true): self { $this->cantSplit = $this->setBoolVal($value, $this->cantSplit); @@ -103,22 +91,16 @@ public function setCantSplit($value = true) /** * Is exactHeight. - * - * @return bool */ - public function isExactHeight() + public function isExactHeight(): bool { return $this->exactHeight; } /** * Set exactHeight. - * - * @param bool $value - * - * @return self */ - public function setExactHeight($value = true) + public function setExactHeight(bool $value = true): self { $this->exactHeight = $this->setBoolVal($value, $this->exactHeight); diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index 21e8a74823..2510a3fa72 100644 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -23,7 +23,6 @@ use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\SimpleType\JcTable; use PhpOffice\PhpWord\SimpleType\TblWidth; -use PhpOffice\PhpWord\Style; class Table extends Border { @@ -136,7 +135,7 @@ class Table extends Border /** * @var null|float|int cell spacing value */ - protected $cellSpacing; + private $cellSpacing; /** * @var string Table Layout @@ -171,21 +170,29 @@ class Table extends Border /** * Create new table style. - * - * @param mixed $tableStyle - * @param mixed $firstRowStyle */ - public function __construct($tableStyle = null, $firstRowStyle = null) + public function __construct(?array $tableStyle = null, ?array $firstRowStyle = null) { // Clone first row from table style, but with certain properties disabled - if ($firstRowStyle !== null && is_array($firstRowStyle)) { + if ($firstRowStyle !== null) { $this->firstRowStyle = clone $this; $this->firstRowStyle->isFirstRow = true; - unset($this->firstRowStyle->firstRowStyle, $this->firstRowStyle->borderInsideHSize, $this->firstRowStyle->borderInsideHColor, $this->firstRowStyle->borderInsideVSize, $this->firstRowStyle->borderInsideVColor, $this->firstRowStyle->cellMarginTop, $this->firstRowStyle->cellMarginLeft, $this->firstRowStyle->cellMarginRight, $this->firstRowStyle->cellMarginBottom, $this->firstRowStyle->cellSpacing); + unset( + $this->firstRowStyle->firstRowStyle, + $this->firstRowStyle->borderInsideHSize, + $this->firstRowStyle->borderInsideHColor, + $this->firstRowStyle->borderInsideVSize, + $this->firstRowStyle->borderInsideVColor, + $this->firstRowStyle->cellMarginTop, + $this->firstRowStyle->cellMarginLeft, + $this->firstRowStyle->cellMarginRight, + $this->firstRowStyle->cellMarginBottom, + $this->firstRowStyle->cellSpacing + ); $this->firstRowStyle->setStyleByArray($firstRowStyle); } - if ($tableStyle !== null && is_array($tableStyle)) { + if ($tableStyle !== null) { $this->setStyleByArray($tableStyle); } } @@ -193,9 +200,11 @@ public function __construct($tableStyle = null, $firstRowStyle = null) /** * @param null|float|int $cellSpacing */ - public function setCellSpacing($cellSpacing = null): void + public function setCellSpacing($cellSpacing = null): self { $this->cellSpacing = $cellSpacing; + + return $this; } /** diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 7dd99af291..073393ffc4 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -133,7 +133,10 @@ public function __construct($documentTemplate) $this->tempDocumentMainPart = $this->readPartWithRels($this->getMainPartName()); $this->tempDocumentSettingsPart = $this->readPartWithRels($this->getSettingsPartName()); - $this->tempDocumentContentTypes = $this->zipClass->getFromName($this->getDocumentContentTypesName()); + $tempDocumentContentTypes = $this->zipClass->getFromName($this->getDocumentContentTypesName()); + if (is_string($tempDocumentContentTypes)) { + $this->tempDocumentContentTypes = $tempDocumentContentTypes; + } } public function __destruct() @@ -316,8 +319,8 @@ public function setComplexBlock($search, Element\AbstractElement $complexType): } /** - * @param mixed $search - * @param mixed $replace + * @param array|string $search + * @param null|array|bool|float|int|string $replace * @param int $limit */ public function setValue($search, $replace, $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT): void @@ -337,7 +340,7 @@ public function setValue($search, $replace, $limit = self::MAXIMUM_REPLACEMENTS_ } unset($item); } else { - $replace = static::ensureUtf8Encoded($replace); + $replace = static::ensureUtf8Encoded(null === $replace ? null : (string) $replace); } if (Settings::isOutputEscapingEnabled()) { @@ -678,7 +681,7 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM foreach ($searchReplace as $searchString => $replaceImage) { $varsToReplace = array_filter($partVariables, function ($partVar) use ($searchString) { - return ($partVar == $searchString) || preg_match('/^' . preg_quote($searchString) . ':/', $partVar); + return ($partVar == $searchString) || preg_match('/^' . preg_quote($searchString, '/') . ':/', $partVar); }); foreach ($varsToReplace as $varNameWithArgs) { @@ -697,7 +700,7 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM // replace variable $varNameWithArgsFixed = static::ensureMacroCompleted($varNameWithArgs); $matches = []; - if (preg_match('/(<[^<]+>)([^<]*)(' . preg_quote($varNameWithArgsFixed) . ')([^>]*)(<[^>]+>)/Uu', $partContent, $matches)) { + if (preg_match('/(<[^<]+>)([^<]*)(' . preg_quote($varNameWithArgsFixed, '/') . ')([^>]*)(<[^>]+>)/Uu', $partContent, $matches)) { $wholeTag = $matches[0]; array_shift($matches); [$openTag, $prefix, , $postfix, $closeTag] = $matches; @@ -1078,12 +1081,12 @@ function ($match) { /** * Find and replace macros in the given XML section. * - * @param mixed $search - * @param mixed $replace + * @param array|string $search + * @param array|string $replace * @param array|string $documentPartXML * @param int $limit * - * @return string + * @return ($documentPartXML is string ? string : array) */ protected function setValueForPart($search, $replace, $documentPartXML, $limit) { diff --git a/src/PhpWord/Writer/EPub3/Part/ContentXhtml.php b/src/PhpWord/Writer/EPub3/Part/ContentXhtml.php index 3ebd82638e..74143a30ef 100644 --- a/src/PhpWord/Writer/EPub3/Part/ContentXhtml.php +++ b/src/PhpWord/Writer/EPub3/Part/ContentXhtml.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpWord\Writer\EPub3\Part; +use PhpOffice\PhpWord\Element\AbstractElement; use PhpOffice\PhpWord\Element\Text; use PhpOffice\PhpWord\Element\TextRun; use PhpOffice\PhpWord\PhpWord; @@ -91,14 +92,14 @@ public function write(): string return $xmlWriter->outputMemory(true); } - protected function writeTextElement(\PhpOffice\PhpWord\Element\AbstractElement $textElement, XMLWriter $xmlWriter): void + protected function writeTextElement(AbstractElement $textElement, XMLWriter $xmlWriter): void { if ($textElement instanceof Text) { $text = $textElement->getText(); if ($text !== null) { $xmlWriter->text((string) $text); } - } elseif (is_object($textElement) && method_exists($textElement, 'getText')) { + } elseif (method_exists($textElement, 'getText')) { $text = $textElement->getText(); if ($text instanceof TextRun) { $this->writeTextRun($text, $xmlWriter); diff --git a/src/PhpWord/Writer/HTML.php b/src/PhpWord/Writer/HTML.php index 41229b45d5..acaa3c4800 100644 --- a/src/PhpWord/Writer/HTML.php +++ b/src/PhpWord/Writer/HTML.php @@ -21,7 +21,7 @@ use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Shared\Validate; -use PhpOffice\PhpWord\Style\Font; +use PhpOffice\PhpWord\Writer\HTML\Part\AbstractPart; /** * HTML writer. @@ -76,9 +76,9 @@ public function __construct(?PhpWord $phpWord = null) $this->parts = ['Head', 'Body']; foreach ($this->parts as $partName) { - $partClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Part\\' . $partName; + $partClass = self::class . '\\Part\\' . $partName; if (class_exists($partClass)) { - /** @var HTML\Part\AbstractPart $part Type hint */ + /** @var AbstractPart $part Type hint */ $part = new $partClass(); $part->setParentWriter($this); $this->writerParts[strtolower($partName)] = $part; diff --git a/src/PhpWord/Writer/HTML/Element/Ruby.php b/src/PhpWord/Writer/HTML/Element/Ruby.php index b3ab13c35e..5648d85e2f 100644 --- a/src/PhpWord/Writer/HTML/Element/Ruby.php +++ b/src/PhpWord/Writer/HTML/Element/Ruby.php @@ -100,10 +100,6 @@ private function getPropertyCssForRtTag(RubyProperties $properties): string private function getParagraphStyleForTextRun(TextRun $textRun, string $extraCSS): string { $style = ''; - if (!method_exists($textRun, 'getParagraphStyle')) { - return $style; - } - $paragraphStyle = $textRun->getParagraphStyle(); $pStyleIsObject = ($paragraphStyle instanceof Paragraph); if ($pStyleIsObject) { diff --git a/src/PhpWord/Writer/HTML/Style/Table.php b/src/PhpWord/Writer/HTML/Style/Table.php index a59d1cdba4..6d3e43e812 100644 --- a/src/PhpWord/Writer/HTML/Style/Table.php +++ b/src/PhpWord/Writer/HTML/Style/Table.php @@ -35,19 +35,19 @@ public function write() } $css = []; - if (is_object($style) && method_exists($style, 'getLayout')) { + if (method_exists($style, 'getLayout')) { if ($style->getLayout() == StyleTable::LAYOUT_FIXED) { $css['table-layout'] = 'fixed'; } elseif ($style->getLayout() == StyleTable::LAYOUT_AUTO) { $css['table-layout'] = 'auto'; } } - if (is_object($style) && method_exists($style, 'isBidiVisual')) { + if (method_exists($style, 'isBidiVisual')) { if ($style->isBidiVisual()) { $css['direction'] = 'rtl'; } } - if (is_object($style) && method_exists($style, 'getVAlign')) { + if (method_exists($style, 'getVAlign')) { $css['vertical-align'] = $style->getVAlign(); } diff --git a/src/PhpWord/Writer/ODText/Element/Field.php b/src/PhpWord/Writer/ODText/Element/Field.php index 6b54807869..2f81eb3f3d 100644 --- a/src/PhpWord/Writer/ODText/Element/Field.php +++ b/src/PhpWord/Writer/ODText/Element/Field.php @@ -57,12 +57,12 @@ private function writeDefault(\PhpOffice\PhpWord\Element\Field $element, $type): $xmlWriter = $this->getXmlWriter(); $xmlWriter->startElement('text:span'); - if (method_exists($element, 'getFontStyle')) { - $fstyle = $element->getFontStyle(); - if (is_string($fstyle)) { - $xmlWriter->writeAttribute('text:style-name', $fstyle); - } + + $fstyle = $element->getFontStyle(); + if (is_string($fstyle)) { + $xmlWriter->writeAttribute('text:style-name', $fstyle); } + switch ($type) { case 'date': $xmlWriter->startElement('text:date'); diff --git a/src/PhpWord/Writer/ODText/Part/Content.php b/src/PhpWord/Writer/ODText/Part/Content.php index de82ed96d1..b4958e84e6 100644 --- a/src/PhpWord/Writer/ODText/Part/Content.php +++ b/src/PhpWord/Writer/ODText/Part/Content.php @@ -106,7 +106,8 @@ public function write() $xmlWriter->writeElement('dc:date', $trackedChange->getDate()->format('Y-m-d\TH:i:s\Z')); } $xmlWriter->endElement(); // office:change-info - if ($trackedChange->getChangeType() == TrackChange::DELETED) { + if ($trackedChange->getChangeType() == TrackChange::DELETED && method_exists($trackedElement, 'getText')) { + // @phpstan-ignore-next-line $xmlWriter->writeElement('text:p', $trackedElement->getText()); } diff --git a/src/PhpWord/Writer/ODText/Style/Font.php b/src/PhpWord/Writer/ODText/Style/Font.php index 85720f0274..95582ec48b 100644 --- a/src/PhpWord/Writer/ODText/Style/Font.php +++ b/src/PhpWord/Writer/ODText/Style/Font.php @@ -36,7 +36,7 @@ public function write(): void } $xmlWriter = $this->getXmlWriter(); - $stylep = (method_exists($style, 'getParagraph')) ? $style->getParagraph() : null; + $stylep = $style->getParagraph(); if ($stylep instanceof \PhpOffice\PhpWord\Style\Paragraph) { $temp1 = clone $stylep; $temp1->setStyleName($style->getStyleName()); diff --git a/src/PhpWord/Writer/ODText/Style/Paragraph.php b/src/PhpWord/Writer/ODText/Style/Paragraph.php index 99963bd4a3..ca22a0934c 100644 --- a/src/PhpWord/Writer/ODText/Style/Paragraph.php +++ b/src/PhpWord/Writer/ODText/Style/Paragraph.php @@ -76,9 +76,7 @@ public function write(): void $psm = 'Heading_' . substr($styleName, 2); $stylep = Style::getStyle($psm); if ($stylep instanceof Style\Font) { - if (method_exists($stylep, 'getParagraph')) { - $stylep = $stylep->getParagraph(); - } + $stylep = $stylep->getParagraph(); } if ($stylep instanceof Style\Paragraph) { if ($stylep->hasPageBreakBefore()) { diff --git a/src/PhpWord/Writer/RTF/Part/Document.php b/src/PhpWord/Writer/RTF/Part/Document.php index bd659c6794..484393477d 100644 --- a/src/PhpWord/Writer/RTF/Part/Document.php +++ b/src/PhpWord/Writer/RTF/Part/Document.php @@ -55,27 +55,33 @@ public function write() private function writeInfo() { $docProps = $this->getParentWriter()->getPhpWord()->getDocInfo(); - $properties = ['title', 'subject', 'category', 'keywords', 'comment', - 'author', 'operator', 'creatim', 'revtim', 'company', 'manager', ]; - $mapping = [ + $properties = [ + 'title' => 'title', + 'subject' => 'subject', + 'category' => 'category', + 'keywords' => 'keywords', 'comment' => 'description', 'author' => 'creator', 'operator' => 'lastModifiedBy', 'creatim' => 'created', - 'revtim' => 'modified', ]; + 'revtim' => 'modified', + 'company' => 'company', + 'manager' => 'manager', + ]; $dateFields = ['creatim', 'revtim']; $content = ''; $content .= '{'; $content .= '\info'; - foreach ($properties as $property) { - $method = 'get' . ($mapping[$property] ?? $property); + foreach ($properties as $property => $propertyMethod) { + $method = 'get' . $propertyMethod; + + $value = $docProps->$method(); if (!in_array($property, $dateFields) && Settings::isOutputEscapingEnabled()) { - $value = $this->escaper->escape($docProps->$method()); - } else { - $value = $docProps->$method(); + $value = $this->escaper->escape($value); } + $value = in_array($property, $dateFields) ? $this->getDateValue($value) : $value; $content .= "{\\{$property} {$value}}"; } diff --git a/src/PhpWord/Writer/RTF/Style/Border.php b/src/PhpWord/Writer/RTF/Style/Border.php index 8f3024493c..c8dc943579 100644 --- a/src/PhpWord/Writer/RTF/Style/Border.php +++ b/src/PhpWord/Writer/RTF/Style/Border.php @@ -85,7 +85,7 @@ private function writeSide($side, $width, $color = '') if ($rtfWriter !== null) { $colorTable = $rtfWriter->getColorTable(); $index = array_search($color, $colorTable); - if ($index !== false && $colorIndex !== null) { + if ($index !== false) { $colorIndex = $index + 1; } } diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index e54bfa8ad2..9702c210bd 100644 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -222,7 +222,7 @@ private function addNotes(ZipArchive $zip, &$rId, $noteType = 'footnote'): void $phpWord = $this->getPhpWord(); $noteType = ($noteType == 'endnote') ? 'endnote' : 'footnote'; $partName = "{$noteType}s"; - $method = 'get' . $partName; + $method = 'get' . ucfirst($partName); $collection = $phpWord->$method(); // Add footnotes media files, relations, and contents diff --git a/src/PhpWord/Writer/Word2007/Element/FormField.php b/src/PhpWord/Writer/Word2007/Element/FormField.php index c5d59a4497..1e58f58e55 100644 --- a/src/PhpWord/Writer/Word2007/Element/FormField.php +++ b/src/PhpWord/Writer/Word2007/Element/FormField.php @@ -29,7 +29,7 @@ * @since 0.12.0 * @see http://www.datypic.com/sc/ooxml/t-w_CT_FFData.html * - * @SuppressWarnings(PHPMD.UnusedPrivateMethod) + * @SuppressWarnings("PHPMD.UnusedPrivateMethod") */ class FormField extends Text { diff --git a/src/PhpWord/Writer/Word2007/Element/SDT.php b/src/PhpWord/Writer/Word2007/Element/SDT.php index 950c293776..dfe5ca9bca 100644 --- a/src/PhpWord/Writer/Word2007/Element/SDT.php +++ b/src/PhpWord/Writer/Word2007/Element/SDT.php @@ -27,7 +27,7 @@ * @since 0.12.0 * @see http://www.datypic.com/sc/ooxml/t-w_CT_SdtBlock.html * - * @SuppressWarnings(PHPMD.UnusedPrivateMethod) + * @SuppressWarnings("PHPMD.UnusedPrivateMethod") */ class SDT extends Text { diff --git a/src/PhpWord/Writer/Word2007/Element/Shape.php b/src/PhpWord/Writer/Word2007/Element/Shape.php index 653c31809a..0af2831ba6 100644 --- a/src/PhpWord/Writer/Word2007/Element/Shape.php +++ b/src/PhpWord/Writer/Word2007/Element/Shape.php @@ -28,7 +28,7 @@ * * @since 0.12.0 * - * @SuppressWarnings(PHPMD.UnusedPrivateMethod) + * @SuppressWarnings("PHPMD.UnusedPrivateMethod") */ class Shape extends AbstractElement { diff --git a/src/PhpWord/Writer/Word2007/Part/Numbering.php b/src/PhpWord/Writer/Word2007/Part/Numbering.php index ca29cd01d5..2dee7c2b1e 100644 --- a/src/PhpWord/Writer/Word2007/Part/Numbering.php +++ b/src/PhpWord/Writer/Word2007/Part/Numbering.php @@ -69,10 +69,8 @@ public function write() $xmlWriter->writeAttribute('w:val', $style->getType()); $xmlWriter->endElement(); // w:multiLevelType - if (is_array($levels)) { - foreach ($levels as $level) { - $this->writeLevel($xmlWriter, $level); - } + foreach ($levels as $level) { + $this->writeLevel($xmlWriter, $level); } $xmlWriter->endElement(); // w:abstractNum } diff --git a/src/PhpWord/Writer/Word2007/Part/Styles.php b/src/PhpWord/Writer/Word2007/Part/Styles.php index edf0314cc2..0ed9d6b6fb 100644 --- a/src/PhpWord/Writer/Word2007/Part/Styles.php +++ b/src/PhpWord/Writer/Word2007/Part/Styles.php @@ -32,7 +32,7 @@ * * @todo Do something with the numbering style introduced in 0.10.0 * - * @SuppressWarnings(PHPMD.UnusedPrivateMethod) For writeFontStyle, writeParagraphStyle, and writeTableStyle + * @SuppressWarnings("PHPMD.UnusedPrivateMethod") For writeFontStyle, writeParagraphStyle, and writeTableStyle */ class Styles extends AbstractPart { diff --git a/src/PhpWord/Writer/Word2007/Style/Table.php b/src/PhpWord/Writer/Word2007/Style/Table.php index 446fc3b1a4..711f3ecde7 100644 --- a/src/PhpWord/Writer/Word2007/Style/Table.php +++ b/src/PhpWord/Writer/Word2007/Style/Table.php @@ -151,7 +151,7 @@ private function writeBorder(XMLWriter $xmlWriter, TableStyle $style): void * * @param string $elementName * @param string $unit - * @param float|int $width + * @param null|float|int $width */ private function writeTblWidth(XMLWriter $xmlWriter, $elementName, $unit, $width = null): void { @@ -159,7 +159,7 @@ private function writeTblWidth(XMLWriter $xmlWriter, $elementName, $unit, $width return; } $xmlWriter->startElement($elementName); - $xmlWriter->writeAttributeIf(null !== $width, 'w:w', $width); + $xmlWriter->writeAttribute('w:w', $width); $xmlWriter->writeAttribute('w:type', $unit); $xmlWriter->endElement(); } diff --git a/tests/PhpWordTests/ComplexType/RubyPropertiesTest.php b/tests/PhpWordTests/ComplexType/RubyPropertiesTest.php index 6d16ebb21d..62bc0b0739 100644 --- a/tests/PhpWordTests/ComplexType/RubyPropertiesTest.php +++ b/tests/PhpWordTests/ComplexType/RubyPropertiesTest.php @@ -34,15 +34,14 @@ class RubyPropertiesTest extends \PHPUnit\Framework\TestCase public function testConstruct(): void { $properties = new RubyProperties(); - self::assertInstanceOf('PhpOffice\\PhpWord\\ComplexType\\RubyProperties', $properties); self::assertIsString($properties->getAlignment()); - self::assertTrue($properties->getAlignment() !== '' && $properties->getAlignment() !== null); + self::assertNotEmpty($properties->getAlignment()); self::assertIsFloat($properties->getFontFaceSize()); self::assertIsFloat($properties->getFontPointsAboveBaseText()); self::assertIsFloat($properties->getFontSizeForBaseText()); self::assertIsString($properties->getLanguageId()); - self::assertTrue($properties->getLanguageId() !== '' && $properties->getLanguageId() !== null); + self::assertTrue($properties->getLanguageId() !== ''); } /** @@ -52,7 +51,7 @@ public function testAlignment(): void { $properties = new RubyProperties(); self::assertIsString($properties->getAlignment()); - self::assertTrue($properties->getAlignment() !== '' && $properties->getAlignment() !== null); + self::assertNotEmpty($properties->getAlignment()); $properties->setAlignment(RubyProperties::ALIGNMENT_RIGHT_VERTICAL); self::assertEquals(RubyProperties::ALIGNMENT_RIGHT_VERTICAL, $properties->getAlignment()); } @@ -133,7 +132,7 @@ public function testLanguageId(): void { $properties = new RubyProperties(); - self::assertTrue($properties->getLanguageId() !== '' && $properties->getLanguageId() !== null); + self::assertNotEmpty($properties->getLanguageId()); $properties->setLanguageId('en-US'); self::assertIsString($properties->getLanguageId()); self::assertEquals('en-US', $properties->getLanguageId()); diff --git a/tests/PhpWordTests/Element/AbstractElementTest.php b/tests/PhpWordTests/Element/AbstractElementTest.php index 307225b8f4..9ce05750b8 100644 --- a/tests/PhpWordTests/Element/AbstractElementTest.php +++ b/tests/PhpWordTests/Element/AbstractElementTest.php @@ -30,6 +30,7 @@ class AbstractElementTest extends \PHPUnit\Framework\TestCase */ public function testElementIndex(): void { + // @phpstan-ignore-next-line if (method_exists($this, 'getMockForAbstractClass')) { $stub = $this->getMockForAbstractClass(AbstractElement::class); } else { @@ -47,6 +48,7 @@ public function testElementIndex(): void */ public function testElementId(): void { + // @phpstan-ignore-next-line if (method_exists($this, 'getMockForAbstractClass')) { $stub = $this->getMockForAbstractClass(AbstractElement::class); } else { diff --git a/tests/PhpWordTests/Element/BookmarkTest.php b/tests/PhpWordTests/Element/BookmarkTest.php index 87abf971e5..097166736f 100644 --- a/tests/PhpWordTests/Element/BookmarkTest.php +++ b/tests/PhpWordTests/Element/BookmarkTest.php @@ -35,7 +35,6 @@ public function testConstruct(): void $bookmarkName = 'test'; $oBookmark = new Bookmark($bookmarkName); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Bookmark', $oBookmark); self::assertEquals($bookmarkName, $oBookmark->getName()); } } diff --git a/tests/PhpWordTests/Element/CellTest.php b/tests/PhpWordTests/Element/CellTest.php index 919d627bfb..2fedcafc24 100644 --- a/tests/PhpWordTests/Element/CellTest.php +++ b/tests/PhpWordTests/Element/CellTest.php @@ -36,7 +36,6 @@ public function testConstruct(): void { $oCell = new Cell(); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Cell', $oCell); self::assertNull($oCell->getWidth()); } diff --git a/tests/PhpWordTests/Element/CheckBoxTest.php b/tests/PhpWordTests/Element/CheckBoxTest.php index 761cfba649..fbdbf36aa3 100644 --- a/tests/PhpWordTests/Element/CheckBoxTest.php +++ b/tests/PhpWordTests/Element/CheckBoxTest.php @@ -36,7 +36,6 @@ public function testConstruct(): void { $oCheckBox = new CheckBox(); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\CheckBox', $oCheckBox); self::assertNull($oCheckBox->getText()); self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oCheckBox->getFontStyle()); self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oCheckBox->getParagraphStyle()); diff --git a/tests/PhpWordTests/Element/CommentTest.php b/tests/PhpWordTests/Element/CommentTest.php index c887362cbc..f76316d890 100644 --- a/tests/PhpWordTests/Element/CommentTest.php +++ b/tests/PhpWordTests/Element/CommentTest.php @@ -45,7 +45,6 @@ public function testConstructDefault(): void $oComment->setStartElement($oText); $oComment->setEndElement($oText); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Comment', $oComment); self::assertEquals($author, $oComment->getAuthor()); self::assertEquals($date, $oComment->getDate()); self::assertEquals($initials, $oComment->getInitials()); diff --git a/tests/PhpWordTests/Element/FieldTest.php b/tests/PhpWordTests/Element/FieldTest.php index f3accf8789..f624e9294d 100644 --- a/tests/PhpWordTests/Element/FieldTest.php +++ b/tests/PhpWordTests/Element/FieldTest.php @@ -29,16 +29,6 @@ */ class FieldTest extends \PHPUnit\Framework\TestCase { - /** - * New instance. - */ - public function testConstructNull(): void - { - $oField = new Field(); - - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); - } - /** * New instance with type. */ @@ -46,7 +36,6 @@ public function testConstructWithType(): void { $oField = new Field('DATE'); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); self::assertEquals('DATE', $oField->getType()); } @@ -57,7 +46,6 @@ public function testConstructWithTypeProperties(): void { $oField = new Field('DATE', ['dateformat' => 'd-M-yyyy']); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); self::assertEquals('DATE', $oField->getType()); self::assertEquals(['dateformat' => 'd-M-yyyy'], $oField->getProperties()); } @@ -69,7 +57,6 @@ public function testConstructWithTypePropertiesOptions(): void { $oField = new Field('DATE', ['dateformat' => 'd-M-yyyy'], ['SakaEraCalendar', 'PreserveFormat']); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); self::assertEquals('DATE', $oField->getType()); self::assertEquals(['dateformat' => 'd-M-yyyy'], $oField->getProperties()); self::assertEquals(['SakaEraCalendar', 'PreserveFormat'], $oField->getOptions()); @@ -82,7 +69,6 @@ public function testConstructWithTypePropertiesOptionsText(): void { $oField = new Field('XE', [], ['Bold', 'Italic'], 'FieldValue'); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); self::assertEquals('XE', $oField->getType()); self::assertEquals([], $oField->getProperties()); self::assertEquals(['Bold', 'Italic'], $oField->getOptions()); @@ -99,7 +85,6 @@ public function testConstructWithTypePropertiesOptionsTextAsTextRun(): void $oField = new Field('XE', [], ['Bold', 'Italic'], $textRun); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); self::assertEquals('XE', $oField->getType()); self::assertEquals([], $oField->getProperties()); self::assertEquals(['Bold', 'Italic'], $oField->getOptions()); @@ -110,7 +95,6 @@ public function testConstructWithOptionValue(): void { $oField = new Field('INDEX', [], ['\\c "3" \\h "A"']); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); self::assertEquals('INDEX', $oField->getType()); self::assertEquals([], $oField->getProperties()); self::assertEquals(['\\c "3" \\h "A"'], $oField->getOptions()); diff --git a/tests/PhpWordTests/Element/FooterTest.php b/tests/PhpWordTests/Element/FooterTest.php index e167204f17..f97b159cb9 100644 --- a/tests/PhpWordTests/Element/FooterTest.php +++ b/tests/PhpWordTests/Element/FooterTest.php @@ -36,7 +36,6 @@ public function testConstruct(): void $iVal = mt_rand(1, 1000); $oFooter = new Footer($iVal); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Footer', $oFooter); self::assertEquals($iVal, $oFooter->getSectionId()); } diff --git a/tests/PhpWordTests/Element/FootnoteTest.php b/tests/PhpWordTests/Element/FootnoteTest.php index cd64d6ddf0..c6297cfc32 100644 --- a/tests/PhpWordTests/Element/FootnoteTest.php +++ b/tests/PhpWordTests/Element/FootnoteTest.php @@ -34,7 +34,6 @@ public function testConstruct(): void { $oFootnote = new Footnote(); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Footnote', $oFootnote); self::assertCount(0, $oFootnote->getElements()); self::assertNull($oFootnote->getParagraphStyle()); } diff --git a/tests/PhpWordTests/Element/FormulaTest.php b/tests/PhpWordTests/Element/FormulaTest.php index 0eb2805d0b..dcb730e9cc 100644 --- a/tests/PhpWordTests/Element/FormulaTest.php +++ b/tests/PhpWordTests/Element/FormulaTest.php @@ -30,16 +30,6 @@ */ class FormulaTest extends AbstractWebServerEmbedded { - /** - * @covers \PhpOffice\PhpWord\Element\Formula::__construct - */ - public function testConstruct(): void - { - $element = new Formula(new Math()); - - self::assertInstanceOf(Formula::class, $element); - } - /** * @covers \PhpOffice\PhpWord\Element\Formula::getMath * @covers \PhpOffice\PhpWord\Element\Formula::setMath @@ -54,11 +44,10 @@ public function testMath(): void $element = new Formula(new Math()); - self::assertInstanceOf(Formula::class, $element); self::assertEquals(new Math(), $element->getMath()); self::assertNotEquals($math, $element->getMath()); - self::assertInstanceOf(Formula::class, $element->setMath($math)); + $element->setMath($math); self::assertNotEquals(new Math(), $element->getMath()); self::assertEquals($math, $element->getMath()); } diff --git a/tests/PhpWordTests/Element/HeaderTest.php b/tests/PhpWordTests/Element/HeaderTest.php index 24b7dd32f9..6d659dbd7c 100644 --- a/tests/PhpWordTests/Element/HeaderTest.php +++ b/tests/PhpWordTests/Element/HeaderTest.php @@ -37,7 +37,6 @@ public function testConstructDefault(): void $iVal = mt_rand(1, 1000); $oHeader = new Header($iVal); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Header', $oHeader); self::assertEquals($iVal, $oHeader->getSectionId()); self::assertEquals(Header::AUTO, $oHeader->getType()); } diff --git a/tests/PhpWordTests/Element/ImageTest.php b/tests/PhpWordTests/Element/ImageTest.php index 86b6b77c58..f56b3da8a9 100644 --- a/tests/PhpWordTests/Element/ImageTest.php +++ b/tests/PhpWordTests/Element/ImageTest.php @@ -35,7 +35,6 @@ public function testConstruct(): void $src = __DIR__ . '/../_files/images/firefox.png'; $oImage = new Image($src); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage); self::assertEquals($src, $oImage->getSource()); self::assertEquals(md5($src), $oImage->getMediaId()); self::assertFalse($oImage->isWatermark()); @@ -72,7 +71,6 @@ public function testImages($source, $type, $extension, $createFunction, $imageFu $nam = ucfirst((string) strtok($source, '.')); $source = __DIR__ . "/../_files/images/{$source}"; $image = new Image($source, null, null, $nam); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $image); self::assertEquals($source, $image->getSource()); self::assertEquals($nam, $image->getName()); self::assertEquals(md5($source), $image->getMediaId()); @@ -129,7 +127,7 @@ public function testInvalidImagePhp(): void { $this->expectException(\PhpOffice\PhpWord\Exception\InvalidImageException::class); $object = new Image('test.php'); - $object->getSource(); + $source = $object->getSource(); } /** @@ -147,7 +145,7 @@ public function testUnsupportedImage(): void ]; stream_context_set_default($arrContextOptions); $object = new Image(self::getRemoteBmpImageUrl()); - $object->getSource(); + $source = $object->getSource(); } /** @@ -204,7 +202,6 @@ public function testConstructFromString(): void $source = file_get_contents(__DIR__ . '/../_files/images/earth.jpg'); $image = new Image($source); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $image); self::assertEquals($source, $image->getSource()); self::assertEquals(md5((string) $source), $image->getMediaId()); self::assertEquals('image/jpeg', $image->getImageType()); @@ -226,7 +223,6 @@ public function testConstructFromGd(): void $source = self::getRemoteImageUrl(); $image = new Image($source); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $image); self::assertEquals($source, $image->getSource()); self::assertEquals(md5($source), $image->getMediaId()); self::assertEquals('image/png', $image->getImageType()); @@ -247,6 +243,6 @@ public function testInvalidImageString(): void { $this->expectException(\PhpOffice\PhpWord\Exception\InvalidImageException::class); $object = new Image('this_is-a_non_valid_image'); - $object->getSource(); + $source = $object->getSource(); } } diff --git a/tests/PhpWordTests/Element/LineTest.php b/tests/PhpWordTests/Element/LineTest.php index 98298fc76c..f4a39b38d1 100644 --- a/tests/PhpWordTests/Element/LineTest.php +++ b/tests/PhpWordTests/Element/LineTest.php @@ -36,7 +36,6 @@ public function testConstruct(): void { $oLine = new Line(); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Line', $oLine); self::assertNull($oLine->getStyle()); } diff --git a/tests/PhpWordTests/Element/LinkTest.php b/tests/PhpWordTests/Element/LinkTest.php index 6a87b308c1..5b5c6f77bf 100644 --- a/tests/PhpWordTests/Element/LinkTest.php +++ b/tests/PhpWordTests/Element/LinkTest.php @@ -37,7 +37,6 @@ public function testConstructDefault(): void { $oLink = new Link('https://github.com/PHPOffice/PHPWord'); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $oLink); self::assertEquals('https://github.com/PHPOffice/PHPWord', $oLink->getSource()); self::assertEquals($oLink->getSource(), $oLink->getText()); self::assertNull($oLink->getFontStyle()); @@ -56,7 +55,6 @@ public function testConstructWithParamsArray(): void ['marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600] ); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $oLink); self::assertEquals('https://github.com/PHPOffice/PHPWord', $oLink->getSource()); self::assertEquals('PHPWord on GitHub', $oLink->getText()); self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oLink->getFontStyle()); diff --git a/tests/PhpWordTests/Element/ListItemRunTest.php b/tests/PhpWordTests/Element/ListItemRunTest.php index 633b8c3e26..69b5f990b0 100644 --- a/tests/PhpWordTests/Element/ListItemRunTest.php +++ b/tests/PhpWordTests/Element/ListItemRunTest.php @@ -34,7 +34,6 @@ public function testConstruct(): void { $oListItemRun = new ListItemRun(); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItemRun', $oListItemRun); self::assertCount(0, $oListItemRun->getElements()); self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oListItemRun->getParagraphStyle()); } @@ -46,7 +45,6 @@ public function testConstructString(): void { $oListItemRun = new ListItemRun(0, null, 'pStyle'); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItemRun', $oListItemRun); self::assertCount(0, $oListItemRun->getElements()); self::assertEquals('pStyle', $oListItemRun->getParagraphStyle()); } @@ -58,7 +56,6 @@ public function testConstructListString(): void { $oListItemRun = new ListItemRun(0, 'numberingStyle'); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItemRun', $oListItemRun); self::assertCount(0, $oListItemRun->getElements()); } @@ -69,7 +66,6 @@ public function testConstructArray(): void { $oListItemRun = new ListItemRun(0, null, ['spacing' => 100]); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItemRun', $oListItemRun); self::assertCount(0, $oListItemRun->getElements()); self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oListItemRun->getParagraphStyle()); } diff --git a/tests/PhpWordTests/Element/ObjectTest.php b/tests/PhpWordTests/Element/ObjectTest.php index 8ad856732b..2ef05567bc 100644 --- a/tests/PhpWordTests/Element/ObjectTest.php +++ b/tests/PhpWordTests/Element/ObjectTest.php @@ -37,7 +37,6 @@ public function testConstructWithSupportedFiles(): void $src = __DIR__ . '/../_files/documents/reader.docx'; $oObject = new OLEObject($src); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\OLEObject', $oObject); self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oObject->getStyle()); self::assertEquals($src, $oObject->getSource()); } @@ -50,7 +49,6 @@ public function testConstructWithSupportedFilesLong(): void $src = __DIR__ . '/../_files/documents/sheet.xls'; $oObject = new OLEObject($src); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\OLEObject', $oObject); self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oObject->getStyle()); self::assertEquals($src, $oObject->getSource()); } @@ -63,7 +61,7 @@ public function testConstructWithNotSupportedFiles(): void $this->expectException(\PhpOffice\PhpWord\Exception\InvalidObjectException::class); $src = __DIR__ . '/../_files/xsl/passthrough.xsl'; $oObject = new OLEObject($src); - $oObject->getSource(); + $source = $oObject->getSource(); } /** @@ -74,7 +72,6 @@ public function testConstructWithSupportedFilesAndStyle(): void $src = __DIR__ . '/../_files/documents/sheet.xls'; $oObject = new OLEObject($src, ['width' => '230px']); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\OLEObject', $oObject); self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Image', $oObject->getStyle()); self::assertEquals($src, $oObject->getSource()); } diff --git a/tests/PhpWordTests/Element/PageBreakTest.php b/tests/PhpWordTests/Element/PageBreakTest.php deleted file mode 100644 index 13cfb937d3..0000000000 --- a/tests/PhpWordTests/Element/PageBreakTest.php +++ /dev/null @@ -1,41 +0,0 @@ -getText()); self::assertNull($oPreserveText->getFontStyle()); self::assertNull($oPreserveText->getParagraphStyle()); diff --git a/tests/PhpWordTests/Element/RowTest.php b/tests/PhpWordTests/Element/RowTest.php index c2881acbc2..6dc8335ffd 100644 --- a/tests/PhpWordTests/Element/RowTest.php +++ b/tests/PhpWordTests/Element/RowTest.php @@ -36,7 +36,6 @@ public function testConstruct(): void { $oRow = new Row(); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Row', $oRow); self::assertNull($oRow->getHeight()); self::assertIsArray($oRow->getCells()); self::assertCount(0, $oRow->getCells()); diff --git a/tests/PhpWordTests/Element/RubyTest.php b/tests/PhpWordTests/Element/RubyTest.php index 0e14994fd2..44d2e5ba3c 100644 --- a/tests/PhpWordTests/Element/RubyTest.php +++ b/tests/PhpWordTests/Element/RubyTest.php @@ -36,14 +36,10 @@ public function testConstruct(): void { $ruby = new Ruby(new TextRun(), new TextRun(), new RubyProperties()); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Ruby', $ruby); self::assertEquals('', $ruby->getBaseTextRun()->getText()); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $ruby->getBaseTextRun()); self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $ruby->getBaseTextRun()->getParagraphStyle()); self::assertEquals('', $ruby->getRubyTextRun()->getText()); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $ruby->getRubyTextRun()); self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $ruby->getRubyTextRun()->getParagraphStyle()); - self::assertInstanceOf('PhpOffice\\PhpWord\\ComplexType\\RubyProperties', $ruby->getProperties()); self::assertEquals(RubyProperties::ALIGNMENT_DISTRIBUTE_SPACE, $ruby->getProperties()->getAlignment()); } @@ -58,7 +54,6 @@ public function testBaseText(): void $tr = new TextRun(); $tr->addText('Hello, world'); $ruby->setBaseTextRun($tr); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $ruby->getBaseTextRun()); self::assertEquals('Hello, world', $ruby->getBaseTextRun()->getText()); } @@ -73,7 +68,6 @@ public function testRubyText(): void $tr = new TextRun(); $tr->addText('Hello, ruby'); $ruby->setRubyTextRun($tr); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $ruby->getRubyTextRun()); self::assertEquals('Hello, ruby', $ruby->getRubyTextRun()->getText()); } @@ -94,7 +88,6 @@ public function testRubyProperties(): void $properties->setLanguageId('en-US'); $ruby->setProperties($properties); - self::assertInstanceOf('PhpOffice\\PhpWord\\ComplexType\\RubyProperties', $ruby->getProperties()); self::assertEquals(RubyProperties::ALIGNMENT_RIGHT_VERTICAL, $ruby->getProperties()->getAlignment()); self::assertEquals(1, $ruby->getProperties()->getFontFaceSize()); self::assertEquals(2, $ruby->getProperties()->getFontPointsAboveBaseText()); diff --git a/tests/PhpWordTests/Element/SDTTest.php b/tests/PhpWordTests/Element/SDTTest.php index 505ef14fe4..d7617160be 100644 --- a/tests/PhpWordTests/Element/SDTTest.php +++ b/tests/PhpWordTests/Element/SDTTest.php @@ -44,7 +44,6 @@ public function testConstruct(): void $object->setAlias($alias); $object->setTag($tag); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\SDT', $object); self::assertEquals($type, $object->getType()); self::assertEquals($types, $object->getListItems()); self::assertEquals($value, $object->getValue()); diff --git a/tests/PhpWordTests/Element/TableTest.php b/tests/PhpWordTests/Element/TableTest.php index 8e941cd056..0628d269e6 100644 --- a/tests/PhpWordTests/Element/TableTest.php +++ b/tests/PhpWordTests/Element/TableTest.php @@ -36,7 +36,6 @@ public function testConstruct(): void { $oTable = new Table(); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Table', $oTable); self::assertNull($oTable->getStyle()); self::assertNull($oTable->getWidth()); self::assertEquals([], $oTable->getRows()); diff --git a/tests/PhpWordTests/Element/TextBoxTest.php b/tests/PhpWordTests/Element/TextBoxTest.php index 6305b4e7ed..9289d1ad47 100644 --- a/tests/PhpWordTests/Element/TextBoxTest.php +++ b/tests/PhpWordTests/Element/TextBoxTest.php @@ -36,7 +36,6 @@ public function testConstruct(): void { $oTextBox = new TextBox(); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextBox', $oTextBox); self::assertNull($oTextBox->getStyle()); } diff --git a/tests/PhpWordTests/Element/TextRunTest.php b/tests/PhpWordTests/Element/TextRunTest.php index e3a2826f0c..b18eca99e4 100644 --- a/tests/PhpWordTests/Element/TextRunTest.php +++ b/tests/PhpWordTests/Element/TextRunTest.php @@ -38,7 +38,6 @@ public function testConstruct(): void { $oTextRun = new TextRun(); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun); self::assertCount(0, $oTextRun->getElements()); self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle()); } @@ -50,7 +49,6 @@ public function testConstructString(): void { $oTextRun = new TextRun('pStyle'); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun); self::assertCount(0, $oTextRun->getElements()); self::assertEquals('pStyle', $oTextRun->getParagraphStyle()); } @@ -62,7 +60,6 @@ public function testConstructArray(): void { $oTextRun = new TextRun(['spacing' => 100]); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun); self::assertCount(0, $oTextRun->getElements()); self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle()); } @@ -76,7 +73,6 @@ public function testConstructObject(): void $oParagraphStyle->setAlignment(Jc::BOTH); $oTextRun = new TextRun($oParagraphStyle); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun); self::assertCount(0, $oTextRun->getElements()); self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle()); self::assertEquals(Jc::BOTH, $oTextRun->getParagraphStyle()->getAlignment()); diff --git a/tests/PhpWordTests/Element/TextTest.php b/tests/PhpWordTests/Element/TextTest.php index ee3c67edd9..693430a87f 100644 --- a/tests/PhpWordTests/Element/TextTest.php +++ b/tests/PhpWordTests/Element/TextTest.php @@ -36,7 +36,6 @@ public function testConstruct(): void { $oText = new Text(); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $oText); self::assertNull($oText->getText()); self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', $oText->getFontStyle()); self::assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oText->getParagraphStyle()); diff --git a/tests/PhpWordTests/Element/TitleTest.php b/tests/PhpWordTests/Element/TitleTest.php index 2663189dd6..ab1e38102f 100644 --- a/tests/PhpWordTests/Element/TitleTest.php +++ b/tests/PhpWordTests/Element/TitleTest.php @@ -40,7 +40,6 @@ public function testConstruct(): void { $title = new Title('text'); - self::assertInstanceOf(Title::class, $title); self::assertEquals('text', $title->getText()); self::assertEquals(1, $title->getDepth()); self::assertNull($title->getPageNumber()); @@ -73,7 +72,6 @@ public function testConstructWithPageNumber(): void { $title = new Title('text', 1, 0); - self::assertInstanceOf(Title::class, $title); self::assertEquals('text', $title->getText()); self::assertEquals(0, $title->getPageNumber()); self::assertNull($title->getStyle()); diff --git a/tests/PhpWordTests/Element/TrackChangeTest.php b/tests/PhpWordTests/Element/TrackChangeTest.php index bf2e1dea1c..1d37fb3dd4 100644 --- a/tests/PhpWordTests/Element/TrackChangeTest.php +++ b/tests/PhpWordTests/Element/TrackChangeTest.php @@ -41,7 +41,6 @@ public function testConstructDefault(): void $oText = new Text('dummy text'); $oText->setTrackChange($oTrackChange); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TrackChange', $oTrackChange); self::assertEquals($author, $oTrackChange->getAuthor()); self::assertEquals($date, $oTrackChange->getDate()); self::assertEquals(TrackChange::INSERTED, $oTrackChange->getChangeType()); @@ -59,7 +58,6 @@ public function testConstructDefaultWithInvalidDate(): void $oText = new Text('dummy text'); $oText->setTrackChange($oTrackChange); - self::assertInstanceOf('PhpOffice\\PhpWord\\Element\\TrackChange', $oTrackChange); self::assertEquals($author, $oTrackChange->getAuthor()); self::assertEquals($date, null); self::assertEquals(TrackChange::INSERTED, $oTrackChange->getChangeType()); diff --git a/tests/PhpWordTests/Exception/CopyFileExceptionTest.php b/tests/PhpWordTests/Exception/CopyFileExceptionTest.php index a11ac79afa..9ca06d2b09 100644 --- a/tests/PhpWordTests/Exception/CopyFileExceptionTest.php +++ b/tests/PhpWordTests/Exception/CopyFileExceptionTest.php @@ -29,8 +29,6 @@ class CopyFileExceptionTest extends \PHPUnit\Framework\TestCase { /** * CopyFileException can be thrown. - * - * @covers ::__construct() */ public function testCopyFileExceptionCanBeThrown(): void { diff --git a/tests/PhpWordTests/Exception/CreateTemporaryFileExceptionTest.php b/tests/PhpWordTests/Exception/CreateTemporaryFileExceptionTest.php index dd8f395318..31015c62a8 100644 --- a/tests/PhpWordTests/Exception/CreateTemporaryFileExceptionTest.php +++ b/tests/PhpWordTests/Exception/CreateTemporaryFileExceptionTest.php @@ -29,8 +29,6 @@ class CreateTemporaryFileExceptionTest extends \PHPUnit\Framework\TestCase { /** * CreateTemporaryFileException can be thrown. - * - * @covers ::__construct() */ public function testCreateTemporaryFileExceptionCanBeThrown(): void { diff --git a/tests/PhpWordTests/Metadata/SettingsTest.php b/tests/PhpWordTests/Metadata/SettingsTest.php index c7f165db28..0c5a6f1593 100644 --- a/tests/PhpWordTests/Metadata/SettingsTest.php +++ b/tests/PhpWordTests/Metadata/SettingsTest.php @@ -230,9 +230,11 @@ public function testDefaultDoNotHyphenateCaps(): void public function testBookFoldPrinting(): void { $oSettings = new Settings(); - self::assertInstanceOf(Settings::class, $oSettings->setBookFoldPrinting(true)); + + $oSettings->setBookFoldPrinting(true); self::assertTrue($oSettings->hasBookFoldPrinting()); - self::assertInstanceOf(Settings::class, $oSettings->setBookFoldPrinting(false)); + + $oSettings->setBookFoldPrinting(false); self::assertFalse($oSettings->hasBookFoldPrinting()); } diff --git a/tests/PhpWordTests/SettingsTest.php b/tests/PhpWordTests/SettingsTest.php index 13b32e1293..f8b9af661d 100644 --- a/tests/PhpWordTests/SettingsTest.php +++ b/tests/PhpWordTests/SettingsTest.php @@ -276,7 +276,7 @@ public function testSetGetDefaultFontColor(): void public function testSetGetDefaultPaper(): void { $dflt = Settings::DEFAULT_PAPER; - $chng = ($dflt === 'A4') ? 'Letter' : 'A4'; + $chng = 'A4'; $doc = new PhpWord(); self::assertEquals($dflt, Settings::getDefaultPaper()); $sec1 = $doc->addSection(); diff --git a/tests/PhpWordTests/Shared/XMLReaderTest.php b/tests/PhpWordTests/Shared/XMLReaderTest.php index 212f20e1ba..18750b5ef9 100644 --- a/tests/PhpWordTests/Shared/XMLReaderTest.php +++ b/tests/PhpWordTests/Shared/XMLReaderTest.php @@ -91,10 +91,7 @@ public function testThrowsExceptionOnNonExistingArchive(): void */ public function testThrowsExceptionOnZipArchiveOpenErrors(): void { - /** - * @var string - */ - $tempPath = tempnam(sys_get_temp_dir(), 'PhpWord'); + $tempPath = tempnam(sys_get_temp_dir(), 'PhpWord') ?: 'tempNameFile'; // Simulate a corrupt archive file_put_contents($tempPath, mt_rand()); @@ -150,6 +147,7 @@ public function testShouldThrowExceptionIfNamespaceIsNotKnown(): void self::assertEquals('AAA', $reader->getElement('/element/test:child')->textContent); self::fail(); } catch (Exception $e) { + // @phpstan-ignore-next-line self::assertTrue(true); } } diff --git a/tests/PhpWordTests/Style/AbstractStyleTest.php b/tests/PhpWordTests/Style/AbstractStyleTest.php index 2b8d3d8d17..679c9f5e49 100644 --- a/tests/PhpWordTests/Style/AbstractStyleTest.php +++ b/tests/PhpWordTests/Style/AbstractStyleTest.php @@ -36,6 +36,7 @@ class AbstractStyleTest extends \PHPUnit\Framework\TestCase */ public function testSetStyleByArray(): void { + // @phpstan-ignore-next-line if (method_exists($this, 'getMockForAbstractClass')) { $stub = $this->getMockForAbstractClass(AbstractStyle::class); } else { @@ -69,6 +70,7 @@ public function testSetStyleByArrayWithAlignment(): void */ public function testSetValNormal(): void { + // @phpstan-ignore-next-line if (method_exists($this, 'getMockForAbstractClass')) { $stub = $this->getMockForAbstractClass(AbstractStyle::class); } else { @@ -89,6 +91,7 @@ public function testSetValNormal(): void */ public function testSetValDefault(): void { + // @phpstan-ignore-next-line if (method_exists($this, 'getMockForAbstractClass')) { $stub = $this->getMockForAbstractClass(AbstractStyle::class); } else { @@ -109,6 +112,7 @@ public function testSetValDefault(): void public function testSetValEnumException(): void { $this->expectException(InvalidArgumentException::class); + // @phpstan-ignore-next-line if (method_exists($this, 'getMockForAbstractClass')) { $stub = $this->getMockForAbstractClass(AbstractStyle::class); } else { diff --git a/tests/PhpWordTests/Style/CellTest.php b/tests/PhpWordTests/Style/CellTest.php index 9015d0feea..56b099c05f 100644 --- a/tests/PhpWordTests/Style/CellTest.php +++ b/tests/PhpWordTests/Style/CellTest.php @@ -30,29 +30,42 @@ */ class CellTest extends \PHPUnit\Framework\TestCase { - /** - * Test setting style with normal value. - */ - public function testSetGetNormal(): void + public function testSetGetNormalInt(): void { $object = new Cell(); - $attributes = [ + foreach ([ + 'borderTopSize' => 120, + 'borderLeftSize' => 120, + 'borderRightSize' => 120, + 'borderBottomSize' => 120, + 'gridSpan' => 2, + ] as $key => $value) { + $set = "set{$key}"; + $get = "get{$key}"; + + self::assertNull($object->$get()); // Init with null value + + $object->$set($value); + + self::assertEquals($value, $object->$get()); + } + } + + public function testSetGetNormalString(): void + { + $object = new Cell(); + + foreach ([ 'valign' => VerticalJc::TOP, 'textDirection' => Cell::TEXT_DIR_BTLR, 'bgColor' => 'FFFF00', - 'borderTopSize' => 120, 'borderTopColor' => 'FFFF00', - 'borderLeftSize' => 120, 'borderLeftColor' => 'FFFF00', - 'borderRightSize' => 120, 'borderRightColor' => 'FFFF00', - 'borderBottomSize' => 120, 'borderBottomColor' => 'FFFF00', - 'gridSpan' => 2, 'vMerge' => Cell::VMERGE_RESTART, - ]; - foreach ($attributes as $key => $value) { + ] as $key => $value) { $set = "set{$key}"; $get = "get{$key}"; diff --git a/tests/PhpWordTests/Style/ChartTest.php b/tests/PhpWordTests/Style/ChartTest.php index cece336e0f..e8e75901d7 100644 --- a/tests/PhpWordTests/Style/ChartTest.php +++ b/tests/PhpWordTests/Style/ChartTest.php @@ -165,8 +165,6 @@ public function testSetGetCategoryAxisTitle(): void { $chart = new Chart(); - $chart->getCategoryAxisTitle(); - self::assertEquals($chart->getCategoryAxisTitle(), null); $chart->setCategoryAxisTitle('Test Category Axis Title'); @@ -181,8 +179,6 @@ public function testSetGetValueAxisTitle(): void { $chart = new Chart(); - $chart->getValueAxisTitle(); - self::assertEquals($chart->getValueAxisTitle(), null); $chart->setValueAxisTitle('Test Value Axis Title'); diff --git a/tests/PhpWordTests/Style/ImageTest.php b/tests/PhpWordTests/Style/ImageTest.php index 1223069c67..759a85441e 100644 --- a/tests/PhpWordTests/Style/ImageTest.php +++ b/tests/PhpWordTests/Style/ImageTest.php @@ -31,26 +31,33 @@ */ class ImageTest extends \PHPUnit\Framework\TestCase { - /** - * Test setting style with normal value. - */ - public function testSetGetNormal(): void + public function testSetGetNormalInt(): void { $object = new Image(); - - $properties = [ + foreach ([ 'width' => 200, 'height' => 200, - 'alignment' => Jc::START, 'marginTop' => 240, 'marginLeft' => 240, - 'wrappingStyle' => 'inline', 'wrapDistanceLeft' => 10, 'wrapDistanceRight' => 20, 'wrapDistanceTop' => 30, 'wrapDistanceBottom' => 40, - ]; - foreach ($properties as $key => $value) { + ] as $key => $value) { + $set = "set{$key}"; + $get = "get{$key}"; + $object->$set($value); + self::assertEquals($value, $object->$get()); + } + } + + public function testSetGetNormalString(): void + { + $object = new Image(); + foreach ([ + 'alignment' => Jc::START, + 'wrappingStyle' => 'inline', + ] as $key => $value) { $set = "set{$key}"; $get = "get{$key}"; $object->$set($value); diff --git a/tests/PhpWordTests/Style/LanguageTest.php b/tests/PhpWordTests/Style/LanguageTest.php index e6b6d1630a..848284e5e3 100644 --- a/tests/PhpWordTests/Style/LanguageTest.php +++ b/tests/PhpWordTests/Style/LanguageTest.php @@ -29,19 +29,32 @@ */ class LanguageTest extends \PHPUnit\Framework\TestCase { - /** - * Test get/set. - */ - public function testGetSetProperties(): void + public function testGetSetPropertiesInt(): void + { + $object = new Language(); + foreach ([ + 'langId' => [null, 1036], + ] as $property => $value) { + [$default, $expected] = $value; + $get = "get{$property}"; + $set = "set{$property}"; + + self::assertEquals($default, $object->$get()); // Default value + + $object->$set($expected); + + self::assertEquals($expected, $object->$get()); // New value + } + } + + public function testGetSetPropertiesString(): void { $object = new Language(); - $properties = [ + foreach ([ 'latin' => [null, 'fr-BE'], 'eastAsia' => [null, 'ja-JP'], 'bidirectional' => [null, 'ar-SA'], - 'langId' => [null, 1036], - ]; - foreach ($properties as $property => $value) { + ] as $property => $value) { [$default, $expected] = $value; $get = "get{$property}"; $set = "set{$property}"; diff --git a/tests/PhpWordTests/Style/LineNumberingTest.php b/tests/PhpWordTests/Style/LineNumberingTest.php index d91e7ccfbc..c76bddf56c 100644 --- a/tests/PhpWordTests/Style/LineNumberingTest.php +++ b/tests/PhpWordTests/Style/LineNumberingTest.php @@ -27,19 +27,32 @@ */ class LineNumberingTest extends \PHPUnit\Framework\TestCase { - /** - * Test get/set. - */ - public function testGetSetProperties(): void + public function testGetSetPropertiesInt(): void { $object = new LineNumbering(); - $properties = [ + foreach ([ 'start' => [1, 2], 'increment' => [1, 10], 'distance' => [null, 10], + ] as $property => $value) { + [$default, $expected] = $value; + $get = "get{$property}"; + $set = "set{$property}"; + + self::assertEquals($default, $object->$get()); // Default value + + $object->$set($expected); + + self::assertEquals($expected, $object->$get()); // New value + } + } + + public function testGetSetPropertiesString(): void + { + $object = new LineNumbering(); + foreach ([ 'restart' => [null, 'continuous'], - ]; - foreach ($properties as $property => $value) { + ] as $property => $value) { [$default, $expected] = $value; $get = "get{$property}"; $set = "set{$property}"; diff --git a/tests/PhpWordTests/Style/LineTest.php b/tests/PhpWordTests/Style/LineTest.php index 3e4db2438f..26049d5cd8 100644 --- a/tests/PhpWordTests/Style/LineTest.php +++ b/tests/PhpWordTests/Style/LineTest.php @@ -29,22 +29,31 @@ */ class LineTest extends \PHPUnit\Framework\TestCase { - /** - * Test setting style with normal value. - */ - public function testSetGetNormal(): void + public function testSetGetNormalInt(): void { $object = new Line(); - $properties = [ + foreach ([ + 'weight' => 10, + ] as $key => $value) { + $set = "set{$key}"; + $get = "get{$key}"; + $object->$set($value); + self::assertEquals($value, $object->$get()); + } + } + + public function testSetGetNormalString(): void + { + $object = new Line(); + + foreach ([ 'connectorType' => Line::CONNECTOR_TYPE_STRAIGHT, 'beginArrow' => Line::ARROW_STYLE_BLOCK, 'endArrow' => Line::ARROW_STYLE_OVAL, 'dash' => Line::DASH_STYLE_LONG_DASH_DOT_DOT, - 'weight' => 10, 'color' => 'red', - ]; - foreach ($properties as $key => $value) { + ] as $key => $value) { $set = "set{$key}"; $get = "get{$key}"; $object->$set($value); diff --git a/tests/PhpWordTests/Style/NumberingLevelTest.php b/tests/PhpWordTests/Style/NumberingLevelTest.php index 07721408bf..b34e445e94 100644 --- a/tests/PhpWordTests/Style/NumberingLevelTest.php +++ b/tests/PhpWordTests/Style/NumberingLevelTest.php @@ -28,25 +28,36 @@ */ class NumberingLevelTest extends \PHPUnit\Framework\TestCase { - /** - * Test setting style with normal value. - */ - public function testSetGetNormal(): void + public function testSetGetNormalInt(): void { $object = new NumberingLevel(); $attributes = [ 'level' => 1, 'start' => 1, - 'format' => 'decimal', 'restart' => 1, + 'left' => 360, + 'hanging' => 360, + 'tabPos' => 360, + ]; + foreach ($attributes as $key => $value) { + $set = "set{$key}"; + $get = "get{$key}"; + $object->$set($value); + self::assertEquals($value, $object->$get()); + } + } + + public function testSetGetNormalString(): void + { + $object = new NumberingLevel(); + + $attributes = [ + 'format' => 'decimal', 'pStyle' => 'pStyle', 'suffix' => 'space', 'text' => '%1.', 'alignment' => Jc::START, - 'left' => 360, - 'hanging' => 360, - 'tabPos' => 360, 'font' => 'Arial', 'hint' => 'default', ]; diff --git a/tests/PhpWordTests/Style/NumberingTest.php b/tests/PhpWordTests/Style/NumberingTest.php index ce39509d0b..153e4b9284 100644 --- a/tests/PhpWordTests/Style/NumberingTest.php +++ b/tests/PhpWordTests/Style/NumberingTest.php @@ -27,17 +27,30 @@ */ class NumberingTest extends \PHPUnit\Framework\TestCase { - /** - * Test get/set. - */ - public function testGetSetProperties(): void + public function testGetSetPropertiesInt(): void { $object = new Numbering(); - $properties = [ + foreach ([ 'numId' => [null, 1], + ] as $property => $value) { + [$default, $expected] = $value; + $get = "get{$property}"; + $set = "set{$property}"; + + self::assertEquals($default, $object->$get()); // Default value + + $object->$set($expected); + + self::assertEquals($expected, $object->$get()); // New value + } + } + + public function testGetSetPropertiesString(): void + { + $object = new Numbering(); + foreach ([ 'type' => [null, 'singleLevel'], - ]; - foreach ($properties as $property => $value) { + ] as $property => $value) { [$default, $expected] = $value; $get = "get{$property}"; $set = "set{$property}"; @@ -50,9 +63,6 @@ public function testGetSetProperties(): void } } - /** - * Test get level. - */ public function testGetLevels(): void { $object = new Numbering(); diff --git a/tests/PhpWordTests/Style/ParagraphTest.php b/tests/PhpWordTests/Style/ParagraphTest.php index 6a6e777cfc..67645fa4d8 100644 --- a/tests/PhpWordTests/Style/ParagraphTest.php +++ b/tests/PhpWordTests/Style/ParagraphTest.php @@ -150,13 +150,17 @@ public function testHanging(): void $object = new Paragraph(); self::assertNull($object->getHanging()); - self::assertInstanceOf(Paragraph::class, $object->setHanging($rand)); + + $object->setHanging($rand); self::assertEquals($rand, $object->getHanging()); - self::assertInstanceOf(Paragraph::class, $object->setHanging(null)); + + $object->setHanging(null); self::assertNull($object->getHanging()); - self::assertInstanceOf(Paragraph::class, $object->setHanging($rand)); + + $object->setHanging($rand); self::assertEquals($rand, $object->getHanging()); - self::assertInstanceOf(Paragraph::class, $object->setHanging()); + + $object->setHanging(); self::assertNull($object->getHanging()); } @@ -166,13 +170,17 @@ public function testIndent(): void $object = new Paragraph(); self::assertNull($object->getIndent()); - self::assertInstanceOf(Paragraph::class, $object->setIndent($rand)); + + $object->setIndent($rand); self::assertEquals($rand, $object->getIndent()); - self::assertInstanceOf(Paragraph::class, $object->setIndent(null)); + + $object->setIndent(null); self::assertNull($object->getIndent()); - self::assertInstanceOf(Paragraph::class, $object->setIndent($rand)); + + $object->setIndent($rand); self::assertEquals($rand, $object->getIndent()); - self::assertInstanceOf(Paragraph::class, $object->setIndent()); + + $object->setIndent(); self::assertNull($object->getIndent()); } @@ -184,60 +192,60 @@ public function testIndentation(): void $object = new Paragraph(); self::assertNull($object->getIndentation()); // Set Basic indentation - self::assertInstanceOf(Paragraph::class, $object->setIndentation([])); + $object->setIndentation([]); self::assertNotNull($object->getIndentation()); self::assertEquals(0, $object->getIndentation()->getLeft()); self::assertEquals(0, $object->getIndentation()->getRight()); self::assertEquals(0, $object->getIndentation()->getHanging()); self::assertEquals(0, $object->getIndentation()->getFirstLine()); // Set indentation : left - self::assertInstanceOf(Paragraph::class, $object->setIndentation([ + $object->setIndentation([ 'left' => $rand, - ])); + ]); self::assertNotNull($object->getIndentation()); self::assertEquals($rand, $object->getIndentation()->getLeft()); self::assertEquals(0, $object->getIndentation()->getRight()); self::assertEquals(0, $object->getIndentation()->getHanging()); self::assertEquals(0, $object->getIndentation()->getFirstLine()); // Set indentation : right - self::assertInstanceOf(Paragraph::class, $object->setIndentation([ + $object->setIndentation([ 'right' => $rand, - ])); + ]); self::assertNotNull($object->getIndentation()); self::assertEquals($rand, $object->getIndentation()->getLeft()); self::assertEquals($rand, $object->getIndentation()->getRight()); self::assertEquals(0, $object->getIndentation()->getHanging()); self::assertEquals(0, $object->getIndentation()->getFirstLine()); // Set indentation : hanging - self::assertInstanceOf(Paragraph::class, $object->setIndentation([ + $object->setIndentation([ 'hanging' => $rand, - ])); + ]); self::assertNotNull($object->getIndentation()); self::assertEquals($rand, $object->getIndentation()->getLeft()); self::assertEquals($rand, $object->getIndentation()->getRight()); self::assertEquals($rand, $object->getIndentation()->getHanging()); self::assertEquals(0, $object->getIndentation()->getFirstLine()); // Set indentation : firstline - self::assertInstanceOf(Paragraph::class, $object->setIndentation([ + $object->setIndentation([ 'firstline' => $rand, - ])); + ]); self::assertNotNull($object->getIndentation()); self::assertEquals($rand, $object->getIndentation()->getLeft()); self::assertEquals($rand, $object->getIndentation()->getRight()); self::assertEquals($rand, $object->getIndentation()->getHanging()); self::assertEquals($rand, $object->getIndentation()->getFirstLine()); // Replace indentation : left & firstline - self::assertInstanceOf(Paragraph::class, $object->setIndentation([ + $object->setIndentation([ 'left' => $rand2, 'firstline' => $rand2, - ])); + ]); self::assertNotNull($object->getIndentation()); self::assertEquals($rand2, $object->getIndentation()->getLeft()); self::assertEquals($rand, $object->getIndentation()->getRight()); self::assertEquals($rand, $object->getIndentation()->getHanging()); self::assertEquals($rand2, $object->getIndentation()->getFirstLine()); // Replace indentation : N/A - self::assertInstanceOf(Paragraph::class, $object->setIndentation()); + $object->setIndentation(); self::assertNotNull($object->getIndentation()); self::assertEquals($rand2, $object->getIndentation()->getLeft()); self::assertEquals($rand, $object->getIndentation()->getRight()); @@ -251,13 +259,13 @@ public function testIndentFirstLine(): void $object = new Paragraph(); self::assertNull($object->getIndentFirstLine()); - self::assertInstanceOf(Paragraph::class, $object->setIndentFirstLine($rand)); + $object->setIndentFirstLine($rand); self::assertEquals($rand, $object->getIndentFirstLine()); - self::assertInstanceOf(Paragraph::class, $object->setIndentFirstLine(null)); + $object->setIndentFirstLine(null); self::assertNull($object->getIndentFirstLine()); - self::assertInstanceOf(Paragraph::class, $object->setIndentFirstLine($rand)); + $object->setIndentFirstLine($rand); self::assertEquals($rand, $object->getIndentFirstLine()); - self::assertInstanceOf(Paragraph::class, $object->setIndentFirstLine()); + $object->setIndentFirstLine(); self::assertNull($object->getIndentFirstLine()); } @@ -267,13 +275,13 @@ public function testIndentLeft(): void $object = new Paragraph(); self::assertNull($object->getIndentLeft()); - self::assertInstanceOf(Paragraph::class, $object->setIndentLeft($rand)); + $object->setIndentLeft($rand); self::assertEquals($rand, $object->getIndentLeft()); - self::assertInstanceOf(Paragraph::class, $object->setIndentLeft(null)); + $object->setIndentLeft(null); self::assertNull($object->getIndentLeft()); - self::assertInstanceOf(Paragraph::class, $object->setIndentLeft($rand)); + $object->setIndentLeft($rand); self::assertEquals($rand, $object->getIndentLeft()); - self::assertInstanceOf(Paragraph::class, $object->setIndentLeft()); + $object->setIndentLeft(); self::assertNull($object->getIndentLeft()); } @@ -283,13 +291,13 @@ public function testIndentRight(): void $object = new Paragraph(); self::assertNull($object->getIndentRight()); - self::assertInstanceOf(Paragraph::class, $object->setIndentRight($rand)); + $object->setIndentRight($rand); self::assertEquals($rand, $object->getIndentRight()); - self::assertInstanceOf(Paragraph::class, $object->setIndentRight(null)); + $object->setIndentRight(null); self::assertNull($object->getIndentRight()); - self::assertInstanceOf(Paragraph::class, $object->setIndentRight($rand)); + $object->setIndentRight($rand); self::assertEquals($rand, $object->getIndentRight()); - self::assertInstanceOf(Paragraph::class, $object->setIndentRight()); + $object->setIndentRight(); self::assertNull($object->getIndentRight()); } @@ -350,11 +358,11 @@ public function testBidiVisual(): void { $object = new Paragraph(); self::assertNull($object->isBidi()); - self::assertInstanceOf(Paragraph::class, $object->setBidi(true)); + $object->setBidi(true); self::assertTrue($object->isBidi()); - self::assertInstanceOf(Paragraph::class, $object->setBidi(false)); + $object->setBidi(false); self::assertFalse($object->isBidi()); - self::assertInstanceOf(Paragraph::class, $object->setBidi(null)); + $object->setBidi(null); self::assertNull($object->isBidi()); } diff --git a/tests/PhpWordTests/Style/RowTest.php b/tests/PhpWordTests/Style/RowTest.php index 688a6b7b35..d3cc480ac7 100644 --- a/tests/PhpWordTests/Style/RowTest.php +++ b/tests/PhpWordTests/Style/RowTest.php @@ -56,24 +56,4 @@ public function testBooleanValue(): void self::assertEquals($expected, $object->$get()); } } - - /** - * Test properties with nonboolean values, which will return default value. - */ - public function testNonBooleanValue(): void - { - $object = new Row(); - - $properties = [ - 'tblHeader' => 'a', - 'cantSplit' => 'b', - 'exactHeight' => 'c', - ]; - foreach ($properties as $key => $value) { - $set = "set{$key}"; - $get = "is{$key}"; - $object->$set($value); - self::assertFalse($object->$get()); - } - } } diff --git a/tests/PhpWordTests/Style/SpacingTest.php b/tests/PhpWordTests/Style/SpacingTest.php index 874fd5c5dd..351b9f1b54 100644 --- a/tests/PhpWordTests/Style/SpacingTest.php +++ b/tests/PhpWordTests/Style/SpacingTest.php @@ -27,16 +27,31 @@ */ class SpacingTest extends \PHPUnit\Framework\TestCase { - /** - * Test get/set. - */ - public function testGetSetProperties(): void + public function testGetSetPropertiesInt(): void { $object = new Spacing(); $properties = [ 'before' => [null, 10], 'after' => [null, 10], 'line' => [null, 10], + ]; + foreach ($properties as $property => $value) { + [$default, $expected] = $value; + $get = "get{$property}"; + $set = "set{$property}"; + + self::assertEquals($default, $object->$get()); // Default value + + $object->$set($expected); + + self::assertEquals($expected, $object->$get()); // New value + } + } + + public function testGetSetPropertiesString(): void + { + $object = new Spacing(); + $properties = [ 'lineRule' => ['auto', 'exact'], ]; foreach ($properties as $property => $value) { diff --git a/tests/PhpWordTests/Style/TOCTest.php b/tests/PhpWordTests/Style/TOCTest.php index 118c8c4199..039bc11340 100644 --- a/tests/PhpWordTests/Style/TOCTest.php +++ b/tests/PhpWordTests/Style/TOCTest.php @@ -27,18 +27,31 @@ */ class TOCTest extends \PHPUnit\Framework\TestCase { - /** - * Test get/set. - */ - public function testGetSet(): void + public function testGetSetInt(): void { $object = new TOC(); - $properties = [ - 'tabLeader' => [TOC::TAB_LEADER_DOT, TOC::TAB_LEADER_UNDERSCORE], + foreach ([ 'tabPos' => [9062, 10], 'indent' => [200, 10], - ]; - foreach ($properties as $property => $value) { + ] as $property => $value) { + [$default, $expected] = $value; + $get = "get{$property}"; + $set = "set{$property}"; + + self::assertEquals($default, $object->$get()); // Default value + + $object->$set($expected); + + self::assertEquals($expected, $object->$get()); // New value + } + } + + public function testGetSetString(): void + { + $object = new TOC(); + foreach ([ + 'tabLeader' => [TOC::TAB_LEADER_DOT, TOC::TAB_LEADER_UNDERSCORE], + ] as $property => $value) { [$default, $expected] = $value; $get = "get{$property}"; $set = "set{$property}"; diff --git a/tests/PhpWordTests/Style/TabTest.php b/tests/PhpWordTests/Style/TabTest.php index a1a6a2e0db..43b9ff2c85 100644 --- a/tests/PhpWordTests/Style/TabTest.php +++ b/tests/PhpWordTests/Style/TabTest.php @@ -30,15 +30,34 @@ class TabTest extends \PHPUnit\Framework\TestCase /** * Test get/set. */ - public function testGetSetProperties(): void + public function testGetSetPropertiesInt(): void { $object = new Tab(); - $properties = [ + foreach ([ + 'position' => [0, 10], + ] as $property => $value) { + [$default, $expected] = $value; + $get = "get{$property}"; + $set = "set{$property}"; + + self::assertEquals($default, $object->$get()); // Default value + + $object->$set($expected); + + self::assertEquals($expected, $object->$get()); // New value + } + } + + /** + * Test get/set. + */ + public function testGetSetPropertiesString(): void + { + $object = new Tab(); + foreach ([ 'type' => [Tab::TAB_STOP_CLEAR, Tab::TAB_STOP_RIGHT], 'leader' => [Tab::TAB_LEADER_NONE, Tab::TAB_LEADER_DOT], - 'position' => [0, 10], - ]; - foreach ($properties as $property => $value) { + ] as $property => $value) { [$default, $expected] = $value; $get = "get{$property}"; $set = "set{$property}"; diff --git a/tests/PhpWordTests/Style/TablePositionTest.php b/tests/PhpWordTests/Style/TablePositionTest.php index ba0bb9dd09..695bb3d297 100644 --- a/tests/PhpWordTests/Style/TablePositionTest.php +++ b/tests/PhpWordTests/Style/TablePositionTest.php @@ -42,23 +42,38 @@ public function testConstruct(): void /** * Test setting style with normal value. */ - public function testSetGetNormal(): void + public function testSetGetNormalInt(): void { $object = new TablePosition(); - $attributes = [ + foreach ([ 'leftFromText' => 4, 'rightFromText' => 4, 'topFromText' => 4, 'bottomFromText' => 4, + 'tblpX' => 5, + 'tblpY' => 6, + ] as $key => $value) { + $set = "set{$key}"; + $get = "get{$key}"; + $object->$set($value); + self::assertEquals($value, $object->$get()); + } + } + + /** + * Test setting style with normal value. + */ + public function testSetGetNormalString(): void + { + $object = new TablePosition(); + + foreach ([ 'vertAnchor' => TablePosition::VANCHOR_PAGE, 'horzAnchor' => TablePosition::HANCHOR_TEXT, 'tblpXSpec' => TablePosition::XALIGN_CENTER, - 'tblpX' => 5, 'tblpYSpec' => TablePosition::YALIGN_OUTSIDE, - 'tblpY' => 6, - ]; - foreach ($attributes as $key => $value) { + ] as $key => $value) { $set = "set{$key}"; $get = "get{$key}"; $object->$set($value); diff --git a/tests/PhpWordTests/TemplateProcessorTest.php b/tests/PhpWordTests/TemplateProcessorTest.php index 093879a042..8ae4dfa59a 100644 --- a/tests/PhpWordTests/TemplateProcessorTest.php +++ b/tests/PhpWordTests/TemplateProcessorTest.php @@ -70,7 +70,6 @@ protected function tearDown(): void public function testTheConstruct(): void { $object = $this->getTemplateProcessor(__DIR__ . '/_files/templates/blank.docx'); - self::assertInstanceOf('PhpOffice\\PhpWord\\TemplateProcessor', $object); self::assertEquals([], $object->getVariables()); $object->save(); diff --git a/tests/PhpWordTests/Writer/EPub3/Part/AbstractPartTest.php b/tests/PhpWordTests/Writer/EPub3/Part/AbstractPartTest.php index c12980c7e2..24db959f6e 100644 --- a/tests/PhpWordTests/Writer/EPub3/Part/AbstractPartTest.php +++ b/tests/PhpWordTests/Writer/EPub3/Part/AbstractPartTest.php @@ -15,6 +15,7 @@ class AbstractPartTest extends TestCase protected function setUp(): void { + // @phpstan-ignore-next-line if (method_exists($this, 'getMockForAbstractClass')) { $this->part = $this->getMockForAbstractClass(AbstractPart::class); } else { diff --git a/tests/PhpWordTests/Writer/EPub3/Style/AbstractStyleTest.php b/tests/PhpWordTests/Writer/EPub3/Style/AbstractStyleTest.php index 12be0f2e4f..e06cef8a3b 100644 --- a/tests/PhpWordTests/Writer/EPub3/Style/AbstractStyleTest.php +++ b/tests/PhpWordTests/Writer/EPub3/Style/AbstractStyleTest.php @@ -14,6 +14,7 @@ class AbstractStyleTest extends TestCase public function testParentWriter(): void { $parentWriter = new EPub3(); + // @phpstan-ignore-next-line if (method_exists($this, 'getMockForAbstractClass')) { $style = $this->getMockForAbstractClass(AbstractStyle::class); } else { diff --git a/tests/PhpWordTests/Writer/HTMLTest.php b/tests/PhpWordTests/Writer/HTMLTest.php index 6dba003eb4..cedd9f2f32 100644 --- a/tests/PhpWordTests/Writer/HTMLTest.php +++ b/tests/PhpWordTests/Writer/HTMLTest.php @@ -57,11 +57,13 @@ public function testEditCallback(): void $object = new HTML(new PhpWord()); self::assertNull($object->getEditCallback()); - self::assertInstanceOf(HTML::class, $object->setEditCallback(function (string $html): string { + + $object->setEditCallback(function (string $html): string { return $html; - })); + }); self::assertIsCallable($object->getEditCallback()); - self::assertInstanceOf(HTML::class, $object->setEditCallback(null)); + + $object->setEditCallback(null); self::assertNull($object->getEditCallback()); } @@ -70,9 +72,11 @@ public function testDefaultGenericFont(): void $object = new HTML(new PhpWord()); self::assertEquals('', $object->getDefaultGenericFont()); - self::assertInstanceOf(HTML::class, $object->setDefaultGenericFont('test')); + + $object->setDefaultGenericFont('test'); self::assertEquals('', $object->getDefaultGenericFont()); - self::assertInstanceOf(HTML::class, $object->setDefaultGenericFont('cursive')); + + $object->setDefaultGenericFont('cursive'); self::assertEquals('cursive', $object->getDefaultGenericFont()); } @@ -81,9 +85,11 @@ public function testDefaultWhiteSpace(): void $object = new HTML(new PhpWord()); self::assertEquals('', $object->getDefaultWhiteSpace()); - self::assertInstanceOf(HTML::class, $object->setDefaultWhiteSpace('test')); + + $object->setDefaultWhiteSpace('test'); self::assertEquals('', $object->getDefaultWhiteSpace()); - self::assertInstanceOf(HTML::class, $object->setDefaultWhiteSpace('pre-line')); + + $object->setDefaultWhiteSpace('pre-line'); self::assertEquals('pre-line', $object->getDefaultWhiteSpace()); } diff --git a/tests/PhpWordTests/Writer/ODText/ElementTest.php b/tests/PhpWordTests/Writer/ODText/ElementTest.php index 4df140aaa4..8ca327717c 100644 --- a/tests/PhpWordTests/Writer/ODText/ElementTest.php +++ b/tests/PhpWordTests/Writer/ODText/ElementTest.php @@ -113,8 +113,10 @@ public function testTableElements(): void $p2s = '/office:document-content/office:automatic-styles'; $tableStyleNum = 1; - $tableStyleName = ''; - while ($tableStyleName === '') { + /** @var null|string $tableStyleName */ + $tableStyleName = null; + $element = ''; + while ($tableStyleName === null) { $element = "$p2s/style:style[$tableStyleNum]"; if (!$doc->elementExists($element)) { break; @@ -126,7 +128,7 @@ public function testTableElements(): void } ++$tableStyleNum; } - self::AssertNotEquals('', $tableStyleName); + self::assertNotNull($tableStyleName); $element = "$element/style:table-properties"; self::assertTrue($doc->elementExists($element)); self::assertEquals(\PhpOffice\PhpWord\SimpleType\JcTable::CENTER, $doc->getElementAttribute($element, 'table:align')); diff --git a/tests/PhpWordTests/Writer/ODText/Part/AbstractPartTest.php b/tests/PhpWordTests/Writer/ODText/Part/AbstractPartTest.php index 27008dae82..049b9e7cb4 100644 --- a/tests/PhpWordTests/Writer/ODText/Part/AbstractPartTest.php +++ b/tests/PhpWordTests/Writer/ODText/Part/AbstractPartTest.php @@ -34,6 +34,7 @@ class AbstractPartTest extends \PHPUnit\Framework\TestCase */ public function testSetGetParentWriter(): void { + // @phpstan-ignore-next-line if (method_exists($this, 'getMockForAbstractClass')) { $object = $this->getMockForAbstractClass(ODText\Part\AbstractPart::class); } else { @@ -56,6 +57,7 @@ public function testSetGetParentWriterNull(): void { $this->expectException(Exception::class); $this->expectExceptionMessage('No parent WriterInterface assigned.'); + // @phpstan-ignore-next-line if (method_exists($this, 'getMockForAbstractClass')) { $object = $this->getMockForAbstractClass(ODText\Part\AbstractPart::class); } else { diff --git a/tests/PhpWordTests/Writer/Word2007/Part/AbstractPartTest.php b/tests/PhpWordTests/Writer/Word2007/Part/AbstractPartTest.php index ca38d5d3fc..1bcd43f50e 100644 --- a/tests/PhpWordTests/Writer/Word2007/Part/AbstractPartTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Part/AbstractPartTest.php @@ -32,6 +32,7 @@ class AbstractPartTest extends \PHPUnit\Framework\TestCase */ public function testSetGetParentWriter(): void { + // @phpstan-ignore-next-line if (method_exists($this, 'getMockForAbstractClass')) { $stub = $this->getMockForAbstractClass(Word2007\Part\AbstractPart::class); } else { @@ -54,6 +55,7 @@ public function testSetGetParentWriterNull(): void { $this->expectException(Exception::class); $this->expectExceptionMessage('No parent WriterInterface assigned.'); + // @phpstan-ignore-next-line if (method_exists($this, 'getMockForAbstractClass')) { $stub = $this->getMockForAbstractClass(Word2007\Part\AbstractPart::class); } else { diff --git a/tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php b/tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php index ed38c57ac6..d1f5b2585d 100644 --- a/tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Part/DocumentTest.php @@ -407,9 +407,10 @@ public function testWriteImage(): void // behind $element = $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:pict/v:shape'); $style = $element->getAttribute('style'); + // @phpstan-ignore-next-line if (method_exists(self::class, 'assertMatchesRegularExpression')) { self::assertMatchesRegularExpression('/z\-index:\-[0-9]*/', $style); - } elseif (method_exists(self::class, 'assertRegExp')) { + } elseif (method_exists(self::class, 'assertRegExp')) { // @phpstan-ignore-line self::assertRegExp('/z\-index:\-[0-9]*/', $style); } else { self::fail('Unsure how to test regexp'); diff --git a/tests/PhpWordTests/Writer/Word2007/StyleTest.php b/tests/PhpWordTests/Writer/Word2007/StyleTest.php index d458ea45e7..0c0d0b854a 100644 --- a/tests/PhpWordTests/Writer/Word2007/StyleTest.php +++ b/tests/PhpWordTests/Writer/Word2007/StyleTest.php @@ -19,6 +19,9 @@ namespace PhpOffice\PhpWordTests\Writer\Word2007; use PhpOffice\PhpWord\Shared\XMLWriter; +use PhpOffice\PhpWord\Writer\Word2007\Style\Frame; +use PhpOffice\PhpWord\Writer\Word2007\Style\Line; +use PhpOffice\PhpWord\Writer\Word2007\Style\TextBox; /** * Test class for PhpOffice\PhpWord\Writer\Word2007\Style subnamespace. @@ -48,20 +51,36 @@ public function testEmptyStyles(): void /** * Test method exceptions. */ - public function testMethodExceptions(): void + public function testMethodExceptionsFrame(): void { - $styles = [ - 'Frame' => 'writeAlignment', - 'Line' => 'writeStroke', - 'TextBox' => 'writeBorder', - ]; - foreach ($styles as $style => $method) { - $objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Style\\' . $style; - $xmlWriter = new XMLWriter(); - $object = new $objectClass($xmlWriter); - $object->$method(); + $xmlWriter = new XMLWriter(); + $object = new Frame($xmlWriter); + $object->writeAlignment(); - self::assertEquals('', $xmlWriter->getData()); - } + self::assertEquals('', $xmlWriter->getData()); + } + + /** + * Test method exceptions. + */ + public function testMethodExceptionsLine(): void + { + $xmlWriter = new XMLWriter(); + $object = new Line($xmlWriter); + $object->writeStroke(); + + self::assertEquals('', $xmlWriter->getData()); + } + + /** + * Test method exceptions. + */ + public function testMethodExceptionsTextBox(): void + { + $xmlWriter = new XMLWriter(); + $object = new TextBox($xmlWriter); + $object->writeBorder(); + + self::assertEquals('', $xmlWriter->getData()); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f9e0ca2388..d94d68d880 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -59,6 +59,7 @@ function utf8decode(string $value, string $toEncoding = 'ISO-8859-1'): string return $result === false ? '' : $result; } +// @phpstan-ignore-next-line if (!method_exists(PHPUnit\Framework\TestCase::class, 'setOutputCallback')) { ini_set('error_reporting', (string) E_ALL); set_error_handler('phpunit10ErrorHandler');