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 new sniff for strict _types declaration #911

Closed
Xerkus opened this issue Mar 5, 2016 · 12 comments
Closed

Add new sniff for strict _types declaration #911

Xerkus opened this issue Mar 5, 2016 · 12 comments

Comments

@Xerkus
Copy link

Xerkus commented Mar 5, 2016

New sniff for php 7 is needed to allow to enforce declare(strict_types=1)

@aik099
Copy link
Contributor

aik099 commented Mar 5, 2016

Do you add declare(strict_types=1) line in each PHP file or just in a bootstrap file of a project?

@Xerkus
Copy link
Author

Xerkus commented Mar 5, 2016

It is file local and must be the first directive. https://secure.php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict

Strict typing applies to function calls made from within the file with strict typing enabled, not to the functions declared within that file. If a file without strict typing enabled makes a call to a function that was defined in a file with strict typing, the caller's preference (weak typing) will be respected, and the value will be coerced.

Feature rfc page: https://wiki.php.net/rfc/scalar_type_hints_v5

@Xerkus
Copy link
Author

Xerkus commented Mar 5, 2016

In mixed content file, strict type declaration must be before anything non-php(fatal error otherwise):

<?php declare(strict_types=1); ?>
<html>
...

So, for the sniff only first directive needs to be checked, except for comments.

@aik099
Copy link
Contributor

aik099 commented Mar 5, 2016

Feel free to send a PR adding that sniff to Generic standard when you're ready.

@gsherwood
Copy link
Member

Feel free to send a PR adding that sniff to Generic standard when you're ready.

Or leave this here as a feature request and I'll get around to it eventually.

@glen-84
Copy link
Contributor

glen-84 commented Mar 14, 2016

  1. Would this include formatting? I prefer to use spaces around the equals sign (similar to default parameter values, etc.).
  2. What about files that don't invoke any functions or methods – would it still make sense to require the declaration?

@sebastianbergmann
Copy link
Contributor

sebastianbergmann commented Aug 18, 2016

I just missed this feature yet again. Hope to get around implementing it next week.

@nicwortel
Copy link

@gsherwood / @sebastianbergmann any progress on this issue?

I think there should be a sniff for the existence of a strict_types=1 declaration at the top of a file, as well as one for the formatting of it (usage of spaces).

The draft of PSR-12 (Extended Coding Style Guide) states that declare statements should not contain spaces, so that is probably a sensible default.

The question is of course if the second sniff (formatting) falls within the scope of this issue. Perhaps it is enough to start with the existence of the declaration, and add a sniff for the formatting later on.

@gsherwood
Copy link
Member

@gsherwood / @sebastianbergmann any progress on this issue?

There has been no progress within this project and no ETA for starting.

@glen-84
Copy link
Contributor

glen-84 commented Apr 14, 2017

It looks like this is available here.

@jrjohnson
Copy link

This is resolved in #2365 which added RequireStrictTypesSniff

@gsherwood
Copy link
Member

This is resolved in #2365 which added RequireStrictTypesSniff

That PR was not merged - but a sniff was written and released as part of PSR12 support. So you are right, this can be closed. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants