PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.16k stars 2.68k forks source link

Call of $this->zipClass->close() repeat twice #2548

Open idem84 opened 5 months ago

idem84 commented 5 months ago

Describe the Bug

Inside file TemplateProcessor.php in functions __destruct() and save() repeat code that close ZipArchive, and becouse of that error appears: ZipArchive::close() Invalid or unitialized Zip object

Steps to Reproduce

Please provide a code sample that reproduces the issue.

function __destruct() 
// ZipClass
        if ($this->zipClass) {
            try {
                $this->zipClass->close();
            } catch (Throwable $e) {
                // Nothing to do here.
            }
        }

function save()
// Close zip file
        if (false === $this->zipClass->close()) {
            throw new Exception('Could not close zip file.'); // @codeCoverageIgnore
        }

Expected Behavior

I think inside _destruct need to remove close() function

Context

Please fill in your environment information:

oleibman commented 5 months ago

What is the problem you are seeing? The close in the destructor is in a try-catch block, so I think it should just fail silently and harmlessly in your case; I don't see any error messages or any other problem when I run tests.

oleibman commented 5 months ago

Hmm, I think I do see a warning message when I run under Php7.4. That is long out of support, but I'll investigate further.

oleibman commented 5 months ago

This is very strange. I can reproduce the problem (for Php7) when running on its own. But I cannot reproduce it when running Phpunit tests with Php7, which is why this code passed all unit tests. I can fix it for Php7, but I can't formally prove that it's been fixed.

oleibman commented 5 months ago

Less of a mystery now. Php seems to have changed this from a warning to a fatal error in Php8. So Php8 has no warning message to suppress, and, without an at-sign, Php7 has no reason to suppress a message.

ukasinn commented 3 months ago

I'm having the same problem with php 7.3 I replaced 【$this->zipClass->close();】with【@$this->zipClass->close();】 Will there be any other side effects?

oleibman commented 3 months ago

@ukasinn I think your change will not have side effects. PR #2554 will be a more robust solution when it is merged.