From 07c0d58e20d09cdb35b984255534d00d88d7a760 Mon Sep 17 00:00:00 2001 From: Kim Pepper Date: Tue, 17 Nov 2026 17:43:44 +1100 Subject: [PATCH] fix: resolve phpstan errors from doctrine/dbal 4.6 deprecations Use the dbal 4.5+ schema editor API when available, falling back to the old Table methods on dbal 3 and 4.0-4.4. Drop the table via the platform's DROP TABLE SQL, which also fixes drop() throwing TableDoesNotExist. Remove an always-false null check in Node. --- src/Node.php | 3 - src/Storage/DbalNestedSetSchema.php | 116 +++++++++++++++++++++------- 2 files changed, 88 insertions(+), 31 deletions(-) diff --git a/src/Node.php b/src/Node.php index dc03d8c0..fe968c57 100644 --- a/src/Node.php +++ b/src/Node.php @@ -25,9 +25,6 @@ class Node { * The depth. */ public function __construct(NodeKey $nodeKey, int $left, int $right, int $depth) { - if ($nodeKey == NULL) { - throw new \InvalidArgumentException("Node key cannot be NULL"); - } $this->nodeKey = $nodeKey; if ($left < 1) { throw new \InvalidArgumentException("Left value must be > 0"); diff --git a/src/Storage/DbalNestedSetSchema.php b/src/Storage/DbalNestedSetSchema.php index ec335342..5bc4d791 100644 --- a/src/Storage/DbalNestedSetSchema.php +++ b/src/Storage/DbalNestedSetSchema.php @@ -2,9 +2,12 @@ namespace PNX\NestedSet\Storage; +use Doctrine\DBAL\Schema\Column; +use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Name\UnqualifiedName; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Schema; +use Doctrine\DBAL\Schema\Table; use PNX\NestedSet\NestedSetSchemaInterface; /** @@ -12,50 +15,107 @@ */ class DbalNestedSetSchema extends BaseDbalStorage implements NestedSetSchemaInterface { + /** + * The table columns. + */ + private const COLUMNS = ['id', 'revision_id', 'left_pos', 'right_pos', 'depth']; + + /** + * The primary key columns. + */ + private const PRIMARY_KEY = ['id', 'revision_id']; + + /** + * The columns of each index. + */ + private const INDEXES = [ + ['revision_id'], + ['id', 'revision_id', 'left_pos', 'right_pos', 'depth'], + ['left_pos', 'right_pos'], + ['right_pos'], + ]; + /** * {@inheritdoc} */ public function create(): void { - $schema = new Schema(); - $tree = $schema->createTable($this->tableName); - $tree->addColumn("id", "integer", ["unsigned" => TRUE]); - $tree->addColumn("revision_id", "integer", ["unsigned" => TRUE]); - $tree->addColumn("left_pos", "integer", ["unsigned" => TRUE]); - $tree->addColumn("right_pos", "integer", ["unsigned" => TRUE]); - $tree->addColumn("depth", "integer", ["unsigned" => TRUE]); - - if (\class_exists('\Doctrine\DBAL\Schema\Name\UnqualifiedName')) { + $schema = new Schema([$this->buildTable()]); + foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) { + $this->connection->executeStatement($sql); + } + } + + /** + * {@inheritdoc} + */ + public function drop(): void { + $this->connection->executeStatement( + $this->connection->getDatabasePlatform()->getDropTableSQL($this->tableName) + ); + } + + /** + * Builds the nested set table definition. + */ + private function buildTable(): Table { + // Version shim: only constant for a single installed dbal version. + // @phpstan-ignore function.alreadyNarrowedType + if (\method_exists(Table::class, 'editor')) { + // doctrine/dbal >= 4.5. + $editor = Table::editor()->setUnquotedName($this->tableName); + foreach (self::COLUMNS as $column) { + // @phpstan-ignore staticMethod.notFound + $editor->addColumn(Column::editor()->setUnquotedName($column)->setTypeName('integer')->setUnsigned(TRUE)->create()); + } + // @phpstan-ignore class.notFound + $editor->addPrimaryKeyConstraint($this->buildPrimaryKeyConstraint()); + foreach (self::INDEXES as $columns) { + // @phpstan-ignore staticMethod.notFound + $editor->addIndex(Index::editor()->setUnquotedColumnNames(...$columns)); + } + return $editor->create(); + } + + // doctrine/dbal 3 and < 4.5. + $table = new Table($this->tableName); + foreach (self::COLUMNS as $column) { + // @phpstan-ignore method.deprecated + $table->addColumn($column, 'integer', ['unsigned' => TRUE]); + } + if (\class_exists(UnqualifiedName::class)) { // doctrine/dbal 4. - // @phpstan-ignore method.notFound, class.notFound - $tree->addPrimaryKeyConstraint(new PrimaryKeyConstraint(UnqualifiedName::unquoted('primary'), [ - UnqualifiedName::unquoted('id'), - UnqualifiedName::unquoted('revision_id'), - ], FALSE)); + // @phpstan-ignore method.deprecated, method.notFound + $table->addPrimaryKeyConstraint($this->buildPrimaryKeyConstraint()); } else { // doctrine/dbal 3. // @phpstan-ignore method.deprecated - $tree->setPrimaryKey(['id', 'revision_id']); + $table->setPrimaryKey(self::PRIMARY_KEY); } - $tree->addIndex(['revision_id']); - $tree->addIndex(['id', 'revision_id', 'left_pos', 'right_pos', 'depth']); - $tree->addIndex(['left_pos', 'right_pos']); - $tree->addIndex(['right_pos']); - - foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) { - $this->connection->executeStatement($sql); + foreach (self::INDEXES as $columns) { + // @phpstan-ignore method.deprecated + $table->addIndex($columns); } + return $table; } /** - * {@inheritdoc} + * Builds the primary key constraint for doctrine/dbal 4. + * + * @return \Doctrine\DBAL\Schema\PrimaryKeyConstraint + * The primary key constraint. */ - public function drop(): void { - $schema = new Schema(); - $schema->dropTable($this->tableName); - foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) { - $this->connection->executeStatement($sql); + // @phpstan-ignore class.notFound + private function buildPrimaryKeyConstraint(): object { + $columnNames = []; + foreach (self::PRIMARY_KEY as $column) { + // @phpstan-ignore class.notFound + $columnNames[] = UnqualifiedName::unquoted($column); } + // @phpstan-ignore class.notFound + $name = UnqualifiedName::unquoted('primary'); + // @phpstan-ignore class.notFound + return new PrimaryKeyConstraint($name, $columnNames, FALSE); } }