Skip to content

Commit 57e4231

Browse files
authored
Treat trait properties as definitions (#40)
* Tests: add test for traits with properties * findVariableScope: consider traits also
1 parent a5a01ba commit 57e4231

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

VariableAnalysis/Lib/Helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public static function findVariableScope(File $phpcsFile, int $stackPtr) {
174174
if (($scopeCode === T_FUNCTION) || ($scopeCode === T_CLOSURE)) {
175175
return $scopePtr;
176176
}
177-
if (($scopeCode === T_CLASS) || ($scopeCode === T_INTERFACE)) {
177+
if (in_array($scopeCode, [T_CLASS, T_INTERFACE, T_TRAIT])) {
178178
$in_class = true;
179179
}
180180
}

VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,16 @@ public function testVariableFunctionCallsCountAsUsage() {
401401
$expectedErrors = [];
402402
$this->assertEquals($expectedErrors, $lines);
403403
}
404+
405+
public function testTraitsAllowPropertyDefinitions() {
406+
$fixtureFile = $this->getFixture('TraitWithPropertiesFixture.php');
407+
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
408+
$phpcsFile->process();
409+
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
410+
$expectedWarnings = [];
411+
$this->assertEquals($expectedWarnings, $lines);
412+
$lines = $this->getErrorLineNumbersFromFile($phpcsFile);
413+
$expectedErrors = [];
414+
$this->assertEquals($expectedErrors, $lines);
415+
}
404416
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
trait Hello {
4+
protected $storedHello;
5+
public $helloOptions = [];
6+
public function sayHelloWorld() {
7+
echo "hello world";
8+
}
9+
abstract public function getWorld();
10+
}
11+

0 commit comments

Comments
 (0)