From 90f035b0d941c1e1b4c94845debe5af971201b67 Mon Sep 17 00:00:00 2001 From: Thilo Ratnaweera Date: Fri, 18 Nov 2022 17:02:34 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20Resolves=20#8=20-=20check=20pla?= =?UTF-8?q?tforms=20via=20class=20names=20-=20removed=20deprecated=20usage?= =?UTF-8?q?=20of=20getName()=20-=20added=20PHPStan=20to=20find=20deprecati?= =?UTF-8?q?ons=20-=20removed=20unnecessary=20code=20-=20cleanup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 6 ++- phpstan.dist.neon | 4 ++ src/Command/MakeVersionCommand.php | 16 +++--- src/Services/CompareService.php | 29 +++++++---- src/Services/JobService.php | 19 ++++--- src/Services/Sql/ExecuteService.php | 81 ++++++++++------------------- 6 files changed, 70 insertions(+), 85 deletions(-) create mode 100644 phpstan.dist.neon diff --git a/composer.json b/composer.json index 3a5c604..6c9e243 100644 --- a/composer.json +++ b/composer.json @@ -36,5 +36,9 @@ "name": "Thilo Ratnaweera", "email": "thilo.ratnaweera@netbrothers.de" } - ] + ], + "require-dev": { + "phpstan/phpstan": "^1.9", + "phpstan/phpstan-deprecation-rules": "^1.0" + } } diff --git a/phpstan.dist.neon b/phpstan.dist.neon new file mode 100644 index 0000000..ac6ac9e --- /dev/null +++ b/phpstan.dist.neon @@ -0,0 +1,4 @@ +parameters: + level: 1 +includes: + - vendor/phpstan/phpstan-deprecation-rules/rules.neon diff --git a/src/Command/MakeVersionCommand.php b/src/Command/MakeVersionCommand.php index 7ae8243..9e9f338 100644 --- a/src/Command/MakeVersionCommand.php +++ b/src/Command/MakeVersionCommand.php @@ -4,7 +4,6 @@ * NetBrothers VersionBundle * * @author Stefan Wessel, NetBrothers GmbH - * @date 19.03.21 */ namespace NetBrothers\VersionBundle\Command; @@ -135,7 +134,6 @@ protected function initCommand( $this->entityManager = $entityManager; $con = $this->entityManager->getConnection(); $con->getConfiguration()->setSchemaAssetsFilter(null); - $schemaManager = $con->createSchemaManager(); $compareService = new CompareService($schemaManager, $excludeColumnNames); $this->jobService = new JobService($schemaManager, $compareService, $ignoreTables); @@ -209,9 +207,9 @@ private function printReport(SymfonyStyle $io): int $errors = []; $warnings = []; foreach ($this->jobService->getReport() as $message) { - if (preg_match("/^ERROR/", $message)) { + if (preg_match('/^ERROR/', $message)) { $errors[] = $message; - } elseif (preg_match("/^WARNING/", $message)) { + } elseif (preg_match('/^WARNING/', $message)) { $warnings[] = $message; } else { $io->writeln($message); @@ -277,7 +275,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->setJobs($tableName); } catch (\Exception $exception) { $io->error($exception->getMessage()); - return 1; + return Command::FAILURE; } $this->sql = []; if (null !== $tableName) { @@ -302,14 +300,14 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($this->jobService->hasError()) { $this->printReport($io); $io->newLine(2); - $io->note("Operation canceled"); - return 1; + $io->note('Operation canceled'); + return Command::FAILURE; } $this->executeService->execute($this->sql); if ($this->jobService->hasWarning()) { $this->printReport($io); } - $io->success("Operation success"); - return 0; + $io->success('Operation success'); + return Command::SUCCESS; } } diff --git a/src/Services/CompareService.php b/src/Services/CompareService.php index 5fbac7f..2b1c60a 100644 --- a/src/Services/CompareService.php +++ b/src/Services/CompareService.php @@ -128,26 +128,33 @@ private function getColumn(Table $table, string $columnName): ?Column */ private function compareOneColumn(Column $column, Column $compareColumn): bool { - $type = $column->getType()->getName(); - $compareColumnType = $compareColumn->getType()->getName(); $valid = true; - if ($type !== $compareColumnType) { - $this->errors[] = "different types"; + + // compare types + $columnType = $column->getType(); + $compareColumnType = $compareColumn->getType(); + if (! $columnType instanceof $compareColumnType) { + $this->errors[] = 'different types'; $valid = false; } + + // compare default values $defaultValue = $column->getDefault(); $compareValue = $compareColumn->getDefault(); if ($defaultValue !== $compareValue) { - $this->errors[] = "different default values ($defaultValue != $compareValue)"; + $this->errors[] = sprintf( + 'different default values (%s != %s)', + $defaultValue, + $compareValue + ); $valid = false; } - $value = $column->getNotnull(); - $compare = $compareColumn->getNotnull(); - if ($value !== $compare) { - $this->errors[] = "different NULL-definition"; + + // compare NULL definition + if ($column->getNotnull() !== $compareColumn->getNotnull()) { + $this->errors[] = "different NULL definitions"; $valid = false; } return $valid; } - -} \ No newline at end of file +} diff --git a/src/Services/JobService.php b/src/Services/JobService.php index 8ba3892..1e394e5 100644 --- a/src/Services/JobService.php +++ b/src/Services/JobService.php @@ -1,4 +1,5 @@ schemaManager = $schemaManager; $this->ignoreTables = $ignoreTables; @@ -168,7 +172,6 @@ private function originTableExists(Table $versionTable): bool $tName = $versionTable->getName(); $orgName = preg_replace("/" . Definitions::VERSION_TABLE_NAME_POSTFIX ."$/", '', $tName); return $this->schemaManager->tablesExist([$orgName]); - } /** @@ -238,16 +241,12 @@ public function getVersionTableFromOrigin(Table $table): ?Table */ public function getOneTable(string $tableName): ?Table { - if (!$this->schemaManager->tablesExist([$tableName])) { + if (! $this->schemaManager->tablesExist([$tableName])) { return null; } if (is_null($this->schema)) { - $this->schema = $this->schemaManager->createSchema(); + $this->schema = $this->schemaManager->introspectSchema(); } return $this->schema->getTable($tableName); } - - - - -} \ No newline at end of file +} diff --git a/src/Services/Sql/ExecuteService.php b/src/Services/Sql/ExecuteService.php index 7d7b095..60a48f2 100644 --- a/src/Services/Sql/ExecuteService.php +++ b/src/Services/Sql/ExecuteService.php @@ -1,35 +1,32 @@ getConnection()->getDatabasePlatform()->getName() !== 'mysql') { - throw new \Exception(__CLASS__ . ' can only be executed safely on \'mysql\'.'); + $platform = $entityManager->getConnection()->getDatabasePlatform(); + if (! in_array($platform, self::SUPPORTED_PLATFORMS)) { + echo sprintf( + 'WARNING: Unsupported database platform: %s', $platform::class + ) . PHP_EOL; } - $this->entityManager = $entityManager; $this->connection = $entityManager->getConnection(); } /** - * @param array $sql - * @return bool - * @throws DriverException - * @throws \Exception + * Executes the assembled queries. + * + * @param array $sql SQL queries priorly generated by the `GenerateService`. + * @return void + * @throws Exception */ - public function execute(array $sql = []): bool + public function execute(array $sql = []): void { - if (!is_array($sql)) { - throw new \Exception(__CLASS__ . ': only sql-statements packed in array can be executed.'); + if (empty($sql)) { + return; } - if (0 < count($sql)) { - try { - foreach ($sql as $query) { - if (true !== $this->_execute($query)) { - return false; - } - } - } catch (\Exception $e) { - throw new \Exception("Cannot commit", 500, $e); - } + foreach ($sql as $query) { + $this->connection->prepare($query)->executeQuery(); } - return true; } - - /** - * @param string $query - * @return bool - * @throws \Doctrine\DBAL\Exception - */ - private function _execute(string $query): bool - { - try { - $statement = $this->connection->prepare($query); - $result = $statement->executeQuery(); - return true; - } catch ( DriverException $e) { - $this->errMsg = "Cannot execute SQL: $query"; - return false; - } - } - } From 7dd881b5d9d1323056ef177e769d92b159507ddb Mon Sep 17 00:00:00 2001 From: Thilo Ratnaweera Date: Fri, 18 Nov 2022 17:09:31 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9B=20fixes=20#9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Services/Sql/ExecuteService.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Services/Sql/ExecuteService.php b/src/Services/Sql/ExecuteService.php index 60a48f2..24f3a52 100644 --- a/src/Services/Sql/ExecuteService.php +++ b/src/Services/Sql/ExecuteService.php @@ -36,9 +36,10 @@ class ExecuteService public function __construct(EntityManagerInterface $entityManager) { $platform = $entityManager->getConnection()->getDatabasePlatform(); - if (! in_array($platform, self::SUPPORTED_PLATFORMS)) { + if (! in_array(get_class($platform), self::SUPPORTED_PLATFORMS)) { echo sprintf( - 'WARNING: Unsupported database platform: %s', $platform::class + 'WARNING: Unsupported database platform: %s', + get_class($platform) ) . PHP_EOL; } $this->connection = $entityManager->getConnection();