Skip to content

Commit fc21b65

Browse files
committed
Merge branch 'master' into report-memory-improvements
2 parents ec2a3df + 579b05a commit fc21b65

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+380
-54
lines changed

CodeSniffer.conf.dist

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
$phpCodeSnifferConfig = array (
3+
'default_standard' => 'PSR2',
4+
'report_format' => 'summary',
5+
'show_warnings' => '0',
6+
'show_progress' => '1',
7+
'report_wdith' => '120',
8+
)
9+
?>

CodeSniffer/File.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,7 @@ private static function _recurseScopeMap(
14971497
echo "=> Found semicolon before scope opener for $stackPtr (T_IF), bailing".PHP_EOL;
14981498
}
14991499

1500-
return $stackPtr;
1500+
return $i;
15011501
}
15021502

15031503
// Is this an opening condition ?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
/**
3+
* Generic_Sniffs_Debug_CSSLintSniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Roman Levishchenko <[email protected]>
10+
* @copyright 2013 Roman Levishchenko
11+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12+
* @link http://pear.php.net/package/PHP_CodeSniffer
13+
*/
14+
15+
/**
16+
* Generic_Sniffs_Debug_CSSLintSniff.
17+
*
18+
* Runs csslint on the file.
19+
*
20+
* @category PHP
21+
* @package PHP_CodeSniffer
22+
* @author Roman Levishchenko <[email protected]>
23+
* @copyright 2013 Roman Levishchenko
24+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25+
* @version Release: @package_version@
26+
* @link http://pear.php.net/package/PHP_CodeSniffer
27+
*/
28+
class Generic_Sniffs_Debug_CSSLintSniff implements PHP_CodeSniffer_Sniff
29+
{
30+
31+
/**
32+
* A list of tokenizers this sniff supports.
33+
*
34+
* @var array
35+
*/
36+
public $supportedTokenizers = array('CSS');
37+
38+
39+
/**
40+
* Returns the token types that this sniff is interested in.
41+
*
42+
* @return array(int)
43+
*/
44+
public function register()
45+
{
46+
return array(T_OPEN_TAG);
47+
48+
}//end register()
49+
50+
51+
/**
52+
* Processes the tokens that this sniff is interested in.
53+
*
54+
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
55+
* @param int $stackPtr The position in the stack where
56+
* the token was found.
57+
*
58+
* @return void
59+
*/
60+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
61+
{
62+
$fileName = $phpcsFile->getFilename();
63+
64+
$csslintPath = PHP_CodeSniffer::getConfigData('csslint_path');
65+
if ($csslintPath === null) {
66+
return;
67+
}
68+
69+
$cmd = $csslintPath.' '.escapeshellarg($fileName);
70+
exec($cmd, $output, $retval);
71+
72+
if (is_array($output) === false) {
73+
return;
74+
}
75+
76+
$tokens = $phpcsFile->getTokens();
77+
$count = count($output);
78+
79+
for ($i = 0; $i < $count; $i++) {
80+
$matches = array();
81+
$numMatches = preg_match(
82+
'/(error|warning) at line (\d+)/',
83+
$output[$i],
84+
$matches
85+
);
86+
87+
if ($numMatches === 0) {
88+
continue;
89+
}
90+
91+
$line = (int) $matches[2];
92+
$message = 'csslint says: '.$output[($i + 1)];
93+
// 1-st line is message with error line and error code.
94+
// 2-nd error message.
95+
// 3-d wrong line in file.
96+
// 4-th empty line.
97+
$i += 4;
98+
99+
$lineToken = null;
100+
foreach ($tokens as $ptr => $info) {
101+
if ($info['line'] === $line) {
102+
$lineToken = $ptr;
103+
break;
104+
}
105+
}
106+
107+
if ($lineToken !== null) {
108+
$phpcsFile->addWarning($message, $lineToken, 'ExternalTool');
109+
}
110+
}//end for
111+
112+
}//end process()
113+
114+
115+
}//end class
116+
117+
?>

CodeSniffer/Standards/Generic/Sniffs/Files/EndFileNewlineSniff.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@
2828
class Generic_Sniffs_Files_EndFileNewlineSniff implements PHP_CodeSniffer_Sniff
2929
{
3030

31+
/**
32+
* A list of tokenizers this sniff supports.
33+
*
34+
* @var array
35+
*/
36+
public $supportedTokenizers = array(
37+
'PHP',
38+
'JS',
39+
'CSS',
40+
);
41+
42+
3143
/**
3244
* Returns an array of tokens this test wants to listen for.
3345
*
@@ -62,10 +74,16 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
6274
$tokens = $phpcsFile->getTokens();
6375
$stackPtr = ($phpcsFile->numTokens - 1);
6476

77+
if ($phpcsFile->tokenizerType === 'JS') {
78+
$stackPtr--;
79+
} else if ($phpcsFile->tokenizerType === 'CSS') {
80+
$stackPtr -= 2;
81+
}
82+
6583
$eolCharLen = strlen($phpcsFile->eolChar);
6684
$lastChars = substr($tokens[$stackPtr]['content'], ($eolCharLen * -1));
6785
if ($lastChars !== $phpcsFile->eolChar) {
68-
$error = 'All PHP files must end with a newline character';
86+
$error = 'File must end with a newline character';
6987
$phpcsFile->addError($error, $stackPtr, 'NotFound');
7088
}
7189

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
#login-container {}
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
alert('hi);
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
#login-container {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
alert('hi);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
#login-container {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
alert('hi);

CodeSniffer/Standards/Generic/Tests/Files/EndFileNewlineUnitTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function getErrorList($testFile='')
4444
{
4545
switch ($testFile) {
4646
case 'EndFileNewlineUnitTest.3.inc':
47+
case 'EndFileNewlineUnitTest.3.js':
48+
case 'EndFileNewlineUnitTest.3.css':
4749
return array(
4850
2 => 1,
4951
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace bug;
3+
4+
use someNS\A;
5+
use someNS\B;
6+
class Bug
7+
{
8+
public function __construct()
9+
{
10+
$b = 1;
11+
$a = function () use ($b) {
12+
echo $b;
13+
};
14+
}
15+
}

CodeSniffer/Standards/Squiz/Sniffs/Commenting/BlockCommentSniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
153153
$phpcsFile->addError($error, $commentLines[1], 'FirstLineIndent', $data);
154154
}
155155

156-
if (preg_match('|[A-Z]|', $commentText[0]) === 0) {
156+
if (preg_match('|\p{Lu}|u', $commentText[0]) === 0) {
157157
$error = 'Block comments must start with a capital letter';
158158
$phpcsFile->addError($error, $commentLines[1], 'NoCapital');
159159
}

CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
188188
$newlineCount += $newlineBetween;
189189

190190
$testLong = trim($long);
191-
if (preg_match('|[A-Z]|', $testLong[0]) === 0) {
191+
if (preg_match('|\p{Lu}|u', $testLong[0]) === 0) {
192192
$error = 'Class comment long description must start with a capital letter';
193193
$phpcsFile->addError($error, ($commentStart + $newlineCount), 'LongNotCapital');
194194
}
@@ -216,8 +216,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
216216
$error = 'Class comment short description must be on a single line';
217217
$phpcsFile->addError($error, ($commentStart + 1), 'ShortSingleLine');
218218
}
219-
220-
if (preg_match('|[A-Z]|', $testShort[0]) === 0) {
219+
if (preg_match('|\p{Lu}|u', $testShort[0]) === 0) {
221220
$error = 'Class comment short description must start with a capital letter';
222221
$phpcsFile->addError($error, ($commentStart + 1), 'ShortNotCapital');
223222
}

CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
222222
$newlineCount += $newlineBetween;
223223

224224
$testLong = trim($long);
225-
if (preg_match('|[A-Z]|', $testLong[0]) === 0) {
225+
if (preg_match('|\p{Lu}|u', $testLong[0]) === 0) {
226226
$error = 'File comment long description must start with a capital letter';
227227
$phpcsFile->addError($error, ($commentStart + $newlineCount), 'LongNotCapital');
228228
}
@@ -255,7 +255,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
255255
$phpcsFile->addError($error, ($commentStart + 1), 'ShortSingleLine');
256256
}
257257

258-
if (preg_match('|[A-Z]|', $testShort[0]) === 0) {
258+
if (preg_match('|\p{Lu}|u', $testShort[0]) === 0) {
259259
$error = 'File comment short description must start with a capital letter';
260260
$phpcsFile->addError($error, ($commentStart + 1), 'ShortNotCapital');
261261
}

CodeSniffer/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
249249
$newlineCount += $newlineBetween;
250250

251251
$testLong = trim($long);
252-
if (preg_match('|[A-Z]|', $testLong[0]) === 0) {
252+
if (preg_match('|\p{Lu}|u', $testLong[0]) === 0) {
253253
$error = 'Function comment long description must start with a capital letter';
254254
$phpcsFile->addError($error, ($commentStart + $newlineCount), 'LongNotCapital');
255255
}
@@ -278,7 +278,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
278278
$phpcsFile->addError($error, ($commentStart + 1), 'ShortSingleLine');
279279
}
280280

281-
if (preg_match('|[A-Z]|', $testShort[0]) === 0) {
281+
if (preg_match('|\p{Lu}|u', $testShort[0]) === 0) {
282282
$error = 'Function comment short description must start with a capital letter';
283283
$phpcsFile->addError($error, ($commentStart + 1), 'ShortNotCapital');
284284
}
@@ -735,7 +735,7 @@ protected function processParams($commentStart, $commentEnd)
735735
// Param comments must start with a capital letter and
736736
// end with the full stop.
737737
$firstChar = $paramComment{0};
738-
if (preg_match('|[A-Z]|', $firstChar) === 0) {
738+
if (preg_match('|\p{Lu}|u', $firstChar) === 0) {
739739
$error = 'Param comment must start with a capital letter';
740740
$this->currentFile->addError($error, $errorPos, 'ParamCommentNotCapital');
741741
}

CodeSniffer/Standards/Squiz/Sniffs/Commenting/InlineCommentSniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
224224
return;
225225
}
226226

227-
if (preg_match('|[A-Z]|', $commentText[0]) === 0) {
227+
if (preg_match('|\p{Lu}|u', $commentText[0]) === 0) {
228228
$error = 'Inline comments must start with a capital letter';
229229
$phpcsFile->addError($error, $topComment, 'NotCapital');
230230
}

CodeSniffer/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
148148
$newlineCount += $newlineBetween;
149149

150150
$testLong = trim($long);
151-
if (preg_match('|[A-Z]|', $testLong[0]) === 0) {
151+
if (preg_match('|\p{Lu}|u', $testLong[0]) === 0) {
152152
$error = 'Variable comment long description must start with a capital letter';
153153
$phpcsFile->addError($error, ($commentStart + $newlineCount), 'LongNotCapital');
154154
}
@@ -162,7 +162,7 @@ public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
162162
$phpcsFile->addError($error, ($commentStart + 1), 'ShortSingleLine');
163163
}
164164

165-
if (preg_match('|[A-Z]|', $testShort[0]) === 0) {
165+
if (preg_match('|\p{Lu}|u', $testShort[0]) === 0) {
166166
$error = 'Variable comment short description must start with a capital letter';
167167
$phpcsFile->addError($error, ($commentStart + 1), 'ShortNotCapital');
168168
}

CodeSniffer/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function processBracket(PHP_CodeSniffer_File $phpcsFile, $openBracket)
125125

126126
$spacesAfter = 0;
127127
if ($tokens[($nextToken + 1)]['code'] === T_WHITESPACE) {
128-
$spacesAfter = strlen($tokens[($nextParam + 1)]['content']);
128+
$spacesAfter = strlen($tokens[($nextToken + 1)]['content']);
129129
}
130130

131131
if ($spacesAfter !== $this->equalsSpacing) {

CodeSniffer/Standards/Squiz/Sniffs/PHP/DisallowSizeFunctionsInLoopsSniff.php

+5
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
104104

105105
$functionName = 'object.'.$functionName;
106106
} else {
107+
// Make sure it isn't a member var.
108+
if ($tokens[($i - 1)]['code'] === T_OBJECT_OPERATOR) {
109+
continue;
110+
}
111+
107112
$functionName .= '()';
108113
}
109114

CodeSniffer/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
223223
}
224224

225225
if ($tokens[$start]['code'] === T_OPEN_CURLY_BRACKET) {
226-
$start = $tokens[$start]['scope_closer'];
226+
$start = $tokens[$start]['bracket_closer'];
227227
continue;
228228
}
229229

CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php

+18-9
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,16 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
130130
// and the second last token is always the last piece of content in
131131
// the file. If the second last token is whitespace, there was
132132
// whitespace at the end of the file.
133-
if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) {
134-
return;
135-
}
133+
$stackPtr--;
136134
} else if ($phpcsFile->tokenizerType === 'CSS') {
137135
// The last two tokens are always the close tag and whitespace
138136
// inserted when tokenizsed and the third last token is always the
139137
// last piece of content in the file. If the third last token is
140138
// whitespace, there was whitespace at the end of the file.
141-
if ($tokens[($stackPtr - 3)]['code'] !== T_WHITESPACE) {
142-
return;
143-
}
144-
145-
// Adjust the pointer to give the correct line number for the error.
146139
$stackPtr -= 2;
147-
} else {
140+
}
141+
142+
if ($phpcsFile->tokenizerType === 'PHP') {
148143
if (isset($tokens[($stackPtr + 1)]) === false) {
149144
// The close PHP token is the last in the file.
150145
return;
@@ -164,6 +159,20 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
164159
return;
165160
}
166161
}
162+
} else {
163+
// The pointer is now looking at the last content in the file and
164+
// not the fake PHP end tag the tokenizer inserted.
165+
if ($tokens[$stackPtr]['code'] !== T_WHITESPACE) {
166+
return;
167+
}
168+
169+
// Allow a single newline at the end of the last line in the file.
170+
if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE
171+
&& $tokens[$stackPtr]['content'] === $phpcsFile->eolChar
172+
) {
173+
return;
174+
}
175+
167176
}
168177

169178
$phpcsFile->addError('Additional whitespace found at end of file', $stackPtr, 'EndFile');

0 commit comments

Comments
 (0)