From 57e4231d22842948129c04ccd1c1f51623017ce6 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Fri, 20 Apr 2018 15:44:52 +0100 Subject: [PATCH] Treat trait properties as definitions (#40) * Tests: add test for traits with properties * findVariableScope: consider traits also --- VariableAnalysis/Lib/Helpers.php | 2 +- .../Tests/CodeAnalysis/VariableAnalysisTest.php | 12 ++++++++++++ .../fixtures/TraitWithPropertiesFixture.php | 11 +++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 VariableAnalysis/Tests/CodeAnalysis/fixtures/TraitWithPropertiesFixture.php diff --git a/VariableAnalysis/Lib/Helpers.php b/VariableAnalysis/Lib/Helpers.php index 01bbbd16..8875f242 100644 --- a/VariableAnalysis/Lib/Helpers.php +++ b/VariableAnalysis/Lib/Helpers.php @@ -174,7 +174,7 @@ public static function findVariableScope(File $phpcsFile, int $stackPtr) { if (($scopeCode === T_FUNCTION) || ($scopeCode === T_CLOSURE)) { return $scopePtr; } - if (($scopeCode === T_CLASS) || ($scopeCode === T_INTERFACE)) { + if (in_array($scopeCode, [T_CLASS, T_INTERFACE, T_TRAIT])) { $in_class = true; } } diff --git a/VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php b/VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php index 67ddfd1f..13052cd9 100644 --- a/VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php +++ b/VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php @@ -401,4 +401,16 @@ public function testVariableFunctionCallsCountAsUsage() { $expectedErrors = []; $this->assertEquals($expectedErrors, $lines); } + + public function testTraitsAllowPropertyDefinitions() { + $fixtureFile = $this->getFixture('TraitWithPropertiesFixture.php'); + $phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile); + $phpcsFile->process(); + $lines = $this->getWarningLineNumbersFromFile($phpcsFile); + $expectedWarnings = []; + $this->assertEquals($expectedWarnings, $lines); + $lines = $this->getErrorLineNumbersFromFile($phpcsFile); + $expectedErrors = []; + $this->assertEquals($expectedErrors, $lines); + } } diff --git a/VariableAnalysis/Tests/CodeAnalysis/fixtures/TraitWithPropertiesFixture.php b/VariableAnalysis/Tests/CodeAnalysis/fixtures/TraitWithPropertiesFixture.php new file mode 100644 index 00000000..5785afb1 --- /dev/null +++ b/VariableAnalysis/Tests/CodeAnalysis/fixtures/TraitWithPropertiesFixture.php @@ -0,0 +1,11 @@ +