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

Add dirname return type provider #8611

Merged
merged 2 commits into from
Oct 23, 2022
Merged

Add dirname return type provider #8611

merged 2 commits into from
Oct 23, 2022

Conversation

mcaskill
Copy link
Contributor

@mcaskill mcaskill commented Oct 22, 2022

@orklah
Copy link
Collaborator

orklah commented Oct 23, 2022

Seems great!

Two issues left to resolve, the Typeprovider should be marked @internal and a test broke due to your PR resolving the type more precisely, you can just replace the literal-string by the literal value

Changed:
- Replaced extraction of `$dir_level` literal value to be from the NodeTypeProvider instead of from the statement; based on similar logic in `RoundReturnTypeProvider`.
- Replaced `new` return type creation with `Type` method creation.
- Replaced `null` return with string type.

Fixed:
- Typo on variable name for extracted `StatementsSource`.
- Edge case where `$dir_level` is less than 1.
- Broken unit tests.
@mcaskill
Copy link
Contributor Author

Two issues left to resolve, the Typeprovider should be marked @internal and a test broke due to your PR resolving the type more precisely, you can just replace the literal-string by the literal value

Thanks for the feedback. Both issues have now been resolved.

@orklah orklah added the release:feature The PR will be included in 'Features' section of the release notes label Oct 23, 2022
@orklah orklah merged commit aea40ef into vimeo:master Oct 23, 2022
@orklah
Copy link
Collaborator

orklah commented Oct 23, 2022

Thanks!

@mcaskill mcaskill deleted the feature/5.x/dirname-return-type-provider branch October 23, 2022 18:53
@kkmuffme
Copy link
Contributor

kkmuffme commented Oct 24, 2022

@mcaskill could you do the same PR for basename too? (perhaps for str_replace too, since dir/basename and str_replace are the most common which would be extremely usable)

@mcaskill
Copy link
Contributor Author

@kkmuffme I should be able to add basename easily. I might be able to also add realpath.

As for str_replace, that will be more complicated from what I've been able to learn from Psalm's codebase so far. There's currently a return type provider that handles str_replace, str_ireplace, substr_replace, preg_replace, and preg_replace_callback, which is a lot of different parameters to figure out how to resolve.

Just this morning, I realized the following use case does not work for dirname, which I'll need to figure out. I erroneously forgot and premused IncludeAnalyzer::getPathTo() would have been able to resolve the variable:

$path = "a/b/c";
$dir = dirname($path); // ← return type is evaluated as 'string' instead of literal string

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/1708b073c2
<?php

/** @psalm-trace $foo */
$foo = __DIR__;

/** @psalm-trace $baz */
$baz = dirname( $foo );

echo $baz;
Psalm output (using commit aea40ef):

INFO: Trace - 4:1 - $foo: '/var/www/vhosts/psalm.dev/httpdocs/src'

INFO: Trace - 7:1 - $baz: string

kkmuffme added a commit to kkmuffme/psalm that referenced this pull request Oct 26, 2022
Like dirname return type provider of vimeo#8611
kkmuffme added a commit to kkmuffme/psalm that referenced this pull request Oct 26, 2022
Like dirname return type provider of vimeo#8611
@kkmuffme
Copy link
Contributor

@mcaskill I created a PR for the basename return type provider #8620 and one for the simplest case of str_(i)replace #8619 which covers what is most commonly used with dirname/basename paths, so that's done.

Feel free to create one for realpath though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:feature The PR will be included in 'Features' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants