Php_codesniffer: Lowercase scalar type hints

Created on 2 Oct 2017  路  5Comments  路  Source: squizlabs/PHP_CodeSniffer

When using PHP 7 it is possible to hint scalar type parameters but it is not required that they are lowercase. The following examples are valid PHP but I would like to enforce that the string type hint is always lowercase:

function (string $name) {
  echo $name
}
function (String $name) {
  echo $name
}

I usually just stick with PSR standards and I'm having a hard time trying to find all the available rules.
Is there an existing rule that comes with phpcs that can sniff this? This seems like a common thing to check so I'm not sure why it's hard to find a sniff that fits.

All 5 comments

No, there is no existing check for this.

Sounds like a useful sniff - both for parameter as well as return type declarations. I'd be happy to write them and pull them to Generic.

@bramcordie Have a look at #1685 and see if that works for you.

Hey @jrfnl, I just tried your feature branch with the following rules and does exactly what I expected, thanks!

<?xml version="1.0"?>
<ruleset name="Vrijwilligers">
    <file>./src</file>
    <file>./tests</file>

    <exclude-pattern>.src/VrijetijdsparticipatieBundle/DoctrineMigrations/*</exclude-pattern>

    <rule ref="PSR2" />

    <rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
        <exclude-pattern>./tests/*Test.php</exclude-pattern>
    </rule>

    <rule ref="Generic.PHP.LowerCaseTypeDeclaration"/>
</ruleset>

@bramcordie Looks like @gsherwood added another sniff (not the one in the PR) to handle this: Generic.PHP.LowerCaseType (https://github.com/squizlabs/PHP_CodeSniffer/commit/5a989395a4318475590c6515512f17e2b8e4df8f). The new sniff will be included in the 3.3.0 release.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jigius picture jigius  路  3Comments

LPodolski picture LPodolski  路  3Comments

stephanvierkant picture stephanvierkant  路  3Comments

justintadlock picture justintadlock  路  3Comments

gmponos picture gmponos  路  3Comments