fix(diagnostics): update checking of $this usage to only error in static methods (#545)
parent
ff746a836d
commit
9b1fafae58
|
@ -1,6 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
function foo()
|
|
||||||
{
|
|
||||||
return $this;
|
|
||||||
}
|
|
|
@ -1,3 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
echo $this;
|
|
|
@ -99,11 +99,9 @@ class TreeAnalyzer
|
||||||
// Find the first ancestor that's a class method. Return an error
|
// Find the first ancestor that's a class method. Return an error
|
||||||
// if there is none, or if the method is static.
|
// if there is none, or if the method is static.
|
||||||
$method = $node->getFirstAncestor(Node\MethodDeclaration::class);
|
$method = $node->getFirstAncestor(Node\MethodDeclaration::class);
|
||||||
if ($method === null || $method->isStatic()) {
|
if ($method->isStatic()) {
|
||||||
$this->diagnostics[] = new Diagnostic(
|
$this->diagnostics[] = new Diagnostic(
|
||||||
$method === null
|
"\$this can not be used in static methods.",
|
||||||
? "\$this can only be used in an object context."
|
|
||||||
: "\$this can not be used in static methods.",
|
|
||||||
Range::fromNode($node),
|
Range::fromNode($node),
|
||||||
null,
|
null,
|
||||||
DiagnosticSeverity::ERROR,
|
DiagnosticSeverity::ERROR,
|
||||||
|
|
|
@ -71,42 +71,6 @@ class InvalidThisUsageTest extends TestCase
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testThisInFunctionProducesError()
|
|
||||||
{
|
|
||||||
$diagnostics = $this->collectDiagnostics(
|
|
||||||
__DIR__ . '/../../fixtures/diagnostics/errors/this_in_function.php'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertCount(1, $diagnostics);
|
|
||||||
$this->assertDiagnostic(
|
|
||||||
$diagnostics[0],
|
|
||||||
'$this can only be used in an object context.',
|
|
||||||
DiagnosticSeverity::ERROR,
|
|
||||||
new Range(
|
|
||||||
new Position(4, 11),
|
|
||||||
new Position(4, 16)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testThisInRoot()
|
|
||||||
{
|
|
||||||
$diagnostics = $this->collectDiagnostics(
|
|
||||||
__DIR__ . '/../../fixtures/diagnostics/errors/this_in_root.php'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertCount(1, $diagnostics);
|
|
||||||
$this->assertDiagnostic(
|
|
||||||
$diagnostics[0],
|
|
||||||
'$this can only be used in an object context.',
|
|
||||||
DiagnosticSeverity::ERROR,
|
|
||||||
new Range(
|
|
||||||
new Position(2, 5),
|
|
||||||
new Position(2, 10)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testThisInMethodProducesNoError()
|
public function testThisInMethodProducesNoError()
|
||||||
{
|
{
|
||||||
$diagnostics = $this->collectDiagnostics(
|
$diagnostics = $this->collectDiagnostics(
|
||||||
|
|
Loading…
Reference in New Issue