-
Notifications
You must be signed in to change notification settings - Fork 488
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Debugging function -
PHPStan\debugScope()
- Loading branch information
1 parent
c50b71f
commit 5909fb2
Showing
8 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Debug; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\MutatingScope; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function count; | ||
use function implode; | ||
use function sprintf; | ||
use function strtolower; | ||
|
||
/** | ||
* @implements Rule<Node\Expr\FuncCall> | ||
*/ | ||
final class DebugScopeRule implements Rule | ||
{ | ||
|
||
public function __construct(private ReflectionProvider $reflectionProvider) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\FuncCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node->name instanceof Node\Name) { | ||
return []; | ||
} | ||
|
||
$functionName = $this->reflectionProvider->resolveFunctionName($node->name, $scope); | ||
if ($functionName === null) { | ||
return []; | ||
} | ||
|
||
if (strtolower($functionName) !== 'phpstan\debugscope') { | ||
return []; | ||
} | ||
|
||
if (!$scope instanceof MutatingScope) { | ||
return []; | ||
} | ||
|
||
$parts = []; | ||
foreach ($scope->debug() as $key => $row) { | ||
$parts[] = sprintf('%s: %s', $key, $row); | ||
} | ||
|
||
if (count($parts) === 0) { | ||
$parts[] = 'Scope is empty'; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message( | ||
implode("\n", $parts), | ||
)->nonIgnorable()->identifier('phpstan.debugScope')->build(), | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan; | ||
|
||
/** | ||
* @phpstan-pure | ||
* @return mixed | ||
* | ||
* @throws void | ||
*/ | ||
function debugScope() // phpcs:ignore Squiz.Functions.GlobalFunction.Found | ||
{ | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Debug; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use function implode; | ||
|
||
/** | ||
* @extends RuleTestCase<DebugScopeRule> | ||
*/ | ||
class DebugScopeRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new DebugScopeRule($this->createReflectionProvider()); | ||
} | ||
|
||
public function testRuleInPhpStanNamespace(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/debug-scope.php'], [ | ||
[ | ||
'Scope is empty', | ||
7, | ||
], | ||
[ | ||
implode("\n", [ | ||
'$a (Yes): int', | ||
'$b (Yes): int', | ||
'$debug (Yes): bool', | ||
'native $a (Yes): int', | ||
'native $b (Yes): int', | ||
'native $debug (Yes): bool', | ||
]), | ||
10, | ||
], | ||
[ | ||
implode("\n", [ | ||
'$a (Yes): int', | ||
'$b (Yes): int', | ||
'$debug (Yes): bool', | ||
'$c (Maybe): 1', | ||
'native $a (Yes): int', | ||
'native $b (Yes): int', | ||
'native $debug (Yes): bool', | ||
'native $c (Maybe): 1', | ||
'condition about $c #1: if $debug=false then $c is *ERROR* (No)', | ||
'condition about $c #2: if $debug=true then $c is 1 (Yes)', | ||
]), | ||
16, | ||
], | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace DebugScope; | ||
|
||
use function PHPStan\debugScope; | ||
|
||
debugScope(); | ||
|
||
function (int $a, int $b, bool $debug): void { | ||
debugScope(); | ||
|
||
if ($debug) { | ||
$c = 1; | ||
} | ||
|
||
debugScope(); | ||
}; |