diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index e0422471..d392f2f5 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -7,122 +7,8 @@ parameters: path: src/AbstractTDBMObject.php - - message: '#^Call to an undefined method Doctrine\\DBAL\\Schema\\AbstractSchemaManager\:\:_getPortableFunctionDefinition\(\)\.$#' - identifier: method.notFound - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Call to an undefined method Doctrine\\DBAL\\Schema\\AbstractSchemaManager\:\:_getPortableFunctionsList\(\)\.$#' - identifier: method.notFound - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Call to an undefined method Doctrine\\DBAL\\Schema\\AbstractSchemaManager\:\:_getPortableTriggerDefinition\(\)\.$#' - identifier: method.notFound - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Call to an undefined method Doctrine\\DBAL\\Schema\\AbstractSchemaManager\:\:_getPortableTriggersList\(\)\.$#' - identifier: method.notFound - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Call to an undefined method Doctrine\\DBAL\\Schema\\AbstractSchemaManager\:\:_getPortableUserDefinition\(\)\.$#' - identifier: method.notFound - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Call to an undefined method Doctrine\\DBAL\\Schema\\AbstractSchemaManager\:\:_getPortableUsersList\(\)\.$#' - identifier: method.notFound - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Call to an undefined method Doctrine\\DBAL\\Schema\\AbstractSchemaManager\:\:getFilterSchemaAssetsExpression\(\)\.$#' - identifier: method.notFound - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Method TheCodingMachine\\TDBM\\Schema\\LockFileSchemaManager\:\:_getPortableFunctionDefinition\(\) has no return type specified\.$#' - identifier: missingType.return - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Method TheCodingMachine\\TDBM\\Schema\\LockFileSchemaManager\:\:_getPortableFunctionDefinition\(\) has parameter \$function with no type specified\.$#' - identifier: missingType.parameter - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Method TheCodingMachine\\TDBM\\Schema\\LockFileSchemaManager\:\:_getPortableFunctionsList\(\) has no return type specified\.$#' - identifier: missingType.return - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Method TheCodingMachine\\TDBM\\Schema\\LockFileSchemaManager\:\:_getPortableFunctionsList\(\) has parameter \$functions with no type specified\.$#' - identifier: missingType.parameter - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Method TheCodingMachine\\TDBM\\Schema\\LockFileSchemaManager\:\:_getPortableTriggerDefinition\(\) has no return type specified\.$#' - identifier: missingType.return - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Method TheCodingMachine\\TDBM\\Schema\\LockFileSchemaManager\:\:_getPortableTriggerDefinition\(\) has parameter \$trigger with no type specified\.$#' - identifier: missingType.parameter - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Method TheCodingMachine\\TDBM\\Schema\\LockFileSchemaManager\:\:_getPortableTriggersList\(\) has no return type specified\.$#' - identifier: missingType.return - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Method TheCodingMachine\\TDBM\\Schema\\LockFileSchemaManager\:\:_getPortableTriggersList\(\) has parameter \$triggers with no type specified\.$#' - identifier: missingType.parameter - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Method TheCodingMachine\\TDBM\\Schema\\LockFileSchemaManager\:\:_getPortableUserDefinition\(\) has no return type specified\.$#' - identifier: missingType.return - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Method TheCodingMachine\\TDBM\\Schema\\LockFileSchemaManager\:\:_getPortableUserDefinition\(\) has parameter \$user with no type specified\.$#' - identifier: missingType.parameter - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Method TheCodingMachine\\TDBM\\Schema\\LockFileSchemaManager\:\:_getPortableUsersList\(\) has no return type specified\.$#' - identifier: missingType.return - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Method TheCodingMachine\\TDBM\\Schema\\LockFileSchemaManager\:\:_getPortableUsersList\(\) has parameter \$users with no type specified\.$#' - identifier: missingType.parameter - count: 1 - path: src/Schema/LockFileSchemaManager.php - - - - message: '#^Method TheCodingMachine\\TDBM\\Schema\\LockFileSchemaManager\:\:getFilterSchemaAssetsExpression\(\) has no return type specified\.$#' - identifier: missingType.return + message: '#^Method TheCodingMachine\\TDBM\\Schema\\LockFileSchemaManager\:\:getDatabasePlatform\(\) should return T of Doctrine\\DBAL\\Platforms\\AbstractPlatform but returns Doctrine\\DBAL\\Platforms\\AbstractPlatform\.$#' + identifier: return.type count: 1 path: src/Schema/LockFileSchemaManager.php diff --git a/src/Commands/GenerateCommand.php b/src/Commands/GenerateCommand.php index 709ad785..c9fc4fbc 100644 --- a/src/Commands/GenerateCommand.php +++ b/src/Commands/GenerateCommand.php @@ -41,7 +41,7 @@ protected function configure(): void ; } - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { // TODO: externalize composer.json file for autoloading (no more parameters for generateAllDaosAndBeans) diff --git a/src/DbRow.php b/src/DbRow.php index 53369bea..ad6e9029 100644 --- a/src/DbRow.php +++ b/src/DbRow.php @@ -198,7 +198,7 @@ public function _dbLoadIfNotLoaded(): void $sql = 'SELECT * FROM '.$connection->quoteIdentifier($this->dbTableName).' WHERE '.$sql_where; $result = $connection->executeQuery($sql, $parameters); - $row = $result->fetch(\PDO::FETCH_ASSOC); + $row = $result->fetchAssociative(); if ($row === false) { throw new TDBMException("Could not retrieve object from table \"$this->dbTableName\" using filter \".$sql_where.\" with data \"".var_export($parameters, true)."\"."); diff --git a/src/QueryFactory/FindObjectsFromRawSqlQueryFactory.php b/src/QueryFactory/FindObjectsFromRawSqlQueryFactory.php index b16beff9..2df404bb 100644 --- a/src/QueryFactory/FindObjectsFromRawSqlQueryFactory.php +++ b/src/QueryFactory/FindObjectsFromRawSqlQueryFactory.php @@ -232,7 +232,9 @@ private function formatSelect(array $baseSelect): array } $table = $this->schema->getTable($tableName); - $pkColumns = $table->getPrimaryKeyColumns(); + $primaryKey = $table->getPrimaryKey(); + assert($primaryKey !== null, 'TDBM Only works on tables with primary keys'); + $pkColumns = $primaryKey->getUnquotedColumns(); foreach ($table->getColumns() as $column) { $columnName = $column->getName(); $alias = AbstractQueryFactory::getColumnAlias($tableName, $columnName); @@ -252,7 +254,7 @@ private function formatSelect(array $baseSelect): array ] ]; $formattedSelect[] = $astColumn; - if (array_key_exists($columnName, $pkColumns)) { + if (in_array($columnName, $pkColumns, true)) { $formattedCountSelect[] = $astColumn; } $columnDescriptors[$alias] = [ diff --git a/src/Schema/LockFileSchemaManager.php b/src/Schema/LockFileSchemaManager.php index dd3681d5..635283de 100644 --- a/src/Schema/LockFileSchemaManager.php +++ b/src/Schema/LockFileSchemaManager.php @@ -9,6 +9,8 @@ use Doctrine\DBAL\Schema\Constraint; use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Doctrine\DBAL\Schema\Index; +use Doctrine\DBAL\Schema\Schema; +use Doctrine\DBAL\Schema\SchemaConfig; use Doctrine\DBAL\Schema\Sequence; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\TableDiff; @@ -20,17 +22,14 @@ * Useful to avoid costly introspection queries at runtime. * * Acts as an adapter on top of an existing schema manager. + * + * @template-covariant T of AbstractPlatform + * @extends AbstractSchemaManager */ class LockFileSchemaManager extends AbstractSchemaManager { - /** - * @var AbstractSchemaManager - */ - private $schemaManager; - /** - * @var SchemaLockFileDumper - */ - private $schemaLockFileDumper; + private AbstractSchemaManager $schemaManager; + private SchemaLockFileDumper $schemaLockFileDumper; public function __construct(AbstractSchemaManager $schemaManager, SchemaLockFileDumper $schemaLockFileDumper) { @@ -38,327 +37,298 @@ public function __construct(AbstractSchemaManager $schemaManager, SchemaLockFile $this->schemaLockFileDumper = $schemaLockFileDumper; } + /** + * @return T + */ public function getDatabasePlatform() { return $this->schemaManager->getDatabasePlatform(); } - public function tryMethod() + public function tryMethod(): mixed { return $this->schemaManager->tryMethod(); } - public function listDatabases() + public function listDatabases(): array { return $this->schemaManager->listDatabases(); } - public function listNamespaceNames() + public function listNamespaceNames(): array { return $this->schemaManager->listNamespaceNames(); } - public function listSequences($database = null) + public function listSequences($database = null): array { return $this->schemaManager->listSequences($database); } - public function listTableColumns($table, $database = null) + public function listTableColumns($table, $database = null): array { return $this->schemaManager->listTableColumns($table, $database); } - public function listTableIndexes($table) + public function listTableIndexes($table): array { return $this->schemaManager->listTableIndexes($table); } - public function tablesExist($tableNames) + public function tablesExist($names): bool { - return $this->schemaManager->tablesExist($tableNames); + return $this->schemaManager->tablesExist($names); } - public function listTableNames() + public function listTableNames(): array { return $this->schemaManager->listTableNames(); } - protected function filterAssetNames($assetNames) + protected function filterAssetNames($assetNames): array { return $this->schemaManager->filterAssetNames($assetNames); } - protected function getFilterSchemaAssetsExpression() - { - return $this->schemaManager->getFilterSchemaAssetsExpression(); - } - - public function listTables() + public function listTables(): array { return $this->schemaManager->listTables(); } - public function listTableDetails($tableName) + public function listTableDetails($name): Table { - return $this->schemaManager->listTableDetails($tableName); + return $this->schemaManager->listTableDetails($name); } - public function listViews() + public function listViews(): array { return $this->schemaManager->listViews(); } - public function listTableForeignKeys($table, $database = null) + public function listTableForeignKeys($table, $database = null): array { return $this->schemaManager->listTableForeignKeys($table, $database); } - public function dropDatabase($database) + public function dropDatabase($database): void { $this->schemaManager->dropDatabase($database); } - public function dropTable($tableName) + public function dropTable($name): void { - $this->schemaManager->dropTable($tableName); + $this->schemaManager->dropTable($name); } - public function dropIndex($index, $table) + public function dropIndex($index, $table): void { $this->schemaManager->dropIndex($index, $table); } - public function dropConstraint(Constraint $constraint, $table) + public function dropConstraint(Constraint $constraint, $table): void { $this->schemaManager->dropConstraint($constraint, $table); } - public function dropForeignKey($foreignKey, $table) + public function dropForeignKey($foreignKey, $table): void { $this->schemaManager->dropForeignKey($foreignKey, $table); } - public function dropSequence($name) + public function dropSequence($name): void { $this->schemaManager->dropSequence($name); } - public function dropView($name) + public function dropView($name): void { $this->schemaManager->dropView($name); } - public function createDatabase($database) + public function createDatabase($database): void { $this->schemaManager->createDatabase($database); } - public function createTable(Table $table) + public function createTable(Table $table): void { $this->schemaManager->createTable($table); } - public function createSequence($sequence) + public function createSequence($sequence): void { $this->schemaManager->createSequence($sequence); } - public function createConstraint(Constraint $constraint, $table) + public function createConstraint(Constraint $constraint, $table): void { $this->schemaManager->createConstraint($constraint, $table); } - public function createIndex(Index $index, $table) + public function createIndex(Index $index, $table): void { $this->schemaManager->createIndex($index, $table); } - public function createForeignKey(ForeignKeyConstraint $foreignKey, $table) + public function createForeignKey(ForeignKeyConstraint $foreignKey, $table): void { $this->schemaManager->createForeignKey($foreignKey, $table); } - public function createView(View $view) + public function createView(View $view): void { $this->schemaManager->createView($view); } - public function dropAndCreateConstraint(Constraint $constraint, $table) + public function dropAndCreateConstraint(Constraint $constraint, $table): void { $this->schemaManager->dropAndCreateConstraint($constraint, $table); } - public function dropAndCreateIndex(Index $index, $table) + public function dropAndCreateIndex(Index $index, $table): void { $this->schemaManager->dropAndCreateIndex($index, $table); } - public function dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table) + public function dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table): void { $this->schemaManager->dropAndCreateForeignKey($foreignKey, $table); } - public function dropAndCreateSequence(Sequence $sequence) + public function dropAndCreateSequence(Sequence $sequence): void { $this->schemaManager->dropAndCreateSequence($sequence); } - public function dropAndCreateTable(Table $table) + public function dropAndCreateTable(Table $table): void { $this->schemaManager->dropAndCreateTable($table); } - public function dropAndCreateDatabase($database) + public function dropAndCreateDatabase($database): void { $this->schemaManager->dropAndCreateDatabase($database); } - public function dropAndCreateView(View $view) + public function dropAndCreateView(View $view): void { $this->schemaManager->dropAndCreateView($view); } - public function alterTable(TableDiff $tableDiff) + public function alterTable(TableDiff $tableDiff): void { $this->schemaManager->alterTable($tableDiff); } - public function renameTable($name, $newName) + public function renameTable($name, $newName): void { $this->schemaManager->renameTable($name, $newName); } - protected function _getPortableDatabasesList($databases) + protected function _getPortableDatabasesList($databases): array { return $this->schemaManager->_getPortableDatabasesList($databases); } - protected function getPortableNamespacesList(array $namespaces) + protected function getPortableNamespacesList(array $namespaces): array { return $this->schemaManager->getPortableNamespacesList($namespaces); } - protected function _getPortableDatabaseDefinition($database) + protected function _getPortableDatabaseDefinition($database): mixed { return $this->schemaManager->_getPortableDatabaseDefinition($database); } - protected function getPortableNamespaceDefinition(array $namespace) + protected function getPortableNamespaceDefinition(array $namespace): mixed { return $this->schemaManager->getPortableNamespaceDefinition($namespace); } - protected function _getPortableFunctionsList($functions) - { - return $this->schemaManager->_getPortableFunctionsList($functions); - } - - protected function _getPortableFunctionDefinition($function) - { - return $this->schemaManager->_getPortableFunctionDefinition($function); - } - - protected function _getPortableTriggersList($triggers) - { - return $this->schemaManager->_getPortableTriggersList($triggers); - } - - protected function _getPortableTriggerDefinition($trigger) - { - return $this->schemaManager->_getPortableTriggerDefinition($trigger); - } - - protected function _getPortableSequencesList($sequences) + protected function _getPortableSequencesList($sequences): array { return $this->schemaManager->_getPortableSequencesList($sequences); } - protected function _getPortableSequenceDefinition($sequence) + protected function _getPortableSequenceDefinition($sequence): Sequence { return $this->schemaManager->_getPortableSequenceDefinition($sequence); } - protected function _getPortableTableColumnList($table, $database, $tableColumns) + protected function _getPortableTableColumnList($table, $database, $tableColumns): array { return $this->schemaManager->_getPortableTableColumnList($table, $database, $tableColumns); } - protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null) + protected function _getPortableTableIndexesList($tableIndexes, $tableName = null): array { - return $this->schemaManager->_getPortableTableIndexesList($tableIndexRows, $tableName); + return $this->schemaManager->_getPortableTableIndexesList($tableIndexes, $tableName); } - protected function _getPortableTablesList($tables) + protected function _getPortableTablesList($tables): array { return $this->schemaManager->_getPortableTablesList($tables); } - protected function _getPortableTableDefinition($table) + protected function _getPortableTableDefinition($table): string { return $this->schemaManager->_getPortableTableDefinition($table); } - protected function _getPortableUsersList($users) - { - return $this->schemaManager->_getPortableUsersList($users); - } - - protected function _getPortableUserDefinition($user) - { - return $this->schemaManager->_getPortableUserDefinition($user); - } - - protected function _getPortableViewsList($views) + protected function _getPortableViewsList($views): array { return $this->schemaManager->_getPortableViewsList($views); } + /** + * @return View|false + */ protected function _getPortableViewDefinition($view) { return $this->schemaManager->_getPortableViewDefinition($view); } - protected function _getPortableTableForeignKeysList($tableForeignKeys) + protected function _getPortableTableForeignKeysList($tableForeignKeys): array { return $this->schemaManager->_getPortableTableForeignKeysList($tableForeignKeys); } - protected function _getPortableTableForeignKeyDefinition($tableForeignKey) + protected function _getPortableTableForeignKeyDefinition($tableForeignKey): ForeignKeyConstraint { return $this->schemaManager->_getPortableTableForeignKeyDefinition($tableForeignKey); } - protected function _execSql($sql) + protected function _execSql($sql): void { $this->schemaManager->_execSql($sql); } - public function createSchema() + public function createSchema(): Schema { return $this->schemaLockFileDumper->getSchema(); } - public function createSchemaConfig() + public function createSchemaConfig(): SchemaConfig { return $this->schemaManager->createSchemaConfig(); } - public function getSchemaSearchPaths() + public function getSchemaSearchPaths(): array { return $this->schemaManager->getSchemaSearchPaths(); } - public function extractDoctrineTypeFromComment($comment, $currentType) + public function extractDoctrineTypeFromComment($comment, $currentType): string { return $this->schemaManager->extractDoctrineTypeFromComment($comment, $currentType); } - public function removeDoctrineTypeFromComment($comment, $type) + public function removeDoctrineTypeFromComment($comment, $type): ?string { return $this->schemaManager->removeDoctrineTypeFromComment($comment, $type); } - protected function _getPortableTableColumnDefinition($tableColumn) + protected function _getPortableTableColumnDefinition($tableColumn): Column { return $this->schemaManager->_getPortableTableColumnDefinition($tableColumn); } diff --git a/src/Utils/Annotation/AnnotationParser.php b/src/Utils/Annotation/AnnotationParser.php index 90d27d10..03351a38 100644 --- a/src/Utils/Annotation/AnnotationParser.php +++ b/src/Utils/Annotation/AnnotationParser.php @@ -88,7 +88,7 @@ public function getTableAnnotations(Table $table): Annotations { $options = $table->getOptions(); if (isset($options['comment'])) { - return $this->parse($options['comment'], ' comment in table '.$table->getName()); + return $this->parse($options['comment'], 'comment in table `' . $table->getName() . '`'); } return new Annotations([]); } @@ -99,6 +99,6 @@ public function getColumnAnnotations(Column $column, Table $table): Annotations if ($comment === null) { return new Annotations([]); } - return $this->parse($comment, sprintf('comment of column %s in table %s', $column->getName(), $table->getName())); + return $this->parse($comment, sprintf('comment of column `%s` in table `%s`', $column->getName(), $table->getName())); } } diff --git a/tests/Performance/ManyToOneBench.php b/tests/Performance/ManyToOneBench.php index ef4ec138..779d3f19 100644 --- a/tests/Performance/ManyToOneBench.php +++ b/tests/Performance/ManyToOneBench.php @@ -3,7 +3,6 @@ namespace TheCodingMachine\TDBM\Performance; use Doctrine\Common\Cache\ArrayCache; -use Doctrine\Common\Cache\VoidCache; use Doctrine\DBAL\Connection; use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer; use PhpBench\Benchmark\Metadata\Annotations\Iterations; @@ -11,7 +10,6 @@ use TheCodingMachine\TDBM\Configuration; use TheCodingMachine\TDBM\ConfigurationInterface; use TheCodingMachine\TDBM\ConnectionFactory; -use TheCodingMachine\TDBM\DummyGeneratorListener; use TheCodingMachine\TDBM\SchemaLockFileDumper; use TheCodingMachine\TDBM\TDBMAbstractServiceTest; use TheCodingMachine\TDBM\TDBMSchemaAnalyzer; @@ -70,7 +68,7 @@ private static function initSchema(Connection $connection): void $sqlStmts = $toSchema->getMigrateFromSql($fromSchema, $connection->getDatabasePlatform()); foreach ($sqlStmts as $sqlStmt) { - $connection->exec($sqlStmt); + $connection->executeStatement($sqlStmt); } for ($i = 1; $i < 200; $i++) { diff --git a/tests/TDBMAbstractServiceTest.php b/tests/TDBMAbstractServiceTest.php index c18b015b..e1659958 100644 --- a/tests/TDBMAbstractServiceTest.php +++ b/tests/TDBMAbstractServiceTest.php @@ -444,12 +444,12 @@ private static function initSchema(Connection $connection): void foreach ($sqlStmts as $sqlStmt) { //echo $sqlStmt."\n"; - $connection->exec($sqlStmt); + $connection->executeStatement($sqlStmt); } // Let's generate computed columns if ($connection->getDatabasePlatform() instanceof MySqlPlatform && !self::isMariaDb($connection)) { - $connection->exec('CREATE TABLE `players` ( + $connection->executeStatement('CREATE TABLE `players` ( `id` INT UNSIGNED AUTO_INCREMENT NOT NULL, `player_and_games` JSON NOT NULL, `names_virtual` VARCHAR(20) GENERATED ALWAYS AS (`player_and_games` ->> \'$.name\') NOT NULL COMMENT \'@ReadOnly\', diff --git a/tests/Utils/BeanDescriptorTest.php b/tests/Utils/BeanDescriptorTest.php index 3229f056..9ac3a222 100644 --- a/tests/Utils/BeanDescriptorTest.php +++ b/tests/Utils/BeanDescriptorTest.php @@ -22,11 +22,9 @@ namespace TheCodingMachine\TDBM\Utils; -use Doctrine\Common\Cache\ArrayCache; use Doctrine\Common\Cache\VoidCache; use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\Table; -use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer; use TheCodingMachine\TDBM\Configuration; diff --git a/vendor-bin/couscous/composer.json b/vendor-bin/couscous/composer.json index 7b03202f..ae2dd746 100644 --- a/vendor-bin/couscous/composer.json +++ b/vendor-bin/couscous/composer.json @@ -1,5 +1,10 @@ { "require": { "couscous/couscous": "^1.6.1" + }, + "config": { + "audit": { + "block-insecure": false + } } }