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", 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..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,11 +41,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..cf6024c18 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)) {