hotfix mysql connection resolving (#4399)

This commit is contained in:
Tomas Votruba 2020-10-12 21:06:24 +02:00 committed by GitHub
parent a31837679d
commit cb991af9c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 74 additions and 25 deletions

View File

@ -44,10 +44,12 @@ jobs:
run: bin/rector validate-sets --ansi
-
name: 'Validate PHPStan Compatibility'
run: |
bin/rector sync-types --ansi
bin/rector check-static-type-mappers --ansi
name: 'Validate PHPStan Types Compatibility'
run: bin/rector sync-types --ansi
-
name: 'Validate PHPStan Mappers Compatibility'
run: bin/rector check-static-type-mappers --ansi
-
name: 'PHP Linter'

View File

@ -274,7 +274,6 @@
"packages/symfony-php-config/tests",
"rules/symfony-php-config/tests"
],
"Rector\\SymfonyPhpDocParser\\Tests\\": "packages/simple-php-doc-parser/tests",
"Rector\\Symfony\\Tests\\": "rules/symfony/tests",
"Rector\\Transform\\Tests\\": "rules/transform/tests",
"Rector\\Twig\\Tests\\": "rules/twig/tests",

View File

@ -15,12 +15,12 @@
},
"autoload": {
"psr-4": {
"Rector\\SymfonyPhpDocParser\\": "src"
"Rector\\SimplePhpDocParser\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Rector\\SymfonyPhpDocParser\\Tests\\": "tests"
"Rector\\SimplePhpDocParser\\Tests\\": "tests"
}
}
}

View File

@ -9,8 +9,10 @@ use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PHPStan\Type\ResourceType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
@ -130,22 +132,18 @@ CODE_SAMPLE
return true;
}
$st = $this->getStaticType($node);
$staticType = $this->getStaticType($node);
$resourceType = new ResourceType();
if ($st->equals($resourceType)) {
if ($staticType->equals($resourceType)) {
return true;
}
if ($st instanceof UnionType) {
foreach ($st->getTypes() as $type) {
if ($type->equals($resourceType)) {
return true;
}
}
if ($this->isUnionTypeWithResourceSubType($staticType, $resourceType)) {
return true;
}
return false;
return $node instanceof Variable && $this->isName($node, 'connection');
}
private function findConnectionVariable(FuncCall $funcCall): ?Expr
@ -160,4 +158,17 @@ CODE_SAMPLE
return $connectionAssign !== null ? $connectionAssign->var : null;
}
private function isUnionTypeWithResourceSubType(Type $staticType, ResourceType $resourceType): bool
{
if ($staticType instanceof UnionType) {
foreach ($staticType->getTypes() as $type) {
if ($type->equals($resourceType)) {
return true;
}
}
}
return false;
}
}

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Rector\MysqlToMysqli\Tests\Fixture;
final class FullTests
{
public function run_with_connection()
{
$connection = mysql_connect('host', 'user', 'pass', true, MYSQL_CLIENT_IGNORE_SPACE);
return mysql_close($connection);
}
}
?>
-----
<?php
declare(strict_types=1);
namespace Rector\MysqlToMysqli\Tests\Fixture;
final class FullTests
{
public function run_with_connection()
{
$connection = mysqli_connect('host', 'user', 'pass');
return mysqli_close($connection);
}
}
?>

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Rector\Utils\PHPStanStaticTypeMapperChecker\Command;
use PHPStan\Type\NonexistentParentClassType;
use PHPStan\Type\ParserNodeTypeToPHPStanType;
use Rector\Core\Console\Command\AbstractCommand;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\Utils\PHPStanStaticTypeMapperChecker\Finder\PHPStanTypeClassFinder;
@ -61,14 +62,15 @@ final class CheckStaticTypeMappersCommand extends AbstractCommand
return ShellCode::SUCCESS;
}
$errorMessage = sprintf(
'Add new class to "%s" that implements "%s" for this type',
'packages/phpstan-static-type-mapper/src/TypeMapper',
TypeMapperInterface::class
);
$this->symfonyStyle->error($errorMessage);
$this->symfonyStyle->listing($missingNodeClasses);
foreach ($missingNodeClasses as $missingNodeClass) {
$errorMessage = sprintf(
'Add new class to "%s" that implements "%s" for "%s" type',
'packages/phpstan-static-type-mapper/src/TypeMapper',
TypeMapperInterface::class,
$missingNodeClass
);
$this->symfonyStyle->error($errorMessage);
}
return ShellCode::ERROR;
}
@ -92,7 +94,7 @@ final class CheckStaticTypeMappersCommand extends AbstractCommand
$unsupportedTypeClasses[] = $phpStanTypeClass;
}
$typesToRemove = [NonexistentParentClassType::class];
$typesToRemove = [NonexistentParentClassType::class, ParserNodeTypeToPHPStanType::class];
return array_diff($unsupportedTypeClasses, $typesToRemove);
}