Skip to content

Instantly share code, notes, and snippets.

@mathiasverraes
Created July 12, 2012 07:42
Show Gist options
  • Save mathiasverraes/3096500 to your computer and use it in GitHub Desktop.
Save mathiasverraes/3096500 to your computer and use it in GitHub Desktop.
Recursive PHP Lint script
#!/bin/bash
for file in `find .`
do
EXTENSION="${file##*.}"
if [ "$EXTENSION" == "php" ] || [ "$EXTENSION" == "phtml" ]
then
RESULTS=`php -l $file`
if [ "$RESULTS" != "No syntax errors detected in $file" ]
then
echo $RESULTS
fi
fi
done
@scones
Copy link

scones commented Feb 17, 2017

Maybe this:
find . -type f -name '*.php' -exec php -l {} ; |grep -v "No syntax errors detected"
;)

fixed this to:
find . -type f -name '*.php' -exec php -l {} \; |grep -v "No syntax errors detected"

@fbrinker
Copy link

You may want to invert the return code of grep so a CI build can fail, if grep found some errors:
find . -type f -name '*.php' -exec php -l {} \; | (! grep -v "No syntax errors detected" )

@jakob-stoeck
Copy link

jakob-stoeck commented Apr 25, 2018

you can run it in parallel (using xargs -P) and disable loading of the php config (with -n) for a significant speed bump like this:

find . -type f -name '*.php' -print0 | xargs -0 -n1 -P4 php -l -n | (! grep -v "No syntax errors detected" )

@JacobDB
Copy link

JacobDB commented Sep 27, 2018

@fbrinker and @jakob-stoeck, thanks for that, that's exactly what I was looking for!

@JoSSte
Copy link

JoSSte commented Sep 5, 2019

with vendor folder excluded for ci/cd
find . -path ./app/vendor -prune -o -type f -name '*.php' -print0 | xargs -0 -n1 -P4 php -l -n | (! grep -v "No syntax errors detected" )

@satishgadhave
Copy link

Thanks @JoSSte

@judgej
Copy link

judgej commented Jun 16, 2020

Maybe this:
find . -type f -name '*.php' -exec php -l {} ; |grep -v "No syntax errors detected"
;)

Just redirect stdout to /dev/null then all you will see are the errors. There is then no need to grep stdout.

find . -type f -name '*.php' -exec php -l {} \; >/dev/null

@Maikuolan
Copy link

Something like this would be pretty useful as a GitHub Action, I reckon.

@vertexvaar
Copy link

vertexvaar commented Feb 5, 2021

I combined and added some parts:

  1. @judgej redirect to /dev/null
  2. Add -false after -prune to omit the vendor folder from output
  3. Add $(nproc) to get the actual amount of usable cores

find . -path ./vendor -prune -false -o -type f -name '*.php' -print0 | xargs -0 -n1 -P$(nproc) php -l -n > /dev/null

Edit: Portable version with comment from @CodeBrauer:

find . -path ./vendor -prune -false -o -type f -name '*.php' -print0 | xargs -0 -n1 -P$(nproc 2> /dev/null || sysctl -n hw.ncpu) php -l -n > /dev/null


I ❤️ this discussion! You people are awesome!

@CodeBrauer
Copy link

@vertex

  1. Add $(nproc) to get the actual amount of usable cores

For those using macOS - nproc is not available, but you can use sysctl -n hw.ncpu

@Jimbolino
Copy link

still havent found a good (portable) version for this
redirecting the output of php -l to /dev/null also hides the parse/syntax error messages

#!/bin/sh
set -ex

find . -type f -name '*.php' ! -path './vendor/*' -exec php -l -n {} \; | (! grep -v "No syntax errors detected" )
echo 'syntax OK'

This works fine on ubuntu, but on macOS it stops the script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment