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

implemented constant-type inference in ImplodeFunctionReturnTypeExtension #991

Merged
merged 3 commits into from Feb 6, 2022

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Feb 4, 2022

to allow phpstan-dba to check syntax errors in queries which use implode like

$query = 'SELECT * FROM abc WHERE id IN ('. implode(',', [A::ONE, B::TWO, 3]) .')';

I am aware that such queries are not optimal use and should better be expressed with a prepared statement and separated values, but I think there is a lot of code out their constructed like this .. would be great phpstan-dba could cover such existing but non-optimal query-code

Comment on lines -38 to -51
$accessoryTypes = [];
if ($argType->isIterableAtLeastOnce()->yes() && $argType->getIterableValueType()->isNonEmptyString()->yes()) {
$accessoryTypes[] = new AccessoryNonEmptyStringType();
}
if ($argType->getIterableValueType()->isLiteralString()->yes()) {
$accessoryTypes[] = new AccessoryLiteralStringType();
}

if (count($accessoryTypes) > 0) {
$accessoryTypes[] = new StringType();
return new IntersectionType($accessoryTypes);
}

return new StringType();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this code was duplicated in the extension. de-duplicated the existing logic with the first commit with 180b2af

@@ -7,9 +7,9 @@
class HelloWorld
{

protected function foo(string $message): void
protected function foo(string $message, string $x): void
Copy link
Contributor Author

Choose a reason for hiding this comment

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

adjusted the existing test, so it still leads to the same result as before this PR.

@@ -206,10 +206,10 @@ public function doFoo4(string $s, array $nonEmptyArrayWithNonEmptyStrings): void
assertType('non-empty-string', implode($nonEmptyArrayWithNonEmptyStrings));
}

public function sayHello(): void
public function sayHello(int $i): void
Copy link
Contributor Author

Choose a reason for hiding this comment

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

adjusted the existing test, so it still leads to the same result as before this PR.

@staabm staabm marked this pull request as ready for review February 4, 2022 09:49
@ondrejmirtes ondrejmirtes merged commit 5348490 into phpstan:master Feb 6, 2022
@ondrejmirtes
Copy link
Member

Thank you!

@staabm staabm deleted the constant-implode branch February 6, 2022 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants