Skip to content

Commit 879da2d

Browse files
Levivbsirbrillig
authored andcommitted
allow self and static references in trait (#76)
* allow self and static references in trait * use getWarningLineNumbersFromFile instead of getErrorLineNumbersFromFile for testTraitWithMembersWarnings * Add line numbers of expected warnings for testTraitWithMembersWarnings
1 parent 22ab15e commit 879da2d

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

VariableAnalysis/Lib/Helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static function areAnyConditionsAClosure(File $phpcsFile, array $conditio
5353

5454
public static function areAnyConditionsAClass(array $conditions) {
5555
foreach (array_reverse($conditions, true) as $scopePtr => $scopeCode) {
56-
if ($scopeCode === T_CLASS) {
56+
if ($scopeCode === T_CLASS || $scopeCode === T_TRAIT) {
5757
return true;
5858
}
5959
}

VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,28 @@ public function testClassWithMembersErrors() {
140140
$this->assertEquals($expectedErrors, $lines);
141141
}
142142

143+
public function testTraitWithMembersWarnings() {
144+
$fixtureFile = $this->getFixture('TraitWithMembersFixture.php');
145+
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
146+
$phpcsFile->process();
147+
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
148+
$expectedErrors = [
149+
5,
150+
6,
151+
7,
152+
8,
153+
9,
154+
10,
155+
11,
156+
12,
157+
13,
158+
18,
159+
19,
160+
64,
161+
];
162+
$this->assertEquals($expectedErrors, $lines);
163+
}
164+
143165
public function testClassWithMembersWarnings() {
144166
$fixtureFile = $this->getFixture('ClassWithMembersFixture.php');
145167
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
trait TraitWithoutMembers {
4+
function method_without_param() {
5+
echo $var;
6+
echo "xxx $var xxx";
7+
echo "xxx {$var} xxx";
8+
echo "xxx $var $var2 xxx";
9+
echo "xxx {$var} {$var2} xxx";
10+
func($var);
11+
func(12, $var);
12+
func($var, 12);
13+
func(12, $var, 12);
14+
$var = 'set the var';
15+
echo $var;
16+
echo "xxx $var xxx";
17+
echo "xxx {$var} xxx";
18+
echo "xxx $var $var2 xxx";
19+
echo "xxx {$var} {$var2} xxx";
20+
func($var);
21+
func(12, $var);
22+
func($var, 12);
23+
func(12, $var, 12);
24+
$this->method_with_member_var();
25+
return $var;
26+
}
27+
28+
function method_with_param($param) {
29+
echo $param;
30+
echo "xxx $param xxx";
31+
echo "xxx {$param} xxx";
32+
$param = 'set the param';
33+
echo $param;
34+
echo "xxx $param xxx";
35+
echo "xxx {$param} xxx";
36+
$this->method_with_member_var();
37+
return $param;
38+
}
39+
40+
function method_with_member_var() {
41+
echo $this->member_var;
42+
echo self::$static_member_var;
43+
}
44+
}
45+
46+
trait TraitWithMembers {
47+
public $member_var;
48+
static $static_member_var;
49+
50+
function method_with_member_var() {
51+
echo $this->member_var;
52+
echo $this->no_such_member_var;
53+
echo self::$static_member_var;
54+
echo self::$no_such_static_member_var;
55+
echo SomeOtherClass::$external_static_member_var;
56+
}
57+
}
58+
59+
trait TraitWithLateStaticBinding {
60+
public static $static_member_var;
61+
62+
static function method_with_late_static_binding($param) {
63+
static::some_method($param);
64+
static::some_method($var); // should report a warning
65+
static::some_method(static::CONSTANT, $param);
66+
$called_class = get_called_class();
67+
echo $called_class::$static_member_var;
68+
}
69+
}

0 commit comments

Comments
 (0)