Handle array/nested types

parent 150beaf4
......@@ -36,10 +36,13 @@ final class Type
*/
private $ast;
public function __construct(string $typeAsString, ?Node $ast = null)
private $nestedType;
public function __construct(string $typeAsString, ?Node $ast = null, self $nestedType = null)
{
$this->typeAsString = ltrim($typeAsString, '\\');
$this->ast = $ast;
$this->nestedType = $nestedType;
}
public static function constructUnknownFromNode(Node $node): self
......@@ -114,4 +117,14 @@ final class Type
{
return $this->typeAsString === $otherType->typeAsString;
}
public function hasNestedType(): bool
{
return (bool) $this->nestedType;
}
public function getNestedType(): self
{
return $this->nestedType;
}
}
......@@ -21,6 +21,7 @@ use Hgraca\AppMapper\Infrastructure\Parser\NikicPhpParser\Exception\AstNodeNotFo
use Hgraca\AppMapper\Infrastructure\Parser\NikicPhpParser\Exception\NotImplementedException;
use Hgraca\AppMapper\Infrastructure\Parser\NikicPhpParser\Exception\UnknownFqcnException;
use Hgraca\AppMapper\Infrastructure\Parser\NikicPhpParser\NodeCollection;
use Hgraca\PhpExtension\String\StringHelper;
use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
......@@ -153,6 +154,10 @@ final class TypeFactory
throw new UnknownFqcnException("Can't create the type from '$string'.");
}
if (StringHelper::hasEnding('[]', $string)) {
return new Type('array', null, $this->buildTypeFromString(StringHelper::removeFromEnd('[]', $string)));
}
try {
return new Type($string, $this->nodeCollection->getAstNode($string));
} catch (AstNodeNotFoundException $e) {
......
......@@ -41,6 +41,7 @@ use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Use_;
......@@ -470,6 +471,9 @@ final class TypeResolverInjectorVisitor extends NodeVisitorAbstract
private function getTypeCollectionFromUses(Node $node, string $type): TypeCollection
{
$positionOfBrackets = mb_strpos($type, '[');
$arrayList = $positionOfBrackets ? mb_substr($type, $positionOfBrackets) : '';
$nestedType = rtrim($type, '[]');
$namespaceNode = ParentConnectorVisitor::getFirstParentNodeOfType($node, Namespace_::class);
foreach ($namespaceNode->stmts ?? [] as $use) {
......@@ -481,10 +485,14 @@ final class TypeResolverInjectorVisitor extends NodeVisitorAbstract
$useType = (string) $useUse->name;
if (
$type === $useType
|| $type === (string) $useUse->alias
|| StringHelper::hasEnding($type, $useType)
$nestedType === $useType
|| $nestedType === (string) $useUse->alias
|| StringHelper::hasEnding($nestedType, $useType)
) {
if ($arrayList) {
return new TypeCollection($this->typeFactory->buildTypeFromString($useType . $arrayList));
}
return self::getTypeCollectionFromNode($useUse);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment