Skip to content
Open
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
104 changes: 104 additions & 0 deletions src/Type/Php/PgDmlDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?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 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();
$defaultReturnType = 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(), $defaultReturnType);
}

$flagsExpr = $args[$flagsArgPosition]->value;

return $this->getTypeFromFlags(
$functionName,
$this->bitwiseFlagAnalyser->bitwiseOrContainsConstant($flagsExpr, $scope, 'PGSQL_DML_STRING'),
$this->bitwiseFlagAnalyser->bitwiseOrContainsConstant($flagsExpr, $scope, 'PGSQL_DML_EXEC'),
$defaultReturnType,
);
}

private function getTypeFromFlags(string $functionName, TrinaryLogic $containsString, TrinaryLogic $containsExec, Type $defaultReturnType): ?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(
TypeCombinator::remove($defaultReturnType, new StringType()),
new ConstantBooleanType(true),
);
}
if (!$containsExec->no()) {
return null;
}
if ($containsString->yes()) {
return TypeCombinator::union(new StringType(), new ConstantBooleanType(false));
}
if ($containsString->no()) {
return new BooleanType();
}

return null;
}

if ($containsString->yes()) {
return TypeCombinator::union(new StringType(), new ConstantBooleanType(false));
}
if (!$containsString->no()) {
return null;
}

return TypeCombinator::remove($defaultReturnType, 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);
}

}
46 changes: 46 additions & 0 deletions tests/PHPStan/Analyser/nsrt/pg-dml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace PgDml;

use function PHPStan\Testing\assertType;

class Foo
{

public function doInsert(\PgSql\Connection $connection, int $flags): void
{
assertType('PgSql\Result|false', pg_insert($connection, 'table', []));
assertType('PgSql\Result|false', pg_insert($connection, 'table', [], PGSQL_DML_EXEC));
assertType('PgSql\Result|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|PgSql\Result|string', pg_insert($connection, 'table', [], $flags));
}

public function doUpdate(\PgSql\Connection $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));
}

public function doDelete(\PgSql\Connection $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));
}

public function doSelect(\PgSql\Connection $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));
}

}
27 changes: 27 additions & 0 deletions tests/PHPStan/Analyser/nsrt/pg-last-notice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace PgLastNotice;

use function PHPStan\Testing\assertType;

class Foo
{

/**
* @param mixed $connection
*/
public function doFoo($connection, int $mode): void
{
assertType('string', pg_last_notice($connection));
assertType('string', pg_last_notice($connection, PGSQL_NOTICE_LAST));
assertType('list<string>', pg_last_notice($connection, PGSQL_NOTICE_ALL));
assertType('true', pg_last_notice($connection, PGSQL_NOTICE_CLEAR));
assertType('list<string>|string|true', pg_last_notice($connection, $mode));
assertType('list<string>|string|true', pg_last_notice($connection, 42));

if ($mode === PGSQL_NOTICE_LAST || $mode === PGSQL_NOTICE_ALL) {
assertType('list<string>|string', pg_last_notice($connection, $mode));
}
}

}
22 changes: 22 additions & 0 deletions tests/PHPStan/Analyser/nsrt/pg-result-status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace PgResultStatus;

use function PHPStan\Testing\assertType;

class Foo
{

public function doFoo(\PgSql\Result $result, int $mode): void
{
assertType('int', pg_result_status($result));
assertType('int', pg_result_status($result, PGSQL_STATUS_LONG));
assertType('string', pg_result_status($result, PGSQL_STATUS_STRING));
assertType('int|string', pg_result_status($result, $mode));

if ($mode === PGSQL_STATUS_LONG || $mode === PGSQL_STATUS_STRING) {
assertType('int|string', pg_result_status($result, $mode));
}
}

}
Loading