diff --git a/CodeSniffer/CommentParser/AbstractDocElement.php b/CodeSniffer/CommentParser/AbstractDocElement.php index 0725f9e0ba..2bc6ca4e52 100644 --- a/CodeSniffer/CommentParser/AbstractDocElement.php +++ b/CodeSniffer/CommentParser/AbstractDocElement.php @@ -90,6 +90,13 @@ abstract class PHP_CodeSniffer_CommentParser_AbstractDocElement implements PHP_C */ protected $tokens = array(); + /** + * The file this element is in. + * + * @var array(string) + */ + protected $phpcsFile = null; + /** * The tag that this element represents (omiting the @ symbol). * @@ -105,16 +112,19 @@ abstract class PHP_CodeSniffer_CommentParser_AbstractDocElement implements PHP_C * @param array $tokens The tokens of this element. * @param string $tag The doc element tag this element * represents. + * @param PHP_CodeSniffer_File $phpcsFile The file that this element is in. * * @throws Exception If $previousElement in not a DocElement or if * getSubElements() does not return an array. */ - public function __construct($previousElement, array $tokens, $tag) + public function __construct($previousElement, array $tokens, $tag, PHP_CodeSniffer_File $phpcsFile) { if ($previousElement !== null && ($previousElement instanceof PHP_CodeSniffer_CommentParser_DocElement) === false) { throw new Exception('$previousElement must be an instance of DocElement'); } + $this->phpcsFile = $phpcsFile; + $this->previousElement = $previousElement; if ($previousElement !== null) { $this->previousElement->nextElement = $this; @@ -271,7 +281,7 @@ public function getLine() $previousContent = $this->previousElement->getRawContent(); $previousLine = $this->previousElement->getLine(); - return ($previousLine + substr_count($previousContent, "\n")); + return ($previousLine + substr_count($previousContent, $this->phpcsFile->eolChar)); } }//end getLine() diff --git a/CodeSniffer/CommentParser/AbstractParser.php b/CodeSniffer/CommentParser/AbstractParser.php index 91d1799ade..9d19d5bb1a 100644 --- a/CodeSniffer/CommentParser/AbstractParser.php +++ b/CodeSniffer/CommentParser/AbstractParser.php @@ -66,6 +66,20 @@ abstract class PHP_CodeSniffer_CommentParser_AbstractParser */ protected $comment = null; + /** + * The string content of the comment. + * + * @var string + */ + protected $commentString = ''; + + /** + * The file that the comment exists in. + * + * @var PHP_CodeSniffer_File + */ + protected $phpcsFile = null; + /** * The word tokens that appear in the comment. * @@ -151,11 +165,13 @@ abstract class PHP_CodeSniffer_CommentParser_AbstractParser /** * Constructs a Doc Comment Parser. * - * @param string $comment The comment to parse. + * @param string $comment The comment to parse. + * @param PHP_CodeSniffer_File $phpcsFile The file that this comment is in. */ - public function __construct($comment) + public function __construct($comment, PHP_CodeSniffer_File $phpcsFile) { - $this->_comment = $comment; + $this->commentString = $comment; + $this->phpcsFile = $phpcsFile; }//end __construct() @@ -171,7 +187,7 @@ public function __construct($comment) public function parse() { if ($this->_hasParsed === false) { - $this->_parse($this->_comment); + $this->_parse($this->commentString); } }//end parse() @@ -188,7 +204,7 @@ public function parse() private function _parse($comment) { // Firstly, remove the comment tags and any stars from the left side. - $lines = split("\n", $comment); + $lines = split($this->phpcsFile->eolChar, $comment); foreach ($lines as &$line) { if ($line !== '') { $line = trim($line); @@ -205,7 +221,7 @@ private function _parse($comment) // might be interested in the spaces between words, so tokenize // spaces as well as separate tokens. $flags = (PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); - $words = preg_split('|(\s+)|', $line."\n", -1, $flags); + $words = preg_split('|(\s+)|', $line.$this->phpcsFile->eolChar, -1, $flags); $this->words = array_merge($this->words, $words); } } @@ -248,7 +264,7 @@ private function _parseWords() continue; } - if (isset($this->words[($wordPos - 2)]) === false || $this->words[($wordPos - 2)] !== "\n") { + if (isset($this->words[($wordPos - 2)]) === false || $this->words[($wordPos - 2)] !== $this->phpcsFile->eolChar) { continue; } @@ -326,7 +342,7 @@ protected function getLine($tokenPos) { $newlines = 0; for ($i = 0; $i < $tokenPos; $i++) { - $newlines += substr_count("\n", $this->words[$i]); + $newlines += substr_count($this->phpcsFile->eolChar, $this->words[$i]); } return $newlines; @@ -343,7 +359,7 @@ protected function getLine($tokenPos) */ protected function parseSee($tokens) { - $see = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'see'); + $see = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'see', $this->phpcsFile); $this->sees[] = $see; return $see; @@ -359,7 +375,7 @@ protected function parseSee($tokens) */ protected function parseComment($tokens) { - $this->comment = new PHP_CodeSniffer_CommentParser_CommentElement($this->previousElement, $tokens); + $this->comment = new PHP_CodeSniffer_CommentParser_CommentElement($this->previousElement, $tokens, $this->phpcsFile); return $this->comment; }//end parseComment() @@ -374,7 +390,7 @@ protected function parseComment($tokens) */ protected function parseDeprecated($tokens) { - $this->deprecated = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'deprecated'); + $this->deprecated = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'deprecated', $this->phpcsFile); return $this->deprecated; }//end parseDeprecated() @@ -389,7 +405,7 @@ protected function parseDeprecated($tokens) */ protected function parseSince($tokens) { - $this->since = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'since'); + $this->since = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'since', $this->phpcsFile); return $this->since; }//end parseSince() @@ -404,7 +420,7 @@ protected function parseSince($tokens) */ protected function parseLink($tokens) { - $link = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'link'); + $link = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'link', $this->phpcsFile); $this->links[] = $link; return $link; @@ -505,7 +521,7 @@ protected function parseTag($tag, $start, $end) $this->previousElement = $this->$method($tokens); } else { - $this->previousElement = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, $tag); + $this->previousElement = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, $tag, $this->phpcsFile); } $this->orders[] = $tag; diff --git a/CodeSniffer/CommentParser/ClassCommentParser.php b/CodeSniffer/CommentParser/ClassCommentParser.php index 68ed4fe750..3935cff26a 100644 --- a/CodeSniffer/CommentParser/ClassCommentParser.php +++ b/CodeSniffer/CommentParser/ClassCommentParser.php @@ -110,7 +110,7 @@ protected function getAllowedTags() */ protected function parseLicense($tokens) { - $this->_license = new PHP_CodeSniffer_CommentParser_PairElement($this->previousElement, $tokens, 'license'); + $this->_license = new PHP_CodeSniffer_CommentParser_PairElement($this->previousElement, $tokens, 'license', $this->phpcsFile); return $this->_license; }//end parseLicense() @@ -125,7 +125,7 @@ protected function parseLicense($tokens) */ protected function parseCopyright($tokens) { - $this->_copyright = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'copyright'); + $this->_copyright = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'copyright', $this->phpcsFile); return $this->_copyright; }//end parseCopyright() @@ -140,7 +140,7 @@ protected function parseCopyright($tokens) */ protected function parseCategory($tokens) { - $this->_category = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'category'); + $this->_category = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'category', $this->phpcsFile); return $this->_category; }//end parseCategory() @@ -155,7 +155,7 @@ protected function parseCategory($tokens) */ protected function parseAuthor($tokens) { - $author = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'author'); + $author = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'author', $this->phpcsFile); $this->_authors[] = $author; return $author; @@ -171,7 +171,7 @@ protected function parseAuthor($tokens) */ protected function parseVersion($tokens) { - $this->_version = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'version'); + $this->_version = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'version', $this->phpcsFile); return $this->_version; }//end parseVersion() @@ -186,7 +186,7 @@ protected function parseVersion($tokens) */ protected function parsePackage($tokens) { - $this->_package = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'package'); + $this->_package = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'package', $this->phpcsFile); return $this->_package; }//end parsePackage() @@ -201,7 +201,7 @@ protected function parsePackage($tokens) */ protected function parseSubpackage($tokens) { - $this->_subpackage = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'subpackage'); + $this->_subpackage = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'subpackage', $this->phpcsFile); return $this->_subpackage; }//end parseSubpackage() diff --git a/CodeSniffer/CommentParser/CommentElement.php b/CodeSniffer/CommentParser/CommentElement.php index a6b6822fd2..8b85e7bcfa 100644 --- a/CodeSniffer/CommentParser/CommentElement.php +++ b/CodeSniffer/CommentParser/CommentElement.php @@ -54,10 +54,11 @@ class PHP_CodeSniffer_CommentParser_CommentElement extends PHP_CodeSniffer_Comme * element. * @param array $tokens The tokens that * make up this element. + * @param PHP_CodeSniffer_File $phpcsFile The file that this element is in. */ - public function __construct($previousElement, $tokens) + public function __construct($previousElement, $tokens, PHP_CodeSniffer_File $phpcsFile) { - parent::__construct($previousElement, $tokens, 'comment'); + parent::__construct($previousElement, $tokens, 'comment', $phpcsFile); }//end __construct() @@ -96,13 +97,13 @@ private function _getShortCommentEndPos() foreach ($this->tokens as $pos => $token) { $token = str_replace($whiteSpace, '', $token); - if ($token === "\n") { + if ($token === $this->phpcsFile->eolChar) { if ($found === false) { // Include newlines before short description. continue; } else { if (isset($this->tokens[($pos + 1)]) === true) { - if ($this->tokens[($pos + 1)] === "\n") { + if ($this->tokens[($pos + 1)] === $this->phpcsFile->eolChar) { return ($pos - 1); } } else { @@ -197,11 +198,11 @@ public function getNewlineAfter() { $long = $this->getLongComment(); if ($long !== '') { - return strspn((strrev(rtrim($long, ' '))), "\n"); + return strspn((strrev(rtrim($long, ' '))), $this->phpcsFile->eolChar); } else { $endShort = ($this->_getShortCommentEndPos() + 1); $after = implode('', array_slice($this->tokens, $endShort)); - return strspn((trim($after, ' ')), "\n"); + return strspn((trim($after, ' ')), $this->phpcsFile->eolChar); } }//end getNewlineAfter() diff --git a/CodeSniffer/CommentParser/FunctionCommentParser.php b/CodeSniffer/CommentParser/FunctionCommentParser.php index 5a00c4113c..cf972c0c0d 100644 --- a/CodeSniffer/CommentParser/FunctionCommentParser.php +++ b/CodeSniffer/CommentParser/FunctionCommentParser.php @@ -59,11 +59,12 @@ class PHP_CodeSniffer_CommentParser_FunctionCommentParser extends PHP_CodeSniffe /** * Constructs a PHP_CodeSniffer_CommentParser_FunctionCommentParser. * - * @param string $comment The comment to parse. + * @param string $comment The comment to parse. + * @param PHP_CodeSniffer_File $phpcsFile The file that this comment is in. */ - public function __construct($comment) + public function __construct($comment, PHP_CodeSniffer_File $phpcsFile) { - parent::__construct($comment); + parent::__construct($comment, $phpcsFile); }//end __construct() @@ -77,7 +78,7 @@ public function __construct($comment) */ protected function parseParam($tokens) { - $param = new PHP_CodeSniffer_CommentParser_ParameterElement($this->previousElement, $tokens); + $param = new PHP_CodeSniffer_CommentParser_ParameterElement($this->previousElement, $tokens, $this->phpcsFile); $this->_params[] = $param; return $param; @@ -93,7 +94,7 @@ protected function parseParam($tokens) */ protected function parseReturn($tokens) { - $return = new PHP_CodeSniffer_CommentParser_PairElement($this->previousElement, $tokens, 'return'); + $return = new PHP_CodeSniffer_CommentParser_PairElement($this->previousElement, $tokens, 'return', $this->phpcsFile); $this->_return = $return; return $return; @@ -109,7 +110,7 @@ protected function parseReturn($tokens) */ protected function parseThrows($tokens) { - $throws = new PHP_CodeSniffer_CommentParser_PairElement($this->previousElement, $tokens, 'throws'); + $throws = new PHP_CodeSniffer_CommentParser_PairElement($this->previousElement, $tokens, 'throws', $this->phpcsFile); $this->_throws[] = $throws; return $throws; diff --git a/CodeSniffer/CommentParser/MemberCommentParser.php b/CodeSniffer/CommentParser/MemberCommentParser.php index 644ce31a76..1b3d2c90f8 100644 --- a/CodeSniffer/CommentParser/MemberCommentParser.php +++ b/CodeSniffer/CommentParser/MemberCommentParser.php @@ -48,7 +48,7 @@ class PHP_CodeSniffer_CommentParser_MemberCommentParser extends PHP_CodeSniffer_ */ protected function parseVar($tokens) { - $this->_var = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'var'); + $this->_var = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'var', $this->phpcsFile); return $this->_var; }//end parseVar() diff --git a/CodeSniffer/CommentParser/PairElement.php b/CodeSniffer/CommentParser/PairElement.php index 6de575d709..b6f05e48f9 100644 --- a/CodeSniffer/CommentParser/PairElement.php +++ b/CodeSniffer/CommentParser/PairElement.php @@ -69,10 +69,11 @@ class PHP_CodeSniffer_CommentParser_PairElement extends PHP_CodeSniffer_CommentP * @param PHP_CodeSniffer_CommentParser_DocElement $previousElement The element before this one. * @param array $tokens The tokens that comprise this element. * @param string $tag The tag that this element represents. + * @param PHP_CodeSniffer_File $phpcsFile The file that this element is in. */ - public function __construct($previousElement, $tokens, $tag) + public function __construct($previousElement, $tokens, $tag, PHP_CodeSniffer_File $phpcsFile) { - parent::__construct($previousElement, $tokens, $tag); + parent::__construct($previousElement, $tokens, $tag, $phpcsFile); }//end __construct() diff --git a/CodeSniffer/CommentParser/ParameterElement.php b/CodeSniffer/CommentParser/ParameterElement.php index 3c9a39f630..d5e32dd4e2 100644 --- a/CodeSniffer/CommentParser/ParameterElement.php +++ b/CodeSniffer/CommentParser/ParameterElement.php @@ -83,10 +83,13 @@ class PHP_CodeSniffer_CommentParser_ParameterElement extends PHP_CodeSniffer_Com * @param array $tokens The tokens * that make up * this element. + * @param PHP_CodeSniffer_File $phpcsFile The file that + * this element + * is in. */ - public function __construct($previousElement, $tokens) + public function __construct($previousElement, $tokens, PHP_CodeSniffer_File $phpcsFile) { - parent::__construct($previousElement, $tokens, 'param'); + parent::__construct($previousElement, $tokens, 'param', $phpcsFile); // Handle special variable type: array(x => y). $type = strtolower($this->_type); diff --git a/CodeSniffer/CommentParser/SingleElement.php b/CodeSniffer/CommentParser/SingleElement.php index 497e756026..623ccfe1fa 100644 --- a/CodeSniffer/CommentParser/SingleElement.php +++ b/CodeSniffer/CommentParser/SingleElement.php @@ -57,10 +57,11 @@ class PHP_CodeSniffer_CommentParser_SingleElement extends PHP_CodeSniffer_Commen * @param PHP_CodeSniffer_CommentParser_DocElement $previousElement The element before this one. * @param array $tokens The tokens that comprise this element. * @param string $tag The tag that this element represents. + * @param PHP_CodeSniffer_File $phpcsFile The file that this element is in. */ - public function __construct($previousElement, $tokens, $tag) + public function __construct($previousElement, $tokens, $tag, PHP_CodeSniffer_File $phpcsFile) { - parent::__construct($previousElement, $tokens, $tag); + parent::__construct($previousElement, $tokens, $tag, $phpcsFile); }//end __construct() diff --git a/CodeSniffer/DocGenerators/HTML.php b/CodeSniffer/DocGenerators/HTML.php index ee09dd8e13..20dc3df1e1 100644 --- a/CodeSniffer/DocGenerators/HTML.php +++ b/CodeSniffer/DocGenerators/HTML.php @@ -75,10 +75,10 @@ public function generate() protected function printHeader() { $standard = $this->getStandard(); - echo "\n"; - echo " \n"; - echo " $standard Coding Standards\n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo "

$standard Coding Standards

\n"; + '.PHP_EOL; + echo ' '.PHP_EOL; + echo ' '.PHP_EOL; + echo "

$standard Coding Standards

".PHP_EOL; }//end printHeader() @@ -160,18 +160,18 @@ protected function printHeader() */ protected function printToc($standardFiles) { - echo "

Table of Contents

\n"; - echo " '.PHP_EOL; }//end printToc() @@ -189,11 +189,11 @@ protected function printFooter() echo '
'; echo 'Documentation generated on '.date('r'); echo ' by PHP_CodeSniffer @package_version@'; - echo "
\n"; + echo ''.PHP_EOL; error_reporting(E_ALL | E_STRICT); - echo " \n"; - echo "\n"; + echo ' '.PHP_EOL; + echo ''.PHP_EOL; }//end printFooter() @@ -210,8 +210,8 @@ protected function printFooter() public function processSniff(DOMNode $doc) { $title = $this->getTitle($doc); - echo ' \n"; - echo "

$title

\n"; + echo '
'.PHP_EOL; + echo "

$title

".PHP_EOL; foreach ($doc->childNodes as $node) { if ($node->nodeName === 'standard') { @@ -240,7 +240,7 @@ protected function printTextBlock($node) $content = str_replace('<em>', '', $content); $content = str_replace('</em>', '', $content); - echo "

$content

\n"; + echo "

$content

".PHP_EOL; }//end printTextBlock() @@ -270,16 +270,16 @@ protected function printCodeComparisonBlock($node) $second = str_replace('', '', $second); $second = str_replace('', '', $second); - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo " \n"; - echo "
$firstTitle$secondTitle
$first$second
\n"; + echo ' '.PHP_EOL; + echo ' '.PHP_EOL; + echo " ".PHP_EOL; + echo " ".PHP_EOL; + echo ' '.PHP_EOL; + echo ' '.PHP_EOL; + echo " ".PHP_EOL; + echo " ".PHP_EOL; + echo ' '.PHP_EOL; + echo '
$firstTitle$secondTitle
$first$second
'.PHP_EOL; }//end printCodeComparisonBlock() diff --git a/CodeSniffer/File.php b/CodeSniffer/File.php index 67fa48aeb5..c6a6c28b48 100644 --- a/CodeSniffer/File.php +++ b/CodeSniffer/File.php @@ -129,6 +129,13 @@ class PHP_CodeSniffer_File */ private $_file = ''; + /** + * The EOL character this file uses. + * + * @var string + */ + public $eolChar = ''; + /** * The tokens stack map. * @@ -421,7 +428,7 @@ public function start() foreach ($this->_tokens as $stackPtr => $token) { if (PHP_CODESNIFFER_VERBOSITY > 2) { $type = $token['type']; - $content = str_replace("\n", '\n', $token['content']); + $content = str_replace($this->eolChar, '\n', $token['content']); echo "\t\tProcess token $stackPtr: $type => $content".PHP_EOL; } @@ -460,10 +467,30 @@ public function start() * conforms with the tests. * * @return void - * @throws PHP_CodeSniffer_Exception If the file could not be processed. + * @throws PHP_CodeSniffer_Exception If the file could not be opened. */ private function _parse() { + // Determine the newline character being used in this file. + // Will be either \r, \r\n or \n. + $handle = fopen($this->_file, 'r'); + if ($handle === false) { + throw new PHP_CodeSniffer_Exception('File could not be opened; could not auto-detect line endings'); + } else { + $firstLine = fgets($handle); + fclose($handle); + } + + $eolChar = substr($firstLine, -1); + if ($eolChar === "\n") { + $secondLastChar = substr($firstLine, -2, 1); + if ($secondLastChar === "\r") { + $eolChar = "\r\n"; + } + } + + $this->eolChar = $eolChar; + $contents = file_get_contents($this->_file); $tokens = token_get_all($contents); @@ -472,6 +499,24 @@ private function _parse() for ($stackPtr = 0; $stackPtr < $numTokens; $stackPtr++) { $token = $tokens[$stackPtr]; + // If we are using \r\n newline characters, the \r and \n are sometimes + // split over two tokens. This normally occurs after comments. We need + // to merge these two characters together so that our line endings are + // consistent for all lines. + if (is_array($token) === true && substr($token[1], -1) === "\r") { + if (isset($tokens[($stackPtr + 1)]) === true && is_array($tokens[($stackPtr + 1)]) === true && $tokens[($stackPtr + 1)][1][0] === "\n") { + $token[1] .= "\n"; + + if ($tokens[($stackPtr + 1)][1] === "\n") { + // The next token's content has been merged into this token, + // so we can skip it. + $stackPtr++; + } else { + $tokens[($stackPtr + 1)][1] = substr($tokens[($stackPtr + 1)][1], 1); + } + } + }//end if + // If this is a double quoted string, PHP will tokenise the whole // thing which causes problems with the scope map when braces are // within the string. So we need to merge the tokens together to @@ -496,7 +541,7 @@ private function _parse() // Convert each line within the double quoted string to a // new token, so it conforms with other multiple line tokens. - $tokenLines = explode("\n", $tokenContent); + $tokenLines = explode($this->eolChar, $tokenContent); $numLines = count($tokenLines); $newToken = array(); @@ -507,7 +552,7 @@ private function _parse() break; } } else { - $newToken['content'] .= "\n"; + $newToken['content'] .= $this->eolChar; } $newToken['code'] = T_DOUBLE_QUOTED_STRING; @@ -523,8 +568,9 @@ private function _parse() // If this token has newlines in its content, split each line up // and create a new token for each line. We do this so it's easier // to asertain where errors occur on a line. - if (is_array($token) === true && strpos($token[1], "\n") !== false) { - $tokenLines = explode("\n", $token[1]); + // Note that $token[1] is the token's content. + if (is_array($token) === true && strpos($token[1], $this->eolChar) !== false) { + $tokenLines = explode($this->eolChar, $token[1]); $numLines = count($tokenLines); for ($i = 0; $i < $numLines; $i++) { @@ -534,7 +580,7 @@ private function _parse() break; } } else { - $newToken['content'] .= "\n"; + $newToken['content'] .= $this->eolChar; } $newToken['type'] = token_name($token[0]); @@ -715,7 +761,7 @@ private function _createLineMap() for ($i = 0; $i < $count; $i++) { $this->_tokens[$i]['line'] = $lineNumber; - $lineNumber += substr_count($this->_tokens[$i]['content'], "\n"); + $lineNumber += substr_count($this->_tokens[$i]['content'], $this->eolChar); } }//end _createLineMap() @@ -844,7 +890,7 @@ private function _createScopeMap() if (isset(self::$_scopeOpeners[$this->_tokens[$i]['code']]) === true) { if (PHP_CODESNIFFER_VERBOSITY > 1) { $type = $this->_tokens[$i]['type']; - $content = str_replace("\n", '\n', $this->_tokens[$i]['content']); + $content = str_replace($this->eolChar, '\n', $this->_tokens[$i]['content']); echo "\tStart scope map at $i: $type => $content".PHP_EOL; } @@ -889,7 +935,7 @@ private function _recurseScopeMap($stackPtr, $depth=1) if (PHP_CODESNIFFER_VERBOSITY > 1) { $type = $this->_tokens[$i]['type']; - $content = str_replace("\n", '\n', $this->_tokens[$i]['content']); + $content = str_replace($this->eolChar, '\n', $this->_tokens[$i]['content']); echo str_repeat("\t", $depth); echo "Process token $i ["; if ($opener !== null) { @@ -1098,7 +1144,7 @@ private function _createLevelMap() if (PHP_CODESNIFFER_VERBOSITY > 1) { $type = $this->_tokens[$i]['type']; $line = $this->_tokens[$i]['line']; - $content = str_replace("\n", '\n', $this->_tokens[$i]['content']); + $content = str_replace($this->eolChar, '\n', $this->_tokens[$i]['content']); echo str_repeat("\t", ($level + 1)); echo "Process token $i on line $line [lvl:$level;"; if (empty($conditions) !== true) { diff --git a/CodeSniffer/Standards/AbstractPatternSniff.php b/CodeSniffer/Standards/AbstractPatternSniff.php index b2f8b90b8b..c75b8ebec3 100644 --- a/CodeSniffer/Standards/AbstractPatternSniff.php +++ b/CodeSniffer/Standards/AbstractPatternSniff.php @@ -298,28 +298,28 @@ protected function processPattern($patternInfo, PHP_CodeSniffer_File $phpcsFile, } else { - $next = $phpcsFile->findPrevious($ignoreTokens, $stackPtr, null, true); // Check to see if this important token is the same as the - // next important token in the pattern. If it is not, than + // previous important token in the pattern. If it is not, then // the pattern cannot be for this piece of code. - if ($next === false || $tokens[$next]['code'] !== $pattern[$i]['token']) { + $prev = $phpcsFile->findPrevious($ignoreTokens, $stackPtr, null, true); + if ($prev === false || $tokens[$prev]['code'] !== $pattern[$i]['token']) { return false; } // If we skipped past some whitespace tokens, then add them // to the found string. - if (($stackPtr - $next) > 1) { - for ($j = ($stackPtr - 1); $j > $next; $j--) { + if (($stackPtr - $prev) > 1) { + for ($j = ($stackPtr - 1); $j > $prev; $j--) { $found = $tokens[$j]['content'].$found; } } - $found = $tokens[$next]['content'].$found; + $found = $tokens[$prev]['content'].$found; if (isset($pattern[($i - 1)]) === true && $pattern[($i - 1)]['type'] === 'skip') { - $stackPtr = $next; + $stackPtr = $prev; } else { - $stackPtr = ($next - 1); + $stackPtr = ($prev - 1); } }//end if @@ -346,6 +346,8 @@ protected function processPattern($patternInfo, PHP_CodeSniffer_File $phpcsFile, $stackPtr = ($tokens[$next][$to] - 1); } else if ($pattern[$i]['type'] === 'string') { $found = 'abc'; + } else if ($pattern[$i]['type'] === 'newline') { + $found = 'EOL'; }//end if }//end for }//end if @@ -412,11 +414,10 @@ protected function processPattern($patternInfo, PHP_CodeSniffer_File $phpcsFile, } } else { - - $next = $phpcsFile->findNext($ignoreTokens, $stackPtr, null, true); // Check to see if this important token is the same as the - // next important token in the pattern. If it is not, than + // next important token in the pattern. If it is not, then // the pattern cannot be for this piece of code. + $next = $phpcsFile->findNext($ignoreTokens, $stackPtr, null, true); if ($next === false || $tokens[$next]['code'] !== $pattern[$i]['token']) { return false; } @@ -439,7 +440,14 @@ protected function processPattern($patternInfo, PHP_CodeSniffer_File $phpcsFile, if ($this->_ignoreComments === false || ($this->_ignoreComments === true && $hasComment === false)) { $hasError = true; } - } + + // Even when ignoring comments, we are not allowed to include + // newlines without the pattern specifying them, so everything + // should be on the same line. + if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) { + $hasError = true; + } + }//end if if ($next !== $lastAddedStackPtr) { $found .= $tokens[$next]['content']; @@ -477,6 +485,51 @@ protected function processPattern($patternInfo, PHP_CodeSniffer_File $phpcsFile, } $stackPtr++; + } else if ($pattern[$i]['type'] === 'newline') { + // Find the next token that contains a newline character. + $numTokens = count($tokens); + $newline = 0; + for ($j = $stackPtr; $j < $numTokens; $j++) { + if (strpos($tokens[$j]['content'], $phpcsFile->eolChar) !== false) { + $newline = $j; + break; + } + } + + if ($newline === 0) { + // We didn't find a newline character in the rest of the file. + $next = ($numTokens - 1); + $hasError = true; + } else { + if ($this->_ignoreComments === false) { + // The newline character cannot be part of a comment. + if (in_array($tokens[$newline]['code'], PHP_CodeSniffer_Tokens::$commentTokens) === true) { + $hasError = true; + } + } + + if ($newline === $stackPtr) { + $next = ($stackPtr + 1); + } else { + // Check that there were no significant tokens that we skipped + // over to find our newline character. + $next = $phpcsFile->findNext($ignoreTokens, $stackPtr, null, true); + if ($next < $newline) { + // We skipped a non-ignored token. + $hasError = true; + } else { + $next = ($newline + 1); + } + } + }//end if + + if ($stackPtr !== $lastAddedStackPtr) { + $found .= $phpcsFile->getTokensAsString($stackPtr, ($next - $stackPtr)); + $diff = $next - $stackPtr; + $lastAddedStackPtr = $next; + } + + $stackPtr = $next; }//end if }//end for @@ -500,10 +553,10 @@ protected function processPattern($patternInfo, PHP_CodeSniffer_File $phpcsFile, */ protected function prepareError($found, $patternCode) { - $found = str_replace("\r", '\r', $found); + $found = str_replace("\r\n", '\n', $found); $found = str_replace("\n", '\n', $found); - $expected = str_replace("\r", '\r', $patternCode); - $expected = str_replace("\n", '\n', $expected); + $found = str_replace("\r", '\n', $found); + $expected = str_replace('EOL', '\n', $patternCode); $error = "Expected \"$expected\"; found \"$found\""; @@ -575,6 +628,7 @@ private function _parse($pattern) $specialPattern = false; $isLastChar = ($i === ($length - 1)); + $oldFirstToken = $firstToken; if (substr($pattern, $i, 3) === '...') { // It's a skip pattern. The skip pattern requires the @@ -582,11 +636,18 @@ private function _parse($pattern) // to skip to. $specialPattern = $this->_createSkipPattern($pattern, ($i - 1)); $lastToken = ($i - $firstToken); - $i = ($i + 4); + $firstToken = ($i + 4); + $i = ($i + 3); } else if (substr($pattern, $i, 3) === 'abc') { $specialPattern = array('type' => 'string'); $lastToken = ($i - $firstToken); - $i = ($i + 3); + $firstToken = ($i + 3); + $i = ($i + 2); + } else if (substr($pattern, $i, 3) === 'EOL') { + $specialPattern = array('type' => 'newline'); + $lastToken = ($i - $firstToken); + $firstToken = ($i + 3); + $i = ($i + 2); } if ($specialPattern !== false || $isLastChar === true) { @@ -595,15 +656,14 @@ private function _parse($pattern) if ($isLastChar === true) { // Get the string from the end of the last skip pattern, if any, // to the end of the pattern string. - $str = substr($pattern, $firstToken); + $str = substr($pattern, $oldFirstToken); } else { - // Get the string from the end of the last skip pattern, if any, - // to the start of this skip pattern. - $str = substr($pattern, $firstToken, $lastToken); + // Get the string from the end of the last special pattern, + // if any, to the start of this special pattern. + $str = substr($pattern, $oldFirstToken, $lastToken); } $tokenPatterns = $this->_createTokenPattern($str); - $firstToken = $i; // Make sure we don't skip the last token. if ($isLastChar === false && $i === ($length - 1)) { diff --git a/CodeSniffer/Standards/Generic/Sniffs/Files/LineLengthSniff.php b/CodeSniffer/Standards/Generic/Sniffs/Files/LineLengthSniff.php index a15670e496..44a059a0b4 100644 --- a/CodeSniffer/Standards/Generic/Sniffs/Files/LineLengthSniff.php +++ b/CodeSniffer/Standards/Generic/Sniffs/Files/LineLengthSniff.php @@ -83,11 +83,11 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($tokens[$tokenCount]['line'] === $currentLine) { $currentLineContent .= $tokens[$tokenCount]['content']; } else { - $currentLineContent = trim($currentLineContent, "\n"); + $currentLineContent = trim($currentLineContent, $phpcsFile->eolChar); $lineLength = strlen($currentLineContent); if ($lineLength > $this->lineLimit) { - $longLines[] = ($tokenCount - 1); + $longLines[($tokenCount - 1)] = $lineLength; } $currentLineContent = $tokens[$tokenCount]['content']; @@ -95,8 +95,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) } } - foreach ($longLines as $lineToken) { - $warning = 'Line exceeds '.$this->lineLimit.' characters'; + foreach ($longLines as $lineToken => $lineLength) { + $warning = 'Line exceeds '.$this->lineLimit." characters; contains $lineLength characters"; $phpcsFile->addWarning($warning, $lineToken); } diff --git a/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php b/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php index 5335a081f2..ec00ea045d 100644 --- a/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php +++ b/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php @@ -93,7 +93,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // checking from there, rather than the current token. $lineStart = $stackPtr; while (($lineStart = $phpcsFile->findPrevious(array(T_WHITESPACE), ($lineStart - 1), null, false)) !== false) { - if (strpos($tokens[$lineStart]['content'], "\n") !== false) { + if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) { break; } } diff --git a/CodeSniffer/Standards/PEAR/Sniffs/Commenting/ClassCommentSniff.php b/CodeSniffer/Standards/PEAR/Sniffs/Commenting/ClassCommentSniff.php index f3fa12e680..ceaab40c80 100755 --- a/CodeSniffer/Standards/PEAR/Sniffs/Commenting/ClassCommentSniff.php +++ b/CodeSniffer/Standards/PEAR/Sniffs/Commenting/ClassCommentSniff.php @@ -89,7 +89,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) } $commentStart = ($phpcsFile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1); - $commentNext = $phpcsFile->findPrevious(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, "\n"); + $commentNext = $phpcsFile->findPrevious(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, $phpcsFile->eolChar); // Distinguish file and class comment. $prevClassToken = $phpcsFile->findPrevious(T_CLASS, ($stackPtr - 1)); @@ -100,9 +100,9 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $prevComment = $phpcsFile->findPrevious(T_DOC_COMMENT, ($prevNonComment - 1)); if ($prevComment === false) { // There is only 1 doc comment between open tag and class token. - $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, "\n"); + $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, $phpcsFile->eolChar); if ($newlineToken !== false) { - $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($newlineToken + 1), $stackPtr, false, "\n"); + $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($newlineToken + 1), $stackPtr, false, $phpcsFile->eolChar); if ($newlineToken !== false) { // Blank line between the class and the doc block. // The doc block is most likely a file comment. @@ -118,7 +118,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // Parse the class comment.docblock. try { - $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment); + $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = ($e->getLineWithinComment() + $commentStart); @@ -136,20 +136,20 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // No extra newline before short description. $short = $comment->getShortComment(); $newlineCount = 0; - $newlineSpan = strspn($short, "\n"); + $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' && $newlineSpan > 0) { $line = ($newlineSpan > 1) ? 'newlines' : 'newline'; $error = "Extra $line found before class comment short description"; $phpcsFile->addError($error, ($commentStart + 1)); } - $newlineCount = (substr_count($short, "\n") + 1); + $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1); // Exactly one blank line between short and long description. $long = $comment->getLongComment(); if (empty($long) === false) { $between = $comment->getWhiteSpaceBetween(); - $newlineBetween = substr_count($between, "\n"); + $newlineBetween = substr_count($between, $phpcsFile->eolChar); if ($newlineBetween !== 2) { $error = 'There must be exactly one blank line between descriptions in class comment'; $phpcsFile->addError($error, ($commentStart + $newlineCount + 1)); @@ -165,11 +165,11 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($newlineSpan !== 2) { $error = 'There must be exactly one blank line before the tags in class comment'; if ($long !== '') { - $newlineCount += (substr_count($long, "\n") - $newlineSpan + 1); + $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1); } $phpcsFile->addError($error, ($commentStart + $newlineCount)); - $short = rtrim($short, "\n "); + $short = rtrim($short, $phpcsFile->eolChar.' '); } } diff --git a/CodeSniffer/Standards/PEAR/Sniffs/Commenting/FileCommentSniff.php b/CodeSniffer/Standards/PEAR/Sniffs/Commenting/FileCommentSniff.php index 98fad6b88f..33ce843f70 100755 --- a/CodeSniffer/Standards/PEAR/Sniffs/Commenting/FileCommentSniff.php +++ b/CodeSniffer/Standards/PEAR/Sniffs/Commenting/FileCommentSniff.php @@ -127,9 +127,9 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $commentNext = $phpcsFile->findNext($nextToken, ($commentEnd + 1)); if ($commentNext !== false && $tokens[$commentNext]['code'] !== T_DOC_COMMENT) { // Found a class token right after comment doc block. - $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), $commentNext, false, "\n"); + $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), $commentNext, false, $phpcsFile->eolChar); if ($newlineToken !== false) { - $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($newlineToken + 1), $commentNext, false, "\n"); + $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($newlineToken + 1), $commentNext, false, $phpcsFile->eolChar); if ($newlineToken === false) { // No blank line between the class token and the doc block. // The doc block is most likely a class comment. @@ -143,7 +143,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // Parse the header comment docblock. try { - $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment); + $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = ($e->getLineWithinComment() + $commentStart); @@ -161,20 +161,20 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // No extra newline before short description. $short = $comment->getShortComment(); $newlineCount = 0; - $newlineSpan = strspn($short, "\n"); + $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' && $newlineSpan > 0) { $line = ($newlineSpan > 1) ? 'newlines' : 'newline'; $error = "Extra $line found before file comment short description"; $phpcsFile->addError($error, ($commentStart + 1)); } - $newlineCount = (substr_count($short, "\n") + 1); + $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1); // Exactly one blank line between short and long description. $long = $comment->getLongComment(); if (empty($long) === false) { $between = $comment->getWhiteSpaceBetween(); - $newlineBetween = substr_count($between, "\n"); + $newlineBetween = substr_count($between, $phpcsFile->eolChar); if ($newlineBetween !== 2) { $error = 'There must be exactly one blank line between descriptions in file comment'; $phpcsFile->addError($error, ($commentStart + $newlineCount + 1)); @@ -190,11 +190,11 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($newlineSpan !== 2) { $error = 'There must be exactly one blank line before the tags in file comment'; if ($long !== '') { - $newlineCount += (substr_count($long, "\n") - $newlineSpan + 1); + $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1); } $phpcsFile->addError($error, ($commentStart + $newlineCount)); - $short = rtrim($short, "\n "); + $short = rtrim($short, $phpcsFile->eolChar.' '); } } diff --git a/CodeSniffer/Standards/PEAR/Sniffs/Commenting/FunctionCommentSniff.php b/CodeSniffer/Standards/PEAR/Sniffs/Commenting/FunctionCommentSniff.php index 1d9842fc9c..b06daaa22d 100755 --- a/CodeSniffer/Standards/PEAR/Sniffs/Commenting/FunctionCommentSniff.php +++ b/CodeSniffer/Standards/PEAR/Sniffs/Commenting/FunctionCommentSniff.php @@ -150,7 +150,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $this->_methodName = $phpcsFile->getDeclarationName($stackPtr); try { - $this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment); + $this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = ($e->getLineWithinComment() + $commentStart); @@ -172,20 +172,20 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // No extra newline before short description. $short = $comment->getShortComment(); $newlineCount = 0; - $newlineSpan = strspn($short, "\n"); + $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' && $newlineSpan > 0) { $line = ($newlineSpan > 1) ? 'newlines' : 'newline'; $error = "Extra $line found before function comment short description"; $phpcsFile->addError($error, ($commentStart + 1)); } - $newlineCount = (substr_count($short, "\n") + 1); + $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1); // Exactly one blank line between short and long description. $long = $comment->getLongComment(); if (empty($long) === false) { $between = $comment->getWhiteSpaceBetween(); - $newlineBetween = substr_count($between, "\n"); + $newlineBetween = substr_count($between, $phpcsFile->eolChar); if ($newlineBetween !== 2) { $error = 'There must be exactly one blank line between descriptions in function comment'; $phpcsFile->addError($error, ($commentStart + $newlineCount + 1)); @@ -201,11 +201,11 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($newlineSpan !== 2) { $error = 'There must be exactly one blank line before the tags in function comment'; if ($long !== '') { - $newlineCount += (substr_count($long, "\n") - $newlineSpan + 1); + $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1); } $phpcsFile->addError($error, ($commentStart + $newlineCount)); - $short = rtrim($short, "\n "); + $short = rtrim($short, $phpcsFile->eolChar.' '); } } @@ -299,7 +299,7 @@ private function _processParams($commentStart) if (empty($params) === false) { $lastParm = (count($params) - 1); - if (substr_count($params[$lastParm]->getWhitespaceAfter(), "\n") !== 2) { + if (substr_count($params[$lastParm]->getWhitespaceAfter(), $this->currentFile->eolChar) !== 2) { $error = 'Last parameter comment requires a blank newline after it'; $errorPos = ($params[$lastParm]->getLine() + $commentStart); $this->currentFile->addError($error, $errorPos); diff --git a/CodeSniffer/Standards/PEAR/Sniffs/ControlStructures/ControlSignatureSniff.php b/CodeSniffer/Standards/PEAR/Sniffs/ControlStructures/ControlSignatureSniff.php index 217fed212f..78df31d330 100755 --- a/CodeSniffer/Standards/PEAR/Sniffs/ControlStructures/ControlSignatureSniff.php +++ b/CodeSniffer/Standards/PEAR/Sniffs/ControlStructures/ControlSignatureSniff.php @@ -51,15 +51,15 @@ protected function getPatterns() { return array( - "do {\n...} while (...);\n", - "while (...) {\n", - "for (...) {\n", - "if (...) {\n", - "foreach (...) {\n", - "} else if (...) {\n", - "} elseif (...) {\n", - "} else {\n", - "do {\n", + 'do {EOL...} while (...);EOL', + 'while (...) {EOL', + 'for (...) {EOL', + 'if (...) {EOL', + 'foreach (...) {EOL', + '} else if (...) {EOL', + '} elseif (...) {EOL', + '} else {EOL', + 'do {EOL', ); }//end getPatterns() diff --git a/CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php b/CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php index b2c34eef82..c686377372 100755 --- a/CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php +++ b/CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php @@ -101,7 +101,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) } else { // If there is a newline in the space, then the must be formatting // each argument on a newline, which is valid, so ignore it. - if (strpos($tokens[($nextSeperator + 1)]['content'], "\n") === false) { + if (strpos($tokens[($nextSeperator + 1)]['content'], $phpcsFile->eolChar) === false) { $space = strlen($tokens[($nextSeperator + 1)]['content']); if ($space > 1) { $error = 'Expected 1 space after comma in function call; '; diff --git a/CodeSniffer/Standards/PEAR/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php b/CodeSniffer/Standards/PEAR/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php index 71f49f7963..196965518c 100755 --- a/CodeSniffer/Standards/PEAR/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php +++ b/CodeSniffer/Standards/PEAR/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php @@ -72,7 +72,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // checking from there, rather than the current token. $lineStart = ($stackPtr - 1); for ($lineStart; $lineStart > 0; $lineStart--) { - if (strpos($tokens[$lineStart]['content'], "\n") !== false) { + if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) { break; } } diff --git a/CodeSniffer/Standards/Squiz/Sniffs/Classes/ClassDeclarationSniff.php b/CodeSniffer/Standards/Squiz/Sniffs/Classes/ClassDeclarationSniff.php index b4b543a52f..32785f6e4f 100644 --- a/CodeSniffer/Standards/Squiz/Sniffs/Classes/ClassDeclarationSniff.php +++ b/CodeSniffer/Standards/Squiz/Sniffs/Classes/ClassDeclarationSniff.php @@ -97,7 +97,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $phpcsFile->addError($error, $curlyBrace); } - if ($tokens[($curlyBrace + 1)]['content'] !== "\n") { + if ($tokens[($curlyBrace + 1)]['content'] !== $phpcsFile->eolChar) { $type = strtolower($tokens[$stackPtr]['content']); $error = "Opening $type brace must be on a line by itself"; $phpcsFile->addError($error, $curlyBrace); @@ -109,8 +109,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) { $prevContent = $tokens[($stackPtr - 1)]['content']; - if ($prevContent !== "\n") { - $blankSpace = substr($prevContent, strpos($prevContent, "\n")); + if ($prevContent !== $phpcsFile->eolChar) { + $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar)); $spaces = strlen($blankSpace); if (in_array($tokens[($stackPtr - 2)]['code'], array(T_ABSTRACT, T_FINAL)) === false) { @@ -132,8 +132,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($tokens[($curlyBrace - 1)]['code'] === T_WHITESPACE) { $prevContent = $tokens[($curlyBrace - 1)]['content']; - if ($prevContent !== "\n") { - $blankSpace = substr($prevContent, strpos($prevContent, "\n")); + if ($prevContent !== $phpcsFile->eolChar) { + $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar)); $spaces = strlen($blankSpace); if ($spaces !== 0) { $error = "Expected 0 spaces before opening brace; $spaces found"; @@ -145,8 +145,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $closeBrace = $tokens[$stackPtr]['scope_closer']; if ($tokens[($closeBrace - 1)]['code'] === T_WHITESPACE) { $prevContent = $tokens[($closeBrace - 1)]['content']; - if ($prevContent !== "\n") { - $blankSpace = substr($prevContent, strpos($prevContent, "\n")); + if ($prevContent !== $phpcsFile->eolChar) { + $blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar)); $spaces = strlen($blankSpace); if ($spaces !== 0) { $error = "Expected 0 spaces before closing brace; $spaces found"; @@ -158,7 +158,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // Check the closing brace is on it's own line, but allow // for comments like "//end class". $nextContent = $phpcsFile->findNext(T_COMMENT, ($closeBrace + 1), null, true); - if ($tokens[$nextContent]['content'] !== "\n" && $tokens[$nextContent]['line'] === $tokens[$closeBrace]['line']) { + if ($tokens[$nextContent]['content'] !== $phpcsFile->eolChar && $tokens[$nextContent]['line'] === $tokens[$closeBrace]['line']) { $type = strtolower($tokens[$stackPtr]['content']); $error = "Closing $type brace must be on a line by itself"; $phpcsFile->addError($error, $closeBrace); diff --git a/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php b/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php index eed957dde5..3205bd6c03 100644 --- a/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php +++ b/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php @@ -88,7 +88,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) } $commentStart = ($phpcsFile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1); - $commentNext = $phpcsFile->findPrevious(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, "\n"); + $commentNext = $phpcsFile->findPrevious(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, $phpcsFile->eolChar); // Distinguish file and class comment. $prevClassToken = $phpcsFile->findPrevious(T_CLASS, ($stackPtr - 1)); @@ -99,9 +99,9 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $prevComment = $phpcsFile->findPrevious(T_DOC_COMMENT, ($prevNonComment - 1)); if ($prevComment === false) { // There is only 1 doc comment between open tag and class token. - $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, "\n"); + $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, $phpcsFile->eolChar); if ($newlineToken !== false) { - $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($newlineToken + 1), $stackPtr, false, "\n"); + $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($newlineToken + 1), $stackPtr, false, $phpcsFile->eolChar); if ($newlineToken !== false) { // Blank line between the class and the doc block. // The doc block is most likely a file comment. @@ -116,7 +116,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($prevTokenEnd !== false) { $blankLineBefore = 0; for ($i = ($prevTokenEnd + 1); $i < $commentStart; $i++) { - if ($tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['content'] === "\n") { + if ($tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['content'] === $phpcsFile->eolChar) { $blankLineBefore++; } } @@ -134,7 +134,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // Parse the class comment docblock. try { - $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment); + $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = ($e->getLineWithinComment() + $commentStart); @@ -150,7 +150,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) } // Check for a comment description. - $short = rtrim($comment->getShortComment(), "\n"); + $short = rtrim($comment->getShortComment(), $phpcsFile->eolChar); if (trim($short) === '') { $error = 'Missing short description in class doc comment'; $phpcsFile->addError($error, $commentStart); @@ -159,20 +159,20 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // No extra newline before short description. $newlineCount = 0; - $newlineSpan = strspn($short, "\n"); + $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' && $newlineSpan > 0) { $line = ($newlineSpan > 1) ? 'newlines' : 'newline'; $error = "Extra $line found before class comment short description"; $phpcsFile->addError($error, ($commentStart + 1)); } - $newlineCount = (substr_count($short, "\n") + 1); + $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1); // Exactly one blank line between short and long description. $long = $comment->getLongComment(); if (empty($long) === false) { $between = $comment->getWhiteSpaceBetween(); - $newlineBetween = substr_count($between, "\n"); + $newlineBetween = substr_count($between, $phpcsFile->eolChar); if ($newlineBetween !== 2) { $error = 'There must be exactly one blank line between descriptions in class comment'; $phpcsFile->addError($error, ($commentStart + $newlineCount + 1)); @@ -188,17 +188,17 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($newlineSpan !== 2) { $error = 'There must be exactly one blank line before the tags in class comment'; if ($long !== '') { - $newlineCount += (substr_count($long, "\n") - $newlineSpan + 1); + $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1); } $phpcsFile->addError($error, ($commentStart + $newlineCount)); - $short = rtrim($short, "\n "); + $short = rtrim($short, $phpcsFile->eolChar.' '); } } // Short description must be single line and end with a full stop. $lastChar = $short[(strlen($short) - 1)]; - if (substr_count($short, "\n") !== 0) { + if (substr_count($short, $phpcsFile->eolChar) !== 0) { $error = 'Class comment short description must be on a single line'; $phpcsFile->addError($error, ($commentStart + 1)); } diff --git a/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php b/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php index 48f7a0f770..0dce7a6578 100644 --- a/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php +++ b/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php @@ -120,9 +120,9 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $commentNext = $phpcsFile->findNext($nextToken, ($commentEnd + 1)); if ($commentNext !== false && $tokens[$commentNext]['code'] !== T_DOC_COMMENT) { // Found a class token right after comment doc block. - $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), $commentNext, false, "\n"); + $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), $commentNext, false, $phpcsFile->eolChar); if ($newlineToken !== false) { - $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($newlineToken + 1), $commentNext, false, "\n"); + $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($newlineToken + 1), $commentNext, false, $phpcsFile->eolChar); if ($newlineToken === false) { // No blank line between the class token and the doc block. // The doc block is most likely a class comment. @@ -133,7 +133,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) } // No blank line between the open tag and the file comment. - $blankLineBefore = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, false, "\n"); + $blankLineBefore = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, false, $phpcsFile->eolChar); if ($blankLineBefore !== false && $blankLineBefore < $commentStart) { $error = 'Extra newline found after the open tag'; $phpcsFile->addError($error, ($stackPtr + 1)); @@ -144,7 +144,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($nextTokenStart !== false) { $blankLineAfter = 0; for ($i = ($commentEnd + 1); $i < $nextTokenStart; $i++) { - if ($tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['content'] === "\n") { + if ($tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['content'] === $phpcsFile->eolChar) { $blankLineAfter++; } } @@ -159,7 +159,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // Parse the header comment docblock. try { - $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment); + $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = ($e->getLineWithinComment() + $commentStart); @@ -177,20 +177,20 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // No extra newline before short description. $short = $comment->getShortComment(); $newlineCount = 0; - $newlineSpan = strspn($short, "\n"); + $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' && $newlineSpan > 0) { $line = ($newlineSpan > 1) ? 'newlines' : 'newline'; $error = "Extra $line found before file comment short description"; $phpcsFile->addError($error, ($commentStart + 1)); } - $newlineCount = (substr_count($short, "\n") + 1); + $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1); // Exactly one blank line between short and long description. $long = $comment->getLongComment(); if (empty($long) === false) { $between = $comment->getWhiteSpaceBetween(); - $newlineBetween = substr_count($between, "\n"); + $newlineBetween = substr_count($between, $phpcsFile->eolChar); if ($newlineBetween !== 2) { $error = 'There must be exactly one blank line between descriptions in file comment'; $phpcsFile->addError($error, ($commentStart + $newlineCount + 1)); @@ -206,17 +206,17 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($newlineSpan !== 2) { $error = 'There must be exactly one blank line before the tags in file comment'; if ($long !== '') { - $newlineCount += (substr_count($long, "\n") - $newlineSpan + 1); + $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1); } $phpcsFile->addError($error, ($commentStart + $newlineCount)); - $short = rtrim($short, "\n "); + $short = rtrim($short, $phpcsFile->eolChar.' '); } } // Short description must be single line and end with a full stop. $lastChar = $short[(strlen($short) - 1)]; - if (substr_count($short, "\n") !== 0) { + if (substr_count($short, $phpcsFile->eolChar) !== 0) { $error = 'File comment short description must be on a single line'; $phpcsFile->addError($error, ($commentStart + 1)); } diff --git a/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php b/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php index 3b961f37a0..30f3860ca8 100644 --- a/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php +++ b/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php @@ -159,7 +159,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $this->_methodName = $phpcsFile->getDeclarationName($stackPtr); try { - $this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment); + $this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = ($e->getLineWithinComment() + $commentStart); @@ -190,20 +190,20 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // No extra newline before short description. $newlineCount = 0; - $newlineSpan = strspn($short, "\n"); + $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' && $newlineSpan > 0) { $line = ($newlineSpan > 1) ? 'newlines' : 'newline'; $error = "Extra $line found before function comment short description"; $phpcsFile->addError($error, ($commentStart + 1)); } - $newlineCount = (substr_count($short, "\n") + 1); + $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1); // Exactly one blank line between short and long description. $long = $comment->getLongComment(); if (empty($long) === false) { $between = $comment->getWhiteSpaceBetween(); - $newlineBetween = substr_count($between, "\n"); + $newlineBetween = substr_count($between, $phpcsFile->eolChar); if ($newlineBetween !== 2) { $error = 'There must be exactly one blank line between descriptions in function comment'; $phpcsFile->addError($error, ($commentStart + $newlineCount + 1)); @@ -219,17 +219,17 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($newlineSpan !== 2) { $error = 'There must be exactly one blank line before the tags in function comment'; if ($long !== '') { - $newlineCount += (substr_count($long, "\n") - $newlineSpan + 1); + $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1); } $phpcsFile->addError($error, ($commentStart + $newlineCount)); - $short = rtrim($short, "\n "); + $short = rtrim($short, $phpcsFile->eolChar.' '); } } // Short description must be single line and end with a full stop. $lastChar = $short[(strlen($short) - 1)]; - if (substr_count($short, "\n") !== 0) { + if (substr_count($short, $phpcsFile->eolChar) !== 0) { $error = 'Function comment short description must be on a single line'; $phpcsFile->addError($error, ($commentStart + 1)); } @@ -507,7 +507,7 @@ private function _processParams($commentStart, $commentEnd) if (empty($params) === false) { - if (substr_count($params[(count($params) - 1)]->getWhitespaceAfter(), "\n") !== 2) { + if (substr_count($params[(count($params) - 1)]->getWhitespaceAfter(), $this->currentFile->eolChar) !== 2) { $error = 'Last parameter comment requires a blank newline after it'; $errorPos = ($params[(count($params) - 1)]->getLine() + $commentStart); $this->currentFile->addError($error, $errorPos); diff --git a/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FunctionCommentThrowTagSniff.php b/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FunctionCommentThrowTagSniff.php index 9988bc6d25..ff9549d93e 100644 --- a/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FunctionCommentThrowTagSniff.php +++ b/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FunctionCommentThrowTagSniff.php @@ -71,7 +71,7 @@ protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $sta $comment = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart + 1)); try { - $this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment); + $this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = ($e->getLineWithinComment() + $commentStart); diff --git a/CodeSniffer/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php b/CodeSniffer/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php index 0a78a65ecf..09c85a9c43 100644 --- a/CodeSniffer/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php +++ b/CodeSniffer/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php @@ -90,7 +90,7 @@ public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // Parse the header comment docblock. try { - $this->commentParser = new PHP_CodeSniffer_CommentParser_MemberCommentParser($comment); + $this->commentParser = new PHP_CodeSniffer_CommentParser_MemberCommentParser($comment, $phpcsFile); $this->commentParser->parse(); } catch (PHP_CodeSniffer_CommentParser_ParserException $e) { $line = ($e->getLineWithinComment() + $commentStart); @@ -115,20 +115,20 @@ public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // No extra newline before short description. $newlineCount = 0; - $newlineSpan = strspn($short, "\n"); + $newlineSpan = strspn($short, $phpcsFile->eolChar); if ($short !== '' && $newlineSpan > 0) { $line = ($newlineSpan > 1) ? 'newlines' : 'newline'; $error = "Extra $line found before variable comment short description"; $phpcsFile->addError($error, ($commentStart + 1)); } - $newlineCount = (substr_count($short, "\n") + 1); + $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1); // Exactly one blank line between short and long description. $long = $comment->getLongComment(); if (empty($long) === false) { $between = $comment->getWhiteSpaceBetween(); - $newlineBetween = substr_count($between, "\n"); + $newlineBetween = substr_count($between, $phpcsFile->eolChar); if ($newlineBetween !== 2) { $error = 'There must be exactly one blank line between descriptions in variable comment'; $phpcsFile->addError($error, ($commentStart + $newlineCount + 1)); @@ -144,17 +144,17 @@ public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($newlineSpan !== 2) { $error = 'There must be exactly one blank line before the tags in variable comment'; if ($long !== '') { - $newlineCount += (substr_count($long, "\n") - $newlineSpan + 1); + $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1); } $phpcsFile->addError($error, ($commentStart + $newlineCount)); - $short = rtrim($short, "\n "); + $short = rtrim($short, $phpcsFile->eolChar.' '); } } // Short description must be single line and end with a full stop. $lastChar = $short[(strlen($short) - 1)]; - if (substr_count($short, "\n") !== 0) { + if (substr_count($short, $phpcsFile->eolChar) !== 0) { $error = 'Variable comment short description must be on a single line'; $phpcsFile->addError($error, ($commentStart + 1)); } diff --git a/CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php b/CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php index c6d5078f33..07058a1702 100644 --- a/CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php +++ b/CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.php @@ -40,15 +40,15 @@ class Squiz_Sniffs_ControlStructures_ControlSignatureSniff extends PHP_CodeSniff protected function getPatterns() { return array( - "try {\n...} catch (...) {\n", - "do {\n...} while (...);\n", - "while (...) {\n", - "for (...) {\n", - "if (...) {\n", - "foreach (...) {\n", - "} else if (...) {\n", - "} else {\n", - "do {\n", + 'try {EOL...} catch (...) {EOL', + 'do {EOL...} while (...);EOL', + 'while (...) {EOL', + 'for (...) {EOL', + 'if (...) {EOL', + 'foreach (...) {EOL', + '} else if (...) {EOL', + '} else {EOL', + 'do {EOL', ); }//end getPatterns() diff --git a/CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/SwitchDeclarationSniff.php b/CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/SwitchDeclarationSniff.php index 0890a595a9..61ee2958f2 100644 --- a/CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/SwitchDeclarationSniff.php +++ b/CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/SwitchDeclarationSniff.php @@ -88,7 +88,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // Check that all content within each CASE is indented correctly. $nextSpace = $nextCase; while (($nextSpace = $phpcsFile->findNext(T_WHITESPACE, ($nextSpace + 1), $nextBreak)) !== false) { - if (strpos($tokens[$nextSpace]['content'], "\n") === false) { + if (strpos($tokens[$nextSpace]['content'], $phpcsFile->eolChar) === false) { continue; } @@ -149,7 +149,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // Check that all content within the DEFAULT case is indented correctly. $nextSpace = $default; while (($nextSpace = $phpcsFile->findNext(T_WHITESPACE, ($nextSpace + 1), $nextBreak)) !== false) { - if (strpos($tokens[$nextSpace]['content'], "\n") === false) { + if (strpos($tokens[$nextSpace]['content'], $phpcsFile->eolChar) === false) { continue; } diff --git a/CodeSniffer/Standards/Squiz/Sniffs/Formatting/OutputBufferingIndentSniff.php b/CodeSniffer/Standards/Squiz/Sniffs/Formatting/OutputBufferingIndentSniff.php index f10e5626b3..21bf244565 100644 --- a/CodeSniffer/Standards/Squiz/Sniffs/Formatting/OutputBufferingIndentSniff.php +++ b/CodeSniffer/Standards/Squiz/Sniffs/Formatting/OutputBufferingIndentSniff.php @@ -92,7 +92,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $requiredIndent = ($tokens[$stackPtr]['column'] + 3); for ($stackPtr; $stackPtr < $bufferEnd; $stackPtr++) { - if (strpos($tokens[$stackPtr]['content'], "\n") === false) { + if (strpos($tokens[$stackPtr]['content'], $phpcsFile->eolChar) === false) { continue; } diff --git a/CodeSniffer/Standards/Squiz/Sniffs/Functions/FunctionDeclarationSniff.php b/CodeSniffer/Standards/Squiz/Sniffs/Functions/FunctionDeclarationSniff.php index aea56990b1..9ce7171cba 100644 --- a/CodeSniffer/Standards/Squiz/Sniffs/Functions/FunctionDeclarationSniff.php +++ b/CodeSniffer/Standards/Squiz/Sniffs/Functions/FunctionDeclarationSniff.php @@ -44,7 +44,7 @@ protected function getPatterns() return array( 'function abc(...);', 'abstract function abc(...);', - "function abc(...)\n", + 'function abc(...)EOL', ); }//end getPatterns() diff --git a/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/FunctionClosingBraceSpaceSniff.php b/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/FunctionClosingBraceSpaceSniff.php index aa9e044382..518b375cdd 100644 --- a/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/FunctionClosingBraceSpaceSniff.php +++ b/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/FunctionClosingBraceSpaceSniff.php @@ -72,7 +72,7 @@ public function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackP return; } - if (strpos($tokens[$lastContent]['content'], "\n") !== false) { + if (strpos($tokens[$lastContent]['content'], $phpcsFile->eolChar) !== false) { // Comments add an extra line, so this extra exception needs to be made. $lineDifference = ($tokens[$stackPtr]['line'] - $tokens[$lastContent]['line']); } else { diff --git a/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/FunctionSpacingSniff.php b/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/FunctionSpacingSniff.php index 3544909f6d..49c72c1ac8 100644 --- a/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/FunctionSpacingSniff.php +++ b/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/FunctionSpacingSniff.php @@ -77,7 +77,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $numTokens = count($tokens); $nextLineToken = null; for ($i = $closer; $i < $numTokens; $i++) { - if (strpos($tokens[$i]['content'], "\n") === false) { + if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) === false) { continue; } else { $nextLineToken = ($i + 1); @@ -110,7 +110,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $prevLineToken = null; for ($i = $stackPtr; $i > 0; $i--) { - if (strpos($tokens[$i]['content'], "\n") === false) { + if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) === false) { continue; } else { $prevLineToken = $i; diff --git a/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php b/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php index ff0d5aca2a..e8c15ea564 100644 --- a/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php +++ b/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php @@ -51,7 +51,7 @@ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$commentTokens) === true) { // Skip comments. continue; - } else if (strpos($tokens[$i]['content'], "\n") === false) { + } else if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) === false) { // Not the end of the line. continue; } else { diff --git a/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php b/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php index cba76c6ad1..ee4b77413b 100644 --- a/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php +++ b/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php @@ -126,11 +126,11 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) Check for end of line whitespace. */ - if (strpos($tokens[$stackPtr]['content'], "\n") === false) { + if (strpos($tokens[$stackPtr]['content'], $phpcsFile->eolChar) === false) { return; } - $tokenContent = rtrim($tokens[$stackPtr]['content'], "\n"); + $tokenContent = rtrim($tokens[$stackPtr]['content'], $phpcsFile->eolChar); if (empty($tokenContent) === true) { return; } diff --git a/package.xml b/package.xml index 127ef94545..88cfef1612 100644 --- a/package.xml +++ b/package.xml @@ -32,6 +32,11 @@ http://pear.php.net/dtd/package-2.0.xsd"> BSD License + - BC BREAK: EOL character is now auto-detected and used instead of hard-coded \n + - Pattern sniffs must now specify "EOL" instead of "\n" or "\r\n" to use auto-detection + - Please use $phpcsFile->eolChar to check for newlines instead of hard-coding "\n" or "\r\n" + - Comment parser classes now require you to pass $phpcsFile as an additional argument + - Fixed error where including a directory of sniffs in a coding standard class did not work - Coding standard classes can now specify a list of sniffs to exclude as well as include (feature request #11056) - Two uppercase characters can now be placed side-by-side in class names in Squiz ValidClassNameSniff