Skip to content

Commit

Permalink
Merge pull request #107 from ongr-io/master
Browse files Browse the repository at this point in the history
v2.1.1 release request
  • Loading branch information
saimaz committed Apr 21, 2015
2 parents d06defc + 6356405 commit 1b2a1c7
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 92 deletions.
114 changes: 41 additions & 73 deletions Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,9 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
// }
//
// if ($singleValue === true) {
// Array cannot be empty, so this is a multi-line array with
// a single value. It should be defined on single line.
$error = 'Multi-line array contains a single value; use single-line array instead';
// Array cannot be empty, so this is a multi-line array with
// a single value. It should be defined on single line.
$error = 'Multi-line array contains a single value; use single-line array instead';
// $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultiLineNotAllowed');
//
// if ($fix === true) {
Expand Down Expand Up @@ -635,7 +635,7 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
// $phpcsFile->fixer->addNewlineBefore($value['value']);
// }
// } else
if ($tokens[($value['value'] - 1)]['code'] === T_WHITESPACE) {
if ($tokens[($value['value'] - 1)]['code'] === T_WHITESPACE) {
$expected = $statementStart + 4;

$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $value['value'], true);
Expand Down Expand Up @@ -812,89 +812,57 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
// }
// }//end if
//ongr end
// Check each line ends in a comma or dot (string concat).
// Check each line ends in a comma.
// if ($tokens[$index['value']]['code'] !== T_ARRAY
// && $tokens[$index['value']]['code'] !== T_OPEN_SHORT_ARRAY
// ) {
$valueLine = $tokens[$index['value']]['line'];
$nextComma = false;
$nextDot = false;
for ($i = ($index['value'] + 1); $i < $arrayEnd; $i++) {
// Skip bracketed statements, like function calls.
if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
$i = $tokens[$i]['parenthesis_closer'];
$valueLine = $tokens[$i]['line'];
continue;
}
$valueLine = $tokens[$index['value']]['line'];
$nextComma = false;
for ($i = ($index['value'] + 1); $i < $arrayEnd; $i++) {
// Skip bracketed statements, like function calls.
if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
$i = $tokens[$i]['parenthesis_closer'];
$valueLine = $tokens[$i]['line'];
continue;
}

if ($tokens[$i]['code'] === T_COMMA) {
$nextComma = $i;
break;
}
else if($tokens[$i]['code'] === T_STRING_CONCAT) {
$nextDot = $i;
break;
}
if ($tokens[$i]['code'] === T_COMMA) {
$nextComma = $i;
break;
}
}

if (($nextComma === false && $nextDot === false)
|| ($tokens[$nextComma]['line'] !== $valueLine && $tokens[$nextDot]['line'] !== $valueLine)
&& $tokens[$index['value']]['code'] !== T_OPEN_SHORT_ARRAY
) {
$error = 'Each line in an array declaration must end in a comma';
$fix = $phpcsFile->addFixableError($error, $index['value'], 'NoComma');
if (($nextComma === false) || ($tokens[$nextComma]['line'] !== $valueLine) && $tokens[$index['value']]['code'] !== T_OPEN_SHORT_ARRAY) {
$error = 'Each line in an array declaration must end in a comma';
$fix = $phpcsFile->addFixableError($error, $index['value'], 'NoComma');

if ($fix === true) {
// Find the end of the line and put a comma there.
for ($i = ($index['value'] + 1); $i < $phpcsFile->numTokens; $i++) {
if ($tokens[$i]['line'] > $tokens[$index['value']]['line']) {
break;
}
if ($fix === true) {
// Find the end of the line and put a comma there.
for ($i = ($index['value'] + 1); $i < $phpcsFile->numTokens; $i++) {
if ($tokens[$i]['line'] > $tokens[$index['value']]['line']) {
break;
}

$phpcsFile->fixer->addContentBefore(($i - 1), ',');
}
}

// Check that there is no space before the comma.
if ($nextComma !== false && $tokens[($nextComma - 1)]['code'] === T_WHITESPACE) {
$content = $tokens[($nextComma - 2)]['content'];
$spaceLength = $tokens[($nextComma - 1)]['length'];
$error = 'Expected 0 spaces between "%s" and comma; %s found';
$data = array(
$content,
$spaceLength,
);

$fix = $phpcsFile->addFixableError($error, $nextComma, 'SpaceBeforeComma', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($nextComma - 1), '');
}
$phpcsFile->fixer->addContentBefore(($i - 1), ',');
}
}

if ($nextDot !== false) {
$spacesExpected = $this->getStatementStartColumn($phpcsFile, $nextDot) + 3;

$indentation = $nextDot;
while($tokens[$nextDot]['line'] == $tokens[$indentation]['line']) {
$indentation++;
}

$spacesFound = 0;
if ($tokens[$indentation]['code'] == T_WHITESPACE) {
$spacesFound = $tokens[$indentation]['length'];
}

if ($spacesExpected != $spacesFound) {
$error = 'Array value not aligned correctly; expected %s spaces but found %s';
$data = array(
$spacesExpected,
$spacesFound,
);
// Check that there is no space before the comma.
if ($nextComma !== false && $tokens[($nextComma - 1)]['code'] === T_WHITESPACE) {
$content = $tokens[($nextComma - 2)]['content'];
$spaceLength = $tokens[($nextComma - 1)]['length'];
$error = 'Expected 0 spaces between "%s" and comma; %s found';
$data = array(
$content,
$spaceLength,
);

$phpcsFile->addError($error, $indentation, 'ValueNotAligned', $data);
}
$fix = $phpcsFile->addFixableError($error, $nextComma, 'SpaceBeforeComma', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($nextComma - 1), '');
}
}
// }//end if
}//end foreach

Expand Down
71 changes: 63 additions & 8 deletions Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}
$this->processReturn($phpcsFile, $stackPtr, $commentStart);
$this->processThrows($phpcsFile, $stackPtr, $commentStart);
$this->processDeprecated($phpcsFile, $stackPtr, $commentStart);
$this->processParams($phpcsFile, $stackPtr, $commentStart);
$this->processComments($phpcsFile, $stackPtr, $commentStart);

Expand Down Expand Up @@ -299,9 +300,9 @@ protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
}//end if

$comment = null;
if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) {
if ($tokens[($return + 2)]['code'] === T_DOC_COMMENT_STRING) {
$matches = array();
preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[($tag + 2)]['content'], $matches);
preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[($return + 2)]['content'], $matches);
if (isset($matches[2]) === true) {
$comment = $matches[2];
}
Expand All @@ -312,26 +313,26 @@ protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
$firstChar = $comment{0};
if (preg_match('|\p{Lu}|u', $firstChar) === 0) {
$error = 'Return comment must start with a capital letter';
$fix = $phpcsFile->addFixableError($error, ($tag + 2), 'ReturnCommentNotCapital');
$fix = $phpcsFile->addFixableError($error, ($return + 2), 'ReturnCommentNotCapital');

if ($fix === true) {
$newComment = ucfirst($comment);
$tokenLength = strlen($tokens[($tag + 2)]['content']);
$tokenLength = strlen($tokens[($return + 2)]['content']);
$commentLength = strlen($comment);
$tokenWithoutComment
= substr($tokens[($tag + 2)]['content'], 0, $tokenLength - $commentLength);
= substr($tokens[($return + 2)]['content'], 0, $tokenLength - $commentLength);

$phpcsFile->fixer->replaceToken(($tag + 2), $tokenWithoutComment . $newComment);
$phpcsFile->fixer->replaceToken(($return + 2), $tokenWithoutComment . $newComment);
}
}

$lastChar = substr($comment, -1);
if ($lastChar !== '.') {
$error = 'Return comment must end with a full stop';
$fix = $phpcsFile->addFixableError($error, ($tag + 2), 'ReturnCommentFullStop');
$fix = $phpcsFile->addFixableError($error, ($return + 2), 'ReturnCommentFullStop');

if ($fix === true) {
$phpcsFile->fixer->addContent(($tag + 2), '.');
$phpcsFile->fixer->addContent(($return + 2), '.');
}
}
}
Expand Down Expand Up @@ -414,6 +415,60 @@ protected function processThrows(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co

}//end processThrows()

/**
* Process any deprecated tags that this function comment has.
*
* @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 processDeprecated(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart)
{
$tokens = $phpcsFile->getTokens();
foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) {
if ($tokens[$tag]['content'] !== '@deprecated') {
continue;
}

$content = $tokens[($tag + 2)]['content'];
if (empty($content) === true || $tokens[($tag + 2)]['code'] !== T_DOC_COMMENT_STRING) {
$error = 'Deprecated type missing for @deprecated tag in function comment';
$phpcsFile->addError($error, $tag, 'MissingDeprecatedType');
} else {
//ONGR Validate that return comment begins with capital letter and ends with full stop.
if ($content !== null) {
$firstChar = $content{0};
if (preg_match('|\p{Lu}|u', $firstChar) === 0) {
$error = 'Deprecated comment must start with a capital letter';
$fix = $phpcsFile->addFixableError($error, ($tag + 2), 'DeprecatedCommentNotCapital');

if ($fix === true) {
$newComment = ucfirst($content);
$tokenLength = strlen($tokens[($tag + 2)]['content']);
$commentLength = strlen($content);
$tokenWithoutComment
= substr($tokens[($tag + 2)]['content'], 0, $tokenLength - $commentLength);

$phpcsFile->fixer->replaceToken(($tag + 2), $tokenWithoutComment . $newComment);
}
}

$lastChar = substr($content, -1);
if ($lastChar !== '.') {
$error = 'Deprecated comment must end with a full stop';
$fix = $phpcsFile->addFixableError($error, ($tag + 2), 'DeprecatedCommentFullStop');

if ($fix === true) {
$phpcsFile->fixer->addContent(($tag + 2), '.');
}
}
}
}
}
}

/**
* Process the function parameter comments.
Expand Down
6 changes: 6 additions & 0 deletions Sniffs/WhiteSpace/OperatorSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
return;
}

//Ongr. If it's correct usage of bool operator skip it.
if ($tokens[$stackPtr]['code'] === T_BOOLEAN_NOT) {
// Skip for '!' case.
return;
}

// Skip default values in function declarations.
if ($tokens[$stackPtr]['code'] === T_EQUAL
|| $tokens[$stackPtr]['code'] === T_MINUS
Expand Down
1 change: 0 additions & 1 deletion Tests/Unit/Arrays/ArrayDeclarationSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ protected function getErrorList()
135 => 1,
137 => 1,
148 => 1,
153 => 1,
];
}

Expand Down
9 changes: 0 additions & 9 deletions Tests/Unit/Arrays/ArrayDeclarationSniffTest.phptest
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,3 @@ $data = [
2 => test(2, 3) ,
];

$data = [
0 => 'foo' .
'bar',
];

$data = [
0 => 'foo' .
'bar',
];
4 changes: 3 additions & 1 deletion Tests/Unit/Commenting/FunctionCommentSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ protected function getErrorList()
89 => 1,
99 => 1,
109 => 2,
117 => 1
117 => 1,
129 => 1,
153 => 2
];
}

Expand Down
36 changes: 36 additions & 0 deletions Tests/Unit/Commenting/FunctionCommentSniffTest.phptest
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,40 @@ class FunctionCommentSniffTest
{
return foo($bar);
}

/**
* Mapping is compared with loaded, if needed updates it and returns true.
*
* @param array $types Types to update.
*
* @return bool Foo
*
* @throws \LogicException
*
* @deprecated Will be removed in 1.0. Please now use Bar#foo().
*/
public function foo(array $types = [])
{
return $this->bar($types);
}

/**
* Short description.
*
* @deprecated Will be removed in 1.0. Please now use Bar#foo().
*/
public function updateMapping()
{
return $this->updateTypes();
}

/**
* Short description.
*
* @deprecated will be removed in 1.0. Please now use Connection#updateTypes()
*/
public function updateMapping()
{
return $this->updateTypes();
}
}
1 change: 1 addition & 0 deletions Tests/Unit/WhiteSpace/OperatorSpacingSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected function getErrorList()
{
return [
12 => 1,
16 => 1,
];
}

Expand Down
12 changes: 12 additions & 0 deletions Tests/Unit/WhiteSpace/OperatorSpacingSniffTest.phptest
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@
if (! $foo) {
true;
}

if (! false) {
true;
}

if (!false) {
true;
}

if (!$foo) {
true;
}

0 comments on commit 1b2a1c7

Please sign in to comment.