Skip to content

Commit b4c42ec

Browse files
committed
Merge branch 'master' into report-memory-improvements
2 parents bcf5993 + 34528cf commit b4c42ec

19 files changed

+387
-8
lines changed

CodeSniffer/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ protected function processTokenWithinScope(
8080
return;
8181
}
8282

83+
// Stop if the constructor doesn't have a body, like when it is abstract.
84+
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
85+
return;
86+
}
87+
8388
$endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
8489
$startIndex = $stackPtr;
8590
while ($doubleColonIndex = $phpcsFile->findNext(array(T_DOUBLE_COLON), $startIndex, $endFunctionIndex)) {

CodeSniffer/Standards/Generic/Sniffs/PHP/LowerCaseConstantSniff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
7979
if ($tokens[$prevPtr]['code'] === T_CLASS
8080
|| $tokens[$prevPtr]['code'] === T_EXTENDS
8181
|| $tokens[$prevPtr]['code'] === T_IMPLEMENTS
82+
|| $tokens[$prevPtr]['code'] === T_NEW
8283
) {
8384
return;
8485
}

CodeSniffer/Standards/Generic/Sniffs/PHP/UpperCaseConstantSniff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
7070
if ($tokens[$prevPtr]['code'] === T_CLASS
7171
|| $tokens[$prevPtr]['code'] === T_EXTENDS
7272
|| $tokens[$prevPtr]['code'] === T_IMPLEMENTS
73+
|| $tokens[$prevPtr]['code'] === T_NEW
7374
) {
7475
return;
7576
}

CodeSniffer/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ class Generic_Sniffs_WhiteSpace_ScopeIndentSniff implements PHP_CodeSniffer_Snif
6868
*/
6969
protected $nonIndentingScopes = array();
7070

71+
/**
72+
* Stores the indent of the last PHP open tag we found.
73+
*
74+
* This value is used to calculate the expected indent of top level structures
75+
* so we don't assume they are always at column 1. If PHP code is embedded inside
76+
* HTML (etc.) code, then the starting column for that code may not be column 1.
77+
*
78+
* @var int
79+
*/
80+
private $_openTagIndent = 0;
81+
7182

7283
/**
7384
* Returns an array of tokens this test wants to listen for.
@@ -76,7 +87,9 @@ class Generic_Sniffs_WhiteSpace_ScopeIndentSniff implements PHP_CodeSniffer_Snif
7687
*/
7788
public function register()
7889
{
79-
return PHP_CodeSniffer_Tokens::$scopeOpeners;
90+
$tokens = PHP_CodeSniffer_Tokens::$scopeOpeners;
91+
$tokens[] = T_OPEN_TAG;
92+
return $tokens;
8093

8194
}//end register()
8295

@@ -94,6 +107,16 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
94107
{
95108
$tokens = $phpcsFile->getTokens();
96109

110+
// We only want to record the indent of open tags, not process them.
111+
if ($tokens[$stackPtr]['code'] == T_OPEN_TAG) {
112+
if (empty($tokens[$stackPtr]['conditions']) === true) {
113+
// Only record top-level PHP tags.
114+
$this->_openTagIndent = ($tokens[$stackPtr]['column'] - 1);
115+
}
116+
117+
return;
118+
}
119+
97120
// If this is an inline condition (ie. there is no scope opener), then
98121
// return, as this is not a new scope.
99122
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
@@ -362,7 +385,7 @@ protected function calculateExpectedIndent(array $tokens, $stackPtr)
362385
// carefully in another sniff.
363386
return $tokens[$stackPtr]['column'];
364387
} else {
365-
return 1;
388+
return ($this->_openTagIndent + 1);
366389
}
367390
}
368391

CodeSniffer/Standards/Generic/Tests/NamingConventions/ConstructorNameUnitTest.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ class MyClass extends \MyNamespace\SomeClass
3636
}
3737

3838
}
39+
40+
abstract class MyAbstractClass extends \MyNamespace\SomeClass
41+
{
42+
abstract public function __construct();
43+
}
3944
?>

CodeSniffer/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,13 @@ use Zend\Log\Writer\NULL as NullWriter;
5454
new \Zend\Log\Writer\NULL()
5555

5656
class True extends Null implements False {}
57+
58+
use Something\True;
59+
class MyClass
60+
{
61+
public function myFunction()
62+
{
63+
$var = array('foo' => new True());
64+
}
65+
}
5766
?>

CodeSniffer/Standards/Generic/Tests/PHP/UpperCaseConstantUnitTest.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,13 @@ new \Zend\Log\Writer\Null()
5555

5656
class True extends Null implements False {}
5757

58+
use Something\True;
59+
class MyClass
60+
{
61+
public function myFunction()
62+
{
63+
$var = array('foo' => new True());
64+
}
65+
}
66+
5867
?>

CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.inc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,3 +437,25 @@ class AnonymousFn
437437
}
438438
}
439439
?>
440+
441+
<div>
442+
<?php
443+
if ($myvar == 'test') {
444+
echo 'something';
445+
}
446+
?>
447+
<div>
448+
<div>
449+
<?php
450+
if ($myvar == 'test') {
451+
echo 'something';
452+
}
453+
?>
454+
<div>
455+
<div>
456+
<?php
457+
if ($myvar == 'test') {
458+
echo 'something';
459+
}
460+
?>
461+
<div>

CodeSniffer/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public function getErrorList()
6767
380 => 1,
6868
387 => 1,
6969
397 => 1,
70+
450 => 1,
71+
457 => 1,
72+
458 => 1,
7073
);
7174

7275
}//end getErrorList()
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
/**
3+
* Squiz_Sniffs_CSS_ShorthandSizeSniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <[email protected]>
10+
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
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+
* Squiz_Sniffs_CSS_ShorthandSizeSniff.
17+
*
18+
* Ensure sizes are defined using shorthand notation where possible, except in the
19+
* case where shorthand becomes 3 values.
20+
*
21+
* @category PHP
22+
* @package PHP_CodeSniffer
23+
* @author Greg Sherwood <[email protected]>
24+
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
25+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
26+
* @version Release: @package_version@
27+
* @link http://pear.php.net/package/PHP_CodeSniffer
28+
*/
29+
class Squiz_Sniffs_CSS_ShorthandSizeSniff implements PHP_CodeSniffer_Sniff
30+
{
31+
32+
/**
33+
* A list of tokenizers this sniff supports.
34+
*
35+
* @var array
36+
*/
37+
public $supportedTokenizers = array('CSS');
38+
39+
/**
40+
* A list of styles that we shouldn't check.
41+
*
42+
* These have values that looks like sizes, but are not.
43+
*
44+
* @var array
45+
*/
46+
public $excludeStyles = array(
47+
'background-position',
48+
'box-shadow',
49+
'transform-origin',
50+
'-webkit-transform-origin',
51+
'-ms-transform-origin',
52+
);
53+
54+
55+
/**
56+
* Returns the token types that this sniff is interested in.
57+
*
58+
* @return array(int)
59+
*/
60+
public function register()
61+
{
62+
return array(T_STYLE);
63+
64+
}//end register()
65+
66+
67+
/**
68+
* Processes the tokens that this sniff is interested in.
69+
*
70+
* @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
71+
* @param int $stackPtr The position in the stack where
72+
* the token was found.
73+
*
74+
* @return void
75+
*/
76+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
77+
{
78+
$tokens = $phpcsFile->getTokens();
79+
80+
// Some styles look like shorthand but are not actually a set of 4 sizes.
81+
$style = strtolower($tokens[$stackPtr]['content']);
82+
if (in_array($style, $this->excludeStyles) === true) {
83+
return;
84+
}
85+
86+
// Get the whole style content.
87+
$end = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
88+
$content = $phpcsFile->getTokensAsString(($stackPtr + 1), ($end - $stackPtr - 1));
89+
$content = trim($content, ': ');
90+
91+
// Account for a !important annotation.
92+
if (substr($content, -10) === '!important') {
93+
$content = substr($content, 0, -10);
94+
$content = trim($content);
95+
}
96+
97+
// Check if this style value is a set of numbers with optional prefixes.
98+
$content = preg_replace('/\s+/', ' ', $content);
99+
$values = array();
100+
$num = preg_match_all(
101+
'/([0-9]+)([a-zA-Z]{2}\s+|%\s+|\s+)/',
102+
$content.' ',
103+
$values,
104+
PREG_SET_ORDER
105+
);
106+
107+
// Only interested in styles that have multiple sizes defined.
108+
if ($num < 2) {
109+
return;
110+
}
111+
112+
// Rebuild the content we matched to ensure we got everything.
113+
$matched = '';
114+
foreach ($values as $value) {
115+
$matched .= $value[0];
116+
}
117+
118+
if ($content !== trim($matched)) {
119+
return;
120+
}
121+
122+
if ($num === 3) {
123+
$expected = trim($content.' '.$values[1][1].$values[1][2]);
124+
$error = 'Shorthand syntax not allowed here; use %s instead';
125+
$data = array($expected);
126+
$phpcsFile->addError($error, $stackPtr, 'NotAllowed', $data);
127+
return;
128+
}
129+
130+
if ($num === 2) {
131+
if ($values[0][0] !== $values[1][0]) {
132+
// Both values are different, so it is already shorthand.
133+
return;
134+
}
135+
} else if ($values[0][0] !== $values[2][0] || $values[1][0] !== $values[3][0]) {
136+
// Can't shorthand this.
137+
return;
138+
}
139+
140+
if ($values[0][0] === $values[1][0]) {
141+
// All values are the same.
142+
$expected = $values[0][0];
143+
} else {
144+
$expected = $values[0][0].' '.$values[1][0];
145+
}
146+
147+
$expected = preg_replace('/\s+/', ' ', trim($expected));
148+
149+
$error = 'Size definitions must use shorthand if available; expected "%s" but found "%s"';
150+
$data = array(
151+
$expected,
152+
$content,
153+
);
154+
155+
$phpcsFile->addError($error, $stackPtr, 'NotUsed', $data);
156+
157+
}//end process()
158+
159+
160+
}//end class
161+
?>

0 commit comments

Comments
 (0)