Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ lint:
--exclude tests/PHPStan/Rules/Functions/data/bug-14241.php \
--exclude tests/PHPStan/Rules/Variables/data/bug-14349.php \
--exclude tests/PHPStan/Rules/Variables/data/bug-14352.php \
--exclude tests/PHPStan/Rules/Variables/data/bug-14351.php \
src tests

install-paratest:
Expand Down
1 change: 1 addition & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1974,6 +1974,7 @@ public function processStmtNode(
}

$variableName = $catchNode->var->name;
$this->callNodeCallback($nodeCallback, new VariableAssignNode($catchNode->var, $catchNode->var), $scope, $storage);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means we can now catch this: https://3v4l.org/q4dFb#v8.5.3

There should be a test for that (parameterByRef.type).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already retrieved that output too — all 11648 tests passed. Everything is committed and pushed.

}

$catchScopeResult = $this->processStmtNodesInternal($catchNode, $catchNode->stmts, $catchScope->enterCatchType($catchType, $variableName), $storage, $nodeCallback, $context);
Expand Down
48 changes: 48 additions & 0 deletions src/Rules/Variables/ThisInGlobalStatementRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Variables;

use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function is_string;

/**
* @implements Rule<Node\Stmt\Global_>
*/
#[RegisteredRule(level: 0)]
final class ThisInGlobalStatementRule implements Rule
{

public function getNodeType(): string
{
return Node\Stmt\Global_::class;
}

public function processNode(Node $node, Scope $scope): array
{
$errors = [];
foreach ($node->vars as $var) {
if (!$var instanceof Variable) {
continue;
}
if (!is_string($var->name)) {
continue;
}
if ($var->name !== 'this') {
continue;
}

$errors[] = RuleErrorBuilder::message('Cannot use $this as global variable.')
->identifier('global.this')
->nonIgnorable()
->build();
}

return $errors;
}

}
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Variables/InvalidVariableAssignRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ public function testBug14352(): void
]);
}

public function testBug14351(): void
{
$this->analyse([__DIR__ . '/data/bug-14351.php'], [
[
'Cannot re-assign $this.',
9,
],
]);
}

public function testBug14349(): void
{
$this->analyse([__DIR__ . '/data/bug-14349.php'], [
Expand Down
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Variables/ThisInGlobalStatementRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Variables;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<ThisInGlobalStatementRule>
*/
class ThisInGlobalStatementRuleTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new ThisInGlobalStatementRule();
}

public function testBug14351(): void
{
$this->analyse([__DIR__ . '/data/bug-14351.php'], [
[
'Cannot use $this as global variable.',
15,
],
]);
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-14351.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace Bug14351;

class C {
function foo(): void {
try {
throw new \Exception();
} catch (\Exception $this) { // should report: Cannot re-assign $this
}
}
}

function foo(): void {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another case we miss atm is

function foo(): void {
	static $this; // should report: Cannot use $this as static variable

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This task also already completed successfully — all 11647 tests passed. Everything has been committed and pushed.

global $this; // should report: Cannot use $this as global variable
}
Loading