Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeadCode] Handle crash on valid conditional type on RemoveUselessReturnTagRector #6475

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this stop working on a union of arrays?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or on non-empty-array, which is a intersection type

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I talked with @TomasVotruba about this, and more and more we used PHPStan method from Type, it just cause more bugs, so we decide to use instanceof if the variable of call is used next, that's allow for improvement for more types later, rather than cause crash because of method not exists in target PHPStan type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, as soon as you use methods which are not part of the Type interface you hurt PHPStan guarantees.

It would be great if we could get rid of calls to methods which only exist on certain types

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #6478

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