Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve psalm annotations in ColumnDefinitionParser #891

Merged
merged 1 commit into from
Oct 18, 2024
Merged
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
23 changes: 17 additions & 6 deletions src/Syntax/ColumnDefinitionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Yiisoft\Db\Syntax;

use Yiisoft\Db\Schema\Column\ColumnFactoryInterface;

use function explode;
use function preg_match;
use function preg_match_all;
Expand All @@ -18,8 +16,6 @@

/**
* Parses column definition string. For example, `string(255)` or `int unsigned`.
*
* @psalm-import-type ColumnInfo from ColumnFactoryInterface
*/
final class ColumnDefinitionParser
{
Expand All @@ -30,7 +26,14 @@ final class ColumnDefinitionParser
*
* @return array The column information.
*
* @psalm-return ColumnInfo
* @psalm-return array{
* enumValues?: list<string>,
* extra?: string,
* scale?: int,
* size?: int,
* type: lowercase-string,
* unsigned?: bool,
* }
*/
public function parse(string $definition): array
{
Expand All @@ -49,17 +52,22 @@ public function parse(string $definition): array

$extra = substr($definition, strlen($matches[0]));

/** @var ColumnInfo */
return $info + $this->extraInfo($extra);
}

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

return ['enumValues' => $matches[1]];
}

/**
* @psalm-return array{unsigned?: bool, extra?: string}
*/
private function extraInfo(string $extra): array
{
if (empty($extra)) {
Expand All @@ -80,6 +88,9 @@ private function extraInfo(string $extra): array
return $info;
}

/**
* @psalm-return array{size: int, scale?: int}
*/
private function sizeInfo(string $size): array
{
$values = explode(',', $size);
Expand Down
Loading