Skip to content

Introduce PDO::connect dynamicMethodReturnType #4015

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

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,11 @@ services:
tags:
- phpstan.functionParameterOutTypeExtension

-
class: PHPStan\Type\Php\PDOConnectReturnTypeExtension
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension

-
class: PHPStan\Type\Php\PregMatchTypeSpecifyingExtension
tags:
Expand Down
5 changes: 5 additions & 0 deletions src/Php/PhpVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,9 @@ public function supportsBcMathNumberOperatorOverloading(): bool
return $this->versionId >= 80400;
}

public function hasPDOSubclasses(): bool
{
return $this->versionId >= 80400;
}

}
76 changes: 76 additions & 0 deletions src/Type/Php/PDOConnectReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;
use function str_starts_with;

/**
* @see https://wiki.php.net/rfc/pdo_driver_specific_subclasses
* @see https://github.com/php/php-src/pull/12804
*/
final class PDOConnectReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
{

public function __construct(private PhpVersion $phpVersion)
{
}

public function getClass(): string
{
return 'PDO';
}

public function isStaticMethodSupported(MethodReflection $methodReflection): bool
{
return $this->phpVersion->hasPDOSubclasses() && $methodReflection->getName() === 'connect';
}

public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
{
if (count($methodCall->getArgs()) < 1) {
return null;
}

$valueType = $scope->getType($methodCall->getArgs()[0]->value);
$constantStrings = $valueType->getConstantStrings();
if (count($constantStrings) === 0) {
return null;
}

$subclasses = [];
foreach ($constantStrings as $constantString) {
if (str_starts_with($constantString->getValue(), 'mysql:')) {
$subclasses['PDO\Mysql'] = 'PDO\Mysql';
} elseif (str_starts_with($constantString->getValue(), 'firebird:')) {
$subclasses['PDO\Firebird'] = 'PDO\Firebird';
} elseif (str_starts_with($constantString->getValue(), 'dblib:')) {
$subclasses['PDO\Dblib'] = 'PDO\Dblib';
} elseif (str_starts_with($constantString->getValue(), 'odbc:')) {
$subclasses['PDO\Odbc'] = 'PDO\Odbc';
} elseif (str_starts_with($constantString->getValue(), 'pgsql:')) {
$subclasses['PDO\Pgsql'] = 'PDO\Pgsql';
} elseif (str_starts_with($constantString->getValue(), 'sqlite:')) {
$subclasses['PDO\Sqlite'] = 'PDO\Sqlite';
} else {
return null;
}
}

$returnTypes = [];
foreach ($subclasses as $class) {
$returnTypes[] = new ObjectType($class);
}

return TypeCombinator::union(...$returnTypes);
}

}
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/nsrt/pdo-connect-php84.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php // lint >= 8.4

namespace PdoConnectPHP84;

use function PHPStan\Testing\assertType;

/**
* @param 'mysql:foo'|'pgsql:foo' $mysqlOrPgsql
* @param 'mysql:foo'|'foo:foo' $mysqlOrFoo
*/
function test(
string $string,
string $mysqlOrPgsql,
string $mysqlOrFoo,
) {
assertType('PDO\Mysql', \PDO::connect('mysql:foo'));
assertType('PDO\Firebird', \PDO::connect('firebird:foo'));
assertType('PDO\Dblib', \PDO::connect('dblib:foo'));
assertType('PDO\Odbc', \PDO::connect('odbc:foo'));
assertType('PDO\Pgsql', \PDO::connect('pgsql:foo'));
assertType('PDO\Sqlite', \PDO::connect('sqlite:foo'));

assertType('PDO', \PDO::connect($string));
assertType('PDO\Mysql|PDO\Pgsql', \PDO::connect($mysqlOrPgsql));
assertType('PDO', \PDO::connect($mysqlOrFoo));
}
Loading