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

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

Merged
merged 11 commits into from
Sep 18, 2023

Conversation

samsonasik
Copy link
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
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
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
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
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
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
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
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
Member Author

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

// not directory
&& ! is_dir($skipRule)
// not file
&& ! is_file($skipRule)
// a Rector end
&& str_ends_with($skipRule, 'Rector')
Copy link
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
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
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
39 checks passed
@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
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
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Thanks 👏

Copy link
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
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
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants