Skip to content
Snippets Groups Projects
Commit 31fb0931 authored by Jiří Pudil's avatar Jiří Pudil
Browse files

Merge branch 'fixes' into 'master'

Minor fixes

See merge request !38
parents d98e0552 5fa03116
No related branches found
No related tags found
1 merge request!38Minor fixes
Pipeline #38408 passed
......@@ -11,3 +11,12 @@ parameters:
excludePaths:
- tests/DI/temp/*
ignoreErrors:
# null() / notNull() currently require type parameter so that it can be used in the return annotation.
# A better solution would be to use a type projection in the return type, but that's not currently supported in PHPStan.
# more info: https://gitlab.grifart.cz/grifart/tables/-/merge_requests/38#note_90571, https://github.com/phpstan/phpstan/issues/3290
-
message: "#^Template type ValueType of function Grifart\\\\Tables\\\\Conditions\\\\(null|notNull)\\(\\) is not referenced in a parameter\\.$#"
count: 2
path: src/Conditions/functions.php
......@@ -94,7 +94,8 @@ function notIn(array $values): \Closure {
}
/**
* @return \Closure(Expression<mixed>): IsNull
* @template ValueType
* @return \Closure(Expression<ValueType>): IsNull
*/
function null(): \Closure
{
......@@ -102,7 +103,8 @@ function null(): \Closure
}
/**
* @return \Closure(Expression<mixed>): IsNotNull
* @template ValueType
* @return \Closure(Expression<ValueType>): IsNotNull
*/
function notNull(): \Closure
{
......
......@@ -51,7 +51,8 @@ final class TypeResolver
}
/**
* @param Type<mixed> $type
* @template ValueType
* @param Type<ValueType> $type
*/
public function addType(Type $type): void
{
......@@ -66,7 +67,8 @@ final class TypeResolver
}
/**
* @param Type<mixed> $type
* @template ValueType
* @param Type<ValueType> $type
*/
public function addResolutionByTypeName(string $typeName, Type $type): void
{
......@@ -88,7 +90,8 @@ final class TypeResolver
}
/**
* @param Type<mixed> $type
* @template ValueType
* @param Type<ValueType> $type
*/
public function addResolutionByLocation(string $location, Type $type): void
{
......
......@@ -89,37 +89,37 @@ abstract class CompositeType implements Type
$result = [];
$string = false;
$inString = false;
$isString = false;
$length = \strlen($value);
$item = '';
for ($i = $start + 1; $i < $length; $i++) {
$char = $value[$i];
if ( ! $string && $char === ')') {
if ($item !== '') {
$result[] = $item;
}
if ( ! $inString && $char === ')') {
$result[] = $isString || $item !== '' ? $item : null;
$end = $i;
break;
}
if ( ! $string && $char === '(') {
if ( ! $inString && $char === '(') {
// parse to the end but only keep raw value so that it can be recursively parsed in fromDatabase()
$subCompositeStart = $i;
$this->parseComposite($value, $i, $i);
$item = \substr($value, $subCompositeStart, $i - $subCompositeStart);
} elseif ( ! $string && $char ===',') {
$result[] = $item !== '' ? $item : null;
} elseif ( ! $inString && $char === ',') {
$result[] = $isString || $item !== '' ? $item : null;
$isString = false;
$item = '';
} elseif ( ! $string && $char === '"') {
$string = true;
} elseif ($string && $char === "\\" && $value[$i - 1] === "\\") {
} elseif ( ! $inString && $char === '"') {
$inString = $isString = true;
} elseif ($inString && $char === "\\" && $value[$i - 1] === "\\") {
$item = \substr($item, 0, -1) . $char;
} elseif ($string && $char === '"' && $value[$i + 1] === '"') {
} elseif ($inString && $char === '"' && $value[$i + 1] === '"') {
$item .= $char;
$i++;
} elseif ($string && $char === '"' && $value[$i + 1] !== '"') {
$string = false;
} elseif ($inString && $char === '"' && $value[$i + 1] !== '"') {
$inString = false;
} else {
$item .= $char;
}
......
......@@ -22,12 +22,14 @@ $composite = new class extends CompositeType {
new IntType(),
new TextType(),
new TextType(),
new TextType(),
new TextType(),
);
}
public function getPhpType(): PhpType
{
return tuple('int', nullable('int'), 'string');
return tuple('int', nullable('int'), 'string', 'string', nullable('string'));
}
public function getDatabaseTypes(): array
......@@ -46,5 +48,5 @@ $composite = new class extends CompositeType {
}
};
Assert::same('(42,,"com\\\\ple\\"x","(")', $composite->toDatabase([42, null, 'com\\ple"x', '(']));
Assert::same([42, null, 'com\\ple"x', '('], $composite->fromDatabase('(42,,"com\\\\ple""x","(")'));
Assert::same('(42,,"com\\\\ple\\"x","(","",)', $composite->toDatabase([42, null, 'com\\ple"x', '(', '', null]));
Assert::same([42, null, 'com\\ple"x', '(', '', null], $composite->fromDatabase('(42,,"com\\\\ple""x","(","",)'));
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment