forked from venkatrs/Cake_PHP_CodeSniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding a git pre-commit hook to run phpcs
- Loading branch information
root
authored and
AD7six
committed
Dec 30, 2010
1 parent
ae7437b
commit cf4f3a8
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
class Cake_Sniffs_WhiteSpace_ForceTabIndentSniff extends Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff | ||
{ | ||
|
||
/** | ||
* Processes this test, when one of its tokens is encountered. | ||
* | ||
* Check for any line starting with 2 spaces - which would indicate space indenting | ||
* Also check for "\t " - a tab followed by a space, which is a common similar mistake | ||
* | ||
* @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. | ||
* @param int $stackPtr The position of the current token in | ||
* the stack passed in $tokens. | ||
* | ||
* @return void | ||
*/ | ||
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
$line = $tokens[$stackPtr]['line']; | ||
if ($stackPtr > 0 && $tokens[($stackPtr - 1)]['line'] === $line) { | ||
return; | ||
} | ||
|
||
if (preg_match('@^ @', $tokens[$stackPtr]['content'])) { | ||
$error = 'Space indented: Tabs for indents, spaces for alignment'; | ||
$phpcsFile->addError($error, $stackPtr); | ||
} elseif (preg_match('@\t [^\*]@', $tokens[$stackPtr]['content'])) { | ||
$error = 'Tab followed by space - bad indentation'; | ||
$phpcsFile->addWarning($error, $stackPtr); | ||
} | ||
|
||
}//end process() | ||
|
||
|
||
}//end class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
class Cake_Sniffs_WhiteSpace_ScopeIndentSniff extends Generic_Sniffs_WhiteSpace_ScopeIndentSniff | ||
{ | ||
|
||
/** | ||
* The number of TABS code should be indented. | ||
* | ||
* @var int | ||
*/ | ||
protected $indent = 1; | ||
|
||
}//end class | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/bin/bash | ||
################################################################################ | ||
# | ||
# Pre commit hook to run phpcs and abort commits on failures or warnings | ||
# | ||
# To use, copy or link to the .git/hooks folder of your repository. | ||
# | ||
# This simple script: | ||
# 1. Determins the width of the current console, so the report is full-width | ||
# 2. Determins which commit to diff against (to know what is being commited) - | ||
# using a fictional commit if it's the first commit. | ||
# 3. Skip files which don't exist or are not php files | ||
# 4. If the commit is multiple files run in summary mode, otherwise show details | ||
# | ||
# If phpcs returns a none-zero value (which means errors or warnings where found) | ||
# The commit will be aborted. A developer can override this pre-commit hook and | ||
# thus still commit despite the errors or warnings raised with the command: | ||
# | ||
# git commit --no-verify <same args as before> | ||
# | ||
# Licensed under The MIT License | ||
# Redistributions of files must retain the above copyright notice. | ||
# | ||
# @copyright Copyright (c) 2010, Andy Dawson | ||
# @link www.ad7six.com | ||
# @link pear.php.net/package/PHP_CodeSniffer | ||
# @package cakephp-codesniffs | ||
# @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
# | ||
################################################################################ | ||
|
||
width=$(tput cols); | ||
|
||
if [ `git rev-parse --verify HEAD` ]; then | ||
against='HEAD' | ||
else | ||
against='4b825dc642cb6eb9a060e54bf8d69288fbee4904' | ||
fi | ||
commitFiles=`git diff-index --cached --name-only $against` | ||
|
||
args="-s --report-width=$width" | ||
|
||
phpFiles=""; | ||
phpFilesCount=0; | ||
for f in $commitFiles; do | ||
if [[ ! -e $f ]]; then | ||
continue; | ||
fi | ||
if [[ $f =~ \.(php|ctp)$ ]]; then | ||
phpFilesCount=$phpFilesCount+1 | ||
phpFiles="$phpFiles $f" | ||
fi | ||
done; | ||
|
||
if [[ $phpFilesCount = 0 ]]; then | ||
exit 0; | ||
fi | ||
|
||
if [[ $phpFilesCount > 2 ]]; then | ||
args="$args --report=summary" | ||
fi | ||
|
||
phpcs $args $phpFiles |