stechstudio / laravel-zipstream

Easily create Zip files on-the-fly and provide a streaming download
MIT License
413 stars 58 forks source link

PHPStan fails with Call to an undefined method STS\ZipStream\Contracts\FileContract::setFilesize() #116

Closed iDiegoNL closed 3 months ago

iDiegoNL commented 3 months ago

According to the readme, the following is possible:

$zip->add(
    File::makeFromDisk(...)->setFilesize(...)
);

However, PHPStan fails with the following error: Call to an undefined method STS\ZipStream\Contracts\FileContract::setFilesize(). This is because makeFromDisk() returns a FileContract, and that contract doesn't have a setFileSize() function.

It works fine in reality, since it actually returns a File class (that implements the contract), and that class does have the setFilesize() method. As a workaround to get my PHPStan to pass for now, I have split the snippet up so I can manually typehint it:

/** @var File $file */
$file = File::makeFromDisk(...);

$zip->add(
   $file->setFilesize(...)
);

However, it would still be nice for this to get fixed. From what I can see, makeFromDisk can just directly use File as its return type? Because both of the classes that it can return (S3File and LocalFile) both extend the File class. I can create a PR if this is the desired solution.

jszobody commented 3 months ago

Try updating to v5.0.1. I just changed the return type:

https://github.com/stechstudio/laravel-zipstream/commit/35408de79089fe6c3abb039e13594646d3ab5714

That work for PHPStan?

jszobody commented 3 months ago

Whoops I had changed the return type on make but not makeFromDisk. v5.0.2 tagged

iDiegoNL commented 3 months ago

Thanks a lot for the quick fix! However, 5.0.2 doesn't seem to be tagged yet haha

jszobody commented 3 months ago

Ha. I tagged it locally, that's not good enough? ;-)

Pushed the tag now.