Skip to content

Commit

Permalink
🚨 Bump PHPStan level 4
Browse files Browse the repository at this point in the history
  • Loading branch information
netbrothers-tr committed May 24, 2023
1 parent e174fa7 commit eae20c1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 48 deletions.
7 changes: 6 additions & 1 deletion phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
parameters:
level: 1
level: 4
excludePaths:
analyseAndScan:
- src/NetBrothersVersionBundle.php
- src/DependencyInjection/*

includes:
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
2 changes: 1 addition & 1 deletion src/Command/MakeVersionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected function initCommand(
$con = $this->entityManager->getConnection();
$con->getConfiguration()->setSchemaAssetsFilter(null);
$schemaManager = $con->createSchemaManager();
$compareService = new CompareService($schemaManager, $excludeColumnNames);
$compareService = new CompareService($excludeColumnNames);
$this->jobService = new JobService($schemaManager, $compareService, $ignoreTables);
$this->generateService = new GenerateService($schemaManager, $con->getDatabase());
$this->executeService = new ExecuteService($entityManager);
Expand Down
28 changes: 9 additions & 19 deletions src/Services/CompareService.php
Original file line number Diff line number Diff line change
@@ -1,48 +1,38 @@
<?php

/**
* NetBrothers VersionBundle
*
* @author Stefan Wessel, NetBrothers GmbH
* @date 19.03.21
*
*/

namespace NetBrothers\VersionBundle\Services;


use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\SchemaException;
use Doctrine\DBAL\Schema\Table;

/** check if column definitions are the same in both tables
* Class CompareService
/**
* Provides check, if column definitions are identical in two tables.
*
* @package NetBrothers\VersionBundle\Services
*/
class CompareService
{
/**
* @var AbstractSchemaManager
*/
private $schemaManager;

/** @var array */
private $errors = [];
/** @var string[] */
private array $errors = [];

/** @var null|string */
private $errMessage = null;
private ?string $errMessage = null;

/** @var string[] */
private $excludeColumnNames = ['id', 'version', 'created_at', 'updated_at'];
private array $excludeColumnNames = ['id', 'version', 'created_at', 'updated_at'];

/**
* CompareService constructor.
* @param AbstractSchemaManager $schemaManager
* @param array|null $excludeColumnNames columns should not be compared
*/
public function __construct(AbstractSchemaManager $schemaManager, array $excludeColumnNames = null)
public function __construct(?array $excludeColumnNames = null)
{
$this->schemaManager = $schemaManager;
if (is_array($excludeColumnNames) && 0 < count($excludeColumnNames)) {
$this->excludeColumnNames = $excludeColumnNames;
}
Expand Down
36 changes: 10 additions & 26 deletions src/Services/JobService.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ public function __construct(
AbstractSchemaManager $schemaManager,
CompareService $compareService,
array $ignoreTables = []
)
{
) {
$this->schemaManager = $schemaManager;
$this->schema = $schemaManager->introspectSchema();
$this->ignoreTables = $ignoreTables;
$this->definitionService = new Definitions();
$this->compareService = $compareService;
Expand Down Expand Up @@ -154,12 +154,11 @@ public function getJobForOneTable(string $tableName): array
return $this->jobs;
}
$tableNames = $this->schemaManager->listTableNames();
if (! in_array($tableName, $tableNames)) {
if (!in_array($tableName, $tableNames)) {
$error = "Table $tableName does not exists";
throw new \Exception($error);
}
$schema = $this->schemaManager->introspectSchema();
$this->findJobForTable($schema->getTable($tableName));
$this->findJobForTable($this->schema->getTable($tableName));
return $this->jobs;
}

Expand All @@ -170,7 +169,7 @@ public function getJobForOneTable(string $tableName): array
private function originTableExists(Table $versionTable): bool
{
$tName = $versionTable->getName();
$orgName = preg_replace("/" . Definitions::VERSION_TABLE_NAME_POSTFIX ."$/", '', $tName);
$orgName = preg_replace("/" . Definitions::VERSION_TABLE_NAME_POSTFIX . "$/", '', $tName);
return $this->schemaManager->tablesExist([$orgName]);
}

Expand All @@ -183,7 +182,7 @@ private function findJobForTable(Table $table): bool
{
$tName = $table->getName();
$versionTable = $this->getVersionTableFromOrigin($table);
if (null === $versionTable) { //no table version
if (null === $versionTable) { // no version table
if (true === $this->definitionService->hasTableVersionColumn($table)) {
$this->jobs['Reports'][] = "$tName: Drop triggers, create version table, create triggers";
$this->jobs['DropTrigger'][] = $tName;
Expand All @@ -207,7 +206,8 @@ private function findJobForTable(Table $table): bool
}
// compare not nice!
$this->error = true;
$this->jobs['Reports'][] = "ERROR $tName: " . $this->compareService->getErrMessage();
$compareErr = $this->compareService->getErrMessage() ?? '';
$this->jobs['Reports'][] = "ERROR $tName: " . $compareErr;
$this->jobs['Reports'][] = "$tName: Drop triggers";
$this->jobs['DropTrigger'][] = $tName;
return false;
Expand All @@ -224,29 +224,13 @@ private function findJobForTable(Table $table): bool
* @return Table|null
* @throws SchemaException
*/
public function getVersionTableFromOrigin(Table $table): ?Table
private function getVersionTableFromOrigin(Table $table): ?Table
{
$tName = $table->getName();
$versionTableName = $tName . Definitions::VERSION_TABLE_NAME_POSTFIX;
if ($this->schemaManager->tablesExist([$versionTableName])) {
return $this->getOneTable($versionTableName);
return $this->schema->getTable($versionTableName);
}
return null;
}

/**
* @param string $tableName
* @return Table|null
* @throws SchemaException
*/
public function getOneTable(string $tableName): ?Table
{
if (! $this->schemaManager->tablesExist([$tableName])) {
return null;
}
if (is_null($this->schema)) {
$this->schema = $this->schemaManager->introspectSchema();
}
return $this->schema->getTable($tableName);
}
}
6 changes: 5 additions & 1 deletion src/Services/Sql/ExecuteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
class ExecuteService
{
private const SUPPORTED_PLATFORMS = [
MariaDb1027Platform::class, // @todo remove when merged (Doctrine DBAL 4.0)
/**
* @todo Remove `MariaDb1027Platform` when merged (Doctrine DBAL 4.0).
* @phpstan-ignore-next-line
*/
MariaDb1027Platform::class,
MariaDBPlatform::class,
MySQLPlatform::class,
MySQL80Platform::class,
Expand Down

0 comments on commit eae20c1

Please sign in to comment.