user3002173
user3002173

Reputation: 135

Php trouble with CodeSniffer

When I start this in my terminal this is what I get...

Function name "provjeri_datum" is prefixed with a package name but does not begin with a capital letter

Here's my function in php:

function provjeri_datum($date, $format = 'Y-m-d H:i:s') 
{
    $d = DateTime::createFromFormat($format, $date);
    return $d && $d->format($format) == $date;
}

How should I fix this? Is there any way that I can fix this without changing provjeri_datum to Provjeri_datum?

Upvotes: 1

Views: 1052

Answers (2)

Greg Sherwood
Greg Sherwood

Reputation: 7222

That check is part of the PEAR coding standard, which is the default that PHP_CodeSniffer uses. The part of the standard it is enforcing is here: http://pear.php.net/manual/en/standards.naming.php (Global Variables and Functions).

As this is part of the PEAR standard, the only way to remove this error message is to create your own custom standard. To do this, you'd create a ruleset XML file (call it whatever you want, such as mypear.xml) and make this the contents:

<?xml version="1.0"?>
<ruleset name="MyStandard">
 <description>My custom coding standard.</description>
 <rule ref="PEAR">
  <exclude name="PEAR.NamingConventions.ValidFunctionName.FunctionNoCapital" />
 </rule>
</ruleset>

This custom standard includes the entire PEAR coding standard but excludes that one particular message. You can find the message code by adding the -s command line argument to PHP_CodeSniffer (e.g., phpcs -s /path/to/code).

Then you run PHP_CodeSniffer like this:

phpcs --standard=/path/to/mypear.xml /path/to/code

You can do a lot more with custom coding standards as well. See the docs for more options: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml

If you'd prefer to use a built-in standard instead of creating your own, switching the PSR1 or PSR2 standard is a good option (PSR2 has more rules). These ship with PHP_CodeSniffer, so you can run them like this:

phpcs --standard=PSR2 /path/to/code

Or you can set this as the default standard:

phpcs --config-set default_standard PSR2
phpcs /path/to/code

Upvotes: 2

RVT
RVT

Reputation: 220

Old question, but this still pops up for people who use phpcs.

Basically, this is phpcs complaining about the use of underscores in the plain function name, rather than snake case. In this example, renaming the function to something like provjeriDatum should fix it.

Upvotes: 0

Related Questions