Skip to content

[TypeDeclaration] Handle fallback from param same type object on ReturnTypeFromReturnNewRector#5039

Merged
samsonasik merged 11 commits intomainfrom
fallback-param
Sep 18, 2023
Merged

[TypeDeclaration] Handle fallback from param same type object on ReturnTypeFromReturnNewRector#5039
samsonasik merged 11 commits intomainfrom
fallback-param

Conversation

@samsonasik
Copy link
Copy Markdown
Member

@staabm this is to handle fallback from param on return new object:

final class FallbackFromParam
{
    public function action(SomeResponse $someResponse)
    {
        if (rand(0, 1)) {
            return new SomeResponse();
        }

        return $someResponse;
    }
}

@samsonasik
Copy link
Copy Markdown
Member Author

implemented 🎉

@samsonasik samsonasik changed the title [TypeDeclaration] Handle fallback from param on ReturnTypeFromReturnNewRector [TypeDeclaration] Handle fallback from param same type object on ReturnTypeFromReturnNewRector Sep 17, 2023
@samsonasik
Copy link
Copy Markdown
Member Author

@TomasVotruba the phpstan notice seems due to new phpstan-extensions release that cause invalid check on realpath() which can be false:

------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  packages/Caching/Detector/ChangedFilesDetector.php:90
 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  - '#Strict comparison using \=\=\= between string and false will always evaluate to false#'
 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------

see symplify/phpstan-extensions@b2b82e1#r127599628

@samsonasik
Copy link
Copy Markdown
Member Author

@TomasVotruba I created PR to symplify/phpstan-extensions to revert realpath:


final class FallbackFromParamSelf
{
public function action(self $obj): \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Fixture\FallbackFromParamSelf
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we need another test for a static and/or parent typed param?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

static already error on the first place :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

multiple return already skipped at

$returnType = $this->returnTypeInferer->inferFunctionLike($node);
if ($returnType instanceof UnionType) {

@samsonasik
Copy link
Copy Markdown
Member Author

I updated to use @var string|false and check with more verbose is_dir() && is_file() in various locations based on @TomasVotruba suggestion at

symplify/phpstan-extensions@b2b82e1#r127599628

@samsonasik
Copy link
Copy Markdown
Member Author

All checks have passed 🎉 @TomasVotruba I think it is ready.

Comment thread packages/Config/RectorConfig.php Outdated
// not file
&& ! is_file($skipRule)
// a Rector end
&& str_ends_with($skipRule, 'Rector')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should do this cheap chrck earlier

Copy link
Copy Markdown
Member Author

@samsonasik samsonasik Sep 18, 2023

Choose a reason for hiding this comment

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

updated 8fae378

@samsonasik
Copy link
Copy Markdown
Member Author

All checks have passed 🎉 @TomasVotruba I am merging it to have faster feedback to test ;)

@samsonasik samsonasik merged commit 2cf8d83 into main Sep 18, 2023
@samsonasik samsonasik deleted the fallback-param branch September 18, 2023 06:12
Comment on lines +144 to 147
/** @var string|false $setRealpath */
$setRealpath = realpath($setFile);
$setRealpath = (string) $setRealpath;
$relativeFilePath = Strings::after($setRealpath, getcwd() . '/');
Copy link
Copy Markdown
Member

@TomasVotruba TomasVotruba Sep 18, 2023

Choose a reason for hiding this comment

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

This will be always string, as we use existing set paths:

            $setRealpath = realpath($setFile);
            $relativeFilePath = Strings::after($setRealpath, getcwd() . '/');

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks 👏

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the php process might still not be able to read this file (e.g. because of filesystem persmissions) and return false?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@staabm that's what I thought, but since tools/ directory is not shipped to rector/rector, I guess it safe for user, the concern is probably when directory/set removed in the future and not detected.

The better solution is probably use:

Assert::string($setRealpath);

so it will be detected early when path removed/renamed.

@staabm
Copy link
Copy Markdown
Contributor

staabm commented Sep 18, 2023

Thanks 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants