Skip to content
This repository has been archived by the owner on Oct 25, 2021. It is now read-only.

Update Symfony2 coding standards to be used with PHP_CodeSniffer v2+ #7

Merged
merged 2 commits into from
Jul 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ We also added to this project our own standard for our Symfony2 developments ent

## Credits

Developped by [M6Web](http://tech.m6web.fr/).
Developped by [M6Web](http://tech.m6web.fr/), cloned from [lapistano/Symfony2-coding-standard](https://github.com/lapistano/Symfony2-coding-standard).
The PHP_CodeSniffer 2.0+ Symfony2 standard is very broadly inspired from [escapestudios/Symfony2-coding-standard](https://github.com/escapestudios/Symfony2-coding-standard)

## License

Expand Down
Empty file removed Symfony2/Docs/.placeholder
Empty file.
95 changes: 95 additions & 0 deletions Symfony2/Sniffs/Arrays/MultiLineArrayCommaSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* This file is part of the Symfony2-coding-standard (phpcs standard)
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer-Symfony2
* @author wicliff wolda <[email protected]>
* @license http://spdx.org/licenses/MIT MIT License
* @version GIT: master
* @link https://github.com/M6Web/Symfony2-coding-standard
*/

/**
* Symfony2_Sniffs_WhiteSpace_MultiLineArrayCommaSniff.
*
* Throws warnings if the last item in a multi line array does not have a
* trailing comma
*
* @category PHP
* @package PHP_CodeSniffer-Symfony2
* @author wicliff wolda <[email protected]>
* @license http://spdx.org/licenses/MIT MIT License
* @link https://github.com/M6Web/Symfony2-coding-standard
*/
class Symfony2_Sniffs_Arrays_MultiLineArrayCommaSniff
implements PHP_CodeSniffer_Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array(
'PHP',
);

/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_ARRAY,
T_OPEN_SHORT_ARRAY,
);

}//end register()

/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @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();
$open = $tokens[$stackPtr];

if ($open['code'] === T_ARRAY) {
$closePtr = $open['parenthesis_closer'];
} else {
$closePtr = $open['bracket_closer'];
}

if ($open['line'] <> $tokens[$closePtr]['line']) {
$lastComma = $phpcsFile->findPrevious(T_COMMA, $closePtr);

while ($lastComma < $closePtr -1) {
$lastComma++;

if ($tokens[$lastComma]['code'] !== T_WHITESPACE
&& $tokens[$lastComma]['code'] !== T_COMMENT
) {
$phpcsFile->addError(
'Add a comma after each item in a multi-line array',
$stackPtr,
'Invalid'
);
break;
}
}
}

}//end process()

}//end class

6 changes: 3 additions & 3 deletions Symfony2/Sniffs/Classes/MultipleClassesOneFileSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
*
* @category PHP
* @package PHP_CodeSniffer-Symfony2
* @author Symfony2-phpcs-authors <Symfony2-coding-standard@opensky.github.com>
* @author Symfony2-phpcs-authors <Symfony2-coding-standard@m6web.github.com>
* @license http://spdx.org/licenses/MIT MIT License
* @version GIT: master
* @link https://github.com/opensky/Symfony2-coding-standard
* @link https://github.com/M6Web/Symfony2-coding-standard
*/

/**
Expand All @@ -24,7 +24,7 @@
* @package PHP_CodeSniffer-Symfony2
* @author Dave Hauenstein <[email protected]>
* @license http://spdx.org/licenses/MIT MIT License
* @link https://github.com/opensky/Symfony2-coding-standard
* @link https://github.com/M6Web/Symfony2-coding-standard
*/
class Symfony2_Sniffs_Classes_MultipleClassesOneFileSniff implements PHP_CodeSniffer_Sniff
{
Expand Down
84 changes: 84 additions & 0 deletions Symfony2/Sniffs/Classes/PropertyDeclarationSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/**
* This file is part of the Symfony2-coding-standard (phpcs standard)
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer-Symfony2
* @author wicliff wolda <[email protected]>
* @license http://spdx.org/licenses/MIT MIT License
* @version GIT: master
* @link https://github.com/M6Web/Symfony2-coding-standard
*/

/**
* Symfony2_Sniffs_Classes_PropertyDeclarationSniff.
*
* Throws warnings if properties are declared after methods
*
* @category PHP
* @package PHP_CodeSniffer-Symfony2
* @author wicliff wolda <[email protected]>
* @license http://spdx.org/licenses/MIT MIT License
* @link https://github.com/M6Web/Symfony2-coding-standard
*/
class Symfony2_Sniffs_Classes_PropertyDeclarationSniff implements PHP_CodeSniffer_Sniff
{

/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array(
'PHP',
);

/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_CLASS,
);
}//end register()

/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @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();
$scope = $phpcsFile->findNext(T_FUNCTION, $stackPtr, $tokens[$stackPtr]['scope_closer']);

$wantedTokens = array(
T_PUBLIC,
T_PROTECTED,
T_PRIVATE
);

while ($scope) {
$scope = $phpcsFile->findNext($wantedTokens, $scope + 1, $tokens[$stackPtr]['scope_closer']);

if ($scope && $tokens[$scope + 2]['code'] === T_VARIABLE) {
$phpcsFile->addError(
'Declare class properties before methods',
$scope,
'Invalid'
);
}
}
}//end process()

}//end class
4 changes: 2 additions & 2 deletions Symfony2/Sniffs/Commenting/ClassCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

if (class_exists('PHP_CodeSniffer_CommentParser_ClassCommentParser', true) === false) {
$error = 'Class PHP_CodeSniffer_CommentParser_ClassCommentParser not found';
if (class_exists('PHP_CodeSniffer_Tokenizers_Comment', true) === false) {
$error = 'Class PHP_CodeSniffer_Tokenizers_Comment not found';
throw new PHP_CodeSniffer_Exception($error);
}

Expand Down
55 changes: 35 additions & 20 deletions Symfony2/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*
* @category PHP
* @package PHP_CodeSniffer-Symfony2
* @author Symfony2-phpcs-authors <Symfony2-coding-standard@opensky.github.com>
* @author Symfony2-phpcs-authors <Symfony2-coding-standard@m6web.github.com>
* @license http://spdx.org/licenses/MIT MIT License
* @version GIT: master
* @link https://github.com/opensky/Symfony2-coding-standard
* @link https://github.com/M6Web/Symfony2-coding-standard
*/

if (class_exists('PEAR_Sniffs_Commenting_FunctionCommentSniff', true) === false) {
Expand Down Expand Up @@ -68,29 +68,31 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
/**
* Process the return comment of this function comment.
*
* @param int $commentStart The position in the stack where the comment started.
* @param int $commentEnd The position in the stack where the comment ended.
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $commentStart The position in the stack where the comment started.
*
* @return void
*/
protected function processReturn($commentStart, $commentEnd)
protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart)
{
if ($this->isInheritDoc()) {

if ($this->isInheritDoc($phpcsFile, $stackPtr)) {
return;
}

$tokens = $this->currentFile->getTokens();
$funcPtr = $this->currentFile->findNext(T_FUNCTION, $commentEnd);
$tokens = $phpcsFile->getTokens();

// Only check for a return comment if a non-void return statement exists
if (isset($tokens[$funcPtr]['scope_opener'])) {
$start = $tokens[$funcPtr]['scope_opener'];
if (isset($tokens[$stackPtr]['scope_opener'])) {
$start = $tokens[$stackPtr]['scope_opener'];

// iterate over all return statements of this function,
// run the check on the first which is not only 'return;'
while ($returnToken = $this->currentFile->findNext(T_RETURN, $start, $tokens[$funcPtr]['scope_closer'])) {
while ($returnToken = $phpcsFile->findNext(T_RETURN, $start, $tokens[$stackPtr]['scope_closer'])) {
if ($this->isMatchingReturn($tokens, $returnToken)) {
parent::processReturn($commentStart, $commentEnd);
parent::processReturn($phpcsFile, $stackPtr, $commentStart);
break;
}
$start = $returnToken + 1;
Expand All @@ -102,30 +104,43 @@ protected function processReturn($commentStart, $commentEnd)
/**
* Is the comment an inheritdoc?
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return boolean True if the comment is an inheritdoc
*/
protected function isInheritDoc ()
protected function isInheritDoc(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$content = $this->commentParser->getComment()->getContent();
$tokens = $phpcsFile->getTokens();

$start = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1);
$end = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, $start);

$content = $phpcsFile->getTokensAsString($start, ($end - $start));

return preg_match('#{@inheritdoc}#i', $content) === 1;
} // end isInheritDoc()

/**
* Process the function parameter comments.
*
* @param int $commentStart The position in the stack where
* the comment started.
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $commentStart The position in the stack where the comment started.
*
* @return void
*/
protected function processParams($commentStart)
protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart)
{
if ($this->isInheritDoc()) {
$tokens = $phpcsFile->getTokens();

if ($this->isInheritDoc($phpcsFile, $stackPtr)) {
return;
}

parent::processParams($commentStart);
parent::processParams($phpcsFile, $stackPtr, $commentStart);
} // end processParams()

/**
Expand All @@ -136,7 +151,7 @@ protected function processParams($commentStart)
*
* @return boolean True if the return does not return anything
*/
protected function isMatchingReturn ($tokens, $returnPos)
protected function isMatchingReturn($tokens, $returnPos)
{
do {
$returnPos++;
Expand Down
8 changes: 5 additions & 3 deletions Symfony2/Sniffs/Formatting/BlankLineBeforeReturnSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
*
* @category PHP
* @package PHP_CodeSniffer-Symfony2
* @author Symfony2-phpcs-authors <Symfony2-coding-standard@opensky.github.com>
* @author Symfony2-phpcs-authors <Symfony2-coding-standard@m6web.github.com>
* @license http://spdx.org/licenses/MIT MIT License
* @version GIT: master
* @link https://github.com/opensky/Symfony2-coding-standard
* @link https://github.com/M6Web/Symfony2-coding-standard
*/

/**
Expand All @@ -24,7 +24,7 @@
* @package PHP_CodeSniffer-Symfony2
* @author Dave Hauenstein <[email protected]>
* @license http://spdx.org/licenses/MIT MIT License
* @link https://github.com/opensky/Symfony2-coding-standard
* @link https://github.com/M6Web/Symfony2-coding-standard
*/
class Symfony2_Sniffs_Formatting_BlankLineBeforeReturnSniff implements PHP_CodeSniffer_Sniff
{
Expand Down Expand Up @@ -68,6 +68,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($tokens[$current]['line'] == $previousLine
&& $tokens[$current]['type'] !== 'T_WHITESPACE'
&& $tokens[$current]['type'] !== 'T_COMMENT'
&& $tokens[$current]['type'] !== 'T_DOC_COMMENT_CLOSE_TAG'
&& $tokens[$current]['type'] !== 'T_DOC_COMMENT_WHITESPACE'
) {
$prevLineTokens[] = $tokens[$current]['type'];
}
Expand Down
Loading