Skip to content

Commit

Permalink
Psalm fix
Browse files Browse the repository at this point in the history
  • Loading branch information
huntervk committed Feb 6, 2024
1 parent 893d8c3 commit 4947919
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 146 deletions.
6 changes: 5 additions & 1 deletion src/Parser/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public static function remove(string $key): void

public static function purge(): void
{
foreach (glob(sys_get_temp_dir() . '/type-cache.*') as $fileName) {
$fileNames = glob(sys_get_temp_dir() . '/type-cache.*');
if ($fileNames === false) {
return;
}
foreach ($fileNames as $fileName) {
unlink($fileName);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Parser/DocBlockAstTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ private function traverseTupleNode(ArrayShapeNode $node, ?NameContext $nameConte
}
$fields[] = $this->traverse($item->valueType, $nameContext);
}
/**
* @psalm-suppress MixedArgumentTypeCoercion
*/
return new Types\TupleType($fields);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Resolvers/MappingUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class MappingUtils
private static array $properties = [];

/**
* @template T
* @param mixed $value
* @param array<string,int> $mappedProperties
* @param Type<T> $context
*/
public static function checkMapping(mixed $value, array $mappedProperties, Type $context): void
{
Expand Down
4 changes: 2 additions & 2 deletions src/Types/LiteralType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
* @psalm-internal Hamlet\Type
* @template T of scalar
* @template T of scalar|null
* @extends Type<T>
*/
readonly class LiteralType extends Type
Expand Down Expand Up @@ -56,7 +56,7 @@ public function __construct(mixed ...$values)
#[Override] public function __toString(): string
{
$escape =
function ($a): string {
function (int|bool|float|string|null $a): string {
if (is_string($a)) {
return "'$a'";
}
Expand Down
3 changes: 0 additions & 3 deletions src/Types/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
*/
readonly class MixedType extends Type
{
/**
* @psalm-assert-if-true mixed $value
*/
#[Override] public function matches(mixed $value): bool
{
return true;
Expand Down
17 changes: 12 additions & 5 deletions src/Types/NonEmptyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@
if ($this->matches($value)) {
return $value;
}
if (is_object($value) && !method_exists($value, '__toString')) {
throw new CastException($value, $this);
}
if (is_array($value)) {

if (is_object($value)) {
if (method_exists($value, '__toString')) {
$stringValue = (string)$value;
} else {
throw new CastException($value, $this);
}
} elseif (is_array($value)) {
$stringValue = 'Array';
} else {
} elseif (is_scalar($value) || is_resource($value) || is_null($value)) {
$stringValue = (string)$value;
} else {
throw new CastException($value, $this);
}

if ($stringValue === '') {
throw new CastException($value, $this);
}
Expand Down
20 changes: 15 additions & 5 deletions src/Types/NumericStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,28 @@
{
if ($this->matches($value)) {
return $value;
}

if (is_string($value)) {
$stringValue = $value;
} elseif (is_array($value) || is_null($value)) {
throw new CastException($value, $this);
} elseif (is_scalar($value) || is_resource($value)) {
$value = (string)$value;
} elseif (is_object($value) && method_exists($value, '__toString')) {
$value = (string)$value;
$stringValue = (string)$value;
} elseif (is_object($value)) {
if (method_exists($value, '__toString')) {
$stringValue = (string)$value;
} else {
throw new CastException($value, $this);
}
} else {
throw new CastException($value, $this);
}

if (!is_string($value) || !is_numeric($value)) {
if (!is_numeric($stringValue)) {
throw new CastException($value, $this);
}
return $value;
return $stringValue;
}

#[Override] public function __toString(): string
Expand Down
116 changes: 0 additions & 116 deletions src/Types/ObjectLikeType.php

This file was deleted.

21 changes: 12 additions & 9 deletions src/Types/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@
if ($this->matches($value)) {
return $value;
}
if (is_scalar($value) || is_resource($value)) {
return (string)$value;
}
if (is_object($value) && method_exists($value, '__toString')) {

if (is_object($value)) {
if (method_exists($value, '__toString')) {
return (string)$value;
} else {
throw new CastException($value, $this);
}
} elseif (is_scalar($value) || is_resource($value)) {
return (string)$value;
}
if (is_array($value)) {
} elseif (is_array($value)) {
return 'Array';
}
if ($value === null) {
} elseif ($value === null) {
return '';
} else {
throw new CastException($value, $this);
}
throw new CastException($value, $this);
}

#[Override] public function __toString(): string
Expand Down
23 changes: 19 additions & 4 deletions src/Types/TupleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,50 @@

/**
* @psalm-internal Hamlet\Type
* @template A
* @template A of list
* @extends Type<A>
*/
readonly class TupleType extends Type
{
/**
* @var list<Type<A>>
* @var list<Type>
*/
private array $fields;

/**
* @param list<Type<A>> $fields
* @param list<Type> $fields
*/
public function __construct(array $fields)
{
$this->fields = $fields;
}

/**
* @psalm-assert-if-true list<A> $value
* @psalm-assert-if-true A $value
*/
#[Override] public function matches(mixed $value): bool
{
if (!is_array($value) || !array_is_list($value) || count($value) != count($this->fields)) {
return false;
}
for ($i = 0; $i < count($this->fields); $i++) {
/**
* @psalm-suppress MixedAssignment
*/
$v = $value[$i];
/**
* @psalm-suppress TypeDoesNotContainType
*/
if (!$this->fields[$i]->matches($v)) {
return false;
}
}
return true;
}

/**
* @psalm-suppress InvalidReturnType
*/
#[Override] public function resolveAndCast(mixed $value, Resolver $resolver): array
{
if ($this->matches($value)) {
Expand All @@ -64,8 +73,14 @@ public function __construct(array $fields)
$result = [];
$value = array_values($value);
foreach ($this->fields as $i => $field) {
/**
* @psalm-suppress MixedAssignment
*/
$result[] = $field->resolveAndCast($value[$i], $resolver);
}
/**
* @psalm-suppress InvalidReturnStatement
*/
return $result;
}

Expand Down
6 changes: 5 additions & 1 deletion src/types.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function _null(): Type

/**
* // @todo literal types should be checked, cannot be just anything.
* @template A of scalar
* @template A of scalar|null
* @param array<A> $as
* @return Type<A>
*/
Expand Down Expand Up @@ -205,6 +205,7 @@ function _union(Type $a, Type $b, Type $c = null, Type $d = null, Type $e = null
* @param Type<H>|null $h
* @return Type
* @psalm-return (func_num_args() is 2 ? Type<list{A,B}> : (func_num_args() is 3 ? Type<list{A,B,C}> : (func_num_args() is 4 ? Type<list{A,B,C,D}> : (func_num_args() is 5 ? Type<list{A,B,C,D,E}> : (func_num_args() is 6 ? Type<list{A,B,C,D,E,F}> : (func_num_args() is 7 ? Type<list{A,B,C,D,E,F,G}> : Type<list{A,B,C,D,E,F,G,H}>))))))
* @psalm-suppress InvalidReturnType
* @psalm-suppress MixedReturnTypeCoercion
*/
function _tuple(Type $a, Type $b, Type $c = null, Type $d = null, Type $e = null, Type $f = null, Type $g = null, Type $h = null): Type
Expand All @@ -224,6 +225,9 @@ function _tuple(Type $a, Type $b, Type $c = null, Type $d = null, Type $e = null
$fields[] = $arg;
}
}
/**
* @psalm-suppress InvalidReturnStatement
*/
return new Types\TupleType($fields);
}

Expand Down

0 comments on commit 4947919

Please sign in to comment.