squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.67k stars 1.48k forks source link

some coding standards not checked #426

Closed x00xer closed 9 years ago

x00xer commented 9 years ago

Hello, I'm trying to attach phpcs to my project and I'm not successful in that ;(

this is my conf file

<?php
 $phpCodeSnifferConfig = array (
  'default_standard' => 'Zend',
  'report_format' => 'full',
  'show_warnings' => '1',
  'show_progress' => '1',
  'severity' => '1',
)

this is coding style which I'm checking http://framework.zend.com/manual/1.12/en/coding-standard.coding-style.html

this is my testing file

<?php
$greeting = "Hello ${name}, welcome back!";
$singleQuote = "single quote";
$aname = "eeee {$this}";
if('ee')
{}
$sql = "SELECT `id`, `name` from `people` "
     . "WHERE `name`='Fred' OR `name`='Susan'";
}
$sampleArray = array(1,2,3,'Zend','Studio');

$sampleArray = array('firstKey'  => 'firstValue', 'secondKey'
=> 'secondValue');

from the link above (coding standards) there are few sections which is does not work

and this is result when I'm running phpcs

# phpcs  ~/tmp/1.php 

FILE: tmp/1.php
--------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
--------------------------------------------------------------------------------
 14 | ERROR | [ ] Expected "if (...) {\n"; found "if(...)\n{"
 15 | ERROR | [x] Closing brace must be on a line by itself
--------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------

Time: 17ms; Memory: 2.75Mb

I'm just not sure is it ok ? Should I do something manually ?

aik099 commented 9 years ago

Please be more specific. From your description I can determine 2 issues (correct me if I'm wrong):

  1. you're trying to use Zend standard through PHP_CodeSniffer
  2. the code you're checking has more/less errors, then were reported by PHP_CodeSniffer

What you think is our next step?

gsherwood commented 9 years ago

The Zend coding standard inside PHP_CodeSniffer is not the Zend Framework coding standard. It is just a collection of sniffs contributed to me by people at Zend, but it is not the current ZF coding standard, and probably never will be.

To check your code against the Zend Framework coding standard, you'll need to speak to the ZF developers to see if they have written a standard for PHPCS. I'm not aware of a current one, but I've never used ZF so I'm not up to date with their available tools.

x00xer commented 9 years ago

Ok, I see. Then could you please clarify, from all sniffer what is included into PHP_CodeSniffer, which one can be matched with available one ? PSR, PEAR etc .. ? For example PSR-1 and PSR-2 from PHPCS are different standarts, but from developer point psr2 including psr1. What the best practise to work with phpcs ? create own standard file ?

gsherwood commented 9 years ago

The PSR2 standard in PHPCS already includes all of the PSR1 standard as well: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/PSR2/ruleset.xml#L10

PEAR and Squiz are also complete standards, but less commonly used.

If the built-in ones don't meet your requirements, you can create a custom standard using a ruleset.xml file. This allows you to bring various checks together in a custom way.

The file format is documented here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml

If you are not sure what standard to use, I suggest starting with PSR2.

x00xer commented 9 years ago

thanks for explanation

gsherwood commented 9 years ago

thanks for explanation

No problem. If you need help creating a ruleset, you can contact me directly as well: gsherwood at squiz dot net