Skip to content

Instantly share code, notes, and snippets.

@Daviid-P
Created April 23, 2024 08:14
Show Gist options
  • Save Daviid-P/5752ac8a774bffe42fed86b6230f1987 to your computer and use it in GitHub Desktop.
Save Daviid-P/5752ac8a774bffe42fed86b6230f1987 to your computer and use it in GitHub Desktop.
bat file to run with pucelle's RunOnSave VSCode extension
@echo off
REM Set a temporary file to store the output
set outputFile=%TEMP%\phpcbf_output.txt
REM Run phpcbf command and redirect output to the temporary file
C:\xampp7\php\php.exe "C:\Program Files\PHP_Tools\phpcbf" %* > "%outputFile%"
REM Store the exit code of phpcbf
set exitCode=%ERRORLEVEL%
REM Print the exit code for debugging
echo Exit Code: %exitCode%
REM Check if the output contains the specific phrase
findstr /C:"PHPCBF FAILED TO FIX" "%outputFile%" > nul
REM Check the exit code of phpcbf and the presence of the phrase
IF %exitCode% EQU 0 (
REM phpcbf encountered no fixable errors
exit /b 0
) ELSE IF %exitCode% EQU 1 (
REM phpcbf found error and fixed them or found unfixable errors
IF ERRORLEVEL 1 (
echo "The phrase was not found in the output"
exit /b 0
) ELSE (
echo "The phrase was found in the output"
exit /b 1
)
) ELSE (
REM error code 2, fatal error, worng syntax?
type "%outputFile%"
exit /b 1
)
@Daviid-P
Copy link
Author

This mainly gets around the fact that PHPCBF return 0 if no fixable errors were found, 1 if fixables errors were found and fixed and 1 again if errors were found but not fixed (at least until they release v4.X, see squizlabs/PHP_CodeSniffer#3057 and PHPCSStandards/PHP_CodeSniffer#184) which makes the output console of vscode pop up even if errors where fixed, that´s annoying.

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