mirror of https://github.com/rectorphp/rector.git
Updated Rector to commit 3ef3e35c45970bce4000f45f9c1cb651f078c5a0
3ef3e35c45
[Php71] Skip re-assign as string on AssignArrayToStringRector part 2 (#5661)
This commit is contained in:
parent
dcd788c4dd
commit
cd427d527e
|
@ -3,6 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\Php71\Rector\Assign;
|
||||
|
||||
use PHPStan\Type\UnionType;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Array_;
|
||||
|
@ -186,7 +187,11 @@ CODE_SAMPLE
|
|||
if (!$this->isEmptyString($assign->expr)) {
|
||||
return null;
|
||||
}
|
||||
if ($this->nodeTypeResolver->getNativeType($assign->var)->isArray()->yes()) {
|
||||
$type = $this->nodeTypeResolver->getNativeType($assign->var);
|
||||
if ($type->isArray()->yes()) {
|
||||
return null;
|
||||
}
|
||||
if ($type instanceof UnionType) {
|
||||
return null;
|
||||
}
|
||||
$variableAssignArrayDimFetches = $this->findSameNamedVariableAssigns($assign->var, $node);
|
||||
|
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '2d1f468bc6e41bc84dc273c8ac94033b60761a76';
|
||||
public const PACKAGE_VERSION = '3ef3e35c45970bce4000f45f9c1cb651f078c5a0';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2024-02-23 20:04:16';
|
||||
public const RELEASE_DATE = '2024-02-24 02:26:27';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
|
@ -858,17 +858,17 @@
|
|||
},
|
||||
{
|
||||
"name": "phpstan\/phpdoc-parser",
|
||||
"version": "1.25.0",
|
||||
"version_normalized": "1.25.0.0",
|
||||
"version": "1.26.0",
|
||||
"version_normalized": "1.26.0.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https:\/\/github.com\/phpstan\/phpdoc-parser.git",
|
||||
"reference": "bd84b629c8de41aa2ae82c067c955e06f1b00240"
|
||||
"reference": "231e3186624c03d7e7c890ec662b81e6b0405227"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https:\/\/api.github.com\/repos\/phpstan\/phpdoc-parser\/zipball\/bd84b629c8de41aa2ae82c067c955e06f1b00240",
|
||||
"reference": "bd84b629c8de41aa2ae82c067c955e06f1b00240",
|
||||
"url": "https:\/\/api.github.com\/repos\/phpstan\/phpdoc-parser\/zipball\/231e3186624c03d7e7c890ec662b81e6b0405227",
|
||||
"reference": "231e3186624c03d7e7c890ec662b81e6b0405227",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
|
@ -885,7 +885,7 @@
|
|||
"phpunit\/phpunit": "^9.5",
|
||||
"symfony\/process": "^5.2"
|
||||
},
|
||||
"time": "2024-01-04T17:06:16+00:00",
|
||||
"time": "2024-02-23T16:05:55+00:00",
|
||||
"type": "library",
|
||||
"installation-source": "dist",
|
||||
"autoload": {
|
||||
|
@ -902,7 +902,7 @@
|
|||
"description": "PHPDoc parser with support for nullable, intersection and generic types",
|
||||
"support": {
|
||||
"issues": "https:\/\/github.com\/phpstan\/phpdoc-parser\/issues",
|
||||
"source": "https:\/\/github.com\/phpstan\/phpdoc-parser\/tree\/1.25.0"
|
||||
"source": "https:\/\/github.com\/phpstan\/phpdoc-parser\/tree\/1.26.0"
|
||||
},
|
||||
"install-path": "..\/phpstan\/phpdoc-parser"
|
||||
},
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -4,24 +4,29 @@ declare (strict_types=1);
|
|||
namespace PHPStan\PhpDocParser\Ast\Type;
|
||||
|
||||
use PHPStan\PhpDocParser\Ast\NodeAttributes;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
|
||||
use function implode;
|
||||
class CallableTypeNode implements \PHPStan\PhpDocParser\Ast\Type\TypeNode
|
||||
{
|
||||
use NodeAttributes;
|
||||
/** @var IdentifierTypeNode */
|
||||
public $identifier;
|
||||
/** @var TemplateTagValueNode[] */
|
||||
public $templateTypes;
|
||||
/** @var CallableTypeParameterNode[] */
|
||||
public $parameters;
|
||||
/** @var TypeNode */
|
||||
public $returnType;
|
||||
/**
|
||||
* @param CallableTypeParameterNode[] $parameters
|
||||
* @param TemplateTagValueNode[] $templateTypes
|
||||
*/
|
||||
public function __construct(\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode $identifier, array $parameters, \PHPStan\PhpDocParser\Ast\Type\TypeNode $returnType)
|
||||
public function __construct(\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode $identifier, array $parameters, \PHPStan\PhpDocParser\Ast\Type\TypeNode $returnType, array $templateTypes = [])
|
||||
{
|
||||
$this->identifier = $identifier;
|
||||
$this->parameters = $parameters;
|
||||
$this->returnType = $returnType;
|
||||
$this->templateTypes = $templateTypes;
|
||||
}
|
||||
public function __toString() : string
|
||||
{
|
||||
|
@ -29,7 +34,8 @@ class CallableTypeNode implements \PHPStan\PhpDocParser\Ast\Type\TypeNode
|
|||
if ($returnType instanceof self) {
|
||||
$returnType = "({$returnType})";
|
||||
}
|
||||
$template = $this->templateTypes !== [] ? '<' . implode(', ', $this->templateTypes) . '>' : '';
|
||||
$parameters = implode(', ', $this->parameters);
|
||||
return "{$this->identifier}({$parameters}): {$returnType}";
|
||||
return "{$this->identifier}{$template}({$parameters}): {$returnType}";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -337,7 +337,9 @@ class PhpDocParser
|
|||
case '@template-contravariant':
|
||||
case '@phpstan-template-contravariant':
|
||||
case '@psalm-template-contravariant':
|
||||
$tagValue = $this->parseTemplateTagValue($tokens, \true);
|
||||
$tagValue = $this->typeParser->parseTemplateTagValue($tokens, function ($tokens) {
|
||||
return $this->parseOptionalDescription($tokens);
|
||||
});
|
||||
break;
|
||||
case '@extends':
|
||||
case '@phpstan-extends':
|
||||
|
@ -638,16 +640,20 @@ class PhpDocParser
|
|||
}
|
||||
private function parseMethodTagValue(\PHPStan\PhpDocParser\Parser\TokenIterator $tokens) : Ast\PhpDoc\MethodTagValueNode
|
||||
{
|
||||
$isStatic = $tokens->tryConsumeTokenValue('static');
|
||||
$startLine = $tokens->currentTokenLine();
|
||||
$startIndex = $tokens->currentTokenIndex();
|
||||
$returnTypeOrMethodName = $this->typeParser->parse($tokens);
|
||||
$staticKeywordOrReturnTypeOrMethodName = $this->typeParser->parse($tokens);
|
||||
if ($staticKeywordOrReturnTypeOrMethodName instanceof Ast\Type\IdentifierTypeNode && $staticKeywordOrReturnTypeOrMethodName->name === 'static') {
|
||||
$isStatic = \true;
|
||||
$returnTypeOrMethodName = $this->typeParser->parse($tokens);
|
||||
} else {
|
||||
$isStatic = \false;
|
||||
$returnTypeOrMethodName = $staticKeywordOrReturnTypeOrMethodName;
|
||||
}
|
||||
if ($tokens->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER)) {
|
||||
$returnType = $returnTypeOrMethodName;
|
||||
$methodName = $tokens->currentTokenValue();
|
||||
$tokens->next();
|
||||
} elseif ($returnTypeOrMethodName instanceof Ast\Type\IdentifierTypeNode) {
|
||||
$returnType = $isStatic ? $this->typeParser->enrichWithAttributes($tokens, new Ast\Type\IdentifierTypeNode('static'), $startLine, $startIndex) : null;
|
||||
$returnType = $isStatic ? $staticKeywordOrReturnTypeOrMethodName : null;
|
||||
$methodName = $returnTypeOrMethodName->name;
|
||||
$isStatic = \false;
|
||||
} else {
|
||||
|
@ -660,7 +666,7 @@ class PhpDocParser
|
|||
do {
|
||||
$startLine = $tokens->currentTokenLine();
|
||||
$startIndex = $tokens->currentTokenIndex();
|
||||
$templateTypes[] = $this->enrichWithAttributes($tokens, $this->parseTemplateTagValue($tokens, \false), $startLine, $startIndex);
|
||||
$templateTypes[] = $this->enrichWithAttributes($tokens, $this->typeParser->parseTemplateTagValue($tokens), $startLine, $startIndex);
|
||||
} while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA));
|
||||
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET);
|
||||
}
|
||||
|
@ -700,27 +706,6 @@ class PhpDocParser
|
|||
}
|
||||
return $this->enrichWithAttributes($tokens, new Ast\PhpDoc\MethodTagValueParameterNode($parameterType, $isReference, $isVariadic, $parameterName, $defaultValue), $startLine, $startIndex);
|
||||
}
|
||||
private function parseTemplateTagValue(\PHPStan\PhpDocParser\Parser\TokenIterator $tokens, bool $parseDescription) : Ast\PhpDoc\TemplateTagValueNode
|
||||
{
|
||||
$name = $tokens->currentTokenValue();
|
||||
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
|
||||
if ($tokens->tryConsumeTokenValue('of') || $tokens->tryConsumeTokenValue('as')) {
|
||||
$bound = $this->typeParser->parse($tokens);
|
||||
} else {
|
||||
$bound = null;
|
||||
}
|
||||
if ($tokens->tryConsumeTokenValue('=')) {
|
||||
$default = $this->typeParser->parse($tokens);
|
||||
} else {
|
||||
$default = null;
|
||||
}
|
||||
if ($parseDescription) {
|
||||
$description = $this->parseOptionalDescription($tokens);
|
||||
} else {
|
||||
$description = '';
|
||||
}
|
||||
return new Ast\PhpDoc\TemplateTagValueNode($name, $bound, $description, $default);
|
||||
}
|
||||
private function parseExtendsTagValue(string $tagName, \PHPStan\PhpDocParser\Parser\TokenIterator $tokens) : Ast\PhpDoc\PhpDocTagValueNode
|
||||
{
|
||||
$startLine = $tokens->currentTokenLine();
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace PHPStan\PhpDocParser\Parser;
|
|||
|
||||
use LogicException;
|
||||
use PHPStan\PhpDocParser\Ast;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
|
||||
use PHPStan\PhpDocParser\Lexer\Lexer;
|
||||
use function in_array;
|
||||
use function str_replace;
|
||||
|
@ -128,12 +129,16 @@ class TypeParser
|
|||
if ($isHtml) {
|
||||
return $type;
|
||||
}
|
||||
$type = $this->parseGeneric($tokens, $type);
|
||||
if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
|
||||
$type = $this->tryParseArrayOrOffsetAccess($tokens, $type);
|
||||
$origType = $type;
|
||||
$type = $this->tryParseCallable($tokens, $type, \true);
|
||||
if ($type === $origType) {
|
||||
$type = $this->parseGeneric($tokens, $type);
|
||||
if ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
|
||||
$type = $this->tryParseArrayOrOffsetAccess($tokens, $type);
|
||||
}
|
||||
}
|
||||
} elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_PARENTHESES)) {
|
||||
$type = $this->tryParseCallable($tokens, $type);
|
||||
$type = $this->tryParseCallable($tokens, $type, \false);
|
||||
} elseif ($tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_SQUARE_BRACKET)) {
|
||||
$type = $this->tryParseArrayOrOffsetAccess($tokens, $type);
|
||||
} elseif (in_array($type->name, ['array', 'list', 'object'], \true) && $tokens->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET) && !$tokens->isPrecededByHorizontalWhitespace()) {
|
||||
|
@ -329,9 +334,35 @@ class TypeParser
|
|||
$type = $this->parse($tokens);
|
||||
return [$type, $variance];
|
||||
}
|
||||
/** @phpstan-impure */
|
||||
private function parseCallable(\PHPStan\PhpDocParser\Parser\TokenIterator $tokens, Ast\Type\IdentifierTypeNode $identifier) : Ast\Type\TypeNode
|
||||
/**
|
||||
* @throws ParserException
|
||||
* @param ?callable(TokenIterator): string $parseDescription
|
||||
*/
|
||||
public function parseTemplateTagValue(\PHPStan\PhpDocParser\Parser\TokenIterator $tokens, ?callable $parseDescription = null) : TemplateTagValueNode
|
||||
{
|
||||
$name = $tokens->currentTokenValue();
|
||||
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
|
||||
if ($tokens->tryConsumeTokenValue('of') || $tokens->tryConsumeTokenValue('as')) {
|
||||
$bound = $this->parse($tokens);
|
||||
} else {
|
||||
$bound = null;
|
||||
}
|
||||
if ($tokens->tryConsumeTokenValue('=')) {
|
||||
$default = $this->parse($tokens);
|
||||
} else {
|
||||
$default = null;
|
||||
}
|
||||
if ($parseDescription !== null) {
|
||||
$description = $parseDescription($tokens);
|
||||
} else {
|
||||
$description = '';
|
||||
}
|
||||
return new Ast\PhpDoc\TemplateTagValueNode($name, $bound, $description, $default);
|
||||
}
|
||||
/** @phpstan-impure */
|
||||
private function parseCallable(\PHPStan\PhpDocParser\Parser\TokenIterator $tokens, Ast\Type\IdentifierTypeNode $identifier, bool $hasTemplate) : Ast\Type\TypeNode
|
||||
{
|
||||
$templates = $hasTemplate ? $this->parseCallableTemplates($tokens) : [];
|
||||
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_PARENTHESES);
|
||||
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
|
||||
$parameters = [];
|
||||
|
@ -352,7 +383,36 @@ class TypeParser
|
|||
$startLine = $tokens->currentTokenLine();
|
||||
$startIndex = $tokens->currentTokenIndex();
|
||||
$returnType = $this->enrichWithAttributes($tokens, $this->parseCallableReturnType($tokens), $startLine, $startIndex);
|
||||
return new Ast\Type\CallableTypeNode($identifier, $parameters, $returnType);
|
||||
return new Ast\Type\CallableTypeNode($identifier, $parameters, $returnType, $templates);
|
||||
}
|
||||
/**
|
||||
* @return Ast\PhpDoc\TemplateTagValueNode[]
|
||||
*
|
||||
* @phpstan-impure
|
||||
*/
|
||||
private function parseCallableTemplates(\PHPStan\PhpDocParser\Parser\TokenIterator $tokens) : array
|
||||
{
|
||||
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET);
|
||||
$templates = [];
|
||||
$isFirst = \true;
|
||||
while ($isFirst || $tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
|
||||
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
|
||||
// trailing comma case
|
||||
if (!$isFirst && $tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET)) {
|
||||
break;
|
||||
}
|
||||
$isFirst = \false;
|
||||
$templates[] = $this->parseCallableTemplateArgument($tokens);
|
||||
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
|
||||
}
|
||||
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET);
|
||||
return $templates;
|
||||
}
|
||||
private function parseCallableTemplateArgument(\PHPStan\PhpDocParser\Parser\TokenIterator $tokens) : Ast\PhpDoc\TemplateTagValueNode
|
||||
{
|
||||
$startLine = $tokens->currentTokenLine();
|
||||
$startIndex = $tokens->currentTokenIndex();
|
||||
return $this->enrichWithAttributes($tokens, $this->parseTemplateTagValue($tokens), $startLine, $startIndex);
|
||||
}
|
||||
/** @phpstan-impure */
|
||||
private function parseCallableParameter(\PHPStan\PhpDocParser\Parser\TokenIterator $tokens) : Ast\Type\CallableTypeParameterNode
|
||||
|
@ -447,11 +507,11 @@ class TypeParser
|
|||
}
|
||||
}
|
||||
/** @phpstan-impure */
|
||||
private function tryParseCallable(\PHPStan\PhpDocParser\Parser\TokenIterator $tokens, Ast\Type\IdentifierTypeNode $identifier) : Ast\Type\TypeNode
|
||||
private function tryParseCallable(\PHPStan\PhpDocParser\Parser\TokenIterator $tokens, Ast\Type\IdentifierTypeNode $identifier, bool $hasTemplate) : Ast\Type\TypeNode
|
||||
{
|
||||
try {
|
||||
$tokens->pushSavePoint();
|
||||
$type = $this->parseCallable($tokens, $identifier);
|
||||
$type = $this->parseCallable($tokens, $identifier, $hasTemplate);
|
||||
$tokens->dropSavePoint();
|
||||
} catch (\PHPStan\PhpDocParser\Parser\ParserException $e) {
|
||||
$tokens->rollback();
|
||||
|
|
|
@ -90,7 +90,7 @@ final class Printer
|
|||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private $listInsertionMap = [PhpDocNode::class . '->children' => "\n * ", UnionTypeNode::class . '->types' => '|', IntersectionTypeNode::class . '->types' => '&', ArrayShapeNode::class . '->items' => ', ', ObjectShapeNode::class . '->items' => ', ', CallableTypeNode::class . '->parameters' => ', ', GenericTypeNode::class . '->genericTypes' => ', ', ConstExprArrayNode::class . '->items' => ', ', MethodTagValueNode::class . '->parameters' => ', ', DoctrineArray::class . '->items' => ', ', DoctrineAnnotation::class . '->arguments' => ', '];
|
||||
private $listInsertionMap = [PhpDocNode::class . '->children' => "\n * ", UnionTypeNode::class . '->types' => '|', IntersectionTypeNode::class . '->types' => '&', ArrayShapeNode::class . '->items' => ', ', ObjectShapeNode::class . '->items' => ', ', CallableTypeNode::class . '->parameters' => ', ', CallableTypeNode::class . '->templateTypes' => ', ', GenericTypeNode::class . '->genericTypes' => ', ', ConstExprArrayNode::class . '->items' => ', ', MethodTagValueNode::class . '->parameters' => ', ', DoctrineArray::class . '->items' => ', ', DoctrineAnnotation::class . '->arguments' => ', '];
|
||||
/**
|
||||
* [$find, $extraLeft, $extraRight]
|
||||
*
|
||||
|
@ -294,10 +294,13 @@ final class Printer
|
|||
} else {
|
||||
$returnType = $this->printType($node->returnType);
|
||||
}
|
||||
$template = $node->templateTypes !== [] ? '<' . implode(', ', array_map(function (TemplateTagValueNode $templateNode) : string {
|
||||
return $this->print($templateNode);
|
||||
}, $node->templateTypes)) . '>' : '';
|
||||
$parameters = implode(', ', array_map(function (CallableTypeParameterNode $parameterNode) : string {
|
||||
return $this->print($parameterNode);
|
||||
}, $node->parameters));
|
||||
return "{$node->identifier}({$parameters}): {$returnType}";
|
||||
return "{$node->identifier}{$template}({$parameters}): {$returnType}";
|
||||
}
|
||||
if ($node instanceof ConditionalTypeForParameterNode) {
|
||||
return sprintf('(%s %s %s ? %s : %s)', $node->parameterName, $node->negated ? 'is not' : 'is', $this->printType($node->targetType), $this->printType($node->if), $this->printType($node->else));
|
||||
|
|
Loading…
Reference in New Issue