Skip to content

Commit

Permalink
Fixed some problems with checking spacing when none exists + unit tes…
Browse files Browse the repository at this point in the history
…ts + coding standards + changelog
  • Loading branch information
gsherwood committed Jan 30, 2014
1 parent f60430e commit 72af7c0
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function register()
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen;
$this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen;
$this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose;
$tokens = $phpcsFile->getTokens();

Expand Down Expand Up @@ -174,10 +174,10 @@ public function processSingleLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr
{

$closer = $tokens[$openBracket]['parenthesis_closer'];

if ($openBracket === ($closer - 1)) {
return;
}

if ($this->requiredSpacesAfterOpen === 0 && $tokens[($openBracket + 1)]['code'] === T_WHITESPACE) {
// Checking this: $value = my_function([*]...).
$error = 'Space after opening parenthesis of function call prohibited';
Expand All @@ -187,36 +187,30 @@ public function processSingleLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr
if ($tokens[($openBracket + 1)]['code'] === T_WHITESPACE) {
$spaceAfterOpen = strlen($tokens[($openBracket + 1)]['content']);
}

if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) {
$error = 'Expected %d spaces after opening bracket; %d found';
$data = array($this->requiredSpacesAfterOpen, $spaceAfterOpen);
$error = 'Expected %s spaces after opening bracket; %s found';
$data = array(
$this->requiredSpacesAfterOpen,
$spaceAfterOpen,
);
$phpcsFile->addError($error, $stackPtr, 'SpaceAfterOpenBracket', $data);
}
}

// Checking this: $value = my_function(...[*]).
$spaceBeforeClose = 0;
if ($tokens[($closer - 1)]['code'] === T_WHITESPACE) {
// Checking this: $value = my_function(...[*]).
$between = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), null, true);

// Only throw an error if there is some content between the parenthesis.
// i.e., Checking for this: $value = my_function().
// If there is no content, then we would have thrown an error in the
// previous IF statement because it would look like this:
// $value = my_function( ).
if ($this->requiredSpacesBeforeClose === 0 && $between !== $closer) {
$error = 'Space before closing parenthesis of function call prohibited';
$phpcsFile->addError($error, $closer, 'SpaceBeforeCloseBracket');
} else if ($this->requiredSpacesBeforeClose > 0) {
$spaceBeforeClose = 0;
if ($tokens[($closer - 1)]['code'] === T_WHITESPACE) {
$spaceBeforeClose = strlen($tokens[($closer - 1)]['content']);
}
if ($spaceBeforeClose !== $this->requiredSpacesBeforeClose) {
$error = 'Expected %d spaces before closing bracket; %d found';
$data = array($this->requiredSpacesBeforeClose, $spaceBeforeClose);
$phpcsFile->addError($error, $stackPtr, 'SpaceBeforeCloseBracket', $data);
}
}
$spaceBeforeClose = strlen($tokens[($closer - 1)]['content']);
}

if ($spaceBeforeClose !== $this->requiredSpacesBeforeClose) {
$error = 'Expected %s spaces before closing bracket; %s found';
$data = array(
$this->requiredSpacesBeforeClose,
$spaceBeforeClose,
);
$phpcsFile->addError($error, $stackPtr, 'SpaceBeforeCloseBracket', $data);
}

}//end processSingleLineCall()
Expand Down Expand Up @@ -372,4 +366,3 @@ public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr,


}//end class
?>
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,11 @@ function foo()
'string'.
);
}
?>

// @codingStandardsChangeSetting PEAR.Functions.FunctionCallSignature requiredSpacesAfterOpen 1
// @codingStandardsChangeSetting PEAR.Functions.FunctionCallSignature requiredSpacesBeforeClose 1
ftest($arg, $arg2);
test( $arg, $arg2 );
test( $arg, $arg2 );
// @codingStandardsChangeSetting PEAR.Functions.FunctionCallSignature requiredSpacesAfterOpen 0
// @codingStandardsChangeSetting PEAR.Functions.FunctionCallSignature requiredSpacesBeforeClose 0
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getErrorList()
{
return array(
5 => 1,
6 => 1,
6 => 2,
7 => 1,
8 => 1,
9 => 2,
Expand All @@ -53,7 +53,7 @@ public function getErrorList()
18 => 1,
31 => 1,
34 => 1,
43 => 1,
43 => 2,
57 => 1,
59 => 1,
63 => 1,
Expand All @@ -69,6 +69,8 @@ public function getErrorList()
142 => 2,
171 => 1,
203 => 1,
211 => 2,
213 => 2,
);

}//end getErrorList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,24 @@ public function register()
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen;
$this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen;
$this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose;
$tokens = $phpcsFile->getTokens();

if (isset($tokens[$stackPtr]['parenthesis_opener']) === true) {
$parenOpener = $tokens[$stackPtr]['parenthesis_opener'];
$parenCloser = $tokens[$stackPtr]['parenthesis_closer'];
$parenOpener = $tokens[$stackPtr]['parenthesis_opener'];
$parenCloser = $tokens[$stackPtr]['parenthesis_closer'];
$spaceAfterOpen = 0;
if ($tokens[($parenOpener + 1)]['code'] === T_WHITESPACE) {
$spaceAfterOpen = strlen($tokens[($parenOpener + 1)]['content']);
}

if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) {
$error = 'Expected %d spaces after opening bracket; %s found';
$data = array($this->requiredSpacesAfterOpen, $spaceAfterOpen);
$error = 'Expected %s spaces after opening bracket; %s found';
$data = array(
$this->requiredSpacesAfterOpen,
$spaceAfterOpen,
);
$phpcsFile->addError($error, ($parenOpener + 1), 'SpacingAfterOpenBrace', $data);
}

Expand All @@ -98,9 +102,13 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($tokens[($parenCloser - 1)]['code'] === T_WHITESPACE) {
$spaceBeforeClose = strlen($tokens[($parenCloser - 1)]['content']);
}

if ($spaceBeforeClose !== $this->requiredSpacesBeforeClose) {
$error = 'Expected %d spaces before closing bracket; %s found';
$data = array($this->requiredSpacesBeforeClose, $spaceBeforeClose);
$error = 'Expected %s spaces before closing bracket; %s found';
$data = array(
$this->requiredSpacesBeforeClose,
$spaceBeforeClose,
);
$phpcsFile->addError($error, ($parenCloser - 1), 'SpaceBeforeCloseBrace', $data);
}
}
Expand All @@ -110,5 +118,3 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)


}//end class

?>
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@ if (
) {
$settingsUpdated = FALSE;
}
?>

// @codingStandardsChangeSetting PSR2.ControlStructures.ControlStructureSpacing requiredSpacesAfterOpen 1
// @codingStandardsChangeSetting PSR2.ControlStructures.ControlStructureSpacing requiredSpacesBeforeClose 1
foreach ($something as $blah => $that) {}
foreach ( $something as $blah => $that ) {}
foreach ( $something as $blah => $that ) {}
// @codingStandardsChangeSetting PSR2.ControlStructures.ControlStructureSpacing requiredSpacesAfterOpen 0
// @codingStandardsChangeSetting PSR2.ControlStructures.ControlStructureSpacing requiredSpacesBeforeClose 0
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function getErrorList()
26 => 2,
27 => 2,
31 => 1,
41 => 2,
43 => 2,
);

}//end getErrorList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function register()
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen;
$this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen;
$this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose;
$tokens = $phpcsFile->getTokens();

Expand All @@ -84,9 +84,13 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) {
$spaceAfterOpen = strlen($tokens[($openingBracket + 1)]['content']);
}

if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) {
$error = 'Expected %d spaces after opening bracket; %s found';
$data = array($this->requiredSpacesAfterOpen, $spaceAfterOpen);
$error = 'Expected %s spaces after opening bracket; %s found';
$data = array(
$this->requiredSpacesAfterOpen,
$spaceAfterOpen,
);
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterOpen', $data);
}
}
Expand All @@ -99,9 +103,13 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) {
$spaceBeforeClose = strlen($tokens[($closingBracket - 1)]['content']);
}

if ($spaceBeforeClose !== $this->requiredSpacesBeforeClose) {
$error = 'Expected %d spaces before closing bracket; %s found';
$data = array($this->requiredSpacesBeforeClose, $spaceBeforeClose);
$error = 'Expected %s spaces before closing bracket; %s found';
$data = array(
$this->requiredSpacesBeforeClose,
$spaceBeforeClose,
);
$phpcsFile->addError($error, $stackPtr, 'SpaceBeforeClose', $data);
}
}
Expand Down Expand Up @@ -131,7 +139,6 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$data = array($spaces);
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeArrow', $data);
}

}

if ($tokens[($doubleArrow + 1)]['code'] !== T_WHITESPACE) {
Expand All @@ -144,9 +151,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$data = array($spaces);
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterArrow', $data);
}

}

}//end if

if ($tokens[($asToken - 1)]['code'] !== T_WHITESPACE) {
Expand Down Expand Up @@ -177,5 +182,3 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)


}//end class

?>
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function register()
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen;
$this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen;
$this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose;
$tokens = $phpcsFile->getTokens();

Expand All @@ -99,9 +99,13 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) {
$spaceAfterOpen = strlen($tokens[($openingBracket + 1)]['content']);
}

if ($this->requiredSpacesAfterOpen !== $spaceAfterOpen) {
$error = 'Expected %d spaces after opening bracket; %s found';
$data = array($this->requiredSpacesAfterOpen, $spaceAfterOpen);
$error = 'Expected %s spaces after opening bracket; %s found';
$data = array(
$this->requiredSpacesAfterOpen,
$spaceAfterOpen,
);
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterOpen', $data);
}
}
Expand All @@ -114,14 +118,18 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) {
$spaceBeforeClose = strlen($tokens[($closingBracket - 1)]['content']);
}

if ($this->requiredSpacesBeforeClose !== $spaceBeforeClose) {
$error = 'Expected %d spaces before closing bracket; %s found';
$data = array($this->requiredSpacesBeforeClose, $spaceBeforeClose);
$error = 'Expected %s spaces before closing bracket; %s found';
$data = array(
$this->requiredSpacesBeforeClose,
$spaceBeforeClose,
);
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeClose');
}
}

$firstSemicolon = $phpcsFile->findNext(T_SEMICOLON, $openingBracket, $closingBracket);
$firstSemicolon = $phpcsFile->findNext(T_SEMICOLON, $openingBracket, $closingBracket);

// Check whitespace around each of the tokens.
if ($firstSemicolon !== false) {
Expand Down Expand Up @@ -179,5 +187,3 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)


}//end class

?>
Loading

0 comments on commit 72af7c0

Please sign in to comment.