├── README.md ├── Tests ├── TrueTestClass.php └── falsetestclass.php ├── Yii ├── Sniffs │ ├── ControlStructures │ │ └── SingleLineWithoutBracesSniff.php │ ├── Files │ │ └── Utf8EncodingSniff.php │ └── Objects │ │ └── ObjectParenthesesSniff.php └── ruleset.xml └── composer.json /README.md: -------------------------------------------------------------------------------- 1 | Yii Framework PHP CodeSniffer Coding Standard (official repo) 2 | ============================================== 3 | 4 | You can read about Yii standard here: https://github.com/yiisoft/yii/wiki/Core-framework-code-style 5 | 6 | How to install 7 | -------------- 8 | 9 | 1. Install PEAR: 10 | 11 | http://pear.php.net/manual/en/installation.getting.php 12 | 13 | 2. Install PHP_CodeSniffer: 14 | 15 | pear install PHP_CodeSniffer 16 | 17 | 3. Install Yii Coding Standard: 18 | 19 | git clone git://github.com/Ardem/yii-coding-standard.git Yii 20 | sudo ln -sv /path/to/yii-coding-standard/Yii $(pear config-get php_dir)/PHP/CodeSniffer/Standards 21 | 22 | 4. If you want, you can set Yii as coding standard by default: 23 | 24 | phpcs --config-set default_standard Yii 25 | 26 | How to make a simple test 27 | ------------------------- 28 | 29 | 1. Checking a file (if yii-coding-standard is your standard by default) 30 | 31 | phpcs path/to/file.php 32 | 33 | 2. Checking a file (if yii-coding-standard is NOT your standard by default) 34 | 35 | phpcs --standard=Yii /path/to/file.php 36 | 37 | 3. Checking a directory (if yii-coding-standard is your standard by default) 38 | 39 | phpcs /path/to/directory 40 | 41 | 4. Checking a directory (if yii-coding-standard is NOT your standard by default) 42 | 43 | phpcs --standard=Yii /path/to/directory 44 | 45 | How to use CS in IDE 46 | -------------------- 47 | 48 | 1. NetBeans: 49 | 50 | http://plugins.netbeans.org/plugin/40282/phpmd-php-codesniffer-plugin 51 | 52 | 2. PHPStorm 53 | 54 | http://www.jetbrains.com/phpstorm/webhelp/using-php-code-sniffer-tool.html 55 | 56 | 3. Zend Studio 57 | 58 | http://files.zend.com/help/Zend-Studio/content/working_with_php_codesniffer.htm 59 | 60 | How to make a standard coding in the team 61 | ----------------------------------------- 62 | 63 | Use pre-commit hooks, which will make a code standard check for every commit. 64 | 65 | 1. For Git 66 | 67 | http://git-scm.com/book/en/Customizing-Git-Git-Hooks 68 | 69 | 2. For SVN 70 | 71 | http://pear.php.net/manual/ru/package.php.php-codesniffer.svn-pre-commit.php 72 | 73 | Yii-coding-standard and Composer 74 | -------------------------------- 75 | 76 | For using "yii-coding-standard" with Composer, include a dependency for "ardem/yii-coding-standard" in your composer.json file: 77 | 78 | { 79 | "require": { 80 | "ardem/yii-coding-standard": "dev-master" 81 | } 82 | } 83 | 84 | -------------------------------------------------------------------------------- /Tests/TrueTestClass.php: -------------------------------------------------------------------------------- 1 | 11 | * @copyright 2013 Artem Demchenkov 12 | * @license http://www.yiiframework.com/license/ BSD license 13 | * @link http://pear.php.net/package/PHP_CodeSniffer 14 | */ 15 | 16 | /** 17 | * Yii_Sniffs_ControlStructures_SingleLineWithoutBracesSniff. 18 | * 19 | * Ensures that a single line "if" have an expression on next line without braces. 20 | * 21 | * @category PHP 22 | * @package PHP_CodeSniffer 23 | * @author Artem Demchenkov 24 | * @copyright 2013 Artem Demchenkov 25 | * @license http://www.yiiframework.com/license/ BSD license 26 | * @link http://pear.php.net/package/PHP_CodeSniffer 27 | */ 28 | class Yii_Sniffs_ControlStructures_SingleLineWithoutBracesSniff implements PHP_CodeSniffer_Sniff { 29 | 30 | /** 31 | * Process the tokens that this sniff is listening for. 32 | * 33 | * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. 34 | * @param int|array $types The type(s) of tokens to search for. 35 | * @param int $start The position to start searching from in the token stack. 36 | * @param int $end The end position to fail if no token is found. if not specified or null, end will default to the end of the token stack. 37 | * @param bool $exclude If true, find the next token that is NOT of a type specified in $types. 38 | * @param string $value The value that the token(s) must be equal to. If value is omitted, tokens with any value will be returned. 39 | * @param bool $local If true, tokens outside the current statement will not be checked. i.e., checking will stop at the next semi-colon found. 40 | * 41 | * @return void 42 | */ 43 | private function findNext($phpcsFile, $types, $start, $end = null, $exclude = false, $value = null, $local = false) { 44 | return $phpcsFile->findNext($types, $start, $end, $exclude, $value, $local); 45 | } 46 | 47 | /** 48 | * Registers the token types that this sniff wishes to listen to. 49 | * 50 | * @return array 51 | */ 52 | public function register() { 53 | return array( 54 | T_IF, 55 | T_WHILE, 56 | T_FOR, 57 | T_FOREACH, 58 | T_ELSE, 59 | ); 60 | } //end register() 61 | 62 | /** 63 | * Process the tokens that this sniff is listening for. 64 | * 65 | * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. 66 | * @param int $stackPtr The position in the stack where 67 | * the token was found. 68 | * 69 | * @return void 70 | */ 71 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { 72 | 73 | $tokens = $phpcsFile->getTokens(); 74 | $startPosition = $stackPtr + 1; 75 | 76 | $allowedTokens = array( 77 | T_OPEN_CURLY_BRACKET 78 | ); 79 | 80 | // special trick for "FOR". Exclude 2 internal semicolons 81 | if ($tokens[$stackPtr]['code'] == T_FOR) { 82 | 83 | $specialTokens = array( 84 | T_SEMICOLON 85 | ); 86 | 87 | $next = $stackPtr; 88 | 89 | for ($i = 0; $i <= 1; $i++) 90 | $next = $this->findNext($phpcsFile, $specialTokens, ($next + 1), null, false, null, false); 91 | 92 | $startPosition = $next + 1; 93 | } // end if 94 | 95 | $next = $this->findNext($phpcsFile, $allowedTokens, $startPosition, null, false, null, true); 96 | 97 | if ($next === false) { // this is single line structure. 98 | 99 | // check the whitespace after tocken 100 | if ($tokens[$stackPtr + 1]['type'] != 'T_WHITESPACE' || strlen($tokens[$stackPtr + 1]['content']) > 1) { 101 | $error = 'Single line condition must have a one whitespace before opening parenthesis'; 102 | $phpcsFile->addError($error, $stackPtr, 'SingleLineIfMustHaveAWhiteSpace'); 103 | } 104 | 105 | // find the last close parenthesis in the condition. 106 | $i = 0; 107 | $newParenthesis = $stackPtr; 108 | do { 109 | $newParenthesis = $phpcsFile->findNext(array(T_OPEN_PARENTHESIS, T_CLOSE_PARENTHESIS), ($newParenthesis + 1)); 110 | $i = ($tokens[$newParenthesis]['type'] == "T_OPEN_PARENTHESIS") ? $i + 1 : $i - 1; 111 | } while ($i != 0); 112 | 113 | $closeBracket = $newParenthesis; 114 | 115 | // check the new line 116 | $n = 1; 117 | $newline = false; 118 | 119 | while ($tokens[$closeBracket + $n]['type'] == 'T_WHITESPACE') { 120 | $strlen = strlen($tokens[$closeBracket + $n]['content']); 121 | if ($tokens[$closeBracket + $n]['content'][$strlen - 1] == $phpcsFile->eolChar) { 122 | $newline = true; 123 | break; 124 | } 125 | $n++; 126 | } 127 | 128 | if ($newline === false) { 129 | $error = 'Single line "%s" must have an expression started from new line. '; 130 | $phpcsFile->addError($error, $stackPtr, 'SingleLineExpressionMustHaveANewLineExpression', array(strtoupper($tokens[$stackPtr]['content']))); 131 | } 132 | } // end if 133 | } //end process() 134 | } //end class 135 | -------------------------------------------------------------------------------- /Yii/Sniffs/Files/Utf8EncodingSniff.php: -------------------------------------------------------------------------------- 1 | 10 | * @copyright 2013 Artem Demchenkov 11 | * @license http://www.yiiframework.com/license/ BSD license 12 | * @link http://pear.php.net/package/PHP_CodeSniffer 13 | */ 14 | 15 | /** 16 | * Yii_Sniffs_Files_Utf8EncodingSniff. 17 | * 18 | * Ensures that this file have UTF-8 encoding. 19 | * 20 | * @category PHP 21 | * @package PHP_CodeSniffer 22 | * @author Artem Demchenkov 23 | * @copyright 2013 Artem Demchenkov 24 | * @license http://www.yiiframework.com/license/ BSD license 25 | * @link http://pear.php.net/package/PHP_CodeSniffer 26 | */ 27 | class Yii_Sniffs_Files_Utf8EncodingSniff implements PHP_CodeSniffer_Sniff 28 | { 29 | 30 | /** 31 | * A list of tokenizers this sniff supports. 32 | * 33 | * @var array 34 | */ 35 | public $supportedTokenizers = array( 36 | 'PHP', 37 | ); 38 | 39 | /** 40 | * Returns the token types that this sniff is interested in. 41 | * 42 | * @return array(int) 43 | */ 44 | public function register() 45 | { 46 | return array(T_OPEN_TAG); 47 | }//end register() 48 | 49 | /** 50 | * Processes this test, when one of its tokens is encountered. 51 | * 52 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 53 | * @param int $stackPtr The position of the current token 54 | * in the stack passed in $tokens. 55 | * 56 | * @return void 57 | */ 58 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 59 | { 60 | // chech the first open tag. 61 | if ($stackPtr !== 0) { 62 | if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) 63 | return; 64 | } 65 | 66 | $filePath = $phpcsFile->getFilename(); 67 | $fileContent = file_get_contents($filePath); 68 | $encoding = mb_detect_encoding($fileContent, 'UTF-8, ASCII, ISO-8859-1'); 69 | 70 | if ($encoding !== 'UTF-8') { 71 | $fileName = basename($filePath); 72 | 73 | if ($encoding) { 74 | $error = 'File must use only UTF-8 encoding. but %s found'; 75 | $phpcsFile->addError($error, $stackPtr, 'FileIsNotUTF8Encoded', array($encoding)); 76 | } else { 77 | $error = 'File must use only UTF-8 encoding.'; 78 | $phpcsFile->addError($error, 0); 79 | } 80 | } 81 | }//end process() 82 | 83 | }//end class 84 | -------------------------------------------------------------------------------- /Yii/Sniffs/Objects/ObjectParenthesesSniff.php: -------------------------------------------------------------------------------- 1 | 11 | * @copyright 2013 Artem Demchenkov 12 | * @license http://www.yiiframework.com/license/ BSD license 13 | * @link http://pear.php.net/package/PHP_CodeSniffer 14 | */ 15 | 16 | /** 17 | * Yii_Sniffs_Objects_ObjectParenthesesSniff. 18 | * 19 | * Ensures that new object declaration have an open and close parentheses. 20 | * 21 | * @category PHP 22 | * @package PHP_CodeSniffer 23 | * @author Artem Demchenkov 24 | * @copyright 2013 Artem Demchenkov 25 | * @license http://www.yiiframework.com/license/ BSD license 26 | * @link http://pear.php.net/package/PHP_CodeSniffer 27 | */ 28 | class Yii_Sniffs_Objects_ObjectParenthesesSniff implements PHP_CodeSniffer_Sniff { 29 | 30 | /** 31 | * Registers the token types that this sniff wishes to listen to. 32 | * 33 | * @return array 34 | */ 35 | public function register() { 36 | return array(T_NEW); 37 | } //end register() 38 | 39 | /** 40 | * Process the tokens that this sniff is listening for. 41 | * 42 | * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. 43 | * @param int $stackPtr The position in the stack where 44 | * the token was found. 45 | * 46 | * @return void 47 | */ 48 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { 49 | $allowedTokens = array( 50 | T_OPEN_PARENTHESIS, 51 | T_CLOSE_PARENTHESIS, 52 | ); 53 | 54 | $next = $phpcsFile->findNext($allowedTokens, ($stackPtr + 1)); 55 | 56 | if ($next === false) { 57 | $error = 'New object declaration must contain a couple parentheses: new MyClass()'; 58 | $phpcsFile->addError($error, $stackPtr, 'ObjectParenthesisNotFound'); 59 | } 60 | } //end process() 61 | } //end class -------------------------------------------------------------------------------- /Yii/ruleset.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | The Yii Framework coding standard. You can use it for 1.X.X or 2.X.X version of framework. 4 | 5 | 6 | */views/* 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | %s name "%s" is not in StudlyCaps format 35 | 36 | 37 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ardem/yii-coding-standard", 3 | "description": "Yii Framework PHP CodeSniffer Coding Standard.", 4 | "keywords": ["yii", "phpcs", "coding standards"], 5 | "license": "BSD-3-Clause", 6 | "authors": [ 7 | { 8 | "name": "Artem Demchenkov", 9 | "homepage": "https://github.com/Ardem", 10 | "role": "Maintainer" 11 | } 12 | ], 13 | "support": { 14 | "issues": "https://github.com/Ardem/yii-coding-standard/issues" 15 | }, 16 | "require": { 17 | "squizlabs/php_codesniffer": ">=1.4.2" 18 | } 19 | } 20 | --------------------------------------------------------------------------------