Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3d98b67

Browse files
committed
NullPropertyDeclarationFixer - Support grouped properties and comments
1 parent 1665be8 commit 3d98b67

2 files changed

Lines changed: 105 additions & 16 deletions

File tree

src/Fixer/ClassNotation/NullPropertyDeclarationFixer.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,29 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
5656
continue;
5757
}
5858

59-
$varTokenIndex = $tokens->getNextMeaningfulToken($index);
59+
while (true) {
60+
$varTokenIndex = $index = $tokens->getNextMeaningfulToken($index);
6061

61-
if (!$tokens[$varTokenIndex]->isGivenKind(T_VARIABLE)) {
62-
continue;
63-
}
62+
if (!$tokens[$index]->isGivenKind(T_VARIABLE)) {
63+
break;
64+
}
6465

65-
$equalsTokenIndex = $tokens->getNextMeaningfulToken($varTokenIndex);
66+
$index = $tokens->getNextMeaningfulToken($index);
6667

67-
if (!$tokens[$equalsTokenIndex]->equals('=')) {
68-
continue;
69-
}
68+
if ($tokens[$index]->equals('=')) {
69+
$valueTokenIndex = $index = $tokens->getNextMeaningfulToken($index);
7070

71-
$valueTokenIndex = $tokens->getNextMeaningfulToken($equalsTokenIndex);
72-
$valueToken = $tokens[$valueTokenIndex];
71+
if ($tokens[$index]->equals([T_STRING, 'null'], false)) {
72+
$tokens->clearRange($varTokenIndex + 1, $valueTokenIndex);
73+
}
7374

74-
if ($valueToken->equals([T_STRING, 'null'], false)) {
75-
$tokens->clearRange($varTokenIndex + 1, $valueTokenIndex);
76-
}
75+
++$index;
76+
}
7777

78-
$index = $valueTokenIndex;
78+
if (!$tokens[$index]->equals(',')) {
79+
break;
80+
}
81+
}
7982
}
8083
}
8184
}

tests/Fixer/ClassNotation/NullPropertyDeclarationFixerTest.php

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,25 @@ public function provideCases()
5353
'<?php class Foo { var $bar; }',
5454
'<?php class Foo { var $bar = null; }',
5555
],
56+
[
57+
'<?php class Foo { VAR $bar; }',
58+
'<?php class Foo { VAR $bar = null; }',
59+
],
5660
[
5761
'<?php class Foo { public $bar; }',
5862
'<?php class Foo { public $bar = NULL; }',
5963
],
6064
[
61-
'<?php class Foo { public $bar; }',
62-
'<?php class Foo { public $bar = nuLL; }',
65+
'<?php class Foo { PUblic $bar; }',
66+
'<?php class Foo { PUblic $bar = nuLL; }',
67+
],
68+
[
69+
'<?php trait Foo { public $bar; }',
70+
'<?php trait Foo { public $bar = nuLL; }',
71+
],
72+
[
73+
'<?php class Foo {/* */public/* */$bar;/* */}',
74+
'<?php class Foo {/* */public/* */$bar/* */=/* */null;/* */}',
6375
],
6476
[
6577
'<?php class Foo { public $bar; protected $baz; }',
@@ -71,6 +83,80 @@ public function provideCases()
7183
[
7284
'<?php class Foo { public function bar() { return null; } }',
7385
],
86+
[
87+
'<?php class Foo { protected $bar, $baz, $qux; }',
88+
'<?php class Foo { protected $bar = null, $baz = null, $qux = null; }',
89+
],
90+
[
91+
'<?php class Foo { protected $bar, $baz = \'baz\', $qux; }',
92+
'<?php class Foo { protected $bar, $baz = \'baz\', $qux = null; }',
93+
],
94+
[
95+
'<?php trait Foo { public $bar; } abstract class Bar { protected $bar, $baz = \'baz\', $qux; }',
96+
'<?php trait Foo { public $bar = null; } abstract class Bar { protected $bar, $baz = \'baz\', $qux = null; }',
97+
],
98+
[
99+
'<?php class Foo { public function foo() { return null; } public $bar; public function baz() { return null; } }',
100+
'<?php class Foo { public function foo() { return null; } public $bar = null; public function baz() { return null; } }',
101+
],
102+
[
103+
"<?php class#\nFoo#\n{#\nprotected#\n\$bar,#\n\$baz,#\n\$qux;#\n}",
104+
"<?php class#\nFoo#\n{#\nprotected#\n\$bar#\n=#\nnull,#\n\$baz#\n=#\nnull,#\n\$qux#\n=#\nnull;#\n}",
105+
],
106+
[
107+
'<?php class Foo { const FOO = null; }',
108+
],
109+
];
110+
}
111+
112+
/**
113+
* @param string $expected
114+
* @param null|string $input
115+
*
116+
* @requires PHP 7.0
117+
* @dataProvider providePhp70Cases
118+
*/
119+
public function testFixPhp70($expected, $input = null)
120+
{
121+
$this->doTest($expected, $input);
122+
}
123+
124+
public function providePhp70Cases()
125+
{
126+
return [
127+
[
128+
'<?php new class () { public $bar; };',
129+
'<?php new class () { public $bar = null; };',
130+
],
131+
[
132+
'<?php class Foo { public function foo() { return new class() { private $bar; }; } }',
133+
'<?php class Foo { public function foo() { return new class() { private $bar = null; }; } }',
134+
],
135+
[
136+
'<?php class Foo { public function foo() { return new class() { private $bar; }; } } trait Baz { public $baz; }',
137+
'<?php class Foo { public function foo() { return new class() { private $bar = null; }; } } trait Baz { public $baz = null; }',
138+
],
139+
];
140+
}
141+
142+
/**
143+
* @param string $expected
144+
* @param null|string $input
145+
*
146+
* @requires PHP 7.1
147+
* @dataProvider providePhp71Cases
148+
*/
149+
public function testFixPhp71($expected, $input = null)
150+
{
151+
$this->doTest($expected, $input);
152+
}
153+
154+
public function providePhp71Cases()
155+
{
156+
return [
157+
[
158+
'<?php class Foo { public const FOO = null; }',
159+
],
74160
];
75161
}
76162
}

0 commit comments

Comments
 (0)