Search code examples
phprefactoringphpcs

phpcs: detecting old style constructors


I have to work with a very large codebase that has been in continuous development for the last 11 years, with varying maintenance levels.

Older code is slightly messy, as one would expect. And there are tons of PHP4 constructors lying around:

class Foo {
    private $thing;
    public function Foo($thing) {
       $this->thing = $thing;
    }
 }

Plus, more than a few files with more than one class declared on each. And of course, files with only procedural code in them (many with definitions, and some with actual work happening in them).

I'd like to use phpcs to run a report on only these things:

  • A class is declared and there is an old-style constructor
    • Bonus points if we can skip those where a __construct() is also declared.
  • More than one class is declared

And ignore everything else.

Are there some standard sniffs I could use to do this? Or a way to declare a custom ruleset to check for this?

One additional bonus point: detecting those files where a class is defined, and there is code sitting outside of the class.


Solution

  • Finally found here that I could look for old-style constructors using this:

    phpcs --standard=Squiz --extensions=php --sniffs=Generic.NamingConventions.ConstructorName .
    

    Also, I found a standard generator for PhpCS here.