From acbb8171a79245f3c5d44d6883884ee72ea40a7b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr>
Date: Wed, 26 Sep 2018 14:30:37 +0200
Subject: [PATCH] =?UTF-8?q?Lance=20des=20exceptions=20pour=20assurer=20la?=
 =?UTF-8?q?=20coh=C3=A9rence=20des=20classes.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/Metadata/RepresentationMetadata.php | 5 +++++
 src/Models/PHPClass.php                 | 8 ++++++--
 src/SerializationMapper.php             | 8 ++++----
 3 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/src/Metadata/RepresentationMetadata.php b/src/Metadata/RepresentationMetadata.php
index 72fdbee..e13877a 100644
--- a/src/Metadata/RepresentationMetadata.php
+++ b/src/Metadata/RepresentationMetadata.php
@@ -19,6 +19,7 @@
 
 namespace Irstea\NgModelGeneratorBundle\Metadata;
 
+use Irstea\NgModelGeneratorBundle\Exceptions\DomainException;
 use Irstea\NgModelGeneratorBundle\Models\ClassName;
 use Irstea\NgModelGeneratorBundle\Models\HasName;
 
@@ -62,6 +63,10 @@ final class RepresentationMetadata implements ClassName, HasName
      */
     public function __construct(string $name, ClassName $class, ?ClassName $parent, array $properties, bool $abstract)
     {
+        if ($parent && $class->getFullName() === $parent->getFullName()) {
+            throw new DomainException("$class cannot be its own parent");
+        }
+
         $this->class = $class;
         $this->parent = $parent;
 
diff --git a/src/Models/PHPClass.php b/src/Models/PHPClass.php
index e6ff765..69fbee8 100644
--- a/src/Models/PHPClass.php
+++ b/src/Models/PHPClass.php
@@ -100,14 +100,18 @@ final class PHPClass implements ClassName
     }
 
     /**
-     * @param ClassName|string $name
+     * @param ClassName|\ReflectionClass|string $name
      *
      * @return ClassName
      */
     public static function get($name): ClassName
     {
         if ($name instanceof ClassName) {
-            return $name;
+            $name = $name->getFullName();
+        } elseif ($name instanceof \ReflectionClass) {
+            $name = $name->getName();
+        } elseif (!\is_string($name)) {
+            throw new InvalidArgumentException(__METHOD__ . " argument should be a string, ClassName or ReflectionClass, not $name");
         }
 
         static $instances = [];
diff --git a/src/SerializationMapper.php b/src/SerializationMapper.php
index d2b0fc0..a699c31 100644
--- a/src/SerializationMapper.php
+++ b/src/SerializationMapper.php
@@ -160,6 +160,10 @@ final class SerializationMapper implements TypeFactoryInterface
     {
         $classInfo = $this->getClassInfo($repr);
 
+        if ($classInfo->isUndefined()) {
+            throw new DomainException(sprintf('%s has not been rearranged', $repr));
+        }
+
         if ($classInfo->isUnion()) {
             $types = [];
             foreach ($classInfo->iterateInterfaceDescendants() as $child) {
@@ -180,10 +184,6 @@ final class SerializationMapper implements TypeFactoryInterface
             return new IRI([$repr]);
         }
 
-        if (!$classInfo->isInterface()) {
-            throw new DomainException(sprintf('Cannot map %s', $repr));
-        }
-
         $parent = null;
         $parentInfo = $classInfo->getParent();
         if ($parentInfo !== null && $parentInfo->isInterface()) {
-- 
GitLab