Skip to content

Commit

Permalink
Big supper massive changes to the way PHP_CodeSniffer deals with line…
Browse files Browse the repository at this point in the history
… breaks. Instead of hard-coding \n, or using the OSes default end of line character, we now detect the EOL char for each file. A lot of code has changed to remove the user of \n and use the new eolChar public var in PHP_CodeSniffer_File. The idea here is to ensure that PHP_CodeSniffer always produces the same errors no matter what the EOL char is, unless a sniff is checking for it explicitly.

git-svn-id: http://svn.php.net/repository/pear/packages/PHP_CodeSniffer/trunk@236312 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
gsherwood committed May 25, 2007
1 parent b5c2a15 commit 85b4a90
Show file tree
Hide file tree
Showing 35 changed files with 353 additions and 209 deletions.
14 changes: 12 additions & 2 deletions CodeSniffer/CommentParser/AbstractDocElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*
Expand All @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
44 changes: 30 additions & 14 deletions CodeSniffer/CommentParser/AbstractParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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()

Expand All @@ -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()
Expand All @@ -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);
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions CodeSniffer/CommentParser/ClassCommentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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;

Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
13 changes: 7 additions & 6 deletions CodeSniffer/CommentParser/CommentElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 7 additions & 6 deletions CodeSniffer/CommentParser/FunctionCommentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion CodeSniffer/CommentParser/MemberCommentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions CodeSniffer/CommentParser/PairElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
7 changes: 5 additions & 2 deletions CodeSniffer/CommentParser/ParameterElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions CodeSniffer/CommentParser/SingleElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Loading

0 comments on commit 85b4a90

Please sign in to comment.