Skip to content

Instantly share code, notes, and snippets.

@mathiasverraes
Created July 12, 2012 07:42
Show Gist options
  • Select an option

  • Save mathiasverraes/3096500 to your computer and use it in GitHub Desktop.

Select an option

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
@jakob-stoeck

jakob-stoeck commented Apr 25, 2018

Copy link
Copy Markdown

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

JacobDB commented Sep 27, 2018

Copy link
Copy Markdown

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

@JoSSte

JoSSte commented Sep 5, 2019

Copy link
Copy Markdown

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
Copy Markdown

Thanks @JoSSte

@judgej

judgej commented Jun 16, 2020

Copy link
Copy Markdown

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
Copy Markdown

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

@vertexvaar

vertexvaar commented Feb 5, 2021

Copy link
Copy Markdown

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

Edit 2: More modern xargs arguments and portable

find . -path ./vendor -prune -false -o -type f -name '*.php' -print0 | xargs -0 -L1 -P0 php -l -n > /dev/null


I ❤️ this discussion! You people are awesome!

@CodeBrauer

Copy link
Copy Markdown

@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
Copy Markdown

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