Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ jobs:

- name: Run infection.
run: |
vendor/bin/roave-infection-static-analysis-plugin --threads=2 --ignore-msi-with-no-mutations --only-covered
vendor/bin/roave-infection-static-analysis-plugin --threads=2 --ignore-msi-with-no-mutations
env:
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
- Chg #428: Update expression namespaces according to changes in `yiisoft/db` package (@Tigrov)
- Enh #432, #433: Update `DMLQueryBuilder::update()` method to adapt changes in `yiisoft/db` (@rustamwin, @Tigrov)
- Enh #439: Move "Packets out of order" warning suppression from Yii DB (@vjik)
- Chg #447: Refactor `ColumnDefinitionParser` (@vjik)

## 1.2.0 March 21, 2024

Expand Down
44 changes: 41 additions & 3 deletions src/Column/ColumnDefinitionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Db\Mysql\Column;

use Yiisoft\Db\Syntax\AbstractColumnDefinitionParser;

/**
* Parses column definition string. For example, `string(255)` or `int unsigned`.
*
Expand All @@ -19,11 +21,11 @@
* unsigned?: bool
* }
*/
final class ColumnDefinitionParser extends \Yiisoft\Db\Syntax\ColumnDefinitionParser
final class ColumnDefinitionParser extends AbstractColumnDefinitionParser
{
/**
* @psalm-return ExtraInfo
* @psalm-suppress ImplementedReturnTypeMismatch, InvalidReturnType, InvalidReturnStatement
* @inheritDoc
* @psalm-suppress InvalidReturnType, InvalidReturnStatement
*/
protected function extraInfo(string $extra): array
{
Expand All @@ -44,4 +46,40 @@ protected function extraInfo(string $extra): array

return $info;
}

protected function parseTypeParams(string $type, string $params): array
{
return match ($type) {
'bit',
'bigint',
'binary',
'char',
'decimal',
'double',
'float',
'int',
'integer',
'mediumint',
'numeric',
'real',
'smallint',
'string',
'tinyint',
'varbinary',
'varchar',
'year' => $this->parseSizeInfo($params),
'enum' => $this->parseEnumValues($params),
default => [],
};
Comment on lines +52 to +73
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return match ($type) {
'bit',
'bigint',
'binary',
'char',
'decimal',
'double',
'float',
'int',
'integer',
'mediumint',
'numeric',
'real',
'smallint',
'string',
'tinyint',
'varbinary',
'varchar',
'year' => $this->parseSizeInfo($params),
'enum' => $this->parseEnumValues($params),
default => [],
};
return match ($type) {
'enum' => $this->parseEnumValues($params),
default => $this->parseSizeInfo($params),
};

It seems a mistake to limit the list of types for parsing.
We use column definition parser not only for native db types but for abstract types also.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What case for abstract types?

}

/**
* @psalm-return array{enumValues: list<string>}
*/
protected function parseEnumValues(string $params): array
{
preg_match_all("/'([^']*)'/", $params, $matches);

return ['enumValues' => $matches[1]];
}
}
28 changes: 28 additions & 0 deletions tests/ColumnDefinitionParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mysql\Tests;

use PHPUnit\Framework\Attributes\DataProviderExternal;
use Yiisoft\Db\Mysql\Column\ColumnDefinitionParser;
use Yiisoft\Db\Mysql\Tests\Provider\ColumnDefinitionParserProvider;
use Yiisoft\Db\Syntax\ColumnDefinitionParserInterface;
use Yiisoft\Db\Tests\Common\CommonColumnDefinitionParserTest;

/**
* @group mysql
*/
final class ColumnDefinitionParserTest extends CommonColumnDefinitionParserTest
{
#[DataProviderExternal(ColumnDefinitionParserProvider::class, 'parse')]
public function testParse(string $definition, array $expected): void
{
parent::testParse($definition, $expected);
}

protected function createColumnDefinitionParser(): ColumnDefinitionParserInterface
{
return new ColumnDefinitionParser();
}
}
17 changes: 17 additions & 0 deletions tests/Provider/ColumnDefinitionParserProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mysql\Tests\Provider;

final class ColumnDefinitionParserProvider extends \Yiisoft\Db\Tests\Provider\ColumnDefinitionParserProvider
{
public static function parse(): array
{
return [
...parent::parse(),
["enum('a','b','c')", ['type' => 'enum', 'enumValues' => ['a', 'b', 'c']]],
["enum('a','b','c') NOT NULL", ['type' => 'enum', 'enumValues' => ['a', 'b', 'c'], 'notNull' => true]],
];
}
}
Loading