8000 Support anonymous classes (closes GH-153) (#167) · djoos/Symfony-coding-standard@fde0732 · GitHub
[go: up one dir, main page]

Skip to content

Commit fde0732

Browse files
Khartirdjoos
authored andcommitted
Support anonymous classes (closes GH-153) (#167)
1 parent a00d1db commit fde0732

9 files changed

+76
-7
lines changed

Symfony/Sniffs/Classes/PropertyDeclarationSniff.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function register()
5151
{
5252
return array(
5353
T_CLASS,
54+
T_ANON_CLASS
5455
);
5556
}
5657

@@ -81,10 +82,15 @@ public function process(File $phpcsFile, $stackPtr)
8182
$wantedTokens = array(
8283
T_PUBLIC,
8384
T_PROTECTED,
84 10000 -
T_PRIVATE
85+
T_PRIVATE,
86+
T_ANON_CLASS
8587
);
8688

8789
while ($scope) {
90+
if (T_ANON_CLASS === $tokens[$scope]['code']) {
91+
$scope = $tokens[$scope]['scope_closer'];
92+
continue;
93+
}
8894
$scope = $phpcsFile->findNext(
8995
$wantedTokens,
9096
$scope + 1,

Symfony/Sniffs/Functions/ScopeOrderSniff.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function register()
5252
return array(
5353
T_CLASS,
5454
T_INTERFACE,
55+
T_ANON_CLASS,
5556
);
5657
}
5758

@@ -89,11 +90,19 @@ public function process(File $phpcsFile, $stackPtr)
8990
}
9091

9192
$function = $phpcsFile->findNext(
92-
T_FUNCTION,
93+
array(
94+
T_ANON_CLASS,
95+
T_FUNCTION,
96+
),
9397
$function + 1,
9498
$end
9599
);
96100

101+
if (T_ANON_CLASS === $tokens[$function]['code']) {
102+
$function = $tokens[$function]['scope_closer'];
103+
continue;
104+
}
105+
97106
if (isset($tokens[$function]['parenthesis_opener'])) {
98107
$scope = $phpcsFile->findPrevious($scopes, $function -1, $stackPtr);
99108
$name = $phpcsFile->findNext(

Symfony/Sniffs/Objects/ObjectInstantiationSniff.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ public function process(File $phpcsFile, $stackPtr)
8181
$object = $stackPtr;
8282
$line = $tokens[$object]['line'];
8383

84+
if (T_ANON_CLASS === $tokens[$object + 2]['code']) {
85+
if ($tokens[$object + 3]['code'] !== T_OPEN_PARENTHESIS) {
86+
$phpcsFile->addError(
87+
'Use parentheses when instantiating classes',
88+
$stackPtr,
89+
'Invalid'
90+
);
91+
}
92+
return;
93+
}
94+
8495
while ($object && $tokens[$object]['line'] === $line) {
8596
$object = $phpcsFile->findNext($allowed, $object + 1);
8697

Symfony/Tests/Classes/PropertyDeclarationUnitTest.inc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,22 @@ class Foo
77
}
88

99
private $bar;
10-
}
10+
}
11+
new class()
12+
{
13+
public function __construct()
14+
{
15+
}
16+
17+
private $property;
18+
};
19+
class Factory
20+
{
21+
public function createObject()
22+
{
23+
return new class()
24+
{
25+
private $property;
26+
};
27+
}
28+
}

Symfony/Tests/Classes/PropertyDeclarationUnitTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class PropertyDeclarationUnitTest extends AbstractSniffUnitTest
4343
public function getErrorList()
4444
{
4545
return array(
46-
9 => 1,
46+
9 => 1,
47+
17 => 1,
4748
);
4849
}
4950

Symfony/Tests/Functions/ScopeOrderUnitTest.inc

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,23 @@ class Foo
77
protected function baz(){}
88

99
public function qux(){}
10-
}
10+
}
11+
new class()
12+
{
13+
private function bar(){}
14+
15+
protected function baz(){}
16+
17+
public function qux(){}
18+
};
19+
class Factory
20+
{
21+
private function createObject()
22+
{
23+
return new class()
24+
{
25+
public function foo(){}
26+
};
27+
}
28+
}
29+

Symfony/Tests/Functions/ScopeOrderUnitTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ class ScopeOrderUnitTest extends AbstractSniffUnitTest
4343
public function getErrorList()
4444
{
4545
return array(
46-
7 => 1,
47-
9 => 1,
46+
7 => 1,
47+
9 => 1,
48+
15 => 1,
49+
17 => 1,
4850
);
4951
}
5052

Symfony/Tests/Objects/ObjectInstantiationUnitTest.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ new Foo::bar;
1212
new Foo::bar[true];
1313
new $foo->bar;
1414
new $foo->bar[true];
15+
new class {};
1516

1617
// good
1718
new Foo();
@@ -35,3 +36,4 @@ new self(true);
3536
new self($this->foo);
3637
new Foo::bar();
3738
new $foo->bar();
39+
new class() implements AnyInterface {};

Symfony/Tests/Objects/ObjectInstantiationUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function getErrorList()
5454
12 => 1,
5555
13 => 1,
5656
14 => 1,
57+
15 => 1,
5758
);
5859
}
5960

0 commit comments

Comments
 (0)
0