Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always import classes #119

Merged
merged 5 commits into from Nov 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/HookDocsRule.php
Expand Up @@ -11,7 +11,10 @@
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
use PHPStan\PhpDoc\Tag\ParamTag;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
Expand Down Expand Up @@ -67,7 +70,7 @@ public function processNode(Node $node, Scope $scope): array
$this->currentNode = $node;
$this->currentScope = $scope;

if (!($name instanceof \PhpParser\Node\Name)) {
if (!($name instanceof Name)) {
return [];
}

Expand All @@ -91,7 +94,7 @@ public function processNode(Node $node, Scope $scope): array
* @param \PHPStan\PhpDoc\ResolvedPhpDocBlock $resolvedPhpDoc
* @return array<int,\PHPStan\Rules\RuleError>
*/
public function validateDocBlock(\PHPStan\PhpDoc\ResolvedPhpDocBlock $resolvedPhpDoc): array
public function validateDocBlock(ResolvedPhpDocBlock $resolvedPhpDoc): array
{
// Count all documented `@param` tag strings in the docblock.
$numberOfParamTagStrings = substr_count($resolvedPhpDoc->getPhpDocString(), '* @param ');
Expand Down Expand Up @@ -169,7 +172,7 @@ public function validateParamCount(int $numberOfParamTagStrings): void
*/
public function validateParamDocumentation(
int $numberOfParamTags,
\PHPStan\PhpDoc\ResolvedPhpDocBlock $resolvedPhpDoc
ResolvedPhpDocBlock $resolvedPhpDoc
): void {
$nodeArgs = $this->currentNode->getArgs();
$numberOfParams = count($nodeArgs) - 1;
Expand All @@ -182,7 +185,7 @@ public function validateParamDocumentation(
// We might have an invalid `@param` tag because it's named `$this`.
if (strpos($resolvedPhpDoc->getPhpDocString(), ' $this') !== false) {
foreach ($nodeArgs as $param) {
if (($param->value instanceof \PhpParser\Node\Expr\Variable) && $param->value->name === 'this') {
if (($param->value instanceof Variable) && $param->value->name === 'this') {
// PHPStan does not detect param tags named `$this`, it skips the tag.
// We can indirectly detect this by checking the actual parameter name,
// and if one of them is `$this` assume that's the problem.
Expand Down
3 changes: 2 additions & 1 deletion src/IsWpErrorRule.php
Expand Up @@ -10,6 +10,7 @@

use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function processNode(Node $node, Scope $scope): array
{
$name = $node->name;

if (! ($name instanceof \PhpParser\Node\Name)) {
if (! ($name instanceof Name)) {
return [];
}

Expand Down
3 changes: 2 additions & 1 deletion src/WpThemeGetDynamicMethodReturnTypeExtension.php
Expand Up @@ -20,6 +20,7 @@
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use WP_Theme;

class WpThemeGetDynamicMethodReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
{
Expand All @@ -46,7 +47,7 @@ class WpThemeGetDynamicMethodReturnTypeExtension implements \PHPStan\Type\Dynami

public function getClass(): string
{
return 'WP_Theme';
return WP_Theme::class;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
Expand Down
3 changes: 2 additions & 1 deletion tests/HookDocsRuleTest.php
Expand Up @@ -4,6 +4,7 @@

namespace SzepeViktor\PHPStan\WordPress\Tests;

use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\FileTypeMapper;
use SzepeViktor\PHPStan\WordPress\HookDocsRule;
Expand All @@ -13,7 +14,7 @@
*/
class HookDocsRuleTest extends \PHPStan\Testing\RuleTestCase
{
protected function getRule(): \PHPStan\Rules\Rule
protected function getRule(): Rule
{
/** @var \PHPStan\Type\FileTypeMapper */
$fileTypeMapper = self::getContainer()->getByType(FileTypeMapper::class);
Expand Down
3 changes: 2 additions & 1 deletion tests/IsWpErrorRuleTest.php
Expand Up @@ -4,6 +4,7 @@

namespace SzepeViktor\PHPStan\WordPress\Tests;

use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use SzepeViktor\PHPStan\WordPress\IsWpErrorRule;

Expand All @@ -12,7 +13,7 @@
*/
class IsWpErrorRuleTest extends \PHPStan\Testing\RuleTestCase
{
protected function getRule(): \PHPStan\Rules\Rule
protected function getRule(): Rule
{
/** @var \PHPStan\Rules\RuleLevelHelper */
$ruleLevelHelper = self::getContainer()->getByType(RuleLevelHelper::class);
Expand Down
2 changes: 1 addition & 1 deletion tests/data/esc_sql.php
Expand Up @@ -18,4 +18,4 @@
assertType('string', esc_sql('someValueToEscape'));

// Wrong type provided (esc_sql() returns an empty string in that case)
assertType('string', esc_sql(new \stdClass()));
assertType('string', esc_sql(new stdClass()));
3 changes: 2 additions & 1 deletion tests/data/get_post.php
Expand Up @@ -4,10 +4,11 @@

namespace SzepeViktor\PHPStan\WordPress\Tests;

use stdClass;
use function PHPStan\Testing\assertType;

/** @var \WP_Post $wpPostType */
$wpPostType = new \stdClass();
$wpPostType = new stdClass();

// Default output
assertType('WP_Post|null', get_post());
Expand Down