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

Allow only bool|float|int|string for sprintf() #200

Closed
wants to merge 2 commits into from
Closed

Allow only bool|float|int|string for sprintf() #200

wants to merge 2 commits into from

Conversation

simPod
Copy link
Contributor

@simPod simPod commented Feb 25, 2020

Allow only bool|float|int|string for sprintf()

@simPod
Copy link
Contributor Author

simPod commented Feb 25, 2020

is this doable? Should I continue with fixing tests?

@Kharhamel
Copy link
Collaborator

Kharhamel commented Mar 5, 2020

Hello, sorry for the late reply, i didn't get notified for some reason.
What are your trying to do? Restrict the type for ...$args? I don't know if it is doable, it depends how phpstan or psalm consider types on destructured arguments.

@simPod
Copy link
Contributor Author

simPod commented Mar 5, 2020

Yes exactly. Because now it allows me to pass whatever and static analysis cannot catch it. I get notified on runtime and that's already late.

Eg. passing an object without __toString() into Safe\sprintf() passes phpstan checks because $args are typed as mixed.

@Kharhamel
Copy link
Collaborator

Kharhamel commented Mar 5, 2020

About your failed pipeline, i think the reason the test fails is that it cannot find your new sprintf().

The sprintf() used by the test is fetch by 'require_once DIR . '/../../generated/strings.php';' which doesn't match anymore since your new sprintf lives in 'lib/special_cases.php', so you need to edit the require_once

@simPod
Copy link
Contributor Author

simPod commented Mar 5, 2020

Thanks. Ok, so it sounds viable and I can proceed with this?

@Kharhamel
Copy link
Collaborator

Kharhamel commented Mar 5, 2020

Honestly, no idea. You should ask the guys from psalm or phpstan to know if this is possible.

From the point of view of safe, this is actually pretty tricky to test because the php language itself wont throw errors if you don't respect docblocks.
You need to test than phpstan create an error, which means you have to run phpstan in the test and check its result. I think it is already done somewhere but I don't remember, maybe @moufmouf does.

@Kharhamel
Copy link
Collaborator

To be fair, if you are sure than phpstan and psalm will work with your new param then you don't really need to test it. You can just edit the require to make the test succesfull again and be done with it

@Kharhamel
Copy link
Collaborator

Another things you can try, which may be easier and cleaner, is to edit CustomPhpStanFunctionMap.php instead.

We actually use the stubs from phpstan to generate the typehints and the docblock of our generated functions. The file itself allow us to overwrite phpstan stubs.

So you simply add a new line in CustomPhpStanFunctionMap for sprintf with your new type bool|float|int|string. It is cleaner because you don't have to put sprintf in the special_case file. The only possible issue is, I don't know if you can write '...args' in the stubs. But I think you should try this way instead

@simPod
Copy link
Contributor Author

simPod commented Mar 5, 2020

Yea I tested it so that's np. I was rather wondering about the way I solved it here, did not find easier way to fix the typehints then to redefine the function.

edit CustomPhpStanFunctionMap.php instead.

this sounds the best so far, thanks! will try

@Kharhamel
Copy link
Collaborator

Hello @simPod Did you manage to make it work?

@simPod
Copy link
Contributor Author

simPod commented Mar 20, 2020

@Kharhamel yes, I'm still into it. Are you asking to check whether this is stale or need to move it forward?

@Kharhamel
Copy link
Collaborator

No just checking if it is stale don't worry

@Kharhamel
Copy link
Collaborator

Hello @simPod, are you still on it?

@simPod
Copy link
Contributor Author

simPod commented Sep 3, 2020

I needed to resolve these PRs but unfortunately there's no activity.

php/phd#24
php/doc-en#55
salathe/phpdoc-base#2
php/doc-en#57

@Kharhamel
Copy link
Collaborator

If you are blocked by the doc not being updated, you can overload the typings in this file: CustomPhpStanFunctionMap.php
See my previous comment:

Another things you can try, which may be easier and cleaner, is to edit CustomPhpStanFunctionMap.php instead.

We actually use the stubs from phpstan to generate the typehints and the docblock of our generated functions. The file itself allow us to overwrite phpstan stubs.

So you simply add a new line in CustomPhpStanFunctionMap for sprintf with your new type bool|float|int|string. It is cleaner because you don't have to put sprintf in the special_case file. The only possible issue is, I don't know if you can write '...args' in the stubs. But I think you should try this way instead

@Kharhamel
Copy link
Collaborator

@simPod I am closing this PR since it is too old. If you still want to implement this feature, you better start over to not have conflicts.

@Kharhamel Kharhamel closed this Jan 18, 2021
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.

2 participants