Repository navigation
DynamicFunctionReturnTypeExtensions for PostgreSQL #6074
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\Php; | ||
|
|
||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use PHPStan\Reflection\FunctionReflection; | ||
| use PHPStan\Reflection\ParametersAcceptorSelector; | ||
| use PHPStan\TrinaryLogic; | ||
| use PHPStan\Type\BitwiseFlagHelper; | ||
| use PHPStan\Type\BooleanType; | ||
| use PHPStan\Type\Constant\ConstantBooleanType; | ||
| use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
| use PHPStan\Type\StringType; | ||
| use PHPStan\Type\Type; | ||
| use PHPStan\Type\TypeCombinator; | ||
| use PHPStan\Type\UnionType; | ||
| use function array_key_exists; | ||
|
|
||
| #[AutowiredService] | ||
| final class PgDmlDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
| { | ||
|
|
||
| /** @var array<string, int> */ | ||
| private array $flagsArgPositions = [ | ||
| 'pg_insert' => 3, | ||
| 'pg_update' => 4, | ||
| 'pg_delete' => 3, | ||
| 'pg_select' => 3, | ||
| ]; | ||
|
|
||
| public function __construct(private BitwiseFlagHelper $bitwiseFlagAnalyser) | ||
| { | ||
| } | ||
|
|
||
| public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
| { | ||
| return array_key_exists($functionReflection->getName(), $this->flagsArgPositions); | ||
| } | ||
|
|
||
| public function getTypeFromFunctionCall( | ||
| FunctionReflection $functionReflection, | ||
| FuncCall $functionCall, | ||
| Scope $scope, | ||
| ): ?Type | ||
| { | ||
| $functionName = $functionReflection->getName(); | ||
| $args = $functionCall->getArgs(); | ||
| $getDefaultReturnType = static fn (): Type => ParametersAcceptorSelector::selectFromArgs( | ||
| $scope, | ||
| $args, | ||
| $functionReflection->getVariants(), | ||
| )->getReturnType(); | ||
|
|
||
| $flagsArgPosition = $this->flagsArgPositions[$functionName]; | ||
| if (!isset($args[$flagsArgPosition])) { | ||
| // default flags are PGSQL_DML_EXEC | ||
| return $this->getTypeFromFlags($functionName, TrinaryLogic::createNo(), TrinaryLogic::createYes(), $getDefaultReturnType); | ||
| } | ||
|
|
||
| $flagsExpr = $args[$flagsArgPosition]->value; | ||
|
|
||
| return $this->getTypeFromFlags( | ||
| $functionName, | ||
| $this->bitwiseFlagAnalyser->bitwiseOrContainsConstant($flagsExpr, $scope, 'PGSQL_DML_STRING'), | ||
| $this->bitwiseFlagAnalyser->bitwiseOrContainsConstant($flagsExpr, $scope, 'PGSQL_DML_EXEC'), | ||
| $getDefaultReturnType, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @param callable(): Type $getDefaultReturnType | ||
| */ | ||
| private function getTypeFromFlags(string $functionName, TrinaryLogic $containsString, TrinaryLogic $containsExec, callable $getDefaultReturnType): ?Type | ||
| { | ||
| if ($functionName === 'pg_insert') { | ||
| // with PGSQL_DML_EXEC the result object is returned even when PGSQL_DML_STRING is also set | ||
| if ($containsExec->yes()) { | ||
| return TypeCombinator::remove( | ||
| $getDefaultReturnType(), | ||
| new UnionType([new StringType(), new ConstantBooleanType(true)]), | ||
| ); | ||
| } | ||
| if (!$containsExec->no()) { | ||
| return null; | ||
| } | ||
| if ($containsString->yes()) { | ||
| return new UnionType([new StringType(), new ConstantBooleanType(false)]); | ||
| } | ||
| if ($containsString->no()) { | ||
| return new BooleanType(); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| if ($containsString->yes()) { | ||
| return new UnionType([new StringType(), new ConstantBooleanType(false)]); | ||
| } | ||
| if (!$containsString->no()) { | ||
| return null; | ||
| } | ||
|
|
||
| return TypeCombinator::remove($getDefaultReturnType(), new StringType()); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\Php; | ||
|
|
||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use PHPStan\Reflection\FunctionReflection; | ||
| use PHPStan\Type\Accessory\AccessoryArrayListType; | ||
| use PHPStan\Type\ArrayType; | ||
| use PHPStan\Type\Constant\ConstantBooleanType; | ||
| use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
| use PHPStan\Type\IntegerType; | ||
| use PHPStan\Type\StringType; | ||
| use PHPStan\Type\Type; | ||
| use PHPStan\Type\TypeCombinator; | ||
|
|
||
| #[AutowiredService] | ||
| final class PgLastNoticeDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
| { | ||
|
|
||
| private const NOTICE_LAST = 1; | ||
| private const NOTICE_ALL = 2; | ||
| private const NOTICE_CLEAR = 3; | ||
|
|
||
| public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
| { | ||
| return $functionReflection->getName() === 'pg_last_notice'; | ||
| } | ||
|
|
||
| public function getTypeFromFunctionCall( | ||
| FunctionReflection $functionReflection, | ||
| FuncCall $functionCall, | ||
| Scope $scope, | ||
| ): Type | ||
| { | ||
| $args = $functionCall->getArgs(); | ||
| if (!isset($args[1])) { | ||
| return new StringType(); | ||
| } | ||
|
|
||
| $types = []; | ||
| foreach ($scope->getType($args[1]->value)->getConstantScalarValues() as $value) { | ||
| if ($value === self::NOTICE_LAST) { | ||
| $types[] = new StringType(); | ||
| } elseif ($value === self::NOTICE_ALL) { | ||
| $types[] = $this->createAllNoticesType(); | ||
| } elseif ($value === self::NOTICE_CLEAR) { | ||
| $types[] = new ConstantBooleanType(true); | ||
| } else { | ||
| return $this->createUnknownModeType(); | ||
| } | ||
| } | ||
|
|
||
| if ($types === []) { | ||
| return $this->createUnknownModeType(); | ||
| } | ||
|
|
||
| return TypeCombinator::union(...$types); | ||
| } | ||
|
|
||
| private function createAllNoticesType(): Type | ||
| { | ||
| return TypeCombinator::intersect( | ||
| new ArrayType(new IntegerType(), new StringType()), | ||
| new AccessoryArrayListType(), | ||
| ); | ||
| } | ||
|
|
||
| private function createUnknownModeType(): Type | ||
| { | ||
| return TypeCombinator::union( | ||
| new StringType(), | ||
| $this->createAllNoticesType(), | ||
| new ConstantBooleanType(true), | ||
| ); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\Php; | ||
|
|
||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use PHPStan\Reflection\FunctionReflection; | ||
| use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
| use PHPStan\Type\IntegerType; | ||
| use PHPStan\Type\StringType; | ||
| use PHPStan\Type\Type; | ||
| use PHPStan\Type\TypeCombinator; | ||
|
|
||
| #[AutowiredService] | ||
| final class PgResultStatusDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
| { | ||
|
|
||
| private const STATUS_LONG = 1; | ||
| private const STATUS_STRING = 2; | ||
|
|
||
| public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
| { | ||
| return $functionReflection->getName() === 'pg_result_status'; | ||
| } | ||
|
|
||
| public function getTypeFromFunctionCall( | ||
| FunctionReflection $functionReflection, | ||
| FuncCall $functionCall, | ||
| Scope $scope, | ||
| ): ?Type | ||
| { | ||
| $args = $functionCall->getArgs(); | ||
| if (!isset($args[1])) { | ||
| return new IntegerType(); | ||
| } | ||
|
|
||
| $types = []; | ||
| foreach ($scope->getType($args[1]->value)->getConstantScalarValues() as $value) { | ||
| if ($value === self::STATUS_LONG) { | ||
| $types[] = new IntegerType(); | ||
| } elseif ($value === self::STATUS_STRING) { | ||
| $types[] = new StringType(); | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| if ($types === []) { | ||
| return null; | ||
| } | ||
|
|
||
| return TypeCombinator::union(...$types); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?php // lint < 8.1 | ||
|
|
||
| namespace PgDmlResource; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class Foo | ||
| { | ||
|
|
||
| /** | ||
| * @param resource $connection | ||
| */ | ||
| public function doInsert($connection, int $flags): void | ||
| { | ||
| assertType('resource|false', pg_insert($connection, 'table', [])); | ||
| assertType('resource|false', pg_insert($connection, 'table', [], PGSQL_DML_EXEC)); | ||
| assertType('resource|false', pg_insert($connection, 'table', [], PGSQL_DML_EXEC | PGSQL_DML_STRING)); | ||
| assertType('string|false', pg_insert($connection, 'table', [], PGSQL_DML_STRING)); | ||
| assertType('string|false', pg_insert($connection, 'table', [], PGSQL_DML_NO_CONV | PGSQL_DML_STRING)); | ||
| assertType('bool', pg_insert($connection, 'table', [], PGSQL_DML_ASYNC)); | ||
| assertType('bool|resource|string', pg_insert($connection, 'table', [], $flags)); | ||
| } | ||
|
|
||
| /** | ||
| * @param resource $connection | ||
| */ | ||
| public function doUpdate($connection, int $flags): void | ||
| { | ||
| assertType('bool', pg_update($connection, 'table', [], [])); | ||
| assertType('bool', pg_update($connection, 'table', [], [], PGSQL_DML_EXEC)); | ||
| assertType('string|false', pg_update($connection, 'table', [], [], PGSQL_DML_STRING)); | ||
| assertType('string|false', pg_update($connection, 'table', [], [], PGSQL_DML_EXEC | PGSQL_DML_STRING)); | ||
| assertType('bool|string', pg_update($connection, 'table', [], [], $flags)); | ||
| } | ||
|
|
||
| /** | ||
| * @param resource $connection | ||
| */ | ||
| public function doDelete($connection, int $flags): void | ||
| { | ||
| assertType('bool', pg_delete($connection, 'table', [])); | ||
| assertType('bool', pg_delete($connection, 'table', [], PGSQL_DML_EXEC)); | ||
| assertType('string|false', pg_delete($connection, 'table', [], PGSQL_DML_STRING)); | ||
| assertType('bool|string', pg_delete($connection, 'table', [], $flags)); | ||
| } | ||
|
|
||
| /** | ||
| * @param resource $connection | ||
| */ | ||
| public function doSelect($connection, int $flags): void | ||
| { | ||
| assertType('array<int, array>|false', pg_select($connection, 'table', [])); | ||
| assertType('array<int, array>|false', pg_select($connection, 'table', [], PGSQL_DML_EXEC)); | ||
| assertType('string|false', pg_select($connection, 'table', [], PGSQL_DML_STRING)); | ||
| assertType('array<int, array>|string|false', pg_select($connection, 'table', [], $flags)); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.