Skip to content

[NodeTypeResolver] Use Scope->getType() on ArrayDimFetch on NodeTypeResolver::getNativeType()#5037

Merged
TomasVotruba merged 5 commits intomainfrom
use-scope-gettpye
Sep 18, 2023
Merged

[NodeTypeResolver] Use Scope->getType() on ArrayDimFetch on NodeTypeResolver::getNativeType()#5037
TomasVotruba merged 5 commits intomainfrom
use-scope-gettpye

Conversation

@samsonasik
Copy link
Copy Markdown
Member

@staabm this is what I mean for using Scope->getType() on ArrayDimFetch, as using getNativeType() will got mixed on this code:

        $parts = parse_url($url);

        if (!empty($parts['host'])) {
            return $parts['host'];
        }

        return null;

checking with getNativeType() always got MixedType on $parts['host'] which always string by getType(), see fixture:

namespace Rector\Core\Tests\Issues\EmptyBooleanCompare\Fixture;
final class SomeFixture
{
public function checkUrl(string $url)
{
$parts = parse_url($url);
if (!empty($parts['host'])) {
return $parts['host'];
}
return null;
}
}
?>
-----
<?php
namespace Rector\Core\Tests\Issues\EmptyBooleanCompare\Fixture;
final class SomeFixture
{
public function checkUrl(string $url)
{
$parts = parse_url($url);
if (isset($parts['host']) && $parts['host'] !== '') {
return $parts['host'];
}
return null;
}
}

@staabm
Copy link
Copy Markdown
Contributor

staabm commented Sep 17, 2023

Please add tests which show why this is required

@samsonasik
Copy link
Copy Markdown
Member Author

The test case already exists

namespace Rector\Core\Tests\Issues\EmptyBooleanCompare\Fixture;
final class SomeFixture
{
public function checkUrl(string $url)
{
$parts = parse_url($url);
if (!empty($parts['host'])) {
return $parts['host'];
}
return null;
}
}
?>
-----
<?php
namespace Rector\Core\Tests\Issues\EmptyBooleanCompare\Fixture;
final class SomeFixture
{
public function checkUrl(string $url)
{
$parts = parse_url($url);
if (isset($parts['host']) && $parts['host'] !== '') {
return $parts['host'];
}
return null;
}
}

@samsonasik
Copy link
Copy Markdown
Member Author

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

Comment on lines +7 to +10
/**
* @param array{host: string} $parts
*/
public function checkUrl(array $parts)
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 the specific type of $parts['host'] from docblock will be detected as host: StringType instead of MixedType

that can be feature or bug :)

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.

if the shape was returned by a native extension, I would say its a feature.

if the shape is declared as a phpdoc, I would say its a bug :)

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.

how to detect if it is from native extension ?

Copy link
Copy Markdown
Contributor

@staabm staabm Sep 18, 2023

Choose a reason for hiding this comment

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

I think we can't detect it. but maybe we can enhance our own getNativeType() method to apply the phpstan-src native extensions (if thats not the case already).

@samsonasik
Copy link
Copy Markdown
Member Author

For summary,

  1. the ArrayDimFetch on parse_url() is valid use case of getType(), see https://getrector.com/demo/860056e8-3533-4868-9ef0-16b99b2944e0 , with getNativeType() will goes MixedType

  2. on docblock, probably yes or not, see https://getrector.com/demo/0d518afe-01ac-4b9d-8c29-b703664f1987 , but when user define {host: string}, type type should follow?

  3. on direct value assigned is also valid use case (equal type both ->getType() vs getNativeType() ) see https://getrector.com/demo/3fc527af-1aa0-4e80-8f65-102b6925ef86

@staabm
Copy link
Copy Markdown
Contributor

staabm commented Sep 17, 2023

the ArrayDimFetch on parse_url() is valid use case of getType(), see https://getrector.com/demo/860056e8-3533-4868-9ef0-16b99b2944e0 , with getNativeType() will goes MixedType

Are you sure this problem is related to ArrayDimFetch alone and not return type extensions or array-shapes or similar?

@samsonasik
Copy link
Copy Markdown
Member Author

There is no docblock definition on parse_url(), so it seems PHPStan detect by its NativeFunctionReflection->getType() but I probably missing something :)

@staabm
Copy link
Copy Markdown
Contributor

staabm commented Sep 17, 2023

on docblock, probably yes or not, see https://getrector.com/demo/0d518afe-01ac-4b9d-8c29-b703664f1987 , but when user define {host: string}, type type should follow?

We may follow only phpdocs which can be validated by the lie detector

https://phpstan.org/blog/phpstan-1-10-comes-with-lie-detector

@samsonasik
Copy link
Copy Markdown
Member Author

samsonasik commented Sep 17, 2023

ArrayDimFetch is special imo, which the data can by passed from different method which the user that define it should understand the risk and know what the data they want/will to pass.

Most of use cases, when user define @param string[] $param, it will always collection of string

/** @param string[] $param */
function foo(array $param)
{
     var_dump($param[0]); // string... 
}

instead of collection of mixed

@TomasVotruba TomasVotruba merged commit a3d2fcd into main Sep 18, 2023
@TomasVotruba TomasVotruba deleted the use-scope-gettpye branch September 18, 2023 22:04
@TomasVotruba
Copy link
Copy Markdown
Member

Thank you 🙏

@staabm
Copy link
Copy Markdown
Contributor

staabm commented Sep 21, 2023

after applying rector-dev on our codebase we run about all the issues I mentioned above. I think we need to revert it, because atm this PR does apply phpdoc-based array-shape type (if it would/could only apply array-shape types coming from phpstan native extensions it would be fine)

see

@samsonasik
Copy link
Copy Markdown
Member Author

If we can check what variable came from, eg from assign to native function, I think we can get win win solution

@staabm
Copy link
Copy Markdown
Contributor

staabm commented Sep 21, 2023

yeah, my main motivation is making sure this doesn't get into a release in the form it is right now.

if we can improve/iterate on it, even better.

@samsonasik
Copy link
Copy Markdown
Member Author

see #5056

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.

3 participants