Skip to content

Commit

Permalink
Php7 Problem with TemplateProcessor Destructor
Browse files Browse the repository at this point in the history
Fix PHPOffice#2548. A particularly perplexing problem accidentally introduced by PR PHPOffice#2475. Problem does not arise for Php8, and does not arise for Php7 unit tests. But, running *not* under Phpunit auspices with Php7 can cause a warning message at destructor time if the `save` function has been used. A very artificial test is introduced to test this situation.
  • Loading branch information
oleibman committed Jan 18, 2024
1 parent 2f4da6e commit e295f86
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/changes/2.x/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- MsDoc Reader : Correct Font Size Calculation by [@oleibman](https://github.com/oleibman) fixing [#2526](https://github.com/PHPOffice/PHPWord/issues/2526) in [#2531](https://github.com/PHPOffice/PHPWord/pull/2531)
- TemplateProcessor Persist File After Destruct [@oleibman](https://github.com/oleibman) fixing [#2539](https://github.com/PHPOffice/PHPWord/issues/2539) in [#2545](https://github.com/PHPOffice/PHPWord/pull/2545)
- TemplateProcessor Destructor Problem with Php7 [@oleibman](https://github.com/oleibman) fixing [#2548](https://github.com/PHPOffice/PHPWord/issues/2548) in [#2554](https://github.com/PHPOffice/PHPWord/pull/2554)
- bug: TemplateProcessor fix multiline values [@gimler](https://github.com/gimler) fixing [#268](https://github.com/PHPOffice/PHPWord/issues/268), [#2323](https://github.com/PHPOffice/PHPWord/issues/2323) and [#2486](https://github.com/PHPOffice/PHPWord/issues/2486) in [#2522](https://github.com/PHPOffice/PHPWord/pull/2522)

### Miscellaneous
Expand Down
10 changes: 7 additions & 3 deletions src/PhpWord/Shared/ZipArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PclZip;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Settings;
use Throwable;

/**
* ZipArchive wrapper.
Expand Down Expand Up @@ -162,13 +163,16 @@ public function open($filename, $flags = null)
* Close the active archive.
*
* @return bool
*
* @codeCoverageIgnore Can't find any test case. Uncomment when found.
*/
public function close()
{
if (!$this->usePclzip) {
if ($this->zip->close() === false) {
try {
$result = @$this->zip->close();
} catch (Throwable $e) {
$result = false;
}
if ($result === false) {
throw new Exception("Could not close zip file {$this->filename}: ");
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/PhpWordTests/TemplateProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\TemplateProcessor;
use Throwable;
use TypeError;
use ZipArchive;

Expand Down Expand Up @@ -63,12 +64,21 @@ protected function tearDown(): void
*
* @covers ::__construct
* @covers ::__destruct
* @covers \PhpOffice\PhpWord\Shared\ZipArchive::close
*/
public function testTheConstruct(): void
{
$object = $this->getTemplateProcessor(__DIR__ . '/_files/templates/blank.docx');
self::assertInstanceOf('PhpOffice\\PhpWord\\TemplateProcessor', $object);
self::assertEquals([], $object->getVariables());
$object->save();

try {
$object->zip()->close();
self::fail('Expected exception for double close');
} catch (Throwable $e) {
// nothing to do here
}
}

/**
Expand Down

0 comments on commit e295f86

Please sign in to comment.