Skip to content

Instantly share code, notes, and snippets.

@mreschke

mreschke/psr2-converter.sh

Last active Dec 14, 2016
Embed
What would you like to do?
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