Cleanup and var implementation

This commit is contained in:
Jaapio
2022-10-28 16:27:10 +02:00
parent b66b6dc644
commit 25d696587f
12 changed files with 259 additions and 45 deletions
+4 -4
View File
@@ -76,7 +76,7 @@ final class StandardTagFactory implements TagFactory
public const REGEX_TAGNAME = '[\w\-\_\\\\:]+';
/**
* @var array<class-string<Tag>> An array with a tag as a key, and an
* @var array<class-string<Tag>|Factory> An array with a tag as a key, and an
* FQCN to a class that handles it as an array value.
*/
private $tagHandlerMappings = [
@@ -177,7 +177,7 @@ final class StandardTagFactory implements TagFactory
}
if (is_object($handler)) {
Assert::implementsInterface($handler, TagFactory::class);
Assert::implementsInterface($handler, Factory::class);
$this->tagHandlerMappings[$tagName] = $handler;
return;
@@ -283,12 +283,12 @@ final class StandardTagFactory implements TagFactory
}
}
$parameterName = $parameter->getName();
if (isset($locator[$typeHint])) {
$arguments[] = $locator[$typeHint];
$arguments[$parameterName] = $locator[$typeHint];
continue;
}
$parameterName = $parameter->getName();
if (isset($locator[$parameterName])) {
$arguments[$parameterName] = $locator[$parameterName];
continue;
+1 -1
View File
@@ -10,7 +10,7 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
interface PHPStanFactory
{
public function create(PhpDocTagNode $node, Context $context): Tag;
public function create(PhpDocTagNode $node, ?Context $context): Tag;
public function supports(PhpDocTagNode $node, ?Context $context): bool;
}
+1 -1
View File
@@ -28,7 +28,7 @@ final class ParamFactory implements PHPStanFactory
$this->descriptionFactory = $descriptionFactory;
}
public function create(PhpDocTagNode $node, Context $context): Tag
public function create(PhpDocTagNode $node, ?Context $context): Tag
{
$tagValue = $node->value;
Assert::isInstanceOf($tagValue, ParamTagValueNode::class);
+21 -13
View File
@@ -44,7 +44,7 @@ use function strtolower;
/**
* @internal This class is not part of the BC promise of this library.
*/
class TypeFactory
final class TypeFactory
{
private TypeResolver $resolver;
@@ -53,7 +53,7 @@ class TypeFactory
$this->resolver = $resolver;
}
public function createType(TypeNode $type, Context $context): ?Type
public function createType(TypeNode $type, ?Context $context): ?Type
{
switch (get_class($type)) {
case ArrayTypeNode::class:
@@ -77,6 +77,7 @@ class TypeFactory
return $this->createFromCallable($type, $context);
case ConstTypeNode::class:
return null;
case GenericTypeNode::class:
return $this->createFromGeneric($type, $context);
@@ -85,22 +86,29 @@ class TypeFactory
case IntersectionTypeNode::class:
return new Intersection(
array_map(
fn (TypeNode $nestedType) => $this->createType($nestedType, $context),
$type->types
array_filter(
array_map(
fn (TypeNode $nestedType) => $this->createType($nestedType, $context),
$type->types
)
)
);
case NullableTypeNode::class:
return new Nullable(
$this->createType($type->type, $context)
);
$nestedType = $this->createType($type->type, $context);
if ($nestedType === null) {
return null;
}
return new Nullable($nestedType);
case UnionTypeNode::class:
return new Compound(
array_map(
fn (TypeNode $nestedType) => $this->createType($nestedType, $context),
$type->types
array_filter(
array_map(
fn (TypeNode $nestedType) => $this->createType($nestedType, $context),
$type->types
)
)
);
@@ -115,7 +123,7 @@ class TypeFactory
}
}
private function createFromGeneric(GenericTypeNode $type, Context $context): Type
private function createFromGeneric(GenericTypeNode $type, ?Context $context): Type
{
switch (strtolower($type->type->name)) {
case 'array':
@@ -162,7 +170,7 @@ class TypeFactory
}
}
private function createFromCallable(CallableTypeNode $type, Context $context): Callable_
private function createFromCallable(CallableTypeNode $type, ?Context $context): Callable_
{
return new Callable_();
}
+47
View File
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use phpDocumentor\Reflection\Types\Context;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use Webmozart\Assert\Assert;
use function trim;
/**
* @internal This class is not part of the BC promise of this library.
*/
final class VarFactory implements PHPStanFactory
{
private TypeFactory $typeFactory;
private DescriptionFactory $descriptionFactory;
public function __construct(TypeFactory $typeFactory, DescriptionFactory $descriptionFactory)
{
$this->typeFactory = $typeFactory;
$this->descriptionFactory = $descriptionFactory;
}
public function create(PhpDocTagNode $node, ?Context $context): Tag
{
$tagValue = $node->value;
Assert::isInstanceOf($tagValue, VarTagValueNode::class);
return new Var_(
trim($tagValue->variableName, '$'),
$this->typeFactory->createType($tagValue->type, $context),
$this->descriptionFactory->create($tagValue->description, $context)
);
}
public function supports(PhpDocTagNode $node, ?Context $context): bool
{
return $node->value instanceof VarTagValueNode;
}
}