Skip to content

Commit

Permalink
cs, fixes, more tests, 2 more fixable
Browse files Browse the repository at this point in the history
  • Loading branch information
vess committed Sep 5, 2015
1 parent 76df1ae commit b3558ca
Show file tree
Hide file tree
Showing 35 changed files with 1,575 additions and 64 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ sudo: false
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
Expand Down
20 changes: 17 additions & 3 deletions Joomla/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,23 @@ protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
// Joomla change: There must be 3 spaces after the @param tag to make it line up with the @return tag
if ($param['align_space'] !== ' ')
{
$error = 'Expected 3 spaces before variable type, found %s';
$data = array(strlen($param['align_space']));
$phpcsFile->addError($error, $param['tag'], 'BeforeParamType', $data);
$error = 'Expected 3 spaces before variable type, found %s';
$spaces = strlen($param['align_space']);
$data = array($spaces);
$fix = $phpcsFile->addFixableError($error, $param['tag'], 'BeforeParamType', $data);

if ($fix === true)
{
$phpcsFile->fixer->beginChangeset();

for ($i = 0; $i < strlen($param['align_space']); $i++)
{
$phpcsFile->fixer->replaceToken(($param['tag'] + 1), '');
}

$phpcsFile->fixer->addContent($param['tag'], ' ');
$phpcsFile->fixer->endChangeset();
}
}

// Make sure the param name is correct.
Expand Down
6 changes: 3 additions & 3 deletions Joomla/Sniffs/Commenting/SingleCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$data = array($comment{2});
$padding = (strlen($tokens[$stackPtr]['content']) - strlen($comment));
$padding = str_repeat(' ', $padding - 1);
$newComment = ltrim($tokens[$stackPtr]['content'], '* ');
$newComment = $padding . '* ' . ucfirst($newComment);
$padding = str_repeat("\t", $padding - 2);
$newComment = ltrim($comment, '* ');
$newComment = $padding . ' * ' . ucfirst($newComment) . $phpcsFile->eolChar;
}

// Check for a comment on the previous line.
Expand Down
18 changes: 9 additions & 9 deletions Joomla/Sniffs/ControlStructures/ControlSignatureSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class Joomla_Sniffs_ControlStructures_ControlSignatureSniff implements PHP_CodeS
*
* @var array
*/
public $supportedTokenizers = [
public $supportedTokenizers = array(
'PHP',
'JS',
];
);

/**
* Returns an array of tokens this test wants to listen for.
Expand All @@ -44,7 +44,7 @@ class Joomla_Sniffs_ControlStructures_ControlSignatureSniff implements PHP_CodeS
*/
public function register()
{
return [
return array(
T_TRY,
T_CATCH,
T_FINALLY,
Expand All @@ -56,7 +56,7 @@ public function register()
T_ELSE,
T_ELSEIF,
T_SWITCH,
];
);
}

/**
Expand Down Expand Up @@ -103,10 +103,10 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
)
{
$error = 'Expected 1 space after %s keyword; %s found';
$data = [
$data = array(
strtoupper($tokens[$stackPtr]['content']),
$found,
];
);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterKeyword', $data);

if ($fix === true)
Expand Down Expand Up @@ -155,7 +155,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($found !== 1)
{
$error = 'Expected 1 newline after opening brace; %s found';
$data = [$found];
$data = array($found);
$fix = $phpcsFile->addFixableError($error, $opener, 'NewlineAfterOpenBrace', $data);

if ($fix === true)
Expand Down Expand Up @@ -198,7 +198,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($found !== 0)
{
$error = 'Expected 0 spaces before semicolon; %s found';
$data = [$found];
$data = array($found);
$fix = $phpcsFile->addFixableError($error, $closer, 'SpaceBeforeSemicolon', $data);

if ($fix === true)
Expand Down Expand Up @@ -248,7 +248,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if (0 !== $found)
{
$error = 'Expected 0 space after closing brace; %s found';
$data = [$found];
$data = array($found);
$fix = $phpcsFile->addFixableError($error, $closer, 'SpaceAfterCloseBrace', $data);

if (true === $fix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Joomla_Sniffs_ControlStructures_ControlStructuresBracketsSniff implements
*/
public function register()
{
return [
return array(
T_IF,
T_ELSEIF,
T_ELSE,
Expand All @@ -43,7 +43,7 @@ public function register()
T_TRY,
T_CATCH,
T_FINALLY,
];
);
}


Expand All @@ -58,7 +58,7 @@ public function register()
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$errorData = [strtolower($tokens[$stackPtr]['content'])];
$errorData = array(strtolower($tokens[$stackPtr]['content']));

if (false === isset($tokens[$stackPtr]['scope_opener']))
{
Expand Down Expand Up @@ -105,11 +105,11 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($braceLine > ($classLine + 1))
{
$error = 'Opening brace of a %s must be on the line following the %s declaration.; Found %s line(s).';
$data = [
$data = array(
$tokens[$stackPtr]['content'],
$tokens[$stackPtr]['content'],
($braceLine - $classLine - 1),
];
);
$fix = $phpcsFile->addFixableError($error, $curlyBrace, 'OpenBraceWrongLine', $data);

if (true === $fix)
Expand Down Expand Up @@ -163,10 +163,10 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($spaces !== $expected)
{
$error = 'Expected %s spaces before opening brace; %s found';
$data = [
$data = array(
$expected,
$spaces,
];
);
$fix = $phpcsFile->addFixableError($error, $curlyBrace, 'SpaceBeforeBrace', $data);

if ($fix === true)
Expand Down
8 changes: 4 additions & 4 deletions Joomla/Sniffs/ControlStructures/WhiteSpaceBeforeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Joomla_Sniffs_ControlStructures_WhiteSpaceBeforeSniff implements PHP_CodeS
*/
public function register()
{
return [
return array(
T_IF,
T_FOR,
T_FOREACH,
Expand All @@ -72,7 +72,7 @@ public function register()
T_WHILE,
T_DO,
T_RETURN,
];
);
}

/**
Expand All @@ -92,12 +92,12 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
return;
}

$prev = $phpcsFile->findPrevious([T_SEMICOLON, T_CLOSE_CURLY_BRACKET], ($stackPtr - 1), null, false);
$prev = $phpcsFile->findPrevious(array(T_SEMICOLON, T_CLOSE_CURLY_BRACKET), ($stackPtr - 1), null, false);

if ($tokens[$stackPtr]['line'] - 1 === $tokens[$prev]['line'])
{
$error = 'Please consider an empty line before the %s statement;';
$data = [$tokens[$stackPtr]['content']];
$data = array($tokens[$stackPtr]['content']);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBefore', $data);

if ($fix === true)
Expand Down
29 changes: 26 additions & 3 deletions Joomla/Sniffs/Functions/StatementNotFunctionSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,38 @@ public function register()
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

$tokens = $phpcsFile->getTokens();
$nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);

if ($tokens[$nextToken]['code'] === T_OPEN_PARENTHESIS)
{
$error = '"%s" is a statement not a function; no parentheses are required';
$data = array($tokens[$stackPtr]['content']);
$phpcsFile->addError($error, $stackPtr, 'BracketsNotRequired', $data);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'BracketsNotRequired', $data);

if ($fix === true)
{
$end = $phpcsFile->findEndOfStatement($nextToken);
$ignore = PHP_CodeSniffer_Tokens::$emptyTokens;
$ignore[] = T_SEMICOLON;
$closer = $phpcsFile->findPrevious($ignore, ($end - 1), null, true);

$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($nextToken, '');

if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE)
{
$phpcsFile->fixer->replaceToken(($stackPtr + 1), '');
}

if ($tokens[$closer]['code'] === T_CLOSE_PARENTHESIS)
{
$phpcsFile->fixer->replaceToken($closer, '');
}

$phpcsFile->fixer->addContent($stackPtr, ' ');
$phpcsFile->fixer->endChangeset();
}
}
}
}
66 changes: 66 additions & 0 deletions Joomla/Sniffs/Operators/ValidLogicalOperatorsSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Joomla! Coding Standard
*
* @copyright Copyright (C) 2015 Open Source Matters, Inc. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
*/

/**
* Joomla_Sniffs_Operators_ValidLogicalOperatorsSniff
*
* Check to ensure that the logical operators 'and' and 'or' are not used.
* Use the && and || operators instead.
*
* @since 1.0
*/
class Joomla_Sniffs_Operators_ValidLogicalOperatorsSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_LOGICAL_AND,
T_LOGICAL_OR,
);
}

/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The current 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();
$operators = array(
'and' => '&&',
'or' => '||',
);
$operator = strtolower($tokens[$stackPtr]['content']);

if (false === isset($operators[$operator]))
{
return;
}

$error = 'Logical operator "%s" not allowed; use "%s" instead';
$data = array(
$operator,
$operators[$operator],
);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotAllowed', $data);

if (true === $fix)
{
$phpcsFile->fixer->replaceToken($stackPtr, $operators[$operator]);
}
}
}
6 changes: 3 additions & 3 deletions Joomla/Sniffs/WhiteSpace/MemberVarSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($foundLines > 0)
{
$error = 'Expected 0 blank lines after member var comment; %s found';
$data = [$foundLines];
$data = array($foundLines);
$fix = $phpcsFile->addFixableError($error, $prev, 'AfterComment', $data);

if ($fix === true)
Expand Down Expand Up @@ -102,7 +102,7 @@ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($foundLines > 0 && $tokens[$prev]['code'] === T_OPEN_CURLY_BRACKET)
{
$error = 'Expected 0 blank lines before first member var; %s found';
$data = [$foundLines];
$data = array($foundLines);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'FirstMember', $data);

if ($fix === true)
Expand Down Expand Up @@ -136,7 +136,7 @@ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}

$error = 'Expected 1 blank line before member var; %s found';
$data = [$foundLines];
$data = array($foundLines);
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data);

if ($fix === true)
Expand Down
6 changes: 3 additions & 3 deletions Joomla/Tests/Classes/InstantiateNewClassesUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class Joomla_Tests_Classes_InstantiateNewClassesUnitTest extends AbstractSniffUn
*/
public function getErrorList()
{
return [
return array(
16 => 1,
22 => 1,
24 => 1,
];
);
}

/**
Expand All @@ -40,6 +40,6 @@ public function getErrorList()
*/
public function getWarningList()
{
return [];
return array();
}
}
16 changes: 16 additions & 0 deletions Joomla/Tests/Commenting/ClassCommentUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Missing class doc comment.
*/
class MyClass
{
/**
* Some function...
*
* @return integer
*/
public function myFunction()
{
return 2;
}
}
Loading

0 comments on commit b3558ca

Please sign in to comment.