Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions resources/functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7792,7 +7792,7 @@
'pg_copy_from' => ['bool', 'connection'=>'resource', 'table_name'=>'string', 'rows'=>'array', 'delimiter='=>'string', 'null_as='=>'string'],
'pg_copy_to' => ['array|false', 'connection'=>'resource', 'table_name'=>'string', 'delimiter='=>'string', 'null_as='=>'string'],
'pg_dbname' => ['string', 'connection='=>'resource'],
'pg_delete' => ['mixed', 'db'=>'resource', 'table'=>'string', 'ids'=>'array', 'options='=>'int'],
'pg_delete' => ['string|bool', 'db'=>'resource', 'table'=>'string', 'ids'=>'array', 'options='=>'int'],
'pg_end_copy' => ['bool', 'connection='=>'resource'],
'pg_escape_bytea' => ['string', 'connection'=>'resource', 'data'=>'string'],
'pg_escape_bytea\'1' => ['string', 'data'=>'string'],
Expand Down Expand Up @@ -7829,7 +7829,7 @@
'pg_get_pid' => ['int|false', 'connection'=>'resource'],
'pg_get_result' => ['resource|false', 'connection='=>'resource'],
'pg_host' => ['string', 'connection='=>'resource'],
'pg_insert' => ['mixed', 'db'=>'resource', 'table'=>'string', 'values'=>'array', 'options='=>'int'],
'pg_insert' => ['resource|string|bool', 'db'=>'resource', 'table'=>'string', 'values'=>'array', 'options='=>'int'],
'pg_last_error' => ['string', 'connection='=>'resource', 'operation='=>'int'],
'pg_last_notice' => ['string', 'connection'=>'resource', 'option='=>'int'],
'pg_last_oid' => ['string|false', 'result'=>'resource'],
Expand Down Expand Up @@ -7867,8 +7867,8 @@
'pg_result_error' => ['string|false', 'result'=>'resource'],
'pg_result_error_field' => ['string|false|null', 'result'=>'resource', 'fieldcode'=>'int'],
'pg_result_seek' => ['bool', 'result'=>'resource', 'offset'=>'int'],
'pg_result_status' => ['mixed', 'result'=>'resource', 'result_type='=>'int'],
'pg_select' => ['mixed', 'db'=>'resource', 'table'=>'string', 'ids'=>'array', 'options='=>'int', 'result_type='=>'int'],
'pg_result_status' => ['string|int', 'result'=>'resource', 'result_type='=>'int'],
'pg_select' => ['array<int, array>|string|false', 'db'=>'resource', 'table'=>'string', 'ids'=>'array', 'options='=>'int', 'result_type='=>'int'],
'pg_send_execute' => ['bool', 'connection'=>'resource', 'stmtname'=>'string', 'params'=>'array'],
'pg_send_prepare' => ['bool', 'connection'=>'resource', 'stmtname'=>'string', 'query'=>'string'],
'pg_send_query' => ['bool', 'connection'=>'resource', 'query'=>'string'],
Expand All @@ -7885,7 +7885,7 @@
'pg_unescape_bytea' => ['string', 'data'=>'string'],
'pg_untrace' => ['bool', 'connection='=>'resource'],
'pg_untrace\'1' => ['bool'],
'pg_update' => ['mixed', 'db'=>'resource', 'table'=>'string', 'fields'=>'array', 'ids'=>'array', 'options='=>'int'],
'pg_update' => ['string|bool', 'db'=>'resource', 'table'=>'string', 'fields'=>'array', 'ids'=>'array', 'options='=>'int'],
'pg_version' => ['array', 'connection='=>'resource'],
'Phar::__construct' => ['void', 'fname'=>'string', 'flags='=>'int', 'alias='=>'string'],
'Phar::addEmptyDir' => ['', 'dirname'=>'string'],
Expand Down
4 changes: 4 additions & 0 deletions resources/functionMap_php81delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
'UnitEnum::cases' => ['list<static>'],
],
'old' => [
'pg_delete' => ['string|bool', 'db'=>'resource', 'table'=>'string', 'ids'=>'array', 'options='=>'int'],
'pg_escape_bytea' => ['string', 'connection'=>'resource', 'data'=>'string'],
'pg_escape_bytea\'1' => ['string', 'data'=>'string'],
'pg_escape_identifier' => ['string|false', 'connection'=>'resource', 'data'=>'string'],
Expand All @@ -48,6 +49,7 @@
'pg_field_is_null\'1' => ['int', 'result'=>'', 'row'=>'int', 'field_name_or_number'=>'string|int'],
'pg_field_prtlen' => ['int|false', 'result'=>'', 'field_name_or_number'=>''],
'pg_field_prtlen\'1' => ['int', 'result'=>'', 'row'=>'int', 'field_name_or_number'=>'string|int'],
'pg_insert' => ['resource|string|bool', 'db'=>'resource', 'table'=>'string', 'values'=>'array', 'options='=>'int'],
'pg_lo_export' => ['bool', 'connection'=>'resource', 'oid'=>'int', 'filename'=>'string'],
'pg_lo_export\'1' => ['bool', 'oid'=>'int', 'pathname'=>'string'],
'pg_lo_import' => ['int|string|false', 'connection'=>'resource', 'pathname'=>'string', 'oid'=>''],
Expand All @@ -62,6 +64,7 @@
'pg_query\'1' => ['resource|false', 'query'=>'string'],
'pg_query_params' => ['resource|false', 'connection'=>'resource', 'query'=>'string', 'params'=>'array'],
'pg_query_params\'1' => ['resource|false', 'query'=>'string', 'params'=>'array'],
'pg_select' => ['array<int, array>|string|false', 'db'=>'resource', 'table'=>'string', 'ids'=>'array', 'options='=>'int', 'result_type='=>'int'],
'pg_set_client_encoding' => ['int', 'connection'=>'resource', 'encoding'=>'string'],
'pg_set_client_encoding\'1' => ['int', 'encoding'=>'string'],
'pg_set_error_verbosity' => ['int|false', 'connection'=>'resource', 'verbosity'=>'int'],
Expand All @@ -70,5 +73,6 @@
'pg_tty\'1' => ['string'],
'pg_untrace' => ['bool', 'connection='=>'resource'],
'pg_untrace\'1' => ['bool'],
'pg_update' => ['string|bool', 'db'=>'resource', 'table'=>'string', 'fields'=>'array', 'ids'=>'array', 'options='=>'int'],
]
];
108 changes: 108 additions & 0 deletions src/Type/Php/PgDmlDynamicReturnTypeExtension.php
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)]),
);
Comment thread
VincentLanglet marked this conversation as resolved.
}
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());
}

}
79 changes: 79 additions & 0 deletions src/Type/Php/PgLastNoticeDynamicReturnTypeExtension.php
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),
);
}

}
56 changes: 56 additions & 0 deletions src/Type/Php/PgResultStatusDynamicReturnTypeExtension.php
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);
}

}
58 changes: 58 additions & 0 deletions tests/PHPStan/Analyser/nsrt/pg-dml-resource.php
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));
}

}
Loading
Loading