Skip to content

Commit

Permalink
[DeadCode] Handle crash on valid conditional type on RemoveUselessRet…
Browse files Browse the repository at this point in the history
…urnTagRector (#6475)

* [DeadCode] Handle crash on valid conditional type on RemoveUselessReturnTagRector

* avoid using isArray()->yes() usage

* fix

* clean up isArray() check
  • Loading branch information
samsonasik authored Nov 22, 2024
1 parent d49ace3 commit 2f77705
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types = 1);

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\Fixture;

final readonly class SkipValidConditionalReturn
{
/**
* Groups an array by a key returned by a callback.
*
* Example:
*
* return SkipValidConditionalReturn::groupBy(
* $results,
* fn ($ticket) => (string) $ticket->getFile()->getId(),
* );
*
* @template TValue of mixed
* @template TCallbackValue of mixed
* @template TKey of array-key
* @param array<TValue> $input
* @param callable(TValue): TKey $keyCallback
* @param null|callable(TValue): TCallbackValue $valueCallback
*
* @return ($valueCallback is null ? array<TKey, non-empty-list<TValue>> : array<TKey, non-empty-list<TCallbackValue>>)
*/
public static function groupBy(array $input, callable $keyCallback, ?callable $valueCallback = null) : array
{
$output = [];
foreach ($input as $result) {
$output[$keyCallback($result)][] = $valueCallback !== null ? $valueCallback($result) : $result;
}

return $output;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private function shouldSkipArrayForInvalidTypeOrKeys(Expr $expr): bool
}

$arrayStaticType = $this->getType($expr);
if (! $arrayStaticType->isArray()->yes()) {
if (! $arrayStaticType instanceof ArrayType && ! $arrayStaticType instanceof ConstantArrayType) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private function processAddArrayReturnType(
ClassMethod|Function_|Closure $node,
Type $returnType
): ClassMethod|Function_|Closure|null {
if (! $returnType->isArray()->yes()) {
if (! $returnType instanceof ArrayType && ! $returnType instanceof ConstantArrayType) {
return null;
}

Expand All @@ -174,8 +174,10 @@ private function shouldSkip(ClassMethod|Function_|Closure $node, Scope $scope):
);
}

private function changeReturnType(ClassMethod|Function_|Closure $node, ArrayType|ConstantArrayType $arrayType): void
{
private function changeReturnType(
ClassMethod|Function_|Closure $node,
ArrayType|ConstantArrayType $arrayType
): void {
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);

// skip already filled type, on purpose
Expand Down
8 changes: 1 addition & 7 deletions rules/TypeDeclaration/TypeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -40,15 +39,10 @@ public function __construct(
*/
public function normalizeArrayOfUnionToUnionArray(Type $type, int $arrayNesting = 1): Type
{
if (! $type->isArray()->yes()) {
if (! $type instanceof ArrayType && ! $type instanceof ConstantArrayType) {
return $type;
}

if ($type instanceof UnionType || $type instanceof IntersectionType) {
return $type;
}

/** @var ArrayType|ConstantArrayType $type */
if ($type instanceof ConstantArrayType && $arrayNesting === 1) {
return $type;
}
Expand Down

0 comments on commit 2f77705

Please sign in to comment.