Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More precise getimagesize* return type #3183

Merged
merged 3 commits into from
Jun 25, 2024
Merged

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Jun 24, 2024

index 0/1 is width/height of the image which cannot be negative

@@ -3331,8 +3331,8 @@
'gethostbyname' => ['string', 'hostname'=>'string'],
'gethostbynamel' => ['list<string>|false', 'hostname'=>'string'],
'gethostname' => ['string|false'],
'getimagesize' => ['array{0:int, 1: int, 2: int, 3: string, mime: string, channels?: int, bits?: int}|false', 'imagefile'=>'string', '&w_info='=>'array'],
'getimagesizefromstring' => ['array{0:int, 1: int, 2: int, 3: string, mime: string, channels?: int, bits?: int}|false', 'data'=>'string', '&w_info='=>'array'],
'getimagesize' => ['array{0: positive-int, 1: 0|positive-int, 2: int, 3: string, mime: string, channels?: int, bits?: int}|false', 'imagefile'=>'string', '&w_info='=>'array'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already handle imagesx and imagesy as non-zero return, therefore I turned it eventually into just positive-int

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new version of phpstan found this in our code.

https://phpstan.org/r/f0ef9a4e-00d2-4df9-a129-a79857f28273

But the documentation of PHP says:

Some formats may contain no image or may contain multiple images. In these cases, getimagesize() might not be able to properly determine the image size. getimagesize() will return zero for width and height in these cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see thanks. please open a PR which uses 0|positive-int instead of positive-int

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. #3207

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mfruehauf thanks!

Also, note that if width is greater than 0, height will necessarily be greater than 0 too. (In your example, there is no need to check for both)

@ondrejmirtes ondrejmirtes merged commit 36e60f4 into phpstan:1.11.x Jun 25, 2024
447 of 453 checks passed
@ondrejmirtes
Copy link
Member

Thank you.

@staabm staabm deleted the size branch June 25, 2024 11:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants