Skip to content

Commit

Permalink
Add phpstan support
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdekker committed Jun 19, 2024
1 parent 4bbe15c commit d10c9e5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
2 changes: 0 additions & 2 deletions src/PHPStan/ArraysReturnExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace DR\Utils\PHPStan;

use DR\Utils\Arrays;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDoc\TypeStringResolver;
Expand Down Expand Up @@ -38,7 +37,6 @@ public function isStaticMethodSupported(MethodReflection $methodReflection): boo
*/
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type
{
/** @var Array_ $disallowedTypes */
[$items, $disallowedTypes] = $methodCall->getArgs();

/** @var ArrayType $arrayType */
Expand Down
3 changes: 0 additions & 3 deletions src/PHPStan/AssertTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use DR\Utils\Assert;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDoc\TypeStringResolver;
Expand Down Expand Up @@ -38,9 +37,7 @@ public function isStaticMethodSupported(MethodReflection $methodReflection): boo
*/
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type
{
/** @var Array_ $allowedTypes */
[$item, $allowedTypes] = $methodCall->getArgs();

$types = $this->getTypesForArg($item, $scope);

// convert the disallowed types as string to phpstan types
Expand Down
18 changes: 12 additions & 6 deletions src/PHPStan/TypeUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DR\Utils\PHPStan;

use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name;
Expand All @@ -14,21 +15,26 @@
class TypeUtil
{
/**
* @param Arg $arg arg of type Array
*
* @return Type[]
*/
public static function getTypesFromStringArray(TypeStringResolver $typeStringResolver, Array_ $typesAsString): array
public static function getTypesFromStringArray(TypeStringResolver $typeStringResolver, Arg $arg): array
{
$disallowedStanTypes = [];
foreach ($typesAsString->items as $item) {
$argValue = $arg->value;
assert($argValue instanceof Array_);

$types = [];
foreach ($argValue->items as $item) {
if ($item?->value instanceof String_) {
// type definition is string, convert to type object
$disallowedStanTypes[] = $typeStringResolver->resolve($item->value->value);
$types[] = $typeStringResolver->resolve($item->value->value);
} elseif ($item?->value instanceof ClassConstFetch && $item->value->class instanceof Name) {
// type definition is class-string, convert to type object
$disallowedStanTypes[] = $typeStringResolver->resolve($item->value->class->toString());
$types[] = $typeStringResolver->resolve($item->value->class->toString());
}
}

return $disallowedStanTypes;
return $types;
}
}

0 comments on commit d10c9e5

Please sign in to comment.