Skip to content

Commit

Permalink
Updated Rector to commit 7182e19c4b0696ee48ff8c828afe47fd34d36d1c
Browse files Browse the repository at this point in the history
rectorphp/rector-src@7182e19 [TypeDeclaration] Skip array by doc with different assign on ReturnTypeFromStrictNewArrayRector (#5875)
  • Loading branch information
TomasVotruba committed May 14, 2024
1 parent 4bcc8bb commit 5dfd1c6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public function refactorWithScope(Node $node, Scope $scope) : ?Node
if ($stmts === null) {
return null;
}
$variable = $this->matchArrayAssignedVariable($stmts);
if (!$variable instanceof Variable) {
$variables = $this->matchArrayAssignedVariable($stmts);
if ($variables === []) {
return null;
}
// 2. skip yields
Expand All @@ -130,7 +130,8 @@ public function refactorWithScope(Node $node, Scope $scope) : ?Node
if ($returns === []) {
return null;
}
if ($this->isVariableOverriddenWithNonArray($node, $variable)) {
$variables = $this->matchVariableNotOverriddenByNonArray($node, $variables);
if ($variables === []) {
return null;
}
if (\count($returns) > 1) {
Expand All @@ -141,6 +142,9 @@ public function refactorWithScope(Node $node, Scope $scope) : ?Node
if (!$onlyReturn->expr instanceof Variable) {
return null;
}
if (!$this->nodeComparator->isNodeEqual($onlyReturn->expr, $variables)) {
return null;
}
$returnType = $this->nodeTypeResolver->getNativeType($onlyReturn->expr);
return $this->processAddArrayReturnType($node, $returnType);
}
Expand Down Expand Up @@ -193,9 +197,11 @@ private function changeReturnType($node, ArrayType $arrayType) : void
$this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, $narrowArrayType);
}
/**
* @param Variable[] $variables
* @return Variable[]
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $functionLike
*/
private function isVariableOverriddenWithNonArray($functionLike, Variable $variable) : bool
private function matchVariableNotOverriddenByNonArray($functionLike, array $variables) : array
{
// is variable overriden?
/** @var Assign[] $assigns */
Expand All @@ -204,24 +210,28 @@ private function isVariableOverriddenWithNonArray($functionLike, Variable $varia
if (!$assign->var instanceof Variable) {
continue;
}
if (!$this->nodeNameResolver->areNamesEqual($assign->var, $variable)) {
continue;
}
if ($assign->expr instanceof Array_) {
continue;
}
$nativeType = $this->nodeTypeResolver->getNativeType($assign->expr);
if (!$nativeType->isArray()->yes()) {
return \true;
foreach ($variables as $key => $variable) {
if (!$this->nodeNameResolver->areNamesEqual($assign->var, $variable)) {
continue;
}
if ($assign->expr instanceof Array_) {
continue;
}
$nativeType = $this->nodeTypeResolver->getNativeType($assign->expr);
if (!$nativeType->isArray()->yes()) {
unset($variables[$key]);
}
}
}
return \false;
return $variables;
}
/**
* @param Stmt[] $stmts
* @return Variable[]
*/
private function matchArrayAssignedVariable(array $stmts) : ?\PhpParser\Node\Expr\Variable
private function matchArrayAssignedVariable(array $stmts) : array
{
$variables = [];
foreach ($stmts as $stmt) {
if (!$stmt instanceof Expression) {
continue;
Expand All @@ -235,10 +245,10 @@ private function matchArrayAssignedVariable(array $stmts) : ?\PhpParser\Node\Exp
}
$nativeType = $this->nodeTypeResolver->getNativeType($assign->expr);
if ($nativeType->isArray()->yes()) {
return $assign->var;
$variables[] = $assign->var;
}
}
return null;
return $variables;
}
private function shouldAddReturnArrayDocType(ArrayType $arrayType) : bool
{
Expand Down
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '8f59d4e23ed974bf9eba52a075ef4d1e2604c80d';
public const PACKAGE_VERSION = '7182e19c4b0696ee48ff8c828afe47fd34d36d1c';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-05-14 15:26:48';
public const RELEASE_DATE = '2024-05-14 20:09:06';
/**
* @var int
*/
Expand Down

0 comments on commit 5dfd1c6

Please sign in to comment.