2424use function is_object ;
2525use function strtolower ;
2626
27- /** @extends AbstractNamedObject<UnqualifiedName> */
28- class Index extends AbstractNamedObject
27+ /**
28+ * @final
29+ * @extends AbstractNamedObject<UnqualifiedName>
30+ */
31+ final class Index extends AbstractNamedObject
2932{
3033 /**
3134 * Asset identifier instances of the column names the index is associated with.
@@ -55,9 +58,9 @@ class Index extends AbstractNamedObject
5558 private readonly array $ columns ;
5659
5760 /**
58- * @param array<int, string> $columns
59- * @param array<int, string> $flags
60- * @param array<string, mixed> $options
61+ * @param non-empty-list< string> $columns
62+ * @param array<int, string> $flags
63+ * @param array<string, mixed> $options
6164 */
6265 public function __construct (
6366 ?string $ name ,
@@ -80,16 +83,6 @@ public function __construct(
8083 $ this ->_isUnique = $ isUnique || $ isPrimary ;
8184 $ this ->_isPrimary = $ isPrimary ;
8285
83- $ lengths = $ options ['lengths ' ] ?? [];
84-
85- if ($ isPrimary && count ($ lengths ) > 0 ) {
86- Deprecation::trigger (
87- 'doctrine/dbal ' ,
88- 'https://github.com/doctrine/dbal/pull/6787 ' ,
89- 'Declaring column length for primary key indexes is deprecated. ' ,
90- );
91- }
92-
9386 foreach ($ columns as $ column ) {
9487 $ this ->_addColumn ($ column );
9588 }
@@ -98,7 +91,7 @@ public function __construct(
9891 $ this ->addFlag ($ flag );
9992 }
10093
101- $ this ->columns = $ this ->parseColumns ($ columns , $ options ['lengths ' ] ?? []);
94+ $ this ->columns = $ this ->parseColumns ($ isPrimary , $ columns , $ options ['lengths ' ] ?? []);
10295 }
10396
10497 protected function getNameParser (): UnqualifiedNameParser
@@ -128,10 +121,11 @@ protected function _addColumn(string $column): void
128121 /**
129122 * Returns the names of the referencing table columns the constraint is associated with.
130123 *
131- * @return list<string>
124+ * @return non-empty- list<string>
132125 */
133126 public function getColumns (): array
134127 {
128+ /** @phpstan-ignore return.type */
135129 return array_keys ($ this ->_columns );
136130 }
137131
@@ -339,60 +333,71 @@ public function getOptions(): array
339333 }
340334
341335 /**
342- * @param array<string> $columnNames
343- * @param array<int> $lengths
336+ * @param non-empty- array<int, string> $columnNames
337+ * @param array<int> $lengths
344338 *
345339 * @return list<IndexedColumn>
346340 */
347- private function parseColumns (array $ columnNames , array $ lengths ): array
341+ private function parseColumns (bool $ isPrimary , array $ columnNames , array $ lengths ): array
348342 {
349343 $ columns = [];
350344
351345 $ parser = Parsers::getUnqualifiedNameParser ();
352346
353- try {
354- foreach ($ columnNames as $ columnName ) {
355- $ name = $ parser ->parse ($ columnName );
356- $ length = array_shift ($ lengths );
357-
358- if ($ length !== null ) {
359- if (! is_int ($ length )) {
360- Deprecation::trigger (
361- 'doctrine/dbal ' ,
362- 'https://github.com/doctrine/dbal/pull/6787 ' ,
363- 'Indexed column length should be an integer, %s given. ' ,
364- is_object ($ length ) ? $ length ::class : gettype ($ length ),
365- );
366-
367- $ length = (int ) $ length ;
368- }
369-
370- if ($ length < 1 ) {
371- Deprecation::trigger (
372- 'doctrine/dbal ' ,
373- 'https://github.com/doctrine/dbal/pull/6787 ' ,
374- 'Indexed column length should be a positive integer, %d given. ' ,
375- $ length ,
376- );
377-
378- return [];
379- }
347+ foreach ($ columnNames as $ columnName ) {
348+ try {
349+ $ parsedName = $ parser ->parse ($ columnName );
350+ } catch (Throwable $ e ) {
351+ Deprecation::trigger (
352+ 'doctrine/dbal ' ,
353+ 'https://github.com/doctrine/dbal/pull/6787 ' ,
354+ 'Unable to parse column name: %s. ' ,
355+ $ e ->getMessage (),
356+ );
357+
358+ return [];
359+ }
360+
361+ $ length = array_shift ($ lengths );
362+
363+ if ($ length !== null ) {
364+ if ($ isPrimary ) {
365+ Deprecation::trigger (
366+ 'doctrine/dbal ' ,
367+ 'https://github.com/doctrine/dbal/pull/6787 ' ,
368+ 'Declaring column length for primary key indexes is deprecated. ' ,
369+ );
370+
371+ return [];
372+ }
373+
374+ if (! is_int ($ length )) {
375+ Deprecation::trigger (
376+ 'doctrine/dbal ' ,
377+ 'https://github.com/doctrine/dbal/pull/6787 ' ,
378+ 'Indexed column length should be an integer, %s given. ' ,
379+ is_object ($ length ) ? $ length ::class : gettype ($ length ),
380+ );
381+
382+ $ length = (int ) $ length ;
380383 }
381384
382- $ columns [] = new IndexedColumn ($ name , $ length );
383- }
385+ if ($ length < 1 ) {
386+ Deprecation::trigger (
387+ 'doctrine/dbal ' ,
388+ 'https://github.com/doctrine/dbal/pull/6787 ' ,
389+ 'Indexed column length should be a positive integer, %d given. ' ,
390+ $ length ,
391+ );
384392
385- return $ columns ;
386- } catch (Throwable $ e ) {
387- Deprecation::trigger (
388- 'doctrine/dbal ' ,
389- 'https://github.com/doctrine/dbal/pull/6787 ' ,
390- 'Unable to parse column name: %s. ' ,
391- $ e ->getMessage (),
392- );
393+ return [];
394+ }
395+ }
393396
394- return [] ;
397+ $ columns [] = new IndexedColumn ( $ parsedName , $ length ) ;
395398 }
399+
400+ return $ columns ;
396401 }
397402
398403 /**
0 commit comments