From 6d555dcc63b002d072a79c89a4d131dff73abe54 Mon Sep 17 00:00:00 2001 From: Mike Polyakovsky Date: Mon, 13 Jan 2020 00:13:11 +0300 Subject: [PATCH 1/2] Fix stub for DBAL QueryBuilder --- Plugin.php | 4 +- stubs/DBAL/QueryBuilder.php | 91 +++++++++++++++ tests/acceptance/QueryBuilder.feature | 4 +- tests/acceptance/QueryBuilderDbal.feature | 135 ++++++++++++++++++++++ 4 files changed, 231 insertions(+), 3 deletions(-) create mode 100644 stubs/DBAL/QueryBuilder.php create mode 100644 tests/acceptance/QueryBuilderDbal.feature diff --git a/Plugin.php b/Plugin.php index 35ab809..afd5588 100644 --- a/Plugin.php +++ b/Plugin.php @@ -12,6 +12,8 @@ use function class_exists; use function glob; +use const GLOB_BRACE; + class Plugin implements PluginEntryPointInterface { /** @return void */ @@ -27,7 +29,7 @@ public function __invoke(RegistrationInterface $psalm, ?SimpleXMLElement $config /** @return string[] */ private function getStubFiles(): array { - return glob(__DIR__ . '/' . 'stubs/*.php'); + return glob(__DIR__ . '/' . 'stubs/{,*/}*.php', GLOB_BRACE); } /** @return string[] */ diff --git a/stubs/DBAL/QueryBuilder.php b/stubs/DBAL/QueryBuilder.php new file mode 100644 index 0000000..7fa7d7d --- /dev/null +++ b/stubs/DBAL/QueryBuilder.php @@ -0,0 +1,91 @@ +having([$expr])->distinct(); + builder()->andHaving([$expr])->distinct(); """ When I run Psalm Then I see no errors diff --git a/tests/acceptance/QueryBuilderDbal.feature b/tests/acceptance/QueryBuilderDbal.feature new file mode 100644 index 0000000..a29b96f --- /dev/null +++ b/tests/acceptance/QueryBuilderDbal.feature @@ -0,0 +1,135 @@ +Feature: QueryBuilderDbal + In order to use Doctrine DBAL QueryBuilder safely + As a Psalm user + I need Psalm to typecheck QueryBuilder + + Background: + Given I have the following config + """ + + + + + + + + + + """ + And I have the following code preamble + """ + select('field1', 'field2')->distinct(); + """ + When I run Psalm + Then I see no errors + + @QueryBuilderDbal + Scenario: Dbal QueryBuilder ::select accepts array argument + Given I have the following code + """ + builder()->select(['field1', 'field1'])->distinct(); + """ + When I run Psalm + Then I see no errors + + @QueryBuilderDbal + Scenario: Dbal QueryBuilder ::addSelect accepts variadic arguments + Given I have the following code + """ + builder()->addSelect('field1', 'field2')->distinct(); + """ + When I run Psalm + Then I see no errors + + @QueryBuilderDbal + Scenario: Dbal QueryBuilder ::addSelect accepts array argument + Given I have the following code + """ + builder()->addSelect(['field1', 'field2'])->distinct(); + """ + When I run Psalm + Then I see no errors + + @QueryBuilderDbal + Scenario: Dbal QueryBuilder ::where, ::orWhere and ::andWhere accept variadic arguments + Given I have the following code + """ + builder()->where('field1', 'field2') + ->andWhere('field1', 'field2') + ->orWhere('field1', 'field2') + ->distinct(); + """ + When I run Psalm + Then I see no errors + + @QueryBuilderDbal + Scenario: Dbal QueryBuilder ::where, ::orWhere and ::andWhere accept CompositeExpression + Given I have the following code + """ + $expr = builder()->expr(); + $orx = $expr->orX(); + $orx->add($expr->eq('field1', 1)); + $orx->add($expr->eq('field1', 2)); + builder()->where($orx)->andWhere($orx)->orWhere($orx)->distinct(); + """ + When I run Psalm + Then I see no errors + + @QueryBuilderDbal + Scenario: Dbal QueryBuilder ::groupBy ::addGroupBy accept variadic arguments + Given I have the following code + """ + builder()->groupBy('field1', 'field2') + ->addGroupBy('field1', 'field2') + ->distinct(); + """ + When I run Psalm + Then I see no errors + + @QueryBuilderDbal + Scenario: Dbal QueryBuilder ::groupBy ::addGroupBy accept array argument + Given I have the following code + """ + builder()->groupBy(['field1', 'field2']) + ->addGroupBy(['field1', 'field2']) + ->distinct(); + """ + When I run Psalm + Then I see no errors + + @QueryBuilderDbal + Scenario: Dbal QueryBuilder ::having, ::orHaving and ::andHaving accept variadic arguments + Given I have the following code + """ + builder()->having('field1', 'field2') + ->orHaving('field1', 'field2') + ->andHaving('field1', 'field2') + ->distinct(); + """ + When I run Psalm + Then I see no errors + + @QueryBuilderDbal + Scenario: Dbal QueryBuilder ::having, ::orHaving and ::andHaving accept CompositeExpression + Given I have the following code + """ + $andx = builder()->expr()->andX('a = b'); + builder()->having($andx)->orHaving($andx)->andHaving($andx)->distinct(); + """ + When I run Psalm + Then I see no errors From 15f415dc3a1108a51a889a850b0e5cb80310c27e Mon Sep 17 00:00:00 2001 From: Mike Polyakovsky Date: Fri, 17 Jan 2020 21:25:44 +0300 Subject: [PATCH 2/2] Fix QueryBuilderDbal.feature: get rid of distinct() --- tests/acceptance/QueryBuilderDbal.feature | 24 ++++++++++------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/tests/acceptance/QueryBuilderDbal.feature b/tests/acceptance/QueryBuilderDbal.feature index a29b96f..9390220 100644 --- a/tests/acceptance/QueryBuilderDbal.feature +++ b/tests/acceptance/QueryBuilderDbal.feature @@ -33,7 +33,7 @@ Feature: QueryBuilderDbal Scenario: Dbal QueryBuilder ::select accepts variadic arguments Given I have the following code """ - builder()->select('field1', 'field2')->distinct(); + builder()->select('field1', 'field2'); """ When I run Psalm Then I see no errors @@ -42,7 +42,7 @@ Feature: QueryBuilderDbal Scenario: Dbal QueryBuilder ::select accepts array argument Given I have the following code """ - builder()->select(['field1', 'field1'])->distinct(); + builder()->select(['field1', 'field1']); """ When I run Psalm Then I see no errors @@ -51,7 +51,7 @@ Feature: QueryBuilderDbal Scenario: Dbal QueryBuilder ::addSelect accepts variadic arguments Given I have the following code """ - builder()->addSelect('field1', 'field2')->distinct(); + builder()->addSelect('field1', 'field2'); """ When I run Psalm Then I see no errors @@ -60,7 +60,7 @@ Feature: QueryBuilderDbal Scenario: Dbal QueryBuilder ::addSelect accepts array argument Given I have the following code """ - builder()->addSelect(['field1', 'field2'])->distinct(); + builder()->addSelect(['field1', 'field2']); """ When I run Psalm Then I see no errors @@ -71,8 +71,7 @@ Feature: QueryBuilderDbal """ builder()->where('field1', 'field2') ->andWhere('field1', 'field2') - ->orWhere('field1', 'field2') - ->distinct(); + ->orWhere('field1', 'field2'); """ When I run Psalm Then I see no errors @@ -85,7 +84,7 @@ Feature: QueryBuilderDbal $orx = $expr->orX(); $orx->add($expr->eq('field1', 1)); $orx->add($expr->eq('field1', 2)); - builder()->where($orx)->andWhere($orx)->orWhere($orx)->distinct(); + builder()->where($orx)->andWhere($orx)->orWhere($orx); """ When I run Psalm Then I see no errors @@ -95,8 +94,7 @@ Feature: QueryBuilderDbal Given I have the following code """ builder()->groupBy('field1', 'field2') - ->addGroupBy('field1', 'field2') - ->distinct(); + ->addGroupBy('field1', 'field2'); """ When I run Psalm Then I see no errors @@ -106,8 +104,7 @@ Feature: QueryBuilderDbal Given I have the following code """ builder()->groupBy(['field1', 'field2']) - ->addGroupBy(['field1', 'field2']) - ->distinct(); + ->addGroupBy(['field1', 'field2']); """ When I run Psalm Then I see no errors @@ -118,8 +115,7 @@ Feature: QueryBuilderDbal """ builder()->having('field1', 'field2') ->orHaving('field1', 'field2') - ->andHaving('field1', 'field2') - ->distinct(); + ->andHaving('field1', 'field2'); """ When I run Psalm Then I see no errors @@ -129,7 +125,7 @@ Feature: QueryBuilderDbal Given I have the following code """ $andx = builder()->expr()->andX('a = b'); - builder()->having($andx)->orHaving($andx)->andHaving($andx)->distinct(); + builder()->having($andx)->orHaving($andx)->andHaving($andx); """ When I run Psalm Then I see no errors