Skip to content

Commit 9b1fafa

Browse files
MaartenStaafelixfbecker
authored andcommitted
fix(diagnostics): update checking of $this usage to only error in static methods (#545)
1 parent ff746a8 commit 9b1fafa

File tree

4 files changed

+2
-49
lines changed

4 files changed

+2
-49
lines changed

fixtures/diagnostics/errors/this_in_function.php

Lines changed: 0 additions & 6 deletions
This file was deleted.

fixtures/diagnostics/errors/this_in_root.php

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/TreeAnalyzer.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,9 @@ private function collectDiagnostics($node)
9999
// Find the first ancestor that's a class method. Return an error
100100
// if there is none, or if the method is static.
101101
$method = $node->getFirstAncestor(Node\MethodDeclaration::class);
102-
if ($method === null || $method->isStatic()) {
102+
if ($method->isStatic()) {
103103
$this->diagnostics[] = new Diagnostic(
104-
$method === null
105-
? "\$this can only be used in an object context."
106-
: "\$this can not be used in static methods.",
104+
"\$this can not be used in static methods.",
107105
Range::fromNode($node),
108106
null,
109107
DiagnosticSeverity::ERROR,

tests/Diagnostics/InvalidThisUsageTest.php

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -71,42 +71,6 @@ public function testThisInStaticMethodProducesError()
7171
);
7272
}
7373

74-
public function testThisInFunctionProducesError()
75-
{
76-
$diagnostics = $this->collectDiagnostics(
77-
__DIR__ . '/../../fixtures/diagnostics/errors/this_in_function.php'
78-
);
79-
80-
$this->assertCount(1, $diagnostics);
81-
$this->assertDiagnostic(
82-
$diagnostics[0],
83-
'$this can only be used in an object context.',
84-
DiagnosticSeverity::ERROR,
85-
new Range(
86-
new Position(4, 11),
87-
new Position(4, 16)
88-
)
89-
);
90-
}
91-
92-
public function testThisInRoot()
93-
{
94-
$diagnostics = $this->collectDiagnostics(
95-
__DIR__ . '/../../fixtures/diagnostics/errors/this_in_root.php'
96-
);
97-
98-
$this->assertCount(1, $diagnostics);
99-
$this->assertDiagnostic(
100-
$diagnostics[0],
101-
'$this can only be used in an object context.',
102-
DiagnosticSeverity::ERROR,
103-
new Range(
104-
new Position(2, 5),
105-
new Position(2, 10)
106-
)
107-
);
108-
}
109-
11074
public function testThisInMethodProducesNoError()
11175
{
11276
$diagnostics = $this->collectDiagnostics(

0 commit comments

Comments
 (0)