Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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, new TypeExpr($catchType)), $scope, $storage);
}

$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;
}

}
44 changes: 44 additions & 0 deletions src/Rules/Variables/ThisInStaticStatementRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Variables;

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

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

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

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

$errors[] = RuleErrorBuilder::message('Cannot use $this as static variable.')
->identifier('static.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
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Variables/ParameterOutAssignedTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,19 @@ public function testBug14124b(): void
$this->analyse([__DIR__ . '/data/bug-14124b.php'], []);
}

public function testCatchVariable(): void
{
$this->analyse([__DIR__ . '/data/parameter-out-catch-variable.php'], [
[
'Parameter &$p @param-out type of function ParameterOutCatchVariable\foo() expects int, Exception given.',
11,
],
[
'Parameter &$p by-ref type of function ParameterOutCatchVariable\bar() expects int, Exception given.',
19,
'You can change the parameter out type with @param-out PHPDoc tag.',
],
]);
}

}
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,
],
]);
}

}
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Variables/ThisInStaticStatementRuleTest.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<ThisInStaticStatementRule>
*/
class ThisInStaticStatementRuleTest extends RuleTestCase
{

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

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

}
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-14351.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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
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
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
}

function bar(): void {
static $this; // should report: Cannot use $this as static variable
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace ParameterOutCatchVariable;

/**
* @param-out int $p
*/
function foo(&$p): void {
try {
throw new \Exception();
} catch (\Exception $p) {

}
}

function bar(int &$p): void {
try {
throw new \Exception();
} catch (\Exception $p) {

}
}
Loading