Skip to content

Commit

Permalink
Improve psalm annotations in ColumnDefinitionParser
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Oct 18, 2024
1 parent d04a9c4 commit acf6473
Showing 1 changed file with 17 additions and 6 deletions.
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

0 comments on commit acf6473

Please sign in to comment.