Return the backup file contents from Backup::getFile() (#37317) - #41092
Open
lbajsarowicz wants to merge 1 commit into
Open
Return the backup file contents from Backup::getFile() (#37317)#41092lbajsarowicz wants to merge 1 commit into
lbajsarowicz wants to merge 1 commit into
Conversation
Backup::getFile() is declared to return by reference but returned the result of a method call, which PHP cannot bind by reference and reports as "Only variable references should be returned by reference". The notice was masked by a second defect in the same statement: the method called Directory\Read::read(), which lists a directory, on the path of the backup file. FilesystemIterator cannot open a regular file, so the call threw a FileSystemException before the return statement was ever reached and the method could never return the contents it documents. Backup::output() already reads the same file with readFile(). Read the file with readFile() and return it through a local variable, so the contents are returned and the by-reference return no longer raises a notice. Fixes magento#37317
|
Hi @lbajsarowicz. Thank you for your contribution!
Allowed build names are:
You can find more information about the builds here For more details, review the Code Contributions documentation. |
Contributor
Author
|
@magento run all tests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description (*)
Magento\Backup\Model\Backup::getFile()is declared to return by reference but returns the result of a method call:PHP cannot bind a function call result to a reference, so this raises
Only variable references should be returned by reference, as reported.While fixing that, a second defect in the same statement turned up, and it explains why the notice is hard to observe in a running instance:
$this->varDirectory->read()isDirectory\Read::read(), which lists a directory. It is given the path of the backup file, andDriver\File::readDirectory()builds aFilesystemIteratorover it:The exception is thrown before the
returnstatement is reached, so the method can never return the contents its docblock promises (Return content of backup file) — it always fails for an existing backup file.Backup::output()reads the very same file correctly withreadFile(), which is whatgetFile()was meant to do.This PR:
readFile(), so the method returns the file contents it documents;@returndocblock fromarraytostringand documents theFileSystemException.The
&is deliberately kept. Removing it would be the cleaner long-term shape, but it changes the signature of a public method on an@apiclass; returning a variable satisfies the reported expected result ("Only a variable is returned by reference") with no backward compatibility impact.Two pre-existing PHPCS warnings in the touched file (constant visibility/description on line 27, duplicated property short description on line 61) are cleaned up so the file passes the CI ruleset.
Related Pull Requests
None.
Fixed Issues (if relevant)
\Magento\Backup\Model\Backup::getFile()does not return a variable by referenceManual testing scenarios (*)
var/backups/named like1700000000_db_test.sql.Magento\Framework\Exception\FileSystemException: FilesystemIterator::__construct(...): Failed to open directory: Not a directory.After: the contents of the backup file are returned as a string, with no PHP notice.
getFile()for a backup that does not exist and verify it still throwsLocalizedExceptionwith "The backup file does not exist."Questions or comments
Covered by unit tests (contents returned, no notice raised on the by-reference return, exception for a missing file) and by an integration test that exercises the real
vardirectory — the integration test is the one that fails on2.4-developwith theFileSystemExceptionabove, since a mocked directory cannot distinguishread()fromreadFile().Contribution checklist (*)