Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removed the new MixedLineEndings sniff in favour of doing the check w…

…hile tokenizing each file. Mixed line endings can cause a lot of issues for the tokenizer, so users are now warned when they are found. Also added the ability to set the type of a message in a ruleset.xml file so it can be changed from error to warning or vice versa.
  • Loading branch information...
commit e49952ea53216b3f94a51cac2cf7ac45e13e4443 1 parent c18e898
@gsherwood gsherwood authored
View
9 CodeSniffer.php
@@ -924,6 +924,15 @@ public function populateCustomRules($standard=null)
$this->ruleset[$code]['severity'] = (int) $rule->severity;
}
+ // Custom message type.
+ if (isset($rule->type) === true) {
+ if (isset($this->ruleset[$code]) === false) {
+ $this->ruleset[$code] = array();
+ }
+
+ $this->ruleset[$code]['type'] = (string) $rule->type;
+ }
+
// Custom message.
if (isset($rule->message) === true) {
if (isset($this->ruleset[$code]) === false) {
View
36 CodeSniffer/File.php
@@ -607,6 +607,20 @@ private function _parse($contents=null)
$this->_tokens = self::tokenizeString($contents, $tokenizer, $this->eolChar);
$this->numTokens = count($this->_tokens);
+ // Check for mixed line endings as these can cause tokenizer errors and we
+ // should let the user know that the results they get may be incorrect.
+ // This is done by removing all backslashes, removing the newline char we
+ // detected, then converting newlines chars into text. If any backslashes
+ // are left at the end, we have additional newline chars in use.
+ $contents = str_replace('\\', '', $contents);
+ $contents = str_replace($this->eolChar, '', $contents);
+ $contents = str_replace("\n", '\n', $contents);
+ $contents = str_replace("\r", '\r', $contents);
+ if (preg_match('/(\\\\.)+/', $contents) === 1) {
+ $error = 'File has mixed line endings; this may cause incorrect results';
+ $this->addWarning($error, 0, 'Internal.LineEndings.Mixed');
+ }
+
if (PHP_CODESNIFFER_VERBOSITY > 0) {
if ($this->numTokens === 0) {
$numLines = 0;
@@ -685,7 +699,7 @@ public static function detectLineEndings($file, $contents=null)
* @param int $stackPtr The stack position where the error occured.
* @param string $code A violation code unique to the sniff message.
* @param array $data Replacements for the error message.
- * @param int $severity The severity level for this error. A value of
+ * @param int $severity The severity level for this error. A value of 0
* will be converted into the default severity level.
*
* @return void
@@ -718,6 +732,15 @@ public function addError($error, $stackPtr, $code='', $data=array(), $severity=0
}
}
+ // Make sure this message type has not been set to "warning".
+ if (isset($this->ruleset[$sniff]['type']) === true
+ && $this->ruleset[$sniff]['type'] === 'warning'
+ ) {
+ // Pass this off to the warning handler.
+ $this->addWarning($error, $stackPtr, $code, $data, $severity);
+ return;
+ }
+
// Make sure we are interested in this severity level.
if (isset($this->ruleset[$sniff]['severity']) === true) {
$severity = $this->ruleset[$sniff]['severity'];
@@ -791,7 +814,7 @@ public function addError($error, $stackPtr, $code='', $data=array(), $severity=0
* @param int $stackPtr The stack position where the error occured.
* @param string $code A violation code unique to the sniff message.
* @param array $data Replacements for the warning message.
- * @param int $severity The severity level for this warning. A value of
+ * @param int $severity The severity level for this warning. A value of 0
* will be converted into the default severity level.
*
* @return void
@@ -824,6 +847,15 @@ public function addWarning($warning, $stackPtr, $code='', $data=array(), $severi
}
}
+ // Make sure this message type has not been set to "error".
+ if (isset($this->ruleset[$sniff]['type']) === true
+ && $this->ruleset[$sniff]['type'] === 'error'
+ ) {
+ // Pass this off to the error handler.
+ $this->addError($warning, $stackPtr, $code, $data, $severity);
+ return;
+ }
+
// Make sure we are interested in this severity level.
if (isset($this->ruleset[$sniff]['severity']) === true) {
$severity = $this->ruleset[$sniff]['severity'];
View
132 CodeSniffer/Standards/Generic/Sniffs/Files/MixedLineEndingsSniff.php
@@ -1,132 +0,0 @@
-<?php
-/**
- * Generic_Sniffs_Files_MixedLineEndingsSniff.
- *
- * PHP version 5
- *
- * @category PHP
- * @package PHP_CodeSniffer
- * @author Greg Sherwood <gsherwood@squiz.net>
- * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
- * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
- * @link http://pear.php.net/package/PHP_CodeSniffer
- */
-
-/**
- * Generic_Sniffs_Files_MixedLineEndingsSniff.
- *
- * Checks that the file does not use multiple EOL characters.
- *
- * @category PHP
- * @package PHP_CodeSniffer
- * @author Greg Sherwood <gsherwood@squiz.net>
- * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
- * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
- * @version Release: @package_version@
- * @link http://pear.php.net/package/PHP_CodeSniffer
- */
-class Generic_Sniffs_Files_MixedLineEndingsSniff implements PHP_CodeSniffer_Sniff
-{
-
- /**
- * A list of tokenizers this sniff supports.
- *
- * @var array
- */
- public $supportedTokenizers = array(
- 'PHP',
- 'JS',
- 'CSS',
- );
-
-
- /**
- * Returns an array of tokens this test wants to listen for.
- *
- * @return array
- */
- public function register()
- {
- return array(T_OPEN_TAG);
-
- }//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)
- {
- // We are only interested if this is the first open tag.
- if ($stackPtr !== 0) {
- if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) {
- return;
- }
- }
-
- $filename = $phpcsFile->getFilename();
- $handle = fopen($filename, 'r');
- if ($handle === false) {
- $error = 'Error opening file; could not check line endings';
- $phpcsFile->addError($error, 0, 'OpenFailed');
- return;
- }
-
- $expected = $phpcsFile->eolChar;
- $expected = str_replace("\n", '\n', $expected);
- $expected = str_replace("\r", '\r', $expected);
-
- $line = '';
- $nextLine = '';
- while ($nextLine !== false) {
- if ($line === '') {
- $line = fgets($handle);
- }
-
- // PHP will split the /r and /n of a /r/n over two lines, so we need
- // bring them back together so the EOL character can be correctly
- // determined.
- if ($line[0] === "\n") {
- $line = substr($line, 1);
- }
-
- $nextLine = fgets($handle);
- if ($nextLine !== false && $nextLine[0] === "\n") {
- $line .= "\n";
- }
-
- $found = $phpcsFile->detectLineEndings($filename, $line);
- $found = str_replace("\n", '\n', $found);
- $found = str_replace("\r", '\r', $found);
-
- if ($found !== $expected) {
- $error = 'File contains mixed end of line characters; found "%s" and "%s"';
- $data = array(
- $expected,
- $found,
- );
- $phpcsFile->addError($error, 0, 'Found', $data);
- break;
- }
-
- if ($nextLine === false) {
- break;
- }
-
- $line = $nextLine;
- }//end while
-
- fclose($handle);
-
- }//end process()
-
-
-}//end class
-
-?>
View
12 CodeSniffer/Standards/Generic/Tests/Files/MixedLineEndingsUnitTest.css
@@ -1,12 +0,0 @@
-// unix
-#login-container {
- margin-left: -225px;
-}
-// windows
-#login-container {
- margin-left: -225px;
-}
-// OS X
-#login-container {
- margin-left: -225px;
-}
View
12 CodeSniffer/Standards/Generic/Tests/Files/MixedLineEndingsUnitTest.inc
@@ -1,12 +0,0 @@
-<?php
-// unix
-$foo = 1;
-?>
-<?php
-// windows
-$foo = 1;
-?>
-<?php // OS X $foo = 1; ?> <?php
-// unix
-$foo = 1;
-?>
View
4 CodeSniffer/Standards/Generic/Tests/Files/MixedLineEndingsUnitTest.js
@@ -1,4 +0,0 @@
-// OS X
-var foo = 1
-// windows
-var foo = 1
View
67 CodeSniffer/Standards/Generic/Tests/Files/MixedLineEndingsUnitTest.php
@@ -1,67 +0,0 @@
-<?php
-/**
- * Unit test class for the MixedLineEndings sniff.
- *
- * PHP version 5
- *
- * @category PHP
- * @package PHP_CodeSniffer
- * @author Greg Sherwood <gsherwood@squiz.net>
- * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
- * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
- * @link http://pear.php.net/package/PHP_CodeSniffer
- */
-
-/**
- * Unit test class for the MixedLineEndings sniff.
- *
- * A sniff unit test checks a .inc file for expected violations of a single
- * coding standard. Expected errors and warnings are stored in this class.
- *
- * @category PHP
- * @package PHP_CodeSniffer
- * @author Greg Sherwood <gsherwood@squiz.net>
- * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
- * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
- * @version Release: @package_version@
- * @link http://pear.php.net/package/PHP_CodeSniffer
- */
-class Generic_Tests_Files_MixedLineEndingsUnitTest extends AbstractSniffUnitTest
-{
-
-
- /**
- * Returns the lines where errors should occur.
- *
- * The key of the array should represent the line number and the value
- * should represent the number of errors that should occur on that line.
- *
- * @return array(int => int)
- */
- public function getErrorList()
- {
- return array(
- 1 => 1,
- );
-
- }//end getErrorList()
-
-
- /**
- * Returns the lines where warnings should occur.
- *
- * 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.
- *
- * @return array(int => int)
- */
- public function getWarningList()
- {
- return array();
-
- }//end getWarningList()
-
-
-}//end class
-
-?>
View
6 package.xml
@@ -29,10 +29,14 @@ http://pear.php.net/dtd/package-2.0.xsd">
- All ignore patterns have been reverted to being checked against the absolute path of a file
-- Patterns can be specified to be relative in a rulset.xml file, but nowhere else
-- e.g., [exclude-pattern type="relative"]^tests/*[/exclude-pattern] (with angle brackets, not square brackets)
+ - Messages can now be changed from errors to warnings (and vice versa) inside ruleset.xml files
+ -- As you would with "message" and "severity", specify a "type" tag under a "rule" tag and set the value to "error" or "warning"
+ - PHP_CodeSniffer will now generate a warning on files that it detects have mixed line endings
+ -- This warning has the code Internal.LineEndings.Mixed and can be overriden in a ruleset.xml file
+ -- Thanks to Vit Brunner for help with this
- Sniffs inside PHP 5.3 namespaces are now supported, along with the existing underscore-style emulated namespaces
-- For example: namespace MyStandard\Sniffs\Arrays; class ArrayDeclarationSniff implements \PHP_CodeSniffer_Sniff { ...
-- Thanks to Till Klampaeckel for the patch
- - Added Generic MixedLineEndingsSniff to ensure files do not use more than one type of EOL character
- Added Generic DisallowSpaceIndent to check that files are indented using tabs
- Added Generic OneClassPerFileSniff to check that only one class is defined in each file
-- Thanks to Andy Grunwald for the contribution
Please sign in to comment.
Something went wrong with that request. Please try again.