Skip to content

Instantly share code, notes, and snippets.

@juangacovas
Last active December 21, 2023 15:04
Show Gist options
  • Save juangacovas/ca2ad8c341ae912765eaa2e2953dc300 to your computer and use it in GitHub Desktop.
Save juangacovas/ca2ad8c341ae912765eaa2e2953dc300 to your computer and use it in GitHub Desktop.
Windows cmd script for PHP lint recursive (batch script to lookup syntax errors and deprecations)
@echo off
SETLOCAL enabledelayedexpansion
echo.
echo  Recursive PHP lint parser 
echo Will do "php -l" for all php files under given directory tree, telling deprecations and/or syntax errors.
echo Assumes "php.exe" is on PATH
echo.
if "%1"=="" goto :usage
if NOT exist "%1" (
echo.
echo ERROR: Directroy not found: %1
goto :EOF
)
goto gogogo
:usage
echo USAGE: lint path
echo EXAMPLE: lint ..\php\phastard\app
goto :EOF
:gogogo
echo Checking PHP files (php -l) at dir: %1
SET n_some=0
for /R "%1" %%f in (*.php) do (
rem echo %%f
call :lookup %%f
IF %errorlevel% NEQ 0 (
echo Aborting
goto :EOF
)
)
goto :DONE
:lookup
DEL /Q lintResult.txt
php -l %~1 >lintResult.txt 2>NUL
type lintResult.txt | findstr /C:"Deprecated" >NUL 2>NUL
if %errorlevel% EQU 0 (
echo DEPRECATIONS: %~1
SET n_some=0
)
type lintResult.txt | findstr /C:"No syntax errors detected" >NUL 2>NUL
if %errorlevel% NEQ 0 (
echo  SYNTAX ERRORS  %~1
SET n_some=1
EXIT /B 1
) ELSE (
EXIT /B 0
)
:DONE
echo Done.
if "%n_some%"=="1" echo You can do "php -l \path\to\file.php" to see verbose errors/deprecations".
@juangacovas
Copy link
Author

This script is useful for migrations to newer PHP versions: it checks all php files under the given path, including subdirectories, using "php -l"
It will display if there are deprecations and/or syntax errors, and will abort (stop checking) as soon some file has syntax errors.

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