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 "