Skip to content

Conversation

VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Nov 24, 2024

Follow up of #3541 (cc @staabm)

Currently md5('foo') is considered non-falsy-string&lowercase-string, but hash('md5', 'foo') is still non-empty-string.

At first, I planned to list hash algo with lowercase output, but

  • I tried
foreach (hash_algos() as $algo) {
     var_dump(hash($algo, ''));
}

and get everytime a non-falsy-string and lowercase-string (when binary is false)

  • My research on internet/chatGPT give the same result.

So it seems like it's always non-falsy-string&lowercase-string with binary false, and non-falsy-string when binary true.

'HaruPage::textOut' => ['bool', 'x'=>'float', 'y'=>'float', 'text'=>'string'],
'HaruPage::textRect' => ['bool', 'left'=>'float', 'top'=>'float', 'right'=>'float', 'bottom'=>'float', 'text'=>'string', 'align='=>'int'],
'hash' => ['non-empty-string|false', 'algo'=>'string', 'data'=>'string', 'raw_output='=>'bool'],
'hash' => ['(non-falsy-string&lowercase-string)|false', 'algo'=>'string', 'data'=>'string', 'raw_output='=>'bool'],
Copy link
Contributor

@mvorisek mvorisek Nov 24, 2024

Choose a reason for hiding this comment

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

https://www.php.net/manual/en/function.hash.php - must be conditional return, when $binary flag is set, the string can be anything (contain UC easily)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks ! I updated the PR, please take a new look

Copy link
Contributor

Choose a reason for hiding this comment

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

I am still not sure if non-falsy-string in the functionMap.php is correct as short hash functions like crc32 can quite easily produce falsy string.

Copy link
Contributor

Choose a reason for hiding this comment

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

@mvorisek can you show an example on 3v4l.org ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

so your examples shows us a case where it is non-falsy-string - which is the opposite of your thesis?

crc32 can quite easily produce falsy string

Copy link
Contributor

Choose a reason for hiding this comment

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

You are right! '00..' is non-falsy-string per https://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting but only '00..' == 0 is true.

Copy link
Contributor

@staabm staabm Nov 29, 2024

Choose a reason for hiding this comment

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

AFAIK every string with more then 1 char is non-falsy-string

@VincentLanglet VincentLanglet marked this pull request as ready for review November 25, 2024 19:54
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@VincentLanglet
Copy link
Contributor Author

What do you think about this PR @ondrejmirtes ?

If preferred, I can use non-empty-string rather than non-falsy-string in order to be safer (?)

@ondrejmirtes ondrejmirtes merged commit 2cb89ae into phpstan:1.12.x Jan 16, 2025
453 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

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.

5 participants