PHPWord icon indicating copy to clipboard operation
PHPWord copied to clipboard

PHP 8.1 Support for addPreserveText

Open cscrewsandcaptains opened this issue 2 years ago • 0 comments

Describe the Bug

If I use php 8.1 this code doesn't work:

Steps to Reproduce

Please provide a code sample that reproduces the issue.

<?php
require __DIR__ . '/vendor/autoload.php';

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addTitle("test", 1);

$footer = $section->addFooter();
$footer->addPreserveText('{PAGE} / {NUMPAGES}');
	    
header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessing‌​ml.document");
header('Content-Disposition: attachment; filename=test.docx');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save("php://output");

Expected Behavior

The same output that I have if I use PHP 7.4

Current Behavior

preg_split(): Passing null to parameter #3 ($limit) of type int is deprecated in vendor\phpoffice\phpword\src\PhpWord\Element\PreserveText.php at line 63 public function __construct($text = null, $fontStyle = null, $paragraphStyle = null) ... $matches = preg_split('/({.*?})/', $this->text, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

-> preg_split parameter #3 needs to be -1 not null.

During XMLWriter->save(...): Uncaught yii\base\ErrorException: Return type of PhpOffice\PhpWord\Shared\XMLWriter::writeAttribute($name, $value) should either be compatible with XMLWriter::writeAttribute(string $name, string $value): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in vendor\phpoffice\phpword\src\PhpWord\Shared\XMLWriter.php:174

-> add #[\ReturnTypeWillChange] attribute

Context

Please fill in your environment information:

  • PHP Version: 8.1
  • PHPWord Version: 0.18.3

cscrewsandcaptains avatar Apr 28 '22 10:04 cscrewsandcaptains