How to check your php project (many files in a folder) for parse errors

Hey hadn’t wrote anything for a while here, but this is so brilliand and convenient, so I couldn’t resist to share it.

It’s about to check your project for obvious errors before releasing the changes on the production server. I found this answer in StackOverflow, but the solution is for SVN repository and while currently I am working with snv it suits me, but looking forward to migrate to Git, I was thinking “Wasn’t it nice to check the all php files within a folder?”

So here is the solution:

for i in $(find . -iname \*.php ) ; do php -l $i ; done | grep "Parse error"

This should be executed in terminal (at least in Ubuntu it is straight forward) and it print all files which had Parse errors within the folder. I think it’s a must for any php developer who release their code in production server. I already put it as alias in my .bashrc, but I will add it in my “build” script too.

Probably it could be optimized, but for me is pretty much enough.

6 thoughts on “How to check your php project (many files in a folder) for parse errors

  1. Nik Chankov Post author

    Unfortunately the project which I am working on is not CakePHP, but thanks for these 2, I didn’t know about them and definitelly I will try them!

  2. Walther

    Doesn’t matter if it is CakePHP or not. PHP CodeSniffer is just a general tool, and the CakePHP guidelines are just a style rule for PHP CodeSniffer which can be used on any project. CodeSniffer also includes PSR-1 and PSR-2 as style rules.

    CodeSniffer gives you some really nice reports on your project, even if you just want to use it for checking for parse errors.

  3. Nik Chankov Post author

    Agree, I’ve tested it already, it’s quite extensive, good and automatically is integrated with Sublime Editor, but I need to fine tune it, since it add a lot of feedback for spacing and identation, since I don’t need them.

    Thanks for the hint.

Leave a Reply

Your email address will not be published. Required fields are marked *