Skip to content

Instantly share code, notes, and snippets.

@danielebarbaro
Last active January 21, 2020 11:04
Show Gist options
  • Save danielebarbaro/d6e5c5e2db7a7b672ff536cba98edbb2 to your computer and use it in GitHub Desktop.
Save danielebarbaro/d6e5c5e2db7a7b672ff536cba98edbb2 to your computer and use it in GitHub Desktop.
Laravel Standards Checker
<?php
$finder = Symfony\Component\Finder\Finder::create()
->notPath('bootstrap/cache')
->notPath('storage')
->notPath('vendor')
->in(__DIR__)
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
$fixers = [
'-psr0',
'-php_closing_tag',
'blankline_after_open_tag',
'concat_without_spaces',
'double_arrow_multiline_whitespaces',
'duplicate_semicolon',
'empty_return',
'extra_empty_lines',
'include',
'join_function',
'list_commas',
'multiline_array_trailing_comma',
'namespace_no_leading_whitespace',
'newline_after_open_tag',
'no_blank_lines_after_class_opening',
'no_empty_lines_after_phpdocs',
'object_operator',
'operators_spaces',
'phpdoc_indent',
'phpdoc_no_access',
'phpdoc_no_package',
'phpdoc_scalar',
'phpdoc_short_description',
'phpdoc_to_comment',
'phpdoc_trim',
'phpdoc_type_to_var',
'phpdoc_var_without_name',
'remove_leading_slash_use',
'remove_lines_between_uses',
'return',
'self_accessor',
'single_array_no_trailing_comma',
'single_blank_line_before_namespace',
'single_quote',
'spaces_before_semicolon',
'spaces_cast',
'standardize_not_equal',
'ternary_spaces',
'trim_array_spaces',
'unalign_equals',
'unary_operators_spaces',
'whitespacy_lines',
'multiline_spaces_before_semicolon',
'short_array_syntax',
'short_echo_tag',
];
return Symfony\CS\Config\Config::create()
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
->fixers($fixers)
->finder($finder)
->setUsingCache(true);
<?xml version="1.0"?>
<ruleset name="Laravel Standards">
<!--
The name attribute of the ruleset tag is displayed
when running PHP_CodeSniffer with the -v command line
argument. The description tag below is not displayed anywhere
except in this file, so it can contain information for
developers who may change this file in the future.
-->
<description>The Laravel Coding Standards</description>
<!--
If no files or directories are specified on the command line
your custom standard can specify what files should be checked
instead.
Note that specifying any file or directory path
on the command line will ignore all file tags.
-->
<file>app</file>
<file>config</file>
<file>public</file>
<file>resources</file>
<file>routes</file>
<file>tests</file>
<!--
You can hard-code ignore patterns directly into your
custom standard so you don't have to specify the
patterns on the command line.
-->
<exclude-pattern>*/cache/*</exclude-pattern>
<exclude-pattern>*/*.js</exclude-pattern>
<exclude-pattern>*/*.css</exclude-pattern>
<exclude-pattern>*/*.xml</exclude-pattern>
<exclude-pattern>*/*.blade.php</exclude-pattern>
<exclude-pattern>*/autoload.php</exclude-pattern>
<exclude-pattern>*/storage/*</exclude-pattern>
<exclude-pattern>*/docs/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>_ide_helper.php</exclude-pattern>
<exclude-pattern>.phpstorm.meta.php</exclude-pattern>
<!--
You can hard-code command line values into your custom standard.
Note that this does not work for the command line values:
-v[v][v], -l, -d, -sniffs and -standard
The following tags are equivalent to the command line arguments:
-p
-->
<arg name="report" value="summary"/>
<arg name="colors"/>
<arg value="p"/>
<!--
You can hard-code custom php.ini settings into your custom standard.
The following tag sets the memory limit to 64M.
-->
<ini name="memory_limit" value="256M"/>
<!--
Include all sniffs in the PEAR standard. Note that the
path to the standard does not have to be specified as the
PEAR standard exists inside the PHP_CodeSniffer install
directory.
-->
<rule ref="PSR2"/>
</ruleset>
#!/bin/sh
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".php\{0,1\}$")
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
#PASS=true
echo "\nValidating PHPCS:\n"
# Check for phpcs
which ./vendor/bin/phpcs &> /dev/null
if [[ "$?" == 1 ]]; then
echo "\t\033[41mPlease install PHPCS\033[0m"
exit 1
fi
RULESET=phpcs.xml
for FILE in $STAGED_FILES
do
./vendor/bin/phpcs --standard="$RULESET" "$FILE"
if [[ "$?" == 0 ]]; then
echo "\t\033[32mPHPCS Passed: $FILE\033[0m"
else
echo "\t\033[41mPHPCS Failed: $FILE\033[0m"
exec ./vendor/bin/phpcbf --standard="$RULESET" "$FILE"
PASS=false
fi
done
echo "\nPHPCS validation completed!\n"
if ! $PASS; then
echo "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass PHPCS but do not. Please fix the PHPCS errors and try again.\n"
echo "\n\033[41mFix and apply PSR2 Standards"
exit 1
else
echo "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment