Question

I have recently been working on a coding standard and would like to use codesniffer to check my files according to this standard. Since i am new to Codesniffer i figured i'd go test the standards first before creating my own standard.

I have however run into a problem with codesniffer and i am unsure wether this is a bug or something i am doing wrong.

I've had pear install code sniffer using the commandline command pear install CodeSniffer after that i tried using different standards on my test file:

<?php 
public class Application_IndexController() {

    DEFINE WEBSITE_URL = 'https://localhost/';

    public $avast = '';
    private $_ye = '';
    protected $_swabs = '';

    public setAvast($avast){
        $this->avast = $avast;
    }

    private getYe(){
        return $this->_ye;
    }

    protected changeProtected($_swabs){
        if($_swabs && $this->swabs) {
            $this->_swabs = $swabs;
        } elseif($swabs) {
            $this->_swabs = $swabs;
        } else {
            return null;
        }
    }

    public iDontKnowWhatImDoing($harhar) {
        Switch($harhar) {
            case 1:
                $yargh = "Avast ye "
                       . "swabs!";
                echo $yargh;
                break;
            default:
                $array = array(
                               "Sailor 1", "Sailor 2", "Sailor 3", 
                               "Sailor 4", "Sailor 5", "Sailor 5"
                               );
                break;
        }
    }
}
?>

Suffice to say i was in a rather silly mood while writting this file, but regardless it fullfills it's purpose.

I've run all of the standards on this file and only a few of them actually work, the other seem to return the same error all the time.

The 'generic', 'psr1' and 'psr2' standards work perfectly fine yet the 'phpcs', 'pear', 'mysource', 'zend' and 'squiz' standards all result in a single error:

$stackPtr is not a class member var

It seems rather odd that 5 out of 8 just don't work including phpcs's own standard.

Was it helpful?

Solution

Firstly, the reason you are getting that error message is because your file has parse errors. The class definition is not correct and the DEFINE WEBSITE_URL = 'https://localhost/'; is also not valid PHP code. All your function declarations are missing function after the visibility keyword as well.

The best way to check for parse errors it to run php -l /path/to/code (that's a lowercase L).

PHPCS doesn't actually parse your file like PHP does. Instead, it is a static code analyser, but it does get into situations where the code looks wrong and it is unable to continue. This is just one of those cases.

Secondly, the reason why some of the standards work and some don't is because all the standards have different checks they are performing. Many of the standards share these checks. In this particular case, some of the standards have checks for variable names and they failed because of the incorrect function declarations. They can't really figure out why they failed because they are too far into the file (121 tokens) but they know something went wrong. I know the error message isn't very helpful, but it's enough to let you know something bad happened and there is a problem with the file. In this case, the parse errors.

So in summary: if you fix the parse errors, all the PHP_CodeSniffer standards will run on your code.

Going forward, it's always useful to check for parse errors in your code using the php -l command or by including that into a build script (if you use continuous integration) before the PHPCS checks.

In case it helps, here is your code with the parse errors fixed:

<?php
define('WEBSITE_URL', 'https://localhost/');
class Application_IndexController {

    public $avast = '';
    private $_ye = '';
    protected $_swabs = '';

    public function setAvast($avast){
        $this->avast = $avast;
    }

    private function getYe(){
        return $this->_ye;
    }

    protected function changeProtected($_swabs){
        if($_swabs && $this->swabs) {
            $this->_swabs = $swabs;
        } elseif($swabs) {
            $this->_swabs = $swabs;
        } else {
            return null;
        }
    }

    public function iDontKnowWhatImDoing($harhar) {
        Switch($harhar) {
            case 1:
                $yargh = "Avast ye "
                       . "swabs!";
                echo $yargh;
                break;
            default:
                $array = array(
                               "Sailor 1", "Sailor 2", "Sailor 3", 
                               "Sailor 4", "Sailor 5", "Sailor 5"
                               );
                break;
        }
    }
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top