Refactor all node tree algorithms for a single one

parent 9b432fe9
......@@ -18,26 +18,16 @@ declare(strict_types=1);
namespace Hgraca\AppMapper\Infrastructure\Parser\NikicPhpParser\Exception;
use Hgraca\AppMapper\Core\Port\Parser\Exception\ParserException;
use Hgraca\AppMapper\Infrastructure\Parser\NikicPhpParser\NodeTypeManagerTrait;
use PhpParser\Node;
final class CircularReferenceDetectedException extends ParserException
{
public function __construct(Node $node, string $fqcn)
{
$relevantInfo = [];
$loopNode = $node;
while ($loopNode->hasAttribute('parentNode')) {
$relevantInfo[] = get_class($loopNode) . ' => '
. (property_exists($loopNode, 'name')
? $loopNode->name
: 'no_name'
);
$loopNode = $loopNode->getAttribute('parentNode');
}
$message = "Circular reference detected when adding type '$fqcn' to collection in node:\n"
. json_encode($relevantInfo, JSON_PRETTY_PRINT);
parent::__construct($message);
parent::__construct(
"Circular reference detected when adding type '$fqcn' to collection in node:\n"
. NodeTypeManagerTrait::resolveNodeTreeAsJson($node)
);
}
}
......@@ -18,6 +18,7 @@ declare(strict_types=1);
namespace Hgraca\AppMapper\Infrastructure\Parser\NikicPhpParser\Exception;
use Hgraca\AppMapper\Core\Port\Parser\Exception\ParserException;
use Hgraca\AppMapper\Infrastructure\Parser\NikicPhpParser\NodeTypeManagerTrait;
use Hgraca\PhpExtension\Type\TypeHelper;
use PhpParser\Node;
......@@ -25,22 +26,9 @@ final class NotImplementedException extends ParserException
{
public static function constructFromNode(Node $node)
{
$relevantInfo = [];
$loopNode = $node;
while ($loopNode->hasAttribute('parentNode')) {
$relevantInfo[] = get_class($loopNode) . ' => '
. (property_exists($loopNode, 'name')
? $loopNode->name
: (property_exists($loopNode, 'var') && property_exists($loopNode->var, 'name')
? $loopNode->var->name
: 'no_name')
);
$loopNode = $loopNode->getAttribute('parentNode');
}
$message = 'Can\'t build Type from ' . TypeHelper::getType($node) . "\n"
. json_encode($relevantInfo, JSON_PRETTY_PRINT);
return new self($message);
return new self(
'Can\'t build Type from ' . TypeHelper::getType($node) . "\n"
. NodeTypeManagerTrait::resolveNodeTreeAsJson($node)
);
}
}
......@@ -18,28 +18,13 @@ declare(strict_types=1);
namespace Hgraca\AppMapper\Infrastructure\Parser\NikicPhpParser\Exception;
use Hgraca\AppMapper\Core\Port\Parser\Exception\ParserException;
use Hgraca\AppMapper\Infrastructure\Parser\NikicPhpParser\NodeTypeManagerTrait;
use PhpParser\Node;
final class UnresolvableNodeTypeException extends ParserException
{
public function __construct(Node $node)
{
$relevantInfo = [];
$loopNode = $node;
while ($loopNode->hasAttribute('parentNode')) {
$relevantInfo[] = get_class($loopNode) . ' => '
. (property_exists($loopNode, 'name')
? $loopNode->name
: (property_exists($loopNode, 'var') && property_exists($loopNode->var, 'name')
? $loopNode->var->name
: 'no_name')
);
$loopNode = $loopNode->getAttribute('parentNode');
}
$message = "Can't find type collection in node:\n"
. json_encode($relevantInfo, JSON_PRETTY_PRINT);
parent::__construct($message);
parent::__construct(NodeTypeManagerTrait::resolveNodeTreeAsJson($node));
}
}
......@@ -31,7 +31,7 @@ final class TypeResolverVisitor extends NodeVisitorAbstract
if (!$node->hasAttribute(ResolverCollection::getName())) {
StaticLoggerFacade::notice(
"Can't find type resolver in node:\n"
. json_encode($this->getNodeTrace($node), JSON_PRETTY_PRINT),
. NodeTypeManagerTrait::resolveNodeTreeAsJson($node),
[__METHOD__]
);
......@@ -40,22 +40,4 @@ final class TypeResolverVisitor extends NodeVisitorAbstract
self::addTypeCollectionToNode($node, self::resolveType($node));
}
private function getNodeTrace(Node $node): array
{
$relevantInfo = [];
$loopNode = $node;
while ($loopNode->hasAttribute('parentNode')) {
$relevantInfo[] = get_class($loopNode) . ' => '
. (property_exists($loopNode, 'name')
? $loopNode->name
: (property_exists($loopNode, 'var') && property_exists($loopNode->var, 'name')
? $loopNode->var->name
: 'no_name')
);
$loopNode = $loopNode->getAttribute('parentNode');
}
return $relevantInfo;
}
}
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