From b614497ae6dd44280be1c2dda56772198bcd25ae Mon Sep 17 00:00:00 2001 From: troosan Date: Fri, 29 Dec 2017 02:30:53 +0100 Subject: [PATCH] fix dependencies to have 7.1 compatible build (#1228) * add assertions in test methods without assertions * loosen dependencies so 7.0 & 7.1 builds can succeed * fix some scrutinizer errors * update release date --- .scrutinizer.yml | 2 +- .travis.yml | 4 +- CHANGELOG.md | 4 +- composer.json | 9 ++-- docs/ISSUE_TEMPLATE.md | 17 +++++-- docs/PULL_REQUEST_TEMPLATE.md | 7 ++- run_tests.sh | 20 -------- src/PhpWord/Element/AbstractElement.php | 12 ++--- src/PhpWord/Element/Field.php | 2 +- src/PhpWord/Element/TrackChange.php | 2 - src/PhpWord/Metadata/Protection.php | 2 +- src/PhpWord/Reader/Word2007/Settings.php | 2 +- src/PhpWord/Shared/Converter.php | 38 +++++++-------- src/PhpWord/Shared/Html.php | 1 + .../Shared/Microsoft/PasswordEncoder.php | 2 +- src/PhpWord/Shared/ZipArchive.php | 2 +- src/PhpWord/Style/Paper.php | 8 ++-- src/PhpWord/Writer/PDF/MPDF.php | 24 +++++++--- src/PhpWord/Writer/Word2007/Part/Comments.php | 4 +- tests/PhpWord/Shared/ZipArchiveTest.php | 46 +++++++++---------- tests/PhpWord/Writer/ODTextTest.php | 1 + tests/PhpWord/Writer/RTFTest.php | 1 + .../Writer/Word2007/Part/DocumentTest.php | 3 +- 23 files changed, 105 insertions(+), 108 deletions(-) delete mode 100755 run_tests.sh diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 6f982d8e..c8fe57cf 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -15,7 +15,7 @@ tools: ruleset: phpmd.xml.dist external_code_coverage: enabled: true - timeout: 900 + timeout: 1200 php_cpd: true # php_sim: # Temporarily disabled to allow focus on things other than duplicates # min_mass: 40 diff --git a/.travis.yml b/.travis.yml index 0ec84081..d63b7bb2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,14 +9,14 @@ php: - 5.6 - 7.0 - 7.1 + - 7.2 matrix: include: - php: 5.6 env: COVERAGE=1 allow_failures: - - php: 7.0 - - php: 7.1 + - php: 7.2 cache: directories: diff --git a/CHANGELOG.md b/CHANGELOG.md index 47567dfc..93945189 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,10 +3,10 @@ Change Log All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). -v0.14.0 (28 Dec 2017) +v0.14.0 (29 Dec 2017) ---------------------- This release fixes several bugs and adds some new features. -This is the last version to support PHP 5.3 +This version brings compatibility with PHP 7.0 & 7.1 ### Added - Possibility to control the footnote numbering - @troosan #1068 diff --git a/composer.json b/composer.json index 2774ad98..3cc4b131 100644 --- a/composer.json +++ b/composer.json @@ -46,23 +46,22 @@ ] }, "require": { - "php": ">=5.3.3", + "php": "^5.3.3 || ^7.0", "ext-xml": "*", "zendframework/zend-escaper": "^2.2", "zendframework/zend-stdlib": "^2.2 || ^3.0", "phpoffice/common": "^0.2" }, "require-dev": { - "phpunit/phpunit": "^4.8.36", + "phpunit/phpunit": "^4.8.36 || ^5.0", "phpdocumentor/phpdocumentor":"2.*", - "twig/twig":"1.27", "squizlabs/php_codesniffer": "^2.7", "friendsofphp/php-cs-fixer": "^2.0", "phpmd/phpmd": "2.*", - "phploc/phploc": "2.*", + "phploc/phploc": "2.* || 3.* || 4.*", "dompdf/dompdf":"0.8.*", "tecnickcom/tcpdf": "6.*", - "mpdf/mpdf": "5.*" + "mpdf/mpdf": "5.* || 6.* || 7.*" }, "suggest": { "ext-zip": "Allows writing OOXML and ODF", diff --git a/docs/ISSUE_TEMPLATE.md b/docs/ISSUE_TEMPLATE.md index 58981f8e..ee811b00 100644 --- a/docs/ISSUE_TEMPLATE.md +++ b/docs/ISSUE_TEMPLATE.md @@ -1,28 +1,35 @@ -Issue tracker is **ONLY** used for reporting bugs. NO NEW FEATURE ACCEPTED! Use [stackoverflow](https://stackoverflow.com/questions/tagged/phpword) for supporting issues. +This is: + +- [ ] a bug report +- [ ] a feature request +- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpword) # Expected Behavior Please describe the behavior you are expecting. -# Current Behavior +### Current Behavior What is the current behavior? -# Failure Information +### Failure Information Please help provide information about the failure. -## How to Reproduce +### How to Reproduce Please provide a code sample that reproduces the issue. ```php +addSection(); $section->... ``` -## Context +### Context * PHP version: * PHPWord version: 0.14 diff --git a/docs/PULL_REQUEST_TEMPLATE.md b/docs/PULL_REQUEST_TEMPLATE.md index ad9788c4..cff513a3 100644 --- a/docs/PULL_REQUEST_TEMPLATE.md +++ b/docs/PULL_REQUEST_TEMPLATE.md @@ -1,12 +1,11 @@ -# Description +### Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. Fixes # (issue) -# Checklist: +### Checklist: -- [ ] I have commented my code, particularly in hard-to-understand areas -- [ ] I have run phpunit, phpcs, php-cs-fixer, phpmd +- [ ] I have run `composer check` and no errors were reported - [ ] The new code is covered by unit tests - [ ] I have update the documentation to describe the changes diff --git a/run_tests.sh b/run_tests.sh deleted file mode 100755 index a5d94259..00000000 --- a/run_tests.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash -echo "Running composer update" -composer update - -## PHP_CodeSniffer -echo "Running CodeSniffer" -./vendor/bin/phpcs src/ tests/ --standard=PSR2 -n --ignore=src/PhpWord/Shared/PCLZip - -## PHP-CS-Fixer -echo "Running CS Fixer" -./vendor/bin/php-cs-fixer fix --diff --verbose --dry-run - -## PHP Mess Detector -echo "Running Mess Detector" -./vendor/bin/phpmd src/,tests/ text ./phpmd.xml.dist --exclude pclzip.lib.php - -## PHPUnit -echo "Running PHPUnit" -./vendor/bin/phpunit -c ./ - diff --git a/src/PhpWord/Element/AbstractElement.php b/src/PhpWord/Element/AbstractElement.php index 81e18528..a65c50f4 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -228,7 +228,7 @@ abstract class AbstractElement /** * Get element unique ID * - * @return int + * @return string */ public function getElementId() { @@ -425,18 +425,18 @@ abstract class AbstractElement /** * Set enum value * - * @param mixed $value - * @param array $enum - * @param mixed $default + * @param string|null $value + * @param string[] $enum + * @param string|null $default * * @throws \InvalidArgumentException - * @return mixed + * @return string|null * * @todo Merge with the same method in AbstractStyle */ protected function setEnumVal($value = null, $enum = array(), $default = null) { - if ($value != null && trim($value) != '' && !empty($enum) && !in_array($value, $enum)) { + if ($value !== null && trim($value) != '' && !empty($enum) && !in_array($value, $enum)) { throw new \InvalidArgumentException("Invalid style value: {$value}"); } elseif ($value === null || trim($value) == '') { $value = $default; diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php index 6ea63c6b..7b33a479 100644 --- a/src/PhpWord/Element/Field.php +++ b/src/PhpWord/Element/Field.php @@ -211,7 +211,7 @@ class Field extends AbstractElement * @throws \InvalidArgumentException * @return null|string|TextRun */ - public function setText($text) + public function setText($text = null) { if (isset($text)) { if (is_string($text) || $text instanceof TextRun) { diff --git a/src/PhpWord/Element/TrackChange.php b/src/PhpWord/Element/TrackChange.php index d14fc201..44327f26 100644 --- a/src/PhpWord/Element/TrackChange.php +++ b/src/PhpWord/Element/TrackChange.php @@ -52,8 +52,6 @@ class TrackChange extends AbstractContainer { $this->author = $author; $this->date = $date; - - return $this; } /** diff --git a/src/PhpWord/Metadata/Protection.php b/src/PhpWord/Metadata/Protection.php index 35391cb2..39ebc3de 100644 --- a/src/PhpWord/Metadata/Protection.php +++ b/src/PhpWord/Metadata/Protection.php @@ -182,7 +182,7 @@ class Protection /** * Set salt. Salt HAS to be 16 characters, or an exception will be thrown. * - * @param $salt + * @param string $salt * @throws \InvalidArgumentException * @return self */ diff --git a/src/PhpWord/Reader/Word2007/Settings.php b/src/PhpWord/Reader/Word2007/Settings.php index c116425e..ccdbed25 100644 --- a/src/PhpWord/Reader/Word2007/Settings.php +++ b/src/PhpWord/Reader/Word2007/Settings.php @@ -152,7 +152,7 @@ class Settings extends AbstractPart $revisionView = new TrackChangesView(); $revisionView->setMarkup(filter_var($xmlReader->getAttribute('w:markup', $node), FILTER_VALIDATE_BOOLEAN)); $revisionView->setComments($xmlReader->getAttribute('w:comments', $node)); - $revisionView->setInsDel($xmlReader->getAttribute('w:insDel', $node)); + $revisionView->setInsDel(filter_var($xmlReader->getAttribute('w:insDel', $node), FILTER_VALIDATE_BOOLEAN)); $revisionView->setFormatting(filter_var($xmlReader->getAttribute('w:formatting', $node), FILTER_VALIDATE_BOOLEAN)); $revisionView->setInkAnnotations(filter_var($xmlReader->getAttribute('w:inkAnnotations', $node), FILTER_VALIDATE_BOOLEAN)); $phpWord->getSettings()->setRevisionView($revisionView); diff --git a/src/PhpWord/Shared/Converter.php b/src/PhpWord/Shared/Converter.php index bae8985d..56687c98 100644 --- a/src/PhpWord/Shared/Converter.php +++ b/src/PhpWord/Shared/Converter.php @@ -33,7 +33,7 @@ class Converter /** * Convert centimeter to twip * - * @param int $centimeter + * @param float $centimeter * @return float */ public static function cmToTwip($centimeter = 1) @@ -44,7 +44,7 @@ class Converter /** * Convert centimeter to inch * - * @param int $centimeter + * @param float $centimeter * @return float */ public static function cmToInch($centimeter = 1) @@ -55,7 +55,7 @@ class Converter /** * Convert centimeter to pixel * - * @param int $centimeter + * @param float $centimeter * @return float */ public static function cmToPixel($centimeter = 1) @@ -66,7 +66,7 @@ class Converter /** * Convert centimeter to point * - * @param int $centimeter + * @param float $centimeter * @return float */ public static function cmToPoint($centimeter = 1) @@ -77,8 +77,8 @@ class Converter /** * Convert centimeter to EMU * - * @param int $centimeter - * @return int + * @param float $centimeter + * @return float */ public static function cmToEmu($centimeter = 1) { @@ -88,8 +88,8 @@ class Converter /** * Convert inch to twip * - * @param int $inch - * @return int + * @param float $inch + * @return float */ public static function inchToTwip($inch = 1) { @@ -99,7 +99,7 @@ class Converter /** * Convert inch to centimeter * - * @param int $inch + * @param float $inch * @return float */ public static function inchToCm($inch = 1) @@ -110,8 +110,8 @@ class Converter /** * Convert inch to pixel * - * @param int $inch - * @return int + * @param float $inch + * @return float */ public static function inchToPixel($inch = 1) { @@ -121,8 +121,8 @@ class Converter /** * Convert inch to point * - * @param int $inch - * @return int + * @param float $inch + * @return float */ public static function inchToPoint($inch = 1) { @@ -132,8 +132,8 @@ class Converter /** * Convert inch to EMU * - * @param int $inch - * @return int + * @param float $inch + * @return float */ public static function inchToEmu($inch = 1) { @@ -144,7 +144,7 @@ class Converter * Convert pixel to twip * * @param int $pixel - * @return int + * @return float */ public static function pixelToTwip($pixel = 1) { @@ -188,7 +188,7 @@ class Converter * Convert point to twip unit * * @param int $point - * @return int + * @return float */ public static function pointToTwip($point = 1) { @@ -210,7 +210,7 @@ class Converter * Convert point to EMU * * @param int $point - * @return int + * @return float */ public static function pointToEmu($point = 1) { @@ -221,7 +221,7 @@ class Converter * Convert EMU to pixel * * @param int $emu - * @return int + * @return float */ public static function emuToPixel($emu = 1) { diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index 8310e515..d8a10b57 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -518,6 +518,7 @@ class Html private static function parseImage($node, $element) { $style = array(); + $src = null; foreach ($node->attributes as $attribute) { switch ($attribute->name) { case 'src': diff --git a/src/PhpWord/Shared/Microsoft/PasswordEncoder.php b/src/PhpWord/Shared/Microsoft/PasswordEncoder.php index d3a03d97..1c7b4c6c 100644 --- a/src/PhpWord/Shared/Microsoft/PasswordEncoder.php +++ b/src/PhpWord/Shared/Microsoft/PasswordEncoder.php @@ -165,7 +165,7 @@ class PasswordEncoder /** * Returns the algorithm ID * - * @param sting $algorithmName + * @param string $algorithmName * @return int */ public static function getAlgorithmId($algorithmName) diff --git a/src/PhpWord/Shared/ZipArchive.php b/src/PhpWord/Shared/ZipArchive.php index bb42a92a..3d8d0a41 100644 --- a/src/PhpWord/Shared/ZipArchive.php +++ b/src/PhpWord/Shared/ZipArchive.php @@ -161,7 +161,7 @@ class ZipArchive { if (!$this->usePclzip) { if ($this->zip->close() === false) { - throw new Exception("Could not close zip file {$this->filename}."); + throw new Exception("Could not close zip file {$this->filename}: "); } } diff --git a/src/PhpWord/Style/Paper.php b/src/PhpWord/Style/Paper.php index 2fbf59d2..09e4769e 100644 --- a/src/PhpWord/Style/Paper.php +++ b/src/PhpWord/Style/Paper.php @@ -118,14 +118,14 @@ class Paper extends AbstractStyle /** * Width * - * @var int (twip) + * @var float (twip) */ private $width; /** * Height * - * @var int (twip) + * @var float (twip) */ private $height; @@ -175,7 +175,7 @@ class Paper extends AbstractStyle /** * Get width * - * @return int + * @return float */ public function getWidth() { @@ -185,7 +185,7 @@ class Paper extends AbstractStyle /** * Get height * - * @return int + * @return float */ public function getHeight() { diff --git a/src/PhpWord/Writer/PDF/MPDF.php b/src/PhpWord/Writer/PDF/MPDF.php index 80c2eccf..e238057b 100644 --- a/src/PhpWord/Writer/PDF/MPDF.php +++ b/src/PhpWord/Writer/PDF/MPDF.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\PDF; +use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Writer\WriterInterface; /** @@ -27,12 +29,14 @@ use PhpOffice\PhpWord\Writer\WriterInterface; */ class MPDF extends AbstractRenderer implements WriterInterface { - /** - * Name of renderer include file - * - * @var string - */ - protected $includeFile = 'mpdf.php'; + public function __construct(PhpWord $phpWord) + { + if (file_exists(Settings::getPdfRendererPath() . '/mpdf.php')) { + // MPDF version 5.* needs this file to be included, later versions not + $this->includeFile = 'mpdf.php'; + } + parent::__construct($phpWord); + } /** * Save PhpWord to file. @@ -48,7 +52,13 @@ class MPDF extends AbstractRenderer implements WriterInterface $orientation = strtoupper('portrait'); // Create PDF - $pdf = new \mpdf(); + if ($this->includeFile != null) { + // MPDF version 5.* + $pdf = new \mpdf(); + } else { + // MPDF version > 6.* + $pdf = new \Mpdf\Mpdf(); + } $pdf->_setPageSize($paperSize, $orientation); $pdf->addPage($orientation); diff --git a/src/PhpWord/Writer/Word2007/Part/Comments.php b/src/PhpWord/Writer/Word2007/Part/Comments.php index 4551ca92..2b8f9267 100644 --- a/src/PhpWord/Writer/Word2007/Part/Comments.php +++ b/src/PhpWord/Writer/Word2007/Part/Comments.php @@ -29,7 +29,7 @@ class Comments extends AbstractPart /** * Comments collection to be written * - * @var \PhpOffice\PhpWord\Collection\Comments + * @var \PhpOffice\PhpWord\Element\Comment[] */ protected $elements; @@ -92,7 +92,7 @@ class Comments extends AbstractPart /** * Set element * - * @param \PhpOffice\PhpWord\Collection\Comments $elements + * @param \PhpOffice\PhpWord\Element\Comment[] $elements * @return self */ public function setElements($elements) diff --git a/tests/PhpWord/Shared/ZipArchiveTest.php b/tests/PhpWord/Shared/ZipArchiveTest.php index 91f0f030..cb095127 100644 --- a/tests/PhpWord/Shared/ZipArchiveTest.php +++ b/tests/PhpWord/Shared/ZipArchiveTest.php @@ -27,34 +27,34 @@ use PhpOffice\PhpWord\Settings; */ class ZipArchiveTest extends \PHPUnit\Framework\TestCase { - /** - * Test close method exception: Working in local, not working in Travis - * - * expectedException \PhpOffice\PhpWord\Exception\Exception - * expectedExceptionMessage Could not close zip file - * covers ::close - */ - public function testCloseException() - { - // $zipFile = __DIR__ . "/../_files/documents/ziptest.zip"; +// /** +// * Test close method exception: Working in local, not working in Travis +// * +// * expectedException \PhpOffice\PhpWord\Exception\Exception +// * expectedExceptionMessage Could not close zip file +// * covers ::close +// */ +// public function testCloseException() +// { +// $zipFile = __DIR__ . "/../_files/documents/ziptest.zip"; - // $object = new ZipArchive(); - // $object->open($zipFile, ZipArchive::CREATE); - // $object->addFromString('content/string.txt', 'Test'); +// $object = new ZipArchive(); +// $object->open($zipFile, ZipArchive::CREATE); +// $object->addFromString('content/string.txt', 'Test'); - // // Lock the file - // $resource = fopen($zipFile, "w"); - // flock($resource, LOCK_EX); +// // Lock the file +// $resource = fopen($zipFile, "w"); +// flock($resource, LOCK_EX); - // // Closing the file should throws an exception - // $object->close(); +// // Closing the file should throws an exception +// $object->close(); - // // Unlock the file - // flock($resource, LOCK_UN); - // fclose($resource); +// // Unlock the file +// flock($resource, LOCK_UN); +// fclose($resource); - // @unlink($zipFile); - } +// @unlink($zipFile); +// } /** * Test all methods diff --git a/tests/PhpWord/Writer/ODTextTest.php b/tests/PhpWord/Writer/ODTextTest.php index bb1b9538..1984de0f 100644 --- a/tests/PhpWord/Writer/ODTextTest.php +++ b/tests/PhpWord/Writer/ODTextTest.php @@ -110,6 +110,7 @@ class ODTextTest extends \PHPUnit\Framework\TestCase $section->addText('Test'); $writer = new ODText($phpWord); $writer->save('php://output'); + $this->assertNotNull($this->getActualOutput()); } /** diff --git a/tests/PhpWord/Writer/RTFTest.php b/tests/PhpWord/Writer/RTFTest.php index f4442043..803087e5 100644 --- a/tests/PhpWord/Writer/RTFTest.php +++ b/tests/PhpWord/Writer/RTFTest.php @@ -111,5 +111,6 @@ class RTFTest extends \PHPUnit\Framework\TestCase $section->addText(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8')); $writer = new RTF($phpWord); $writer->save('php://output'); + $this->assertNotNull($this->getActualOutput()); } } diff --git a/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php b/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php index 42c098cd..6998e717 100644 --- a/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/DocumentTest.php @@ -58,7 +58,8 @@ class DocumentTest extends \PHPUnit\Framework\TestCase $docInfo->setCustomProperty('key6', new \DateTime()); $docInfo->setCustomProperty('key7', time(), DocInfo::PROPERTY_TYPE_DATE); - TestHelperDOCX::getDocument($phpWord); + $doc = TestHelperDOCX::getDocument($phpWord); + $this->assertNotNull($doc); // $this->assertTrue($doc->elementExists('/Properties/property[name="key1"]/vt:lpwstr')); // $this->assertTrue($doc->elementExists('/Properties/property[name="key2"]/vt:bool'));