Ver Fonte

simplify the schema validation command and factor the csv formatting

Olivier Massot há 11 meses atrás
pai
commit
4692c8e820

+ 9 - 62
src/Commands/Doctrine/SchemaValidateCommand.php

@@ -4,7 +4,6 @@
 
 namespace App\Commands\Doctrine;
 
-use App\Service\Doctrine\SchemaValidation\Difference;
 use App\Service\Doctrine\SchemaValidation\DiffTypeEnum;
 use App\Service\Doctrine\SchemaValidation\SchemaSnippetsMaker;
 use App\Service\Doctrine\SchemaValidation\SchemaValidationService;
@@ -27,7 +26,7 @@ class SchemaValidateCommand extends Command
 {
     public function __construct(
         private readonly SchemaValidationService $schemaValidationService,
-        private readonly SchemaSnippetsMaker $schemaSnippetsMaker
+        private readonly SchemaSnippetsMaker $schemaSnippetsMaker,
     ) {
         parent::__construct();
     }
@@ -43,12 +42,6 @@ class SchemaValidateCommand extends Command
             InputOption::VALUE_OPTIONAL,
             "Filter the type of difference to display (ex: 'MISSING_PROPERTY')."
         );
-        $this->addOption(
-            'csv',
-            null,
-            InputOption::VALUE_NONE,
-            "Print the result in CSV format."
-        );
         $this->addOption(
             'snippets',
             null,
@@ -63,29 +56,19 @@ class SchemaValidateCommand extends Command
 
         $diff = $this->schemaValidationService->validateSchema($filter);
 
-        foreach ($diff as $entity => $value) {
-            if (empty($value)) {
-                continue;
-            }
+        $csv = $this->schemaValidationService->formatToCsv($diff);
 
-            if ($input->getOption('csv')) {
-                $this->printCsv($output, $entity, $value);
-            } else {
-                $this->printVerbose($output, $entity, $value);
-            }
+        if (empty($csv)) {
+            $output->writeln("No difference found");
+            return 0;
         }
 
-        if ($diff) {
-            $count = 0;
-            foreach ($diff as $entity => $value) {
-                $count += (is_array($value) ? count($value) : 1);
-            }
-
-            $output->writeln($count . " differences found");
-        } else {
-            $output->writeln("No difference found");
+        foreach ($csv as $line) {
+            $output->writeln($line);
         }
 
+        $output->writeln(count($csv) . " differences found");
+
         if ($input->getOption('snippets')) {
             $this->schemaSnippetsMaker->makeSnippets($diff);
             $output->writeln("Snippets generated");
@@ -93,40 +76,4 @@ class SchemaValidateCommand extends Command
 
         return 0;
     }
-
-    /**
-     * @param OutputInterface $output
-     * @param string $entity
-     * @param Difference|array<Difference> $differences
-     * @return void
-     */
-    protected function printVerbose(OutputInterface $output, string $entity, Difference | array $differences): void {
-        $output->writeln($entity);
-
-        if (!is_array($differences)) {
-            $output->writeln($differences->getType()->value . " : " . $differences->getMessage());
-        } else {
-            foreach ($differences as $field => $difference) {
-                $output->writeln(" * " . $field . " - " . $difference->getType()->value . " : " . $difference->getMessage());
-            }
-        }
-
-        $output->writeln("\n");
-    }
-
-    /**
-     * @param OutputInterface $output
-     * @param string $entity
-     * @param Difference|array<Difference> $differences
-     * @return void
-     */
-    protected function printCsv(OutputInterface $output, string $entity, Difference | array $differences): void {
-        if (!is_array($differences)) {
-            $output->writeln(implode(';', [$entity, '', $differences->getType()->value]));
-        } else {
-            foreach ($differences as $field => $difference) {
-                $output->writeln(implode(';', [$entity, $field, $difference->getType()->value]));
-            }
-        }
-    }
 }

+ 0 - 2
src/Service/Doctrine/SchemaValidation/Difference.php

@@ -7,8 +7,6 @@ class Difference
 {
     protected DiffTypeEnum $type;
     protected ?string $message;
-
-
     protected string $entity;
     protected string $property;
 

+ 19 - 0
src/Service/Doctrine/SchemaValidation/SchemaValidationService.php

@@ -292,4 +292,23 @@ class SchemaValidationService
             ClassMetadataInfo::MANY_TO_MANY => 'ManyToMany',
         ][$relation['type']] ?? 'Unknown';
     }
+
+    /**
+     * @param array<Difference|array<Difference>> $diff
+     * @return array<string>
+     */
+    public function formatToCsv(array $diff): array
+    {
+        $csv = [];
+        foreach ($diff as $entity => $differences) {
+            if (!is_array($differences)) {
+                $csv[] = implode(';', [$entity, '', $differences->getType()->value]);
+            } else {
+                foreach ($differences as $field => $difference) {
+                    $csv[] = implode(';', [$entity, $field, $difference->getType()->value]);
+                }
+            }
+        }
+        return $csv;
+    }
 }