Repository: laravel-shift/blueprint (php)

Issue:
Unable to generate more than one morphTo
<!-- PLEASE COMPLETE THIS TEMPLATE -->

- Laravel Version: 8.59.0
- PHP Version: 8.0.10
- Blueprint Version: 2.0.0
- Platform: Linux

### Issue:
When I try to generate more than one polymorphic relation on a model, the `morphTo` relation generates in the migration file only one relation (the last one by order) but it is Ok for functions in the generated model.
For example : the folowing draft will generate only _resultable_ in the migration file.

### draft.yaml:
```yaml
models:
 Models\Analysis
  content: string:128
  relationships:
   morphTo: surveyable, resultable

```


Pull request:
Allow multiple morphTo statements. Fixes #501

Patch:
diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php
index b72c41c..7a11bb9 100644
--- a/src/Generators/MigrationGenerator.php
+++ b/src/Generators/MigrationGenerator.php
@@ -56,7 +56,6 @@ class MigrationGenerator extends AbstractClassGenerator implements Generator
          */
         foreach ($tree->models() as $model) {
             $tables['tableNames'][$model->tableName()] = $this->populateStub($stub, $model);
-
             if (!empty($model->pivotTables())) {
                 foreach ($model->pivotTables() as $pivotSegments) {
                     $pivotTableName = $this->getPivotTableName($pivotSegments);
@@ -257,9 +256,13 @@ class MigrationGenerator extends AbstractClassGenerator implements Generator
             $definition .= $column_definition;
         }
 
-        if ($model->morphTo()) {
-            $definition .= self::INDENT . sprintf('$table->unsignedBigInteger(\'%s\');', Str::lower($model->morphTo() . '_id')) . PHP_EOL;
-            $definition .= self::INDENT . sprintf('$table->string(\'%s\');', Str::lower($model->morphTo() . '_type')) . PHP_EOL;
+        $relationships = $model->relationships();
+
+        if (array_key_exists('morphTo', $relationships)) {
+            foreach ($relationships['morphTo'] as $morphTo) {
+                $definition .= self::INDENT . sprintf('$table->unsignedBigInteger(\'%s\');', Str::lower($morphTo . '_id')) . PHP_EOL;
+                $definition .= self::INDENT . sprintf('$table->string(\'%s\');', Str::lower($morphTo . '_type')) . PHP_EOL;
+            }
         }
 
         foreach ($model->indexes() as $index) {
diff --git a/src/Lexers/ModelLexer.php b/src/Lexers/ModelLexer.php
index 45fbcda..aaa0196 100644
--- a/src/Lexers/ModelLexer.php
+++ b/src/Lexers/ModelLexer.php
@@ -158,10 +158,6 @@ class ModelLexer implements Lexer
                 foreach ($columns['relationships'] as $type => $relationships) {
                     foreach (explode(',', $relationships) as $reference) {
                         $model->addRelationship(self::$relationships[strtolower($type)], trim($reference));
-
-                        if ($type === 'morphTo') {
-                            $model->setMorphTo(trim($reference));
-                        }
                     }
                 }
             }
diff --git a/src/Models/Model.php b/src/Models/Model.php
index 1350c47..bad470c 100644
--- a/src/Models/Model.php
+++ b/src/Models/Model.php
@@ -12,7 +12,6 @@ class Model implements BlueprintModel
     private $primaryKey = 'id';
     private $timestamps = 'timestamps';
     private $softDeletes = false;
-    private $morphTo;
     private $columns = [];
     private $relationships = [];
     private $pivotTables = [];
@@ -189,14 +188,4 @@ class Model implements BlueprintModel
     {
         return $this->polymorphicManyToManyTables;
     }
-
-    public function setMorphTo(string $reference)
-    {
-        $this->morphTo = $reference;
-    }
-
-    public function morphTo(): ?string
-    {
-        return $this->morphTo;
-    }
 }


Test patch:
diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php
index f1e66f5..5e15904 100644
--- a/tests/Feature/Generators/MigrationGeneratorTest.php
+++ b/tests/Feature/Generators/MigrationGeneratorTest.php
@@ -549,6 +549,31 @@ class MigrationGeneratorTest extends TestCase
         $this->assertEquals(['created' => [$post_migration, $user_migration, $image_migration]], $this->subject->output($tree));
     }
 
+    /**
+     * @test
+     */
+    public function output_works_with_multiple_morphto_statements_in_polymorphic_relationship()
+    {
+        $this->filesystem->expects('stub')
+            ->with('migration.stub')
+            ->andReturn($this->stub('migration.stub'));
+
+        $now = Carbon::now();
+        Carbon::setTestNow($now);
+
+        $image_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_images_table.php');
+
+        $this->filesystem->expects('exists')->andReturn(false);
+
+        $this->filesystem->expects('put')
+            ->with($image_migration, $this->fixture('migrations/polymorphic_relationships_images_table_multiple_morphto.php'));
+
+        $tokens = $this->blueprint->parse($this->fixture('drafts/polymorphic-relationships-multiple-morphto.yaml'));
+        $tree = $this->blueprint->analyze($tokens);
+
+        $this->assertEquals(['created' => [$image_migration]], $this->subject->output($tree));
+    }
+
     /**
      * @test
      */
diff --git a/tests/Feature/Lexers/ModelLexerTest.php b/tests/Feature/Lexers/ModelLexerTest.php
index 1767bb1..2a60b05 100644
--- a/tests/Feature/Lexers/ModelLexerTest.php
+++ b/tests/Feature/Lexers/ModelLexerTest.php
@@ -632,7 +632,7 @@ class ModelLexerTest extends TestCase
 
         $model = $actual['models']['Model'];
         $this->assertEquals('Model', $model->name());
-        $this->assertEquals('Morphable', $model->morphTo());
+        $this->assertArrayHasKey('morphTo', $model->relationships());
         $this->assertTrue($model->usesTimestamps());
 
         $columns = $model->columns();
diff --git a/tests/fixtures/drafts/polymorphic-relationships-multiple-morphto.yaml b/tests/fixtures/drafts/polymorphic-relationships-multiple-morphto.yaml
new file mode 100644
index 0000000..285b82d
--- /dev/null
+++ b/tests/fixtures/drafts/polymorphic-relationships-multiple-morphto.yaml
@@ -0,0 +1,5 @@
+models:
+  Image:
+    url: string:400
+    relationships:
+      morphTo: Imageable, Tagable
diff --git a/tests/fixtures/migrations/polymorphic_relationships_images_table_multiple_morphto.php b/tests/fixtures/migrations/polymorphic_relationships_images_table_multiple_morphto.php
new file mode 100644
index 0000000..98d15a3
--- /dev/null
+++ b/tests/fixtures/migrations/polymorphic_relationships_images_table_multiple_morphto.php
@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateImagesTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('images', function (Blueprint $table) {
+            $table->id();
+            $table->string('url', 400);
+            $table->unsignedBigInteger('imageable_id');
+            $table->string('imageable_type');
+            $table->unsignedBigInteger('tagable_id');
+            $table->string('tagable_type');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('images');
+    }
+}