Simplify usage of CircularReferenceDetectedException

parent dab72628
......@@ -18,7 +18,26 @@ declare(strict_types=1);
namespace Hgraca\AppMapper\Infrastructure\Parser\NikicPhpParser\Exception;
use Hgraca\AppMapper\Core\SharedKernel\Exception\AppMapperRuntimeException;
use PhpParser\Node;
final class CircularReferenceDetectedException extends AppMapperRuntimeException
{
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);
}
}
......@@ -76,20 +76,7 @@ final class TypeCollection extends Collection
? $this->repeatedTypeAddition[$item->getFcqn()] + 1
: 1;
if ($this->repeatedTypeAddition[$item->getFcqn()] >= self::REPEATED_TYPE_ADD_LIMIT) {
$relevantInfo = [];
$loopNode = $this->node;
while ($loopNode->hasAttribute('parentNode')) {
$relevantInfo[] = get_class($loopNode) . ' => '
. (property_exists($loopNode, 'name')
? $loopNode->name
: 'no_name'
);
$loopNode = $loopNode->getAttribute('parentNode');
}
throw new CircularReferenceDetectedException(
"Circular reference detected when adding type '{$item->getFcqn()}' to collection in node:\n"
. json_encode($relevantInfo, JSON_PRETTY_PRINT)
);
throw new CircularReferenceDetectedException($this->node, $item->getFcqn());
}
}
......
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