-
Notifications
You must be signed in to change notification settings - Fork 522
implement str-case functions return type extension #1325
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
Conversation
$accessoryTypes[] = new AccessoryNonEmptyStringType(); | ||
} | ||
if ($argType->isLiteralString()->yes()) { | ||
$accessoryTypes[] = new AccessoryLiteralStringType(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't sneak that in here, @craigfrancis would disagree ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about the same thing and was not sure. but then I found
phpstan-src/src/Type/Php/ImplodeFunctionReturnTypeExtension.php
Lines 73 to 75 in 28f4503
if ($arrayType->getIterableValueType()->isLiteralString()->yes() && $separatorType->isLiteralString()->yes()) { | |
$accessoryTypes[] = new AccessoryLiteralStringType(); | |
} |
maybe it is also wrong there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
implode
is one of the four functions that can produce literal strings as blessed by the original RFC: https://wiki.php.net/rfc/is_literal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, sorry, the only modification you can do that keep the literal-string
flag is concatenation (this was done to help adoption, and is seen as acceptable by several security people, like the developers behind go-safe-html; where str_repeat()
, str_pad()
, implode()
, and join()
are all glorified forms of concatenation)... other modifications no longer result in a "fixed value in source code" (ref); and while the security properties of some functions (like strtoupper) might be ok, I would like to avoid the discussion of what functions are allowed vs what aren't :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx for the insights
@staabm Please send a PR to phpstan-doctrine after this is merged :) |
if (count($strings) === 1) { | ||
return $strings[0]; | ||
} | ||
return new UnionType($strings); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need the if here. Using TypeCombinator::union()
is more correct. You can have an edge case of calling lcfirst
of 'foo'|'Foo'
and the current code would result in 'foo'|'foo'
which is bad :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh wow... how do you come up with such ideas...? well spotted!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the experience from doing this every day for the past 6 years 😅
Thank you! |
I might be misreading something, but this seems to have changed with 1.7.x-dev (9ad2792): |
what exactly do you mean? |
I'm trying to work on a different thing (using templates), so it might not be related... but lines 19-21 are doing what I expect with the output being seen as a |
Because |
Cool, thanks, I'll update some tests :-) |
closes phpstan/phpstan#6987