Skip to content

Instantly share code, notes, and snippets.

@mreschke
Last active December 14, 2016 08:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mreschke/a0171246f31cc1d7fc09946a58bcfba9 to your computer and use it in GitHub Desktop.
Save mreschke/a0171246f31cc1d7fc09946a58bcfba9 to your computer and use it in GitHub Desktop.
Convert to PSR-2 and Tabs to Spaces
#!/bin/bash
# Convert tabs to spaces in code files
# and convert all PHP files (that begin with <? or <?php, so not laravel blade files) to PSR-2
# Requires https://github.com/FriendsOfPHP/PHP-CS-Fixer in $path
# mReschke 2016-09-28
dir=$(pwd)
# Replace tabs with 4 spaces in particular files
# First find section ignores folders
# Second find section includes files
# Third find section ignores files
echo "Converting spaces to tabs in most code files"
find . \
\( \
-path ./node_modules -prune \
-o -path ./.git -prune \
-o -path ./.svn -prune \
-o -path ./vendor -prune \
-o -path ./storage -prune \
-o -type f \) \
\( \
-iname "*.php" \
-o -iname "*.js" \
-o -iname ".css" \
-o -iname ".txt" \
-o -iname "*.md" \
-o -iname "*.json" \
-o -iname "*.xml" \
-o -iname "*.yml" \
\) \
\( ! -iname "*.min.js" \) \
| xargs sed -i -e 's/\t/ /g'
# Convert to PSR-2
echo "Converting PHP files to PSR-2"
find . \( \
-path ./vendor -prune \
-o -path ./.git -prune \
-o -path ./.svn -prune \
-o -path ./storage -prune \
-o -type f \) \
-iname "*.php" \
-exec php-cs-fixer fix {} --level=psr2 \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment