Skip to content

Commit beeb36f

Browse files
committed
Making sure var sniffs work with types; only Squiz.Commenting.VariableComment needed a change (ref #2517)
1 parent e988f62 commit beeb36f

12 files changed

+110
-17
lines changed

src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc

+21
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,24 @@ class ABC {
4141
$propE;
4242
static private /*comment*/ $propF;
4343
}
44+
45+
class MyClass
46+
{
47+
public string $var = null;
48+
protected ?Folder\ClassName $var = null;
49+
50+
var int $var = null;
51+
static int $var = null;
52+
53+
private int $_var = null;
54+
55+
public $foo, $bar, $var = null;
56+
public ?string $foo,
57+
$bar,
58+
$var = null;
59+
60+
protected
61+
array
62+
$foo,
63+
$bar;
64+
}

src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed

+21
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,24 @@ class ABC {
4040
$propE;
4141
private static /*comment*/ $propF;
4242
}
43+
44+
class MyClass
45+
{
46+
public string $var = null;
47+
protected ?Folder\ClassName $var = null;
48+
49+
var int $var = null;
50+
static int $var = null;
51+
52+
private int $_var = null;
53+
54+
public $foo, $bar, $var = null;
55+
public ?string $foo,
56+
$bar,
57+
$var = null;
58+
59+
protected
60+
array
61+
$foo,
62+
$bar;
63+
}

src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public function getErrorList()
3636
38 => 1,
3737
41 => 1,
3838
42 => 1,
39+
50 => 2,
40+
51 => 1,
41+
55 => 1,
42+
56 => 1,
43+
62 => 1,
3944
];
4045

4146
}//end getErrorList()
@@ -55,6 +60,7 @@ public function getWarningList()
5560
13 => 1,
5661
14 => 1,
5762
15 => 1,
63+
53 => 1,
5864
];
5965

6066
}//end getWarningList()

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

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public function processMemberVar(File $phpcsFile, $stackPtr)
3636
T_VAR,
3737
T_STATIC,
3838
T_WHITESPACE,
39+
T_STRING,
40+
T_NS_SEPARATOR,
41+
T_NULLABLE,
3942
];
4043

4144
$commentEnd = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);

src/Standards/Squiz/Sniffs/NamingConventions/ValidVariableNameSniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected function processVariable(File $phpcsFile, $stackPtr)
6868
// check the main part of the variable name.
6969
$originalVarName = $varName;
7070
if (substr($varName, 0, 1) === '_') {
71-
$objOperator = $phpcsFile->findPrevious([T_WHITESPACE], ($stackPtr - 1), null, true);
71+
$objOperator = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
7272
if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) {
7373
// The variable lives within a class, and is referenced like
7474
// this: MyClass::$_variable, so we don't know its scope.

src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.inc

+21-1
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,26 @@ class Foo
340340
*
341341
* @var array
342342
*/
343-
public static $variableName = array();
343+
public static array $variableName = array();
344344

345345
}
346+
347+
class Foo
348+
{
349+
350+
/**
351+
* Short description of the member variable.
352+
*
353+
* @var array
354+
*/
355+
public array $variableName = array();
356+
357+
358+
// Not "/**" style comment.
359+
//
360+
// @var string
361+
private ?Folder\ClassName $_incorrectCommentStyle = null;
362+
363+
364+
var int $noComment = 1;
365+
}

src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.inc.fixed

+21-1
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,26 @@ class Foo
340340
*
341341
* @var array
342342
*/
343-
public static $variableName = array();
343+
public static array $variableName = array();
344344

345345
}
346+
347+
class Foo
348+
{
349+
350+
/**
351+
* Short description of the member variable.
352+
*
353+
* @var array
354+
*/
355+
public array $variableName = array();
356+
357+
358+
// Not "/**" style comment.
359+
//
360+
// @var string
361+
private ?Folder\ClassName $_incorrectCommentStyle = null;
362+
363+
364+
var int $noComment = 1;
365+
}

src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public function getErrorList()
5656
294 => 1,
5757
311 => 1,
5858
336 => 1,
59+
361 => 1,
60+
364 => 1,
5961
];
6062

6163
}//end getErrorList()

src/Standards/Squiz/Tests/NamingConventions/ValidVariableNameUnitTest.inc

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ class MyClass
1212
$_varName = 'hello';
1313

1414
public $varName = 'hello';
15-
public $var_name = 'hello';
15+
public bool $var_name = false;
1616
public $varname = 'hello';
1717
public $_varName = 'hello';
1818

1919
protected $varName = 'hello';
20-
protected $var_name = 'hello';
20+
protected string $var_name = 'hello';
2121
protected $varname = 'hello';
22-
protected $_varName = 'hello';
22+
protected ?string $_varName = 'hello';
2323

2424
private $_varName = 'hello';
2525
private $_var_name = 'hello';
26-
private $_varname = 'hello';
26+
private int $_varname = 1;
2727
private $varName = 'hello';
2828
}
2929

src/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.inc

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
class MyClass
33
{
44
private $var1 = null;
5-
protected $var2 = null;
5+
protected ?int $var2 = null;
66
public $var3 = null;
77
$var4 = null;
88
}
@@ -54,7 +54,7 @@ class MyClass {
5454
$var = null;
5555
}
5656

57-
public $mCounter, $mSearchUser, $mSearchPeriodStart, $mSearchPeriodEnd,
57+
public string $mCounter, $mSearchUser, $mSearchPeriodStart, $mSearchPeriodEnd,
5858
$mTestFilter;
5959

6060
protected $mCounter,

src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.inc

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class MyClass
44
public $var1 = 'value';
55

66

7-
public $var2 = 'value';
7+
public ?int $var2 = 'value';
88

99
public $var3 = 'value';
1010

@@ -17,7 +17,7 @@ interface MyInterface
1717
public $var1 = 'value';
1818

1919

20-
public $var2 = 'value';
20+
public ?Folder\ClassName $var2 = 'value';
2121

2222
protected $var3 = 'value';
2323
}//end interface
@@ -27,9 +27,9 @@ class MyClass
2727
{
2828

2929

30-
public $var1 = 'value';
30+
public string $var1 = 'value';
3131

32-
private $var2 = 'value';
32+
private string $var2 = 'value';
3333

3434

3535
protected $var3 = 'value';

src/Standards/Squiz/Tests/WhiteSpace/MemberVarSpacingUnitTest.inc.fixed

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class MyClass
44

55
public $var1 = 'value';
66

7-
public $var2 = 'value';
7+
public ?int $var2 = 'value';
88

99
public $var3 = 'value';
1010

@@ -16,7 +16,7 @@ interface MyInterface
1616

1717
public $var1 = 'value';
1818

19-
public $var2 = 'value';
19+
public ?Folder\ClassName $var2 = 'value';
2020

2121
protected $var3 = 'value';
2222
}//end interface
@@ -25,9 +25,9 @@ interface MyInterface
2525
class MyClass
2626
{
2727

28-
public $var1 = 'value';
28+
public string $var1 = 'value';
2929

30-
private $var2 = 'value';
30+
private string $var2 = 'value';
3131

3232
protected $var3 = 'value';
3333

0 commit comments

Comments
 (0)