Skip to content

Conversation

@jlherren
Copy link
Contributor

Fix handling of non-bools as $strict parameter and omit false from return type when $string is valid base64.

This is useful when constant binary strings are required in code, e.g.:

$emptyZipFile = base64_decode('UEsFBgAAAAAAAAAAAAAAAAAAAAAAAA==', true);

Related to phpstan/phpstan#7866

- Detect when the result cannot be false
- Correctly treat non-boolean $strict parameter
@ondrejmirtes
Copy link
Member

This improves things a bit too much as it breaks analysis of PHPStan itself 😀 The base64 call inside the codebase meant to obfuscate a string so that we don't need to baseline related errors. But now we can un-obfuscate it and actually baseline the errors.

$isValidBase64 = TrinaryLogic::lazyExtremeIdentity(
$constantStrings,
static function (ConstantStringType $constantString): TrinaryLogic {
$isValid = base64_decode($constantString->getValue(), true) !== false;
Copy link
Contributor

@staabm staabm Jan 27, 2026

Choose a reason for hiding this comment

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

base64-decoding any string from within the codebase could skyrocket memory needed by phpstan analysis.

e.g. some codebases contain multi-megabyte base64 representations of images

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I'm inclining not doing this at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would a length-limit help?

Copy link
Member

Choose a reason for hiding this comment

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

I'm really not sure why would we want to decode literal strings passed to this method at all.

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