From 0e2155f27153929921bc4efa9b28cf28c58ab091 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 25 Feb 2023 08:40:10 +0100 Subject: [PATCH 1/3] enable phpstan/phpstan-deprecation-rules --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 7624131a0..9a23c9d04 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "dibi/dibi": "^4.2", "php-parallel-lint/php-parallel-lint": "^1.3", "phpstan/extension-installer": "^1.2", + "phpstan/phpstan-deprecation-rules": "^1.1", "phpstan/phpstan-php-parser": "^1.1", "phpstan/phpstan-phpunit": "^1.0", "phpstan/phpstan-strict-rules": "^1.1", From ce01cbde4e1c3c12dbbdc0ca6bd38215a56b1bf8 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 26 Feb 2023 08:05:01 +0100 Subject: [PATCH 2/3] fix and baseline deprecations --- phpstan-baseline.neon | 196 +++++++++++++++++++++- src/SchemaReflection/SchemaReflection.php | 6 +- src/SqlAst/StrCaseReturnTypeExtension.php | 14 +- 3 files changed, 206 insertions(+), 10 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 55441d062..eb532e39e 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -15,20 +15,142 @@ parameters: count: 1 path: src/DbSchema/SchemaHasherMysql.php + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#" + count: 1 + path: src/DoctrineReflection/DoctrineReflection.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#" + count: 1 + path: src/DoctrineReflection/DoctrineReflection.php + + - + message: "#^Class Doctrine\\\\DBAL\\\\Result not found\\.$#" + count: 1 + path: src/DoctrineReflection/DoctrineResultObjectType.php + + - + message: "#^Class Doctrine\\\\DBAL\\\\Statement not found\\.$#" + count: 1 + path: src/DoctrineReflection/DoctrineStatementObjectType.php + + - + message: """ + #^Call to deprecated method resolveQueryString\\(\\) of class staabm\\\\PHPStanDba\\\\QueryReflection\\\\QueryReflection\\: + use resolveQueryStrings\\(\\) instead$# + """ + count: 1 + path: src/Extensions/DeployerRunMysqlQueryDynamicReturnTypeExtension.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#" + count: 1 + path: src/Extensions/DeployerRunMysqlQueryDynamicReturnTypeExtension.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#" + count: 2 + path: src/Extensions/DibiConnectionFetchDynamicReturnTypeExtension.php + + - + message: "#^Class Doctrine\\\\DBAL\\\\Connection not found\\.$#" + count: 1 + path: src/Extensions/DoctrineConnectionExecuteQueryDynamicReturnTypeExtension.php + + - + message: "#^Class Doctrine\\\\DBAL\\\\Connection not found\\.$#" + count: 1 + path: src/Extensions/DoctrineConnectionFetchDynamicReturnTypeExtension.php + + - + message: "#^Class Doctrine\\\\DBAL\\\\Connection not found\\.$#" + count: 1 + path: src/Extensions/DoctrineConnectionPrepareDynamicReturnTypeExtension.php + + - + message: "#^Class Doctrine\\\\DBAL\\\\Connection not found\\.$#" + count: 1 + path: src/Extensions/DoctrineConnectionQueryDynamicReturnTypeExtension.php + + - + message: "#^Class Doctrine\\\\DBAL\\\\Result not found\\.$#" + count: 1 + path: src/Extensions/DoctrineResultDynamicReturnTypeExtension.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#" + count: 1 + path: src/Extensions/DoctrineResultDynamicReturnTypeExtension.php + + - + message: "#^Class Doctrine\\\\DBAL\\\\Statement not found\\.$#" + count: 1 + path: src/Extensions/DoctrineStatementExecuteDynamicReturnTypeExtension.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#" + count: 1 + path: src/Extensions/PdoStatementColumnCountDynamicReturnTypeExtension.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#" + count: 1 + path: src/Extensions/PdoStatementFetchDynamicReturnTypeExtension.php + - message: "#^Strict comparison using \\!\\=\\= between null and PHPStan\\\\Type\\\\Type will always evaluate to true\\.$#" count: 1 path: src/Extensions/PdoStatementFetchDynamicReturnTypeExtension.php + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#" + count: 1 + path: src/Extensions/PdoStatementFetchObjectDynamicReturnTypeExtension.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#" + count: 1 + path: src/PdoReflection/PdoStatementObjectType.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#" + count: 1 + path: src/PdoReflection/PdoStatementObjectType.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#" + count: 1 + path: src/PdoReflection/PdoStatementReflection.php + - message: "#^Strict comparison using \\=\\=\\= between 'dibi' and 'dibi' will always evaluate to true\\.$#" count: 1 path: src/QueryReflection/DbaApi.php - - message: "#^Return type \\(array\\\\}\\>\\|PDOException\\|null\\) of method staabm\\\\PHPStanDba\\\\QueryReflection\\\\PdoPgSqlQueryReflector\\:\\:simulateQuery\\(\\) should be covariant with return type \\(array\\, precision\\: int\\<0, max\\>, pdo_type\\: 0\\|1\\|2\\|3\\|4\\|5\\|6\\|536870912\\|1073741824\\|2147483648\\}\\>\\|PDOException\\|null\\) of method staabm\\\\PHPStanDba\\\\QueryReflection\\\\BasePdoQueryReflector\\:\\:simulateQuery\\(\\)$#" + message: """ + #^Call to deprecated method getConstantArrays\\(\\) of class PHPStan\\\\Type\\\\TypeUtils\\: + Use PHPStan\\\\Type\\\\Type\\:\\:getConstantArrays\\(\\) instead and handle optional keys if necessary\\.$# + """ + count: 1 + path: src/QueryReflection/QueryReflection.php + + - + message: """ + #^Call to deprecated method getConstantStrings\\(\\) of class PHPStan\\\\Type\\\\TypeUtils\\: + Use PHPStan\\\\Type\\\\Type\\:\\:getConstantStrings\\(\\) instead$# + """ count: 1 - path: src/QueryReflection/PdoPgSqlQueryReflector.php + path: src/QueryReflection/QueryReflection.php + + - + message: """ + #^Call to deprecated method resolvePreparedQueryString\\(\\) of class staabm\\\\PHPStanDba\\\\QueryReflection\\\\QueryReflection\\: + use resolvePreparedQueryStrings\\(\\) instead$# + """ + count: 1 + path: src/QueryReflection/QueryReflection.php - message: "#^Casting to \\*NEVER\\* something that's already \\*NEVER\\*\\.$#" @@ -36,7 +158,12 @@ parameters: path: src/QueryReflection/QueryReflection.php - - message: "#^Instanceof between PHPStan\\\\Type\\\\Constant\\\\ConstantIntegerType and PHPStan\\\\Type\\\\Constant\\\\ConstantIntegerType will always evaluate to true\\.$#" + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#" + count: 3 + path: src/QueryReflection/QueryReflection.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#" count: 1 path: src/QueryReflection/QueryReflection.php @@ -45,6 +172,69 @@ parameters: count: 1 path: src/QueryReflection/QueryReflection.php + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Accessory\\\\AccessoryType is error\\-prone and deprecated\\. Use methods on PHPStan\\\\Type\\\\Type instead\\.$#" + count: 1 + path: src/QueryReflection/QuerySimulation.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\ArrayType is error\\-prone and deprecated\\. Use Type\\:\\:isArray\\(\\) or Type\\:\\:getArrays\\(\\) instead\\.$#" + count: 1 + path: src/QueryReflection/QuerySimulation.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\ConstantScalarType is error\\-prone and deprecated\\. Use Type\\:\\:isConstantScalarValue\\(\\) or Type\\:\\:getConstantScalarTypes\\(\\) or Type\\:\\:getConstantScalarValues\\(\\) instead\\.$#" + count: 1 + path: src/QueryReflection/QuerySimulation.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\IntersectionType is error\\-prone and deprecated\\.$#" + count: 2 + path: src/QueryReflection/QuerySimulation.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\ObjectType is error\\-prone and deprecated\\. Use Type\\:\\:isObject\\(\\) or Type\\:\\:getObjectClassNames\\(\\) instead\\.$#" + count: 2 + path: src/QueryReflection/QuerySimulation.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#" + count: 1 + path: src/Rules/DoctrineKeyValueStyleRule.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#" + count: 2 + path: src/Rules/DoctrineKeyValueStyleRule.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#" + count: 1 + path: src/Rules/PdoStatementExecuteMethodRule.php + + - + message: """ + #^Call to deprecated method resolveQueryString\\(\\) of class staabm\\\\PHPStanDba\\\\QueryReflection\\\\QueryReflection\\: + use resolveQueryStrings\\(\\) instead$# + """ + count: 1 + path: src/Rules/SyntaxErrorInDibiPreparedStatementMethodRule.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#" + count: 2 + path: src/Rules/SyntaxErrorInDibiPreparedStatementMethodRule.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\StringType is error\\-prone and deprecated\\. Use Type\\:\\:isString\\(\\) instead\\.$#" + count: 1 + path: src/Rules/SyntaxErrorInDibiPreparedStatementMethodRule.php + + - + message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantArrayType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantArrays\\(\\) instead\\.$#" + count: 1 + path: src/SchemaReflection/SchemaReflection.php + - message: "#^Call to function is_string\\(\\) with string will always evaluate to true\\.$#" count: 2 diff --git a/src/SchemaReflection/SchemaReflection.php b/src/SchemaReflection/SchemaReflection.php index db071411a..56444f9f1 100644 --- a/src/SchemaReflection/SchemaReflection.php +++ b/src/SchemaReflection/SchemaReflection.php @@ -43,11 +43,9 @@ public function getTable(string $tableName): ?Table $valueTypes = $resultType->getValueTypes(); $columns = []; foreach ($keyTypes as $i => $keyType) { - if (! $keyType instanceof ConstantStringType) { - throw new ShouldNotHappenException(); + foreach($keyType->getConstantStrings() as $constantString) { + $columns[] = new Column($constantString->getValue(), $valueTypes[$i]); } - - $columns[] = new Column($keyType->getValue(), $valueTypes[$i]); } return $this->tables[$tableName] = new Table($tableName, $columns); diff --git a/src/SqlAst/StrCaseReturnTypeExtension.php b/src/SqlAst/StrCaseReturnTypeExtension.php index ada95af41..aee3e4f9c 100644 --- a/src/SqlAst/StrCaseReturnTypeExtension.php +++ b/src/SqlAst/StrCaseReturnTypeExtension.php @@ -42,12 +42,20 @@ public function getReturnType(FunctionCall $expression, QueryScope $scope): ?Typ return new NullType(); } - if ($argType instanceof ConstantStringType) { + $results = []; + $constantStrings = $argType->getConstantStrings(); + foreach($constantStrings as $constantString) { if (\in_array($expression->getFunction()->getName(), [BuiltInFunction::LOWER, BuiltInFunction::LCASE], true)) { - return new ConstantStringType(strtolower($argType->getValue())); + $results[] = new ConstantStringType(strtolower($constantString->getValue())); + + continue; } - return new ConstantStringType(strtoupper($argType->getValue())); + $results[] = new ConstantStringType(strtoupper($constantString->getValue())); + } + + if (count($results) > 0) { + return TypeCombinator::union(...$results); } if (TypeCombinator::containsNull($argType)) { From 077aa54f26489275f663994817912e6f57398a30 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 26 Feb 2023 08:07:31 +0100 Subject: [PATCH 3/3] cs --- src/SchemaReflection/SchemaReflection.php | 4 +--- src/SqlAst/StrCaseReturnTypeExtension.php | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/SchemaReflection/SchemaReflection.php b/src/SchemaReflection/SchemaReflection.php index 56444f9f1..f7353bb71 100644 --- a/src/SchemaReflection/SchemaReflection.php +++ b/src/SchemaReflection/SchemaReflection.php @@ -4,9 +4,7 @@ namespace staabm\PHPStanDba\SchemaReflection; -use PHPStan\ShouldNotHappenException; use PHPStan\Type\Constant\ConstantArrayType; -use PHPStan\Type\Constant\ConstantStringType; final class SchemaReflection { @@ -43,7 +41,7 @@ public function getTable(string $tableName): ?Table $valueTypes = $resultType->getValueTypes(); $columns = []; foreach ($keyTypes as $i => $keyType) { - foreach($keyType->getConstantStrings() as $constantString) { + foreach ($keyType->getConstantStrings() as $constantString) { $columns[] = new Column($constantString->getValue(), $valueTypes[$i]); } } diff --git a/src/SqlAst/StrCaseReturnTypeExtension.php b/src/SqlAst/StrCaseReturnTypeExtension.php index aee3e4f9c..cf6024c18 100644 --- a/src/SqlAst/StrCaseReturnTypeExtension.php +++ b/src/SqlAst/StrCaseReturnTypeExtension.php @@ -44,7 +44,7 @@ public function getReturnType(FunctionCall $expression, QueryScope $scope): ?Typ $results = []; $constantStrings = $argType->getConstantStrings(); - foreach($constantStrings as $constantString) { + foreach ($constantStrings as $constantString) { if (\in_array($expression->getFunction()->getName(), [BuiltInFunction::LOWER, BuiltInFunction::LCASE], true)) { $results[] = new ConstantStringType(strtolower($constantString->getValue()));