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

Type hinting: bool or boolean #1864

Open
VijitCoder opened this issue Jan 25, 2018 · 7 comments · May be fixed by #3139
Open

Type hinting: bool or boolean #1864

VijitCoder opened this issue Jan 25, 2018 · 7 comments · May be fixed by #3139

Comments

@VijitCoder
Copy link

The sniffer makes a pretty strange requirement. He is always want to see the boolean as a type hint. But, at the same time PHP 7.1 supports only bool as a type hinting. So, to make everyone happy I need to write the hinting in two versions:

   /**
     * Some comment here
     *
     * @return boolean
     */
    public function isUserActive(): bool
    {
        return false;
    }

This is weird from the coding viewpoint.

Here is a discussion about the right hinting https://stackoverflow.com/a/44009101/5497749

@gsherwood
Copy link
Member

The error message comes from a particular coding standard that enforces that convention. Those standards have not been updated in a while (the PEAR one probably never will be) so there isn't a lot you can do without either writing your own sniff to enforce the rules you want, or finding a sniff someone else has written.

I do intend on changing the Squiz standard, so I'll leave this open as a reminder.

@natanfelles
Copy link

Same for integer, see: codeigniter4/coding-standard#22 . Thank you! 😃

@louisl
Copy link

louisl commented Feb 21, 2018

Just throwing an idea out there:

Could this be as simple as changing the switch case in Common->suggestType() and adding an optional strict param for backwards compatibility. We could then add a $strictType to commenting sniffs that use it that can be set in the ruleset config?

Something like this.

https://gist.github.com/louisl/a1e0fe4fc1ab33b1369f37f8052585e0

@TomasVotruba
Copy link
Contributor

Coding standard can break typehints in many ways, since they don't know the context of parent/child files:

 <?php

 final class SuperCoolSniff implements Sniff
 {
     /**
      * @param int $position
      */
-    public function process(File $file, $position)
+    public function process(File $file, int $position)
     {
         // ...
     }
 }

It's better to use AST for this in tools like Retor instead of token_get_all(): https://www.tomasvotruba.cz/blog/2018/12/10/rocket-science-behind-migration-of-docblock-types-to-php-typehints/

@jrfnl
Copy link
Contributor

jrfnl commented Feb 5, 2019

The adding of an optional parameter to the suggestType() method to allow it to return "short form", rather than "long form" will be sorted in #2189.

@ElvenSpellmaker
Copy link

This still needs fixing for PHP > 7 BTW, much like how it's (almost) fixed for Params. @gsherwood

@filips123
Copy link
Contributor

I created PR #3139 to allow using short forms of type keywords using useShortTypes property on relevant sniffs.

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