Skip to content

Fix usage of --standard parameter and make file extension behavior work like documented. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
5cfa1f2
Fix usage of --standard parameter and make file extension behavior wo…
das-peter Feb 3, 2012
8d3547e
Add sniff for FIXME comments as errors to TodoSniff.
illusori Dec 21, 2011
0d89065
Show sniff sources in emacs report type
dominics Dec 29, 2011
8b3b635
Fixed equal sign identation
gsherwood Jan 9, 2012
18d4204
Updated changelog
gsherwood Jan 9, 2012
ed4521e
Fixed bug #19188 : Lots of PHP Notices when analyzing the Symfony fra…
gsherwood Jan 9, 2012
adcb668
CodeSniffer/Standards/Generic/Sniffs/PHP/LowerCaseConstantSniff.php
gsherwood Jan 9, 2012
911edde
Revert "Add sniff for FIXME comments as errors to TodoSniff."
illusori Jan 9, 2012
a2c957c
Add FixmeSniff for FIXME comments, based on TodoSniff.
illusori Dec 21, 2011
d81a376
Missed a rogue TODO in my cut-n-paste.
illusori Jan 9, 2012
b6acb70
Allow for changing maxPercentage in CommentedOutCode using the xml co…
edorian Jan 10, 2012
610b307
Updated changelog
gsherwood Jan 10, 2012
402fdb6
A couple of minor coding standard and var name changes
gsherwood Jan 10, 2012
6613a7d
Updated changelog
gsherwood Jan 10, 2012
3fe6f86
remove execution bit from files
hashar Jan 18, 2012
618f2f3
Do not generate empty <file> elements.
sebastianbergmann Jan 23, 2012
85e21d1
Added a README section about contributing and the commands you can ru…
gsherwood Jan 25, 2012
274c832
Fixed bug #19256 : T_DOC_COMMENT in CSS files breaks ClassDefinitionN…
gsherwood Jan 30, 2012
a89b170
Fixed bug #19264 : Squiz.PHP.NonExecutableCode does not handle RETURN…
gsherwood Feb 3, 2012
7d338f7
Fixed bug #19270 : DuplicateClassName does not handle namespaces corr…
gsherwood Feb 8, 2012
b69bba3
Fixed bug #19283 : CSS @media rules cause false positives
gsherwood Feb 9, 2012
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
21 changes: 17 additions & 4 deletions CodeSniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ public static function autoload($className)
} else if (is_file(dirname(__FILE__).'/CodeSniffer/Standards/'.$path) === true) {
// Check for included sniffs.
include dirname(__FILE__).'/CodeSniffer/Standards/'.$path;
} else if (!empty($GLOBALS['phpcs'])
&& ($values = $GLOBALS['phpcs']->getCommandLineValues())
&& !empty($values['standard'])
&& is_file(dirname($values['standard']).'/'.$path) === true
) {
// Check if this is a CLI run and the standard dir parameter is set.
include dirname($values['standard']).'/'.$path;
} else {
// Everything else.
@include $path;
Expand Down Expand Up @@ -796,6 +803,11 @@ private function _expandRulesetReference($sniff)

$path = $parts[0].'/Sniffs/'.$parts[1].'/'.$parts[2].'Sniff.php';
$path = realpath(dirname(__FILE__).'/CodeSniffer/Standards/'.$path);

// Check if the standardDir parameter is set.
if (!$path && !empty($this->standardDir)) {
$path = realpath($this->standardDir.'/Sniffs/'.$parts[1].'/'.$parts[2].'Sniff.php');
}
}
}//end if

Expand Down Expand Up @@ -1019,7 +1031,7 @@ public function getFilesToProcess($paths, $local=false)
$files[] = $file->getPathname();
}//end foreach
} else {
if ($this->shouldIgnoreFile($path) === true) {
if ($this->shouldProcessFile($path, false) === false) {
continue;
}

Expand All @@ -1037,11 +1049,12 @@ public function getFilesToProcess($paths, $local=false)
*
* Checks both file extension filters and path ignore filters.
*
* @param string $path The path to the file being checked.
* @param string $path The path to the file being checked.
* @param boolean $checkExtension Whether the extension shall be checked or not.
*
* @return bool
*/
public function shouldProcessFile($path)
public function shouldProcessFile($path, $checkExtension = true)
{
// Check that the file's extension is one we are checking.
// We are strict about checking the extension and we don't
Expand All @@ -1062,7 +1075,7 @@ public function shouldProcessFile($path)
}

$matches = array_intersect_key($extensions, $this->allowedFileExtensions);
if (empty($matches) === true) {
if ($checkExtension === true && empty($matches) === true) {
return false;
}

Expand Down
Empty file modified CodeSniffer/MultiFileSniff.php
100755 → 100644
Empty file.
8 changes: 6 additions & 2 deletions CodeSniffer/Reports/Checkstyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public function generate(

$errorsShown = 0;
foreach ($report['files'] as $filename => $file) {
echo ' <file name="'.$filename.'">'.PHP_EOL;
if (count($file['messages'])) {
echo ' <file name="'.$filename.'">'.PHP_EOL;
}

foreach ($file['messages'] as $line => $lineErrors) {
foreach ($lineErrors as $column => $colErrors) {
Expand All @@ -77,7 +79,9 @@ public function generate(
}
}//end foreach

echo ' </file>'.PHP_EOL;
if (count($file['messages'])) {
echo ' </file>'.PHP_EOL;
}
}//end foreach

echo '</checkstyle>'.PHP_EOL;
Expand Down
6 changes: 5 additions & 1 deletion CodeSniffer/Reports/Emacs.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public function generate(
foreach ($lineErrors as $column => $colErrors) {
foreach ($colErrors as $error) {
$message = $error['message'];
$type = strtolower($error['type']);
if ($showSources === true) {
$message .= ' ('.$error['source'].')';
}

$type = strtolower($error['type']);
echo $filename.':'.$line.':'.$column.': '.$type.' - '.$message.PHP_EOL;
$errorsShown++;
}
Expand Down
Empty file modified CodeSniffer/Standards/Generic/Docs/Files/LineLengthStandard.xml
100755 → 100644
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
60 changes: 39 additions & 21 deletions CodeSniffer/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,49 @@ public function process(array $files)
foreach ($files as $phpcsFile) {
$tokens = $phpcsFile->getTokens();

$stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), 0);
$namespace = '';
$stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_NAMESPACE), 0);
while ($stackPtr !== false) {
$nameToken = $phpcsFile->findNext(T_STRING, $stackPtr);
$name = $tokens[$nameToken]['content'];
$compareName = strtolower($name);
if (isset($foundClasses[$compareName]) === true) {
$type = strtolower($tokens[$stackPtr]['content']);
$file = $foundClasses[$compareName]['file'];
$line = $foundClasses[$compareName]['line'];
$error = 'Duplicate %s name "%s" found; first defined in %s on line %s';
$data = array(
$type,
$name,
$file,
$line,
);
$phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
// Keep track of what namespace we are in.
if ($tokens[$stackPtr]['code'] === T_NAMESPACE) {
$nsEnd = $phpcsFile->findNext(
array(T_NS_SEPARATOR, T_STRING, T_WHITESPACE),
($stackPtr + 1),
null,
true
);

$namespace = trim($phpcsFile->getTokensAsString(($stackPtr + 1), ($nsEnd - $stackPtr - 1)));
$stackPtr = $nsEnd;
} else {
$foundClasses[$compareName] = array(
'file' => $phpcsFile->getFilename(),
'line' => $tokens[$stackPtr]['line'],
);
$nameToken = $phpcsFile->findNext(T_STRING, $stackPtr);
$name = $tokens[$nameToken]['content'];
if ($namespace !== '') {
$name = $namespace.'\\'.$name;
}

$compareName = strtolower($name);
if (isset($foundClasses[$compareName]) === true) {
$type = strtolower($tokens[$stackPtr]['content']);
$file = $foundClasses[$compareName]['file'];
$line = $foundClasses[$compareName]['line'];
$error = 'Duplicate %s name "%s" found; first defined in %s on line %s';
$data = array(
$type,
$name,
$file,
$line,
);
$phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
} else {
$foundClasses[$compareName] = array(
'file' => $phpcsFile->getFilename(),
'line' => $tokens[$stackPtr]['line'],
);
}
}

$stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), ($stackPtr + 1));
$stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_NAMESPACE), ($stackPtr + 1));
}//end while

}//end foreach
Expand Down
92 changes: 92 additions & 0 deletions CodeSniffer/Standards/Generic/Sniffs/Commenting/FixmeSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Generic_Sniffs_Commenting_FixmeSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Sam Graham <php-codesniffer BLAHBLAH illusori.co.uk>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

/**
* Generic_Sniffs_Commenting_FixmeSniff.
*
* Warns about FIXME comments.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Sam Graham <php-codesniffer BLAHBLAH illusori.co.uk>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: @package_version@
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Generic_Sniffs_Commenting_FixmeSniff implements PHP_CodeSniffer_Sniff
{

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


/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return PHP_CodeSniffer_Tokens::$commentTokens;

}//end register()


/**
* Processes this sniff, 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();

$content = $tokens[$stackPtr]['content'];
$matches = array();
if (preg_match('|[^a-z]+fixme[^a-z]+(.*)|i', $content, $matches) !== 0) {
// Clear whitespace and some common characters not required at
// the end of a fixme message to make the error more informative.
$type = 'CommentFound';
$fixmeMessage = trim($matches[1]);
$fixmeMessage = trim($fixmeMessage, '[]().');
$error = 'Comment refers to a FIXME task';
$data = array($fixmeMessage);
if ($fixmeMessage !== '') {
$type = 'TaskFound';
$error .= ' "%s"';
}

$phpcsFile->addError($error, $stackPtr, $type, $data);
}

}//end process()


}//end class

?>
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

// Is this a member var name?
$prevPtr = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
if ($tokens[$prevPtr]['code'] === T_OBJECT_OPERATOR) {
return;
}

$keyword = $tokens[$stackPtr]['content'];
if (strtolower($keyword) !== $keyword) {
$error = 'TRUE, FALSE and NULL must be lowercase; expected "%s" but found "%s"';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

// Is this a member var name?
$prevPtr = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
if ($tokens[$prevPtr]['code'] === T_OBJECT_OPERATOR) {
return;
}

$keyword = $tokens[$stackPtr]['content'];
if (strtoupper($keyword) !== $keyword) {
$error = 'TRUE, FALSE and NULL must be uppercase; expected "%s" but found "%s"';
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace A\B {
class MyClass {}
interface MyInterface {}
}

namespace D {
class MyClass {}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
namespace C;
class MyClass {}
interface MyInterface {}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace C;
class MyClass {}
interface YourInterface {}

namespace D;
class MyClass {}
?>
8 changes: 8 additions & 0 deletions CodeSniffer/Standards/Generic/Tests/Classes/DuplicateClassNameUnitTest.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function getErrorList()
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array(int => int)
*/
public function getWarningList($testFile='')
Expand All @@ -68,6 +70,12 @@ public function getWarningList($testFile='')
3 => 1,
);
break;
case 'DuplicateClassNameUnitTest.5.inc':
return array(
3 => 1,
7 => 1,
);
break;
default:
return array();
break;
Expand Down
22 changes: 22 additions & 0 deletions CodeSniffer/Standards/Generic/Tests/Commenting/FixmeUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* FIXME: really need to fix this before I commit.
*
*/

// FIXME: this is broken, don't forget to fix it
error_log('test');

// FIXME remove this.
Debug::bam('test');

// fixme - fix this.

// Extract info from the array.
// FIXME: this is just a stub, fill in later

// Extract info from the array (fixme: finish later)
// To do this, use a function!
// nofixme! NOFIXME! NOfixme!
//FIXME.
?>
22 changes: 22 additions & 0 deletions CodeSniffer/Standards/Generic/Tests/Commenting/FixmeUnitTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* FIXME: Write this comment
*
*/

// FIXME: remove this.
alert('test');

// FIXME remove this.
alert('test');

// fixme - remove this.

// Extract info from the array.
// FIXME: can this be done faster?

// Extract info from the array (fixme: make it faster)
// To do this, use a function!
// nofixme! NOFIXME! NOfixme!
//FIXME.
?>
Loading