Updated Rector to commit a41ab99080e117c96cfb7107f58ab7b6d9281aa9

a41ab99080 Fix VarAnnotationIncorrectNullableRector, add support for closure params when generating php doc (#2922)
This commit is contained in:
Tomas Votruba 2022-09-12 07:25:54 +00:00
parent f2017153a1
commit 592d268d8b
6 changed files with 82 additions and 33 deletions

View File

@ -37,7 +37,11 @@ final class ClosureTypeMapper implements TypeMapperInterface
{ {
$identifierTypeNode = new IdentifierTypeNode($type->getClassName()); $identifierTypeNode = new IdentifierTypeNode($type->getClassName());
$returnDocTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($type->getReturnType(), $typeKind); $returnDocTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($type->getReturnType(), $typeKind);
return new SpacingAwareCallableTypeNode($identifierTypeNode, [], $returnDocTypeNode); $parameterDocTypeNodes = [];
foreach ($type->getParameters() as $parameterReflection) {
$parameterDocTypeNodes[] = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($parameterReflection->getType(), $typeKind);
}
return new SpacingAwareCallableTypeNode($identifierTypeNode, $parameterDocTypeNodes, $returnDocTypeNode);
} }
/** /**
* @param TypeKind::* $typeKind * @param TypeKind::* $typeKind

View File

@ -5,6 +5,7 @@ namespace Rector\TypeDeclaration\Helper;
use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Param; use PhpParser\Node\Param;
use PHPStan\Type\ClosureType;
use PHPStan\Type\MixedType; use PHPStan\Type\MixedType;
use PHPStan\Type\NullType; use PHPStan\Type\NullType;
use PHPStan\Type\Type; use PHPStan\Type\Type;
@ -72,6 +73,63 @@ final class PhpDocNullableTypeHelper
} }
return $cleanNullableMixed; return $cleanNullableMixed;
} }
/**
* @param array<Type> $updatedDocTypes
*
* @return array<Type>
*/
private function appendOrPrependNullTypeIfAppropriate(bool $isPhpParserTypeContainingNullType, bool $isPhpDocTypeContainingClosureType, array $updatedDocTypes) : array
{
if (!$isPhpParserTypeContainingNullType) {
return $updatedDocTypes;
}
if ($isPhpDocTypeContainingClosureType) {
\array_unshift($updatedDocTypes, new NullType());
} else {
$updatedDocTypes[] = new NullType();
}
return $updatedDocTypes;
}
private function hasClosureType(Type $phpDocType) : bool
{
if ($phpDocType instanceof ClosureType) {
return \true;
}
if ($phpDocType instanceof UnionType) {
foreach ($phpDocType->getTypes() as $subType) {
if ($subType instanceof ClosureType) {
return \true;
}
}
}
return \false;
}
private function hasNullType(Type $phpDocType) : bool
{
$isPhpDocTypeContainingNullType = \false;
if ($phpDocType instanceof UnionType) {
return TypeCombinator::containsNull($phpDocType);
}
return $isPhpDocTypeContainingNullType;
}
/**
* @return Type[]
*/
private function resolveUpdatedDocTypes(Type $phpDocType) : array
{
$updatedDocTypes = [];
if ($phpDocType instanceof UnionType) {
foreach ($phpDocType->getTypes() as $subType) {
if ($subType instanceof NullType) {
continue;
}
$updatedDocTypes[] = $subType;
}
} else {
$updatedDocTypes[] = $phpDocType;
}
return $updatedDocTypes;
}
private function cleanNullableMixed(UnionType $unionType) : Type private function cleanNullableMixed(UnionType $unionType) : Type
{ {
if (!TypeCombinator::containsNull($unionType)) { if (!TypeCombinator::containsNull($unionType)) {
@ -105,26 +163,13 @@ final class PhpDocNullableTypeHelper
} }
private function resolveUpdatedPhpDocTypeFromPhpDocTypeAndPhpParserTypeNullInfo(Type $phpDocType, bool $isPhpParserTypeContainingNullType) : ?Type private function resolveUpdatedPhpDocTypeFromPhpDocTypeAndPhpParserTypeNullInfo(Type $phpDocType, bool $isPhpParserTypeContainingNullType) : ?Type
{ {
/** @var array<(NullType | UnionType)> $updatedDocTypes */ $isPhpDocTypeContainingNullType = $this->hasNullType($phpDocType);
$updatedDocTypes = []; $isPhpDocTypeContainingClosureType = $this->hasClosureType($phpDocType);
$phpDocTypeContainsNullType = \false; $updatedDocTypes = $this->resolveUpdatedDocTypes($phpDocType);
if ($phpDocType instanceof UnionType) { if (!$this->isItRequiredToRemoveOrAddNullTypeToUnion($isPhpDocTypeContainingNullType, $isPhpParserTypeContainingNullType)) {
$phpDocTypeContainsNullType = TypeCombinator::containsNull($phpDocType);
foreach ($phpDocType->getTypes() as $subType) {
if ($subType instanceof NullType) {
continue;
}
$updatedDocTypes[] = $subType;
}
} else {
$updatedDocTypes[] = $phpDocType;
}
if (!$this->isItRequiredToRemoveOrAddNullTypeToUnion($phpDocTypeContainsNullType, $isPhpParserTypeContainingNullType)) {
return null; return null;
} }
if ($isPhpParserTypeContainingNullType) { $updatedDocTypes = $this->appendOrPrependNullTypeIfAppropriate($isPhpParserTypeContainingNullType, $isPhpDocTypeContainingClosureType, $updatedDocTypes);
$updatedDocTypes[] = new NullType();
}
return $this->composeUpdatedPhpDocType($updatedDocTypes); return $this->composeUpdatedPhpDocType($updatedDocTypes);
} }
} }

View File

@ -17,12 +17,12 @@ final class VersionResolver
* @api * @api
* @var string * @var string
*/ */
public const PACKAGE_VERSION = 'effe4d38f68cfe64a38f7c2422befed3a872142e'; public const PACKAGE_VERSION = 'a41ab99080e117c96cfb7107f58ab7b6d9281aa9';
/** /**
* @api * @api
* @var string * @var string
*/ */
public const RELEASE_DATE = '2022-09-11 13:59:51'; public const RELEASE_DATE = '2022-09-12 09:20:11';
/** /**
* @var int * @var int
*/ */

2
vendor/autoload.php vendored
View File

@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php'; require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit38a04ff5251efa5c939ea18fa7ea2d80::getLoader(); return ComposerAutoloaderInitd4775f137b5d69f259413200877cd83b::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer // autoload_real.php @generated by Composer
class ComposerAutoloaderInit38a04ff5251efa5c939ea18fa7ea2d80 class ComposerAutoloaderInitd4775f137b5d69f259413200877cd83b
{ {
private static $loader; private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInit38a04ff5251efa5c939ea18fa7ea2d80
return self::$loader; return self::$loader;
} }
spl_autoload_register(array('ComposerAutoloaderInit38a04ff5251efa5c939ea18fa7ea2d80', 'loadClassLoader'), true, true); spl_autoload_register(array('ComposerAutoloaderInitd4775f137b5d69f259413200877cd83b', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit38a04ff5251efa5c939ea18fa7ea2d80', 'loadClassLoader')); spl_autoload_unregister(array('ComposerAutoloaderInitd4775f137b5d69f259413200877cd83b', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php'; require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit38a04ff5251efa5c939ea18fa7ea2d80::getInitializer($loader)); call_user_func(\Composer\Autoload\ComposerStaticInitd4775f137b5d69f259413200877cd83b::getInitializer($loader));
$loader->setClassMapAuthoritative(true); $loader->setClassMapAuthoritative(true);
$loader->register(true); $loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInit38a04ff5251efa5c939ea18fa7ea2d80::$files; $includeFiles = \Composer\Autoload\ComposerStaticInitd4775f137b5d69f259413200877cd83b::$files;
foreach ($includeFiles as $fileIdentifier => $file) { foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire38a04ff5251efa5c939ea18fa7ea2d80($fileIdentifier, $file); composerRequired4775f137b5d69f259413200877cd83b($fileIdentifier, $file);
} }
return $loader; return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInit38a04ff5251efa5c939ea18fa7ea2d80
* @param string $file * @param string $file
* @return void * @return void
*/ */
function composerRequire38a04ff5251efa5c939ea18fa7ea2d80($fileIdentifier, $file) function composerRequired4775f137b5d69f259413200877cd83b($fileIdentifier, $file)
{ {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload; namespace Composer\Autoload;
class ComposerStaticInit38a04ff5251efa5c939ea18fa7ea2d80 class ComposerStaticInitd4775f137b5d69f259413200877cd83b
{ {
public static $files = array ( public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php', 'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3094,9 +3094,9 @@ class ComposerStaticInit38a04ff5251efa5c939ea18fa7ea2d80
public static function getInitializer(ClassLoader $loader) public static function getInitializer(ClassLoader $loader)
{ {
return \Closure::bind(function () use ($loader) { return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit38a04ff5251efa5c939ea18fa7ea2d80::$prefixLengthsPsr4; $loader->prefixLengthsPsr4 = ComposerStaticInitd4775f137b5d69f259413200877cd83b::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit38a04ff5251efa5c939ea18fa7ea2d80::$prefixDirsPsr4; $loader->prefixDirsPsr4 = ComposerStaticInitd4775f137b5d69f259413200877cd83b::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit38a04ff5251efa5c939ea18fa7ea2d80::$classMap; $loader->classMap = ComposerStaticInitd4775f137b5d69f259413200877cd83b::$classMap;
}, null, ClassLoader::class); }, null, ClassLoader::class);
} }