Skip to content

Commit

Permalink
Deep inspect prefixed PHPDocs for invalid types
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 8, 2021
1 parent 2f456b9 commit 003d235
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
2 changes: 2 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ services:

-
class: PHPStan\PhpDoc\PhpDocNodeResolver
arguments:
deepInspectTypes: %featureToggles.deepInspectTypes%

-
class: PHPStan\PhpDoc\PhpDocStringResolver
Expand Down
28 changes: 27 additions & 1 deletion src/PhpDoc/PhpDocNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeTraverser;

class PhpDocNodeResolver
{
Expand All @@ -37,10 +38,17 @@ class PhpDocNodeResolver

private ConstExprNodeResolver $constExprNodeResolver;

public function __construct(TypeNodeResolver $typeNodeResolver, ConstExprNodeResolver $constExprNodeResolver)
private bool $deepInspectTypes;

public function __construct(
TypeNodeResolver $typeNodeResolver,
ConstExprNodeResolver $constExprNodeResolver,
bool $deepInspectTypes = false
)
{
$this->typeNodeResolver = $typeNodeResolver;
$this->constExprNodeResolver = $constExprNodeResolver;
$this->deepInspectTypes = $deepInspectTypes;
}

/**
Expand Down Expand Up @@ -463,6 +471,24 @@ private function shouldSkipType(string $tagName, Type $type): bool
return false;
}

if ($this->deepInspectTypes) {
$shouldSkip = false;
TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$shouldSkip): Type {
if ($type instanceof ErrorType) {
$shouldSkip = true;
return $type;
}
if ($type instanceof NeverType && !$type->isExplicit()) {
$shouldSkip = true;
return $type;
}

return $traverse($type);
});

return $shouldSkip;
}

if ($type instanceof ErrorType) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,10 @@ public function testDeepInspectTypes(): void
]);
}

public function testBug3723(): void
{
$this->deepInspectTypes = false;
$this->analyse([__DIR__ . '/data/bug-3723.php'], []);
}

}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-3723.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Bug3723;

class HelloWorld
{
/**
* @param array<string, mixed> $bar The raw frame
*
* @psalm-param array{
* foo?: Foo::TEST,
* bar: string
* } $bar
*/
public function foo(array $bar): void
{
}
}

0 comments on commit 003d235

Please sign in to comment.