Skip to content

Commit 85b4a90

Browse files
committed
Big supper massive changes to the way PHP_CodeSniffer deals with line 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
1 parent b5c2a15 commit 85b4a90

35 files changed

+353
-209
lines changed

CodeSniffer/CommentParser/AbstractDocElement.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ abstract class PHP_CodeSniffer_CommentParser_AbstractDocElement implements PHP_C
9090
*/
9191
protected $tokens = array();
9292

93+
/**
94+
* The file this element is in.
95+
*
96+
* @var array(string)
97+
*/
98+
protected $phpcsFile = null;
99+
93100
/**
94101
* The tag that this element represents (omiting the @ symbol).
95102
*
@@ -105,16 +112,19 @@ abstract class PHP_CodeSniffer_CommentParser_AbstractDocElement implements PHP_C
105112
* @param array $tokens The tokens of this element.
106113
* @param string $tag The doc element tag this element
107114
* represents.
115+
* @param PHP_CodeSniffer_File $phpcsFile The file that this element is in.
108116
*
109117
* @throws Exception If $previousElement in not a DocElement or if
110118
* getSubElements() does not return an array.
111119
*/
112-
public function __construct($previousElement, array $tokens, $tag)
120+
public function __construct($previousElement, array $tokens, $tag, PHP_CodeSniffer_File $phpcsFile)
113121
{
114122
if ($previousElement !== null && ($previousElement instanceof PHP_CodeSniffer_CommentParser_DocElement) === false) {
115123
throw new Exception('$previousElement must be an instance of DocElement');
116124
}
117125

126+
$this->phpcsFile = $phpcsFile;
127+
118128
$this->previousElement = $previousElement;
119129
if ($previousElement !== null) {
120130
$this->previousElement->nextElement = $this;
@@ -271,7 +281,7 @@ public function getLine()
271281
$previousContent = $this->previousElement->getRawContent();
272282
$previousLine = $this->previousElement->getLine();
273283

274-
return ($previousLine + substr_count($previousContent, "\n"));
284+
return ($previousLine + substr_count($previousContent, $this->phpcsFile->eolChar));
275285
}
276286

277287
}//end getLine()

CodeSniffer/CommentParser/AbstractParser.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ abstract class PHP_CodeSniffer_CommentParser_AbstractParser
6666
*/
6767
protected $comment = null;
6868

69+
/**
70+
* The string content of the comment.
71+
*
72+
* @var string
73+
*/
74+
protected $commentString = '';
75+
76+
/**
77+
* The file that the comment exists in.
78+
*
79+
* @var PHP_CodeSniffer_File
80+
*/
81+
protected $phpcsFile = null;
82+
6983
/**
7084
* The word tokens that appear in the comment.
7185
*
@@ -151,11 +165,13 @@ abstract class PHP_CodeSniffer_CommentParser_AbstractParser
151165
/**
152166
* Constructs a Doc Comment Parser.
153167
*
154-
* @param string $comment The comment to parse.
168+
* @param string $comment The comment to parse.
169+
* @param PHP_CodeSniffer_File $phpcsFile The file that this comment is in.
155170
*/
156-
public function __construct($comment)
171+
public function __construct($comment, PHP_CodeSniffer_File $phpcsFile)
157172
{
158-
$this->_comment = $comment;
173+
$this->commentString = $comment;
174+
$this->phpcsFile = $phpcsFile;
159175

160176
}//end __construct()
161177

@@ -171,7 +187,7 @@ public function __construct($comment)
171187
public function parse()
172188
{
173189
if ($this->_hasParsed === false) {
174-
$this->_parse($this->_comment);
190+
$this->_parse($this->commentString);
175191
}
176192

177193
}//end parse()
@@ -188,7 +204,7 @@ public function parse()
188204
private function _parse($comment)
189205
{
190206
// Firstly, remove the comment tags and any stars from the left side.
191-
$lines = split("\n", $comment);
207+
$lines = split($this->phpcsFile->eolChar, $comment);
192208
foreach ($lines as &$line) {
193209
if ($line !== '') {
194210
$line = trim($line);
@@ -205,7 +221,7 @@ private function _parse($comment)
205221
// might be interested in the spaces between words, so tokenize
206222
// spaces as well as separate tokens.
207223
$flags = (PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
208-
$words = preg_split('|(\s+)|', $line."\n", -1, $flags);
224+
$words = preg_split('|(\s+)|', $line.$this->phpcsFile->eolChar, -1, $flags);
209225
$this->words = array_merge($this->words, $words);
210226
}
211227
}
@@ -248,7 +264,7 @@ private function _parseWords()
248264
continue;
249265
}
250266

251-
if (isset($this->words[($wordPos - 2)]) === false || $this->words[($wordPos - 2)] !== "\n") {
267+
if (isset($this->words[($wordPos - 2)]) === false || $this->words[($wordPos - 2)] !== $this->phpcsFile->eolChar) {
252268
continue;
253269
}
254270

@@ -326,7 +342,7 @@ protected function getLine($tokenPos)
326342
{
327343
$newlines = 0;
328344
for ($i = 0; $i < $tokenPos; $i++) {
329-
$newlines += substr_count("\n", $this->words[$i]);
345+
$newlines += substr_count($this->phpcsFile->eolChar, $this->words[$i]);
330346
}
331347

332348
return $newlines;
@@ -343,7 +359,7 @@ protected function getLine($tokenPos)
343359
*/
344360
protected function parseSee($tokens)
345361
{
346-
$see = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'see');
362+
$see = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'see', $this->phpcsFile);
347363
$this->sees[] = $see;
348364
return $see;
349365

@@ -359,7 +375,7 @@ protected function parseSee($tokens)
359375
*/
360376
protected function parseComment($tokens)
361377
{
362-
$this->comment = new PHP_CodeSniffer_CommentParser_CommentElement($this->previousElement, $tokens);
378+
$this->comment = new PHP_CodeSniffer_CommentParser_CommentElement($this->previousElement, $tokens, $this->phpcsFile);
363379
return $this->comment;
364380

365381
}//end parseComment()
@@ -374,7 +390,7 @@ protected function parseComment($tokens)
374390
*/
375391
protected function parseDeprecated($tokens)
376392
{
377-
$this->deprecated = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'deprecated');
393+
$this->deprecated = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'deprecated', $this->phpcsFile);
378394
return $this->deprecated;
379395

380396
}//end parseDeprecated()
@@ -389,7 +405,7 @@ protected function parseDeprecated($tokens)
389405
*/
390406
protected function parseSince($tokens)
391407
{
392-
$this->since = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'since');
408+
$this->since = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'since', $this->phpcsFile);
393409
return $this->since;
394410

395411
}//end parseSince()
@@ -404,7 +420,7 @@ protected function parseSince($tokens)
404420
*/
405421
protected function parseLink($tokens)
406422
{
407-
$link = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'link');
423+
$link = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'link', $this->phpcsFile);
408424
$this->links[] = $link;
409425
return $link;
410426

@@ -505,7 +521,7 @@ protected function parseTag($tag, $start, $end)
505521

506522
$this->previousElement = $this->$method($tokens);
507523
} else {
508-
$this->previousElement = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, $tag);
524+
$this->previousElement = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, $tag, $this->phpcsFile);
509525
}
510526

511527
$this->orders[] = $tag;

CodeSniffer/CommentParser/ClassCommentParser.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ protected function getAllowedTags()
110110
*/
111111
protected function parseLicense($tokens)
112112
{
113-
$this->_license = new PHP_CodeSniffer_CommentParser_PairElement($this->previousElement, $tokens, 'license');
113+
$this->_license = new PHP_CodeSniffer_CommentParser_PairElement($this->previousElement, $tokens, 'license', $this->phpcsFile);
114114
return $this->_license;
115115

116116
}//end parseLicense()
@@ -125,7 +125,7 @@ protected function parseLicense($tokens)
125125
*/
126126
protected function parseCopyright($tokens)
127127
{
128-
$this->_copyright = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'copyright');
128+
$this->_copyright = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'copyright', $this->phpcsFile);
129129
return $this->_copyright;
130130

131131
}//end parseCopyright()
@@ -140,7 +140,7 @@ protected function parseCopyright($tokens)
140140
*/
141141
protected function parseCategory($tokens)
142142
{
143-
$this->_category = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'category');
143+
$this->_category = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'category', $this->phpcsFile);
144144
return $this->_category;
145145

146146
}//end parseCategory()
@@ -155,7 +155,7 @@ protected function parseCategory($tokens)
155155
*/
156156
protected function parseAuthor($tokens)
157157
{
158-
$author = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'author');
158+
$author = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'author', $this->phpcsFile);
159159
$this->_authors[] = $author;
160160
return $author;
161161

@@ -171,7 +171,7 @@ protected function parseAuthor($tokens)
171171
*/
172172
protected function parseVersion($tokens)
173173
{
174-
$this->_version = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'version');
174+
$this->_version = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'version', $this->phpcsFile);
175175
return $this->_version;
176176

177177
}//end parseVersion()
@@ -186,7 +186,7 @@ protected function parseVersion($tokens)
186186
*/
187187
protected function parsePackage($tokens)
188188
{
189-
$this->_package = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'package');
189+
$this->_package = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'package', $this->phpcsFile);
190190
return $this->_package;
191191

192192
}//end parsePackage()
@@ -201,7 +201,7 @@ protected function parsePackage($tokens)
201201
*/
202202
protected function parseSubpackage($tokens)
203203
{
204-
$this->_subpackage = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'subpackage');
204+
$this->_subpackage = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'subpackage', $this->phpcsFile);
205205
return $this->_subpackage;
206206

207207
}//end parseSubpackage()

CodeSniffer/CommentParser/CommentElement.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ class PHP_CodeSniffer_CommentParser_CommentElement extends PHP_CodeSniffer_Comme
5454
* element.
5555
* @param array $tokens The tokens that
5656
* make up this element.
57+
* @param PHP_CodeSniffer_File $phpcsFile The file that this element is in.
5758
*/
58-
public function __construct($previousElement, $tokens)
59+
public function __construct($previousElement, $tokens, PHP_CodeSniffer_File $phpcsFile)
5960
{
60-
parent::__construct($previousElement, $tokens, 'comment');
61+
parent::__construct($previousElement, $tokens, 'comment', $phpcsFile);
6162

6263
}//end __construct()
6364

@@ -96,13 +97,13 @@ private function _getShortCommentEndPos()
9697

9798
foreach ($this->tokens as $pos => $token) {
9899
$token = str_replace($whiteSpace, '', $token);
99-
if ($token === "\n") {
100+
if ($token === $this->phpcsFile->eolChar) {
100101
if ($found === false) {
101102
// Include newlines before short description.
102103
continue;
103104
} else {
104105
if (isset($this->tokens[($pos + 1)]) === true) {
105-
if ($this->tokens[($pos + 1)] === "\n") {
106+
if ($this->tokens[($pos + 1)] === $this->phpcsFile->eolChar) {
106107
return ($pos - 1);
107108
}
108109
} else {
@@ -197,11 +198,11 @@ public function getNewlineAfter()
197198
{
198199
$long = $this->getLongComment();
199200
if ($long !== '') {
200-
return strspn((strrev(rtrim($long, ' '))), "\n");
201+
return strspn((strrev(rtrim($long, ' '))), $this->phpcsFile->eolChar);
201202
} else {
202203
$endShort = ($this->_getShortCommentEndPos() + 1);
203204
$after = implode('', array_slice($this->tokens, $endShort));
204-
return strspn((trim($after, ' ')), "\n");
205+
return strspn((trim($after, ' ')), $this->phpcsFile->eolChar);
205206
}
206207

207208
}//end getNewlineAfter()

CodeSniffer/CommentParser/FunctionCommentParser.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ class PHP_CodeSniffer_CommentParser_FunctionCommentParser extends PHP_CodeSniffe
5959
/**
6060
* Constructs a PHP_CodeSniffer_CommentParser_FunctionCommentParser.
6161
*
62-
* @param string $comment The comment to parse.
62+
* @param string $comment The comment to parse.
63+
* @param PHP_CodeSniffer_File $phpcsFile The file that this comment is in.
6364
*/
64-
public function __construct($comment)
65+
public function __construct($comment, PHP_CodeSniffer_File $phpcsFile)
6566
{
66-
parent::__construct($comment);
67+
parent::__construct($comment, $phpcsFile);
6768

6869
}//end __construct()
6970

@@ -77,7 +78,7 @@ public function __construct($comment)
7778
*/
7879
protected function parseParam($tokens)
7980
{
80-
$param = new PHP_CodeSniffer_CommentParser_ParameterElement($this->previousElement, $tokens);
81+
$param = new PHP_CodeSniffer_CommentParser_ParameterElement($this->previousElement, $tokens, $this->phpcsFile);
8182
$this->_params[] = $param;
8283
return $param;
8384

@@ -93,7 +94,7 @@ protected function parseParam($tokens)
9394
*/
9495
protected function parseReturn($tokens)
9596
{
96-
$return = new PHP_CodeSniffer_CommentParser_PairElement($this->previousElement, $tokens, 'return');
97+
$return = new PHP_CodeSniffer_CommentParser_PairElement($this->previousElement, $tokens, 'return', $this->phpcsFile);
9798
$this->_return = $return;
9899
return $return;
99100

@@ -109,7 +110,7 @@ protected function parseReturn($tokens)
109110
*/
110111
protected function parseThrows($tokens)
111112
{
112-
$throws = new PHP_CodeSniffer_CommentParser_PairElement($this->previousElement, $tokens, 'throws');
113+
$throws = new PHP_CodeSniffer_CommentParser_PairElement($this->previousElement, $tokens, 'throws', $this->phpcsFile);
113114
$this->_throws[] = $throws;
114115
return $throws;
115116

CodeSniffer/CommentParser/MemberCommentParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class PHP_CodeSniffer_CommentParser_MemberCommentParser extends PHP_CodeSniffer_
4848
*/
4949
protected function parseVar($tokens)
5050
{
51-
$this->_var = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'var');
51+
$this->_var = new PHP_CodeSniffer_CommentParser_SingleElement($this->previousElement, $tokens, 'var', $this->phpcsFile);
5252
return $this->_var;
5353

5454
}//end parseVar()

CodeSniffer/CommentParser/PairElement.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ class PHP_CodeSniffer_CommentParser_PairElement extends PHP_CodeSniffer_CommentP
6969
* @param PHP_CodeSniffer_CommentParser_DocElement $previousElement The element before this one.
7070
* @param array $tokens The tokens that comprise this element.
7171
* @param string $tag The tag that this element represents.
72+
* @param PHP_CodeSniffer_File $phpcsFile The file that this element is in.
7273
*/
73-
public function __construct($previousElement, $tokens, $tag)
74+
public function __construct($previousElement, $tokens, $tag, PHP_CodeSniffer_File $phpcsFile)
7475
{
75-
parent::__construct($previousElement, $tokens, $tag);
76+
parent::__construct($previousElement, $tokens, $tag, $phpcsFile);
7677

7778
}//end __construct()
7879

CodeSniffer/CommentParser/ParameterElement.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,13 @@ class PHP_CodeSniffer_CommentParser_ParameterElement extends PHP_CodeSniffer_Com
8383
* @param array $tokens The tokens
8484
* that make up
8585
* this element.
86+
* @param PHP_CodeSniffer_File $phpcsFile The file that
87+
* this element
88+
* is in.
8689
*/
87-
public function __construct($previousElement, $tokens)
90+
public function __construct($previousElement, $tokens, PHP_CodeSniffer_File $phpcsFile)
8891
{
89-
parent::__construct($previousElement, $tokens, 'param');
92+
parent::__construct($previousElement, $tokens, 'param', $phpcsFile);
9093

9194
// Handle special variable type: array(x => y).
9295
$type = strtolower($this->_type);

CodeSniffer/CommentParser/SingleElement.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ class PHP_CodeSniffer_CommentParser_SingleElement extends PHP_CodeSniffer_Commen
5757
* @param PHP_CodeSniffer_CommentParser_DocElement $previousElement The element before this one.
5858
* @param array $tokens The tokens that comprise this element.
5959
* @param string $tag The tag that this element represents.
60+
* @param PHP_CodeSniffer_File $phpcsFile The file that this element is in.
6061
*/
61-
public function __construct($previousElement, $tokens, $tag)
62+
public function __construct($previousElement, $tokens, $tag, PHP_CodeSniffer_File $phpcsFile)
6263
{
63-
parent::__construct($previousElement, $tokens, $tag);
64+
parent::__construct($previousElement, $tokens, $tag, $phpcsFile);
6465

6566
}//end __construct()
6667

0 commit comments

Comments
 (0)