Skip to content

Commit 8e441e8

Browse files
authored
Php7 Problem with TemplateProcessor Destructor (#2554)
Fix #2548. A particularly perplexing problem accidentally introduced by PR #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.
1 parent f9ce804 commit 8e441e8

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

docs/changes/2.x/2.0.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
- 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)
1313
- 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)
14+
- 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)
1415
- 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)
1516

1617
- 32-bit Problem in PasswordEncoder [@oleibman](https://github.com/oleibman) fixing [#2550](https://github.com/PHPOffice/PHPWord/issues/2550) in [#2551](https://github.com/PHPOffice/PHPWord/pull/2551)

src/PhpWord/Shared/ZipArchive.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PclZip;
2121
use PhpOffice\PhpWord\Exception\Exception;
2222
use PhpOffice\PhpWord\Settings;
23+
use Throwable;
2324

2425
/**
2526
* ZipArchive wrapper.
@@ -162,13 +163,16 @@ public function open($filename, $flags = null)
162163
* Close the active archive.
163164
*
164165
* @return bool
165-
*
166-
* @codeCoverageIgnore Can't find any test case. Uncomment when found.
167166
*/
168167
public function close()
169168
{
170169
if (!$this->usePclzip) {
171-
if ($this->zip->close() === false) {
170+
try {
171+
$result = @$this->zip->close();
172+
} catch (Throwable $e) {
173+
$result = false;
174+
}
175+
if ($result === false) {
172176
throw new Exception("Could not close zip file {$this->filename}: ");
173177
}
174178
}

tests/PhpWordTests/TemplateProcessorTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use PhpOffice\PhpWord\PhpWord;
2626
use PhpOffice\PhpWord\Settings;
2727
use PhpOffice\PhpWord\TemplateProcessor;
28+
use Throwable;
2829
use TypeError;
2930
use ZipArchive;
3031

@@ -63,12 +64,21 @@ protected function tearDown(): void
6364
*
6465
* @covers ::__construct
6566
* @covers ::__destruct
67+
* @covers \PhpOffice\PhpWord\Shared\ZipArchive::close
6668
*/
6769
public function testTheConstruct(): void
6870
{
6971
$object = $this->getTemplateProcessor(__DIR__ . '/_files/templates/blank.docx');
7072
self::assertInstanceOf('PhpOffice\\PhpWord\\TemplateProcessor', $object);
7173
self::assertEquals([], $object->getVariables());
74+
$object->save();
75+
76+
try {
77+
$object->zip()->close();
78+
self::fail('Expected exception for double close');
79+
} catch (Throwable $e) {
80+
// nothing to do here
81+
}
7282
}
7383

7484
/**

0 commit comments

Comments
 (0)