Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with PHP 7.4, PHP 8.0 and migrate to Laminas Escaper #1946

Merged
merged 23 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
adb9172
allow PHP 7.4, 8.0
liborm85 Oct 17, 2020
e334ecf
compatibility with phpmd 2.9
liborm85 Oct 17, 2020
9dad1d2
PHP 7.4 - fixed invalid value in hexdec
liborm85 Oct 17, 2020
7fd4b64
- composer in php 5.x require increase memory limit
liborm85 Oct 17, 2020
9397821
PHP 8.0 - depracated libxml_disable_entity_loader, is disabled by def…
liborm85 Oct 17, 2020
9c6cf6f
PHP 8.0 fix - ValueError: file_exists(): Argument #1 ($filename) must…
liborm85 Oct 17, 2020
93579e9
forgotten condition for libxml_disable_entity_loader
liborm85 Oct 17, 2020
a36a429
rewrite check to local file
liborm85 Oct 17, 2020
bf1bb2b
PHP 8.0 - fixed Error: Unknown named parameter $styles
liborm85 Oct 17, 2020
2845284
revert from develop branch
liborm85 Oct 17, 2020
236c7c4
fix code format
liborm85 Oct 17, 2020
0edcab0
allow php 5.3, 5.4, 5.5 except xenial os
liborm85 Oct 18, 2020
eaf2212
fix format for coverage
liborm85 Oct 18, 2020
6d49b28
skip TCPDF test in PHP 5.3, because
liborm85 Oct 18, 2020
b7dc71a
scrutinizer fix, allow zip extension
liborm85 Oct 18, 2020
b8ffe04
fix cs issue: "Arguments with default values must be at the end of th…
liborm85 Oct 18, 2020
4e4282a
refixed "must not contain any null bytes"
liborm85 Oct 18, 2020
f2516b0
migrate from abandoned `Zend\Escaper` to `Laminas Escaper`
liborm85 Oct 18, 2020
4cc2cc1
travis upgrade to PHPUnit 8 for PHP 8
liborm85 Oct 19, 2020
6b99923
travis php 8 - convert tests from phpunit 7 format to phpunit 8 format
liborm85 Oct 19, 2020
a9e0125
travis support latest php as 8.0 now
liborm85 Dec 9, 2020
4b4dfb4
update ugly hack for PHPUnit 8 support
liborm85 Dec 10, 2020
36b63a1
skip test, because php 8.0 contain internally validation
liborm85 Dec 11, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ build:
analysis:
tests:
override: [php-scrutinizer-run]
environment:
php:
version: '7.4'
pecl_extensions:
- zip

filter:
excluded_paths: [ 'vendor/*', 'tests/*', 'samples/*', 'src/PhpWord/Shared/PCLZip/*' ]

Expand Down
21 changes: 15 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,30 @@ php:
- 7.1
- 7.2
- 7.3
- 7.4snapshot
- 7.4
- nightly

matrix:
include:
- php: 5.3
dist: precise
env: COMPOSER_MEMORY_LIMIT=3G
- php: 5.4
dist: trusty
- php: 5.5
dist: trusty
- php: 7.0
env: COVERAGE=1
- php: 7.3
- php: nightly
env: DEPENDENCIES="--ignore-platform-reqs"
exclude:
- php: 5.3
dist: xenial
- php: 5.4
dist: xenial
- php: 5.5
- php: 7.0
- php: 7.3
dist: xenial
allow_failures:
- php: 7.4snapshot
- php: nightly

cache:
directories:
Expand All @@ -55,7 +56,15 @@ before_script:
- if [ -z "$COVERAGE" ]; then phpenv config-rm xdebug.ini || echo "xdebug not available" ; fi
## Composer
- composer self-update
## Composer in PHP versions 5.x requires 3 GB memory
- if [ ${TRAVIS_PHP_VERSION:0:2} == "5." ]; then export COMPOSER_MEMORY_LIMIT=3G ; fi
- travis_wait composer install --prefer-source $(if [ -n "$DEPENDENCIES" ]; then echo $DEPENDENCIES; fi)
## PHP 8 require PHPUnit 8
- |
if [[ ${TRAVIS_PHP_VERSION:0:2} == "8." ]] || [[ $TRAVIS_PHP_VERSION == "nightly" ]]; then
travis_wait composer remove phpunit/phpunit --dev --no-interaction --ignore-platform-reqs
travis_wait composer require phpunit/phpunit ^8.0 --dev --ignore-platform-reqs
fi
## PHPDocumentor
##- mkdir -p build/docs
- mkdir -p build/coverage
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ PHPWord requires the following:

- PHP 5.3.3+
- [XML Parser extension](http://www.php.net/manual/en/xml.installation.php)
- [Zend\Escaper component](http://framework.zend.com/manual/current/en/modules/zend.escaper.introduction.html)
- [Zend\Stdlib component](http://framework.zend.com/manual/current/en/modules/zend.stdlib.hydrator.html)
- [Laminas Escaper component](https://docs.laminas.dev/laminas-escaper/intro/)
- [Zip extension](http://php.net/manual/en/book.zip.php) (optional, used to write OOXML and ODF)
- [GD extension](http://php.net/manual/en/book.image.php) (optional, used to add images)
- [XMLWriter extension](http://php.net/manual/en/book.xmlwriter.php) (optional, used to write OOXML and ODF)
Expand Down
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,27 @@
"fix": "Fixes issues found by PHP-CS"
},
"require": {
"php": "^5.3.3 || ^7.0",
"php": "^5.3.3 || ^7.0 || ^8.0",
"ext-xml": "*",
"zendframework/zend-escaper": "^2.2",
"laminas/laminas-escaper": "^2.2",
"phpoffice/common": "^0.2.9"
},
"require-dev": {
"ext-zip": "*",
"ext-gd": "*",
"phpunit/phpunit": "^4.8.36 || ^7.0",
"squizlabs/php_codesniffer": "^2.9",
"squizlabs/php_codesniffer": "^2.9 || ^3.5",
"friendsofphp/php-cs-fixer": "^2.2",
"phpmd/phpmd": "2.*",
"phploc/phploc": "2.* || 3.* || 4.*",
"phploc/phploc": "2.* || 3.* || 4.* || 5.* || 6.* || 7.*",
"dompdf/dompdf":"0.8.*",
"tecnickcom/tcpdf": "6.*",
"mpdf/mpdf": "5.7.4 || 6.* || 7.*",
"mpdf/mpdf": "5.7.4 || 6.* || 7.* || 8.*",
"php-coveralls/php-coveralls": "1.1.0 || ^2.0"
},
"replace": {
"laminas/laminas-zendframework-bridge": "*"
},
"suggest": {
"ext-zip": "Allows writing OOXML and ODF",
"ext-gd2": "Allows adding images",
Expand Down
4 changes: 1 addition & 3 deletions docs/installing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ Mandatory:

- PHP 5.3.3+
- `XML Parser <http://www.php.net/manual/en/xml.installation.php>`__ extension
- `Zend\\Escaper <http://framework.zend.com/manual/current/en/modules/zend.escaper.introduction.html>`__ component
- Zend\\Stdlib component
- `Zend\\Validator <http://framework.zend.com/manual/current/en/modules/zend.validator.html>`__ component
- `Laminas Escaper <https://docs.laminas.dev/laminas-escaper/intro/>`__ component

Optional:

Expand Down
2 changes: 2 additions & 0 deletions phpmd.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<rule ref="rulesets/naming.xml">
<exclude name="ShortVariable" />
<exclude name="ShortClassName" />
<exclude name="LongVariable" />
</rule>
<rule ref="rulesets/naming.xml/LongVariable">
Expand Down
1 change: 1 addition & 0 deletions src/PhpWord/Element/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public function __call($function, $args)
} else {
// All other elements
array_unshift($args, $element); // Prepend element name to the beginning of args array

return call_user_func_array(array($this, 'addElement'), $args);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Element/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ private function setSourceType()
} else {
$this->sourceType = self::SOURCE_GD;
}
} elseif (@file_exists($this->source)) {
} elseif ((strpos($this->source, chr(0)) === false) && @file_exists($this->source)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use PHP8-functions here, with a polyfill from Symfony.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any new feature in php8 or symfony polyfills to solve this problem.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str_contains doesn't solve much... Symfony polyfills require PHP 7, not compatible with PHP 5.x https://github.com/symfony/polyfill/blob/master/src/Php80/composer.json#L23

Copy link

@stephanvierkant stephanvierkant Oct 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that a serious problem? We should drop support for PHP 5 ASAP: #1948

And no, it ‘str_contains‘ doesn't solve a problem, but I think it's nicer. It improves readability for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is problem, because this PR is about compatibility with PHP 8 and PHP 7.4, not about drop support old PHP versions.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true. I opened an issue, I'd like to hear your opinion :)

$this->memoryImage = false;
$this->sourceType = self::SOURCE_LOCAL;
} else {
Expand Down
16 changes: 8 additions & 8 deletions src/PhpWord/Reader/MsDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -1581,7 +1581,7 @@ private function readPrl($data, $pos, $cbNum)
// Variables
$sprmCPicLocation = null;
$sprmCFData = null;
$sprmCFSpec = null;
//$sprmCFSpec = null;

do {
// Variables
Expand Down Expand Up @@ -1830,7 +1830,7 @@ private function readPrl($data, $pos, $cbNum)
break;
// sprmCFSpec
case 0x55:
$sprmCFSpec = $operand;
//$sprmCFSpec = $operand;
break;
// sprmCFtcBi
case 0x5E:
Expand Down Expand Up @@ -2094,11 +2094,11 @@ private function readPrl($data, $pos, $cbNum)
$sprmCPicLocation += 1;

// stPicName
$stPicName = '';
//$stPicName = '';
for ($inc = 0; $inc <= $cchPicName; $inc++) {
$chr = self::getInt1d($this->dataData, $sprmCPicLocation);
//$chr = self::getInt1d($this->dataData, $sprmCPicLocation);
$sprmCPicLocation += 1;
$stPicName .= chr($chr);
//$stPicName .= chr($chr);
}
}

Expand Down Expand Up @@ -2143,11 +2143,11 @@ private function readPrl($data, $pos, $cbNum)
$sprmCPicLocation += 1;
// nameData
if ($cbName > 0) {
$nameData = '';
//$nameData = '';
for ($inc = 0; $inc <= ($cbName / 2); $inc++) {
$chr = self::getInt2d($this->dataData, $sprmCPicLocation);
//$chr = self::getInt2d($this->dataData, $sprmCPicLocation);
$sprmCPicLocation += 2;
$nameData .= chr($chr);
//$nameData .= chr($chr);
}
}
// embeddedBlip
Expand Down
6 changes: 3 additions & 3 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,11 +573,11 @@ private function readCellStyle(XMLReader $xmlReader, \DOMElement $domNode)
* Returns the first child element found
*
* @param XMLReader $xmlReader
* @param \DOMElement $parentNode
* @param string|array $elements
* @param \DOMElement|null $parentNode
* @param string|array|null $elements
* @return string|null
*/
private function findPossibleElement(XMLReader $xmlReader, \DOMElement $parentNode = null, $elements)
private function findPossibleElement(XMLReader $xmlReader, \DOMElement $parentNode = null, $elements = null)
{
if (is_array($elements)) {
//if element is an array, we take the first element that exists in the XML
Expand Down
6 changes: 3 additions & 3 deletions src/PhpWord/Shared/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ public static function htmlToRgb($value)
return false;
}

$red = hexdec($red);
$green = hexdec($green);
$blue = hexdec($blue);
$red = ctype_xdigit($red) ? hexdec($red) : 0;
$green = ctype_xdigit($green) ? hexdec($green) : 0;
$blue = ctype_xdigit($blue) ? hexdec($blue) : 0;

return array($red, $green, $blue);
}
Expand Down
10 changes: 7 additions & 3 deletions src/PhpWord/Shared/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,19 @@ public static function addHtml($element, $html, $fullHTML = false, $preserveWhit
}

// Load DOM
$orignalLibEntityLoader = libxml_disable_entity_loader(true);
if (\PHP_VERSION_ID < 80000) {
$orignalLibEntityLoader = libxml_disable_entity_loader(true);
}
$dom = new \DOMDocument();
$dom->preserveWhiteSpace = $preserveWhiteSpace;
$dom->loadXML($html);
self::$xpath = new \DOMXPath($dom);
$node = $dom->getElementsByTagName('body');

self::parseNode($node->item(0), $element);
libxml_disable_entity_loader($orignalLibEntityLoader);
if (\PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader($orignalLibEntityLoader);
}
}

/**
Expand Down Expand Up @@ -178,7 +182,7 @@ protected static function parseNode($node, $element, $styles = array(), $data =
}
}
$method = "parse{$method}";
$newElement = call_user_func_array(array('PhpOffice\PhpWord\Shared\Html', $method), $arguments);
$newElement = call_user_func_array(array('PhpOffice\PhpWord\Shared\Html', $method), array_values($arguments));

// Retrieve back variables from arguments
foreach ($keys as $key) {
Expand Down
8 changes: 6 additions & 2 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ protected function readPartWithRels($fileName)
*/
protected function transformSingleXml($xml, $xsltProcessor)
{
$orignalLibEntityLoader = libxml_disable_entity_loader(true);
if (\PHP_VERSION_ID < 80000) {
$orignalLibEntityLoader = libxml_disable_entity_loader(true);
}
$domDocument = new \DOMDocument();
if (false === $domDocument->loadXML($xml)) {
throw new Exception('Could not load the given XML document.');
Expand All @@ -180,7 +182,9 @@ protected function transformSingleXml($xml, $xsltProcessor)
if (false === $transformedXml) {
throw new Exception('Could not transform the given XML document.');
}
libxml_disable_entity_loader($orignalLibEntityLoader);
if (\PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader($orignalLibEntityLoader);
}

return $transformedXml;
}
Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/Writer/HTML/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

namespace PhpOffice\PhpWord\Writer\HTML\Element;

use Laminas\Escaper\Escaper;
use PhpOffice\PhpWord\Element\AbstractElement as Element;
use PhpOffice\PhpWord\Writer\AbstractWriter;
use Zend\Escaper\Escaper;

/**
* Abstract HTML element writer
Expand Down Expand Up @@ -50,7 +50,7 @@ abstract class AbstractElement
protected $withoutP = false;

/**
* @var \Zend\Escaper\Escaper|\PhpOffice\PhpWord\Escaper\AbstractEscaper
* @var \Laminas\Escaper\Escaper|\PhpOffice\PhpWord\Escaper\AbstractEscaper
*/
protected $escaper;

Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/Writer/HTML/Part/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

namespace PhpOffice\PhpWord\Writer\HTML\Part;

use Laminas\Escaper\Escaper;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Writer\AbstractWriter;
use Zend\Escaper\Escaper;

/**
* @since 0.11.0
Expand All @@ -32,7 +32,7 @@ abstract class AbstractPart
private $parentWriter;

/**
* @var \Zend\Escaper\Escaper
* @var \Laminas\Escaper\Escaper
*/
protected $escaper;

Expand Down
6 changes: 3 additions & 3 deletions tests/PhpWord/Element/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ public function testCountColumns()
{
$oTable = new Table();
$oTable->addRow();
$element = $oTable->addCell();
$oTable->addCell();
$this->assertEquals($oTable->countColumns(), 1);
$element = $oTable->addCell();
$element = $oTable->addCell();
$oTable->addCell();
$oTable->addCell();
$this->assertEquals($oTable->countColumns(), 3);
}
}
6 changes: 6 additions & 0 deletions tests/PhpWord/Writer/PDF/TCPDFTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class TCPDFTest extends \PHPUnit\Framework\TestCase
*/
public function testConstruct()
{
// TCPDF version 6.3.5 doesn't support PHP 5.3, fixed via https://github.com/tecnickcom/TCPDF/pull/197,
// pending new release.
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
return;
}

$file = __DIR__ . '/../../_files/tcpdf.pdf';

$phpWord = new PhpWord();
Expand Down
8 changes: 6 additions & 2 deletions tests/PhpWord/_includes/XmlDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ public function getFileDom($file = 'word/document.xml')
$this->file = $file;

$file = $this->path . '/' . $file;
$orignalLibEntityLoader = libxml_disable_entity_loader(false);
if (\PHP_VERSION_ID < 80000) {
$orignalLibEntityLoader = libxml_disable_entity_loader(false);
}
$this->dom = new \DOMDocument();
$this->dom->load($file);
libxml_disable_entity_loader($orignalLibEntityLoader);
if (\PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader($orignalLibEntityLoader);
}

return $this->dom;
}
Expand Down