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

[PHPStanStaticTypeMapper] Do not crash on numeric string on ReturnTypeDeclarationRector #1588

Merged
merged 3 commits into from
Dec 29, 2021

Conversation

samsonasik
Copy link
Member

@samsonasik samsonasik commented Dec 29, 2021

It previously got the following error:

There was 1 error:

1) Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Php81RectorTest::test with data set #0 (Symplify\SmartFileSystem\SmartFileInfo Object (...))
Rector\Core\Exception\NotImplementedYetException: Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper::mapToPhpParserNode for PHPStan\Type\Accessory\AccessoryNumericStringType

This PR Fixes rectorphp/rector#6896

@samsonasik
Copy link
Member Author

/cc @internalsystemerror

@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba it is ready for review.

Comment on lines +1 to +11
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\FixtureForPhp81;

class DoNotCrashOnNumericString
{
public function run(): string
{
return bcadd('10', '5');
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Could you add test fixture withotu ): string return type and the other half that adds it? So we're sure this type is completed.

<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\FixtureForPhp81;

class DoNotCrashOnNumericString
{
    public function run()
    {
        return bcadd('10', '5');
    }
} 

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\FixtureForPhp81;

class DoNotCrashOnNumericString
{
    public function run(): string
    {
        return bcadd('10', '5');
    }
} 

?>

Copy link
Member Author

Choose a reason for hiding this comment

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

New PHPStan 1.3 just released https://github.com/phpstan/phpstan/releases/tag/1.3.0 and as we use ^1.2 in composer.json, it will automatically updated and #1589 and make error as new PR #1589

I will pin to PHPStan 1.2 and add new fixture for it.

Copy link
Member

Choose a reason for hiding this comment

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

I will pin to PHPStan 1.2 and add new fixture for it.

Thanks 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

@TomasVotruba I applied:

  • temporary pin phpstan to 1.2 as 1.3 cause error like in PR Update to PHPStan ^1.3 #1589 as currently we use ^1.2

  • add new fixture for no return declaration, it seems currently cause different result diff:

-    public function run(): string
+    public function run()

Copy link
Member Author

Choose a reason for hiding this comment

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

oh, it seems because bcadd() function not exists, so no return type is expected.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed with add the missing function and add string handling 50a27aa so it can add return type declaration.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you 👍

@samsonasik
Copy link
Member Author

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

*/
public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): TypeNode
{
return new IdentifierTypeNode('string');
Copy link
Contributor

Choose a reason for hiding this comment

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

Phpstan also supports numeric-string.. not sure this would be correct to use here though

See https://phpstan.org/developing-extensions/type-system

Copy link
Member

Choose a reason for hiding this comment

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

The PHP doc is also for PHPStorm, that understands only string type.

@TomasVotruba
Copy link
Member

Rebase on main is needed now

@TomasVotruba TomasVotruba enabled auto-merge (squash) December 29, 2021 23:42
@TomasVotruba TomasVotruba merged commit 322e5b6 into main Dec 29, 2021
@TomasVotruba TomasVotruba deleted the do-not-crash-on-numeric-string branch December 29, 2021 23:47
@TomasVotruba
Copy link
Member

Thank you 👍

@internalsystemerror
Copy link
Contributor

@samsonasik @TomasVotruba thank you both!

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.

Exception thrown when using ReturnTypeDeclarationRector::class and bcmath extension
4 participants