Ken Guest’s online diary

November 6, 2015

Scan your code for old-style constructors using phpcs.

Filed under: php — kenguest @ 13:53
Tags:

There are less than seven days left until PHP 7 is released, which drops support for old-style constructors – the ones where a method is a constructor if it shares the same name as the class.

You don’t want to spend too much time scrolling through codebases for that though do you? Better things to do, like watch videos of conference talks you’ve missed and such.

Well, you’re in luck. If you use php_codesniffer (and if you don’t, well shame on you), you’ll be able to get a report of old-style constructors fairly quickly.

This will run php code sniffer and identify old-style constructors.

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

It’s a bit long-winded, so wrap it up in a bash alias, say in your .bashrc:

    $ alias findoldphpconstructors='phpcs --standard=Squiz --extensions=php --sniffs=Generic.NamingConventions.ConstructorName .'

Want a CSV report of the same? Use this:

alias findoldphpconstructorsToCSV=’phpcs –standard=Squiz –report=csv  –extensions=php –sniffs=Generic.NamingConventions.ConstructorName . | cut -f1,2 -d,’

Remember your unit tests.
Naturally when you do anything at all like this you really should write and run unit tests to ensure no side-effects have been caused inadvertently.

Advertisements

1 Comment »

  1. […] You don’t want to spend too much time scrolling through codebases for that though do …read more […]

    Pingback by Scan your code for old-style constructors using phpcs. | PHP Information — November 6, 2015 @ 17:20


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.