Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into 0.2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
MidnightDesign committed Aug 15, 2024
2 parents 0801717 + 81d2be7 commit b6fba9e
Show file tree
Hide file tree
Showing 18 changed files with 1 addition and 58 deletions.
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="true"
findUnusedPsalmSuppress="true"
>
<projectFiles>
<directory name="src"/>
Expand Down
1 change: 0 additions & 1 deletion src/And_.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function __construct(public readonly Expression $left, public readonly Ex

public function __toString(): string
{
/** @psalm-suppress ImplicitToStringCast */
return sprintf('%s && %s', $this->left, $this->right);
}

Expand Down
2 changes: 0 additions & 2 deletions src/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ private static function compareArguments(array $a, array $b): bool

public function __toString(): string
{
/** @psalm-suppress ImplicitToStringCast */
return sprintf('%s.%s:%s(%s)', $this->target, $this->name, $this->type, implode(', ', $this->arguments));
}

Expand All @@ -75,7 +74,6 @@ public function evaluate(Scope $scope): mixed

public function equals(Expression $other): bool
{
/** @psalm-suppress RedundantConditionGivenDocblockType False positive */
return $other instanceof self
&& $this->target->equals($other->target)
&& $this->name === $other->name
Expand Down
1 change: 0 additions & 1 deletion src/Eq.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function __construct(public readonly Expression $left, public readonly Ex

public function __toString(): string
{
/** @psalm-suppress ImplicitToStringCast */
return sprintf('%s === %s', $this->left, $this->right);
}

Expand Down
1 change: 0 additions & 1 deletion src/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function evaluate(Scope $scope): mixed
try {
return $this->typeHint->type->assert($value);
} catch (Parser\TypeError $e) {
/** @psalm-suppress ImplicitToStringCast */
throw new EvaluationError(
sprintf(
'Expected variable "%s" to be of type %s, got %s: %s',
Expand Down
1 change: 0 additions & 1 deletion src/Gt.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function __construct(private readonly Expression $left, private readonly

public function __toString(): string
{
/** @psalm-suppress ImplicitToStringCast */
return sprintf('%s > %s', $this->left, $this->right);
}

Expand Down
2 changes: 0 additions & 2 deletions src/Lambda.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public function __construct(public readonly Expression $body, public readonly ar

public function __toString(): string
{
/** @psalm-suppress ImplicitToStringCast */
return sprintf('|%s| %s', implode(', ', $this->parameters), $this->body);
}

Expand All @@ -40,7 +39,6 @@ public function evaluate(Scope $scope): callable
return function (mixed ...$params) use ($scope): mixed {
$localVars = [];
foreach ($this->parameters as $index => $parameter) {
/** @psalm-suppress MixedAssignment */
$localVars[$parameter] = $params[$index];
}
return $this->body->evaluate($scope->sub($localVars));
Expand Down
1 change: 0 additions & 1 deletion src/ListLiteral.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public function getType(): Type
$elementType = $type;
continue;
}
/** @psalm-suppress RedundantCondition */
if ($elementType->equals($type)) {
continue;
}
Expand Down
3 changes: 0 additions & 3 deletions src/Negative.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public function __toString(): string
return sprintf('-%s', $this->expression);
}

/**
* @psalm-suppress InvalidReturnType False positive
*/
public function evaluate(Scope $scope): float|int
{
$value = $this->expression->evaluate($scope);
Expand Down
1 change: 0 additions & 1 deletion src/Or_.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function __construct(public readonly Expression $left, public readonly Ex

public function __toString(): string
{
/** @psalm-suppress ImplicitToStringCast */
return sprintf('%s || %s', $this->left, $this->right);
}

Expand Down
13 changes: 0 additions & 13 deletions src/Parser/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public static function parse(string $expression, Declarations|Types|null $types
public static function parseTyped(string $expression, Type $type, Declarations|Types|null $types = null): Expression
{
$expr = self::parse($expression, $types);
/** @psalm-suppress ImplicitToStringCast */
return self::assertExpressionType($expr, $type, sprintf(
'Expected parsed expression to be of type %s, got %s',
$type,
Expand Down Expand Up @@ -115,7 +114,6 @@ private static function parseLazy(Expression|null $left, Peekable $tokens, Decla
self::unexpectedToken($parsedToken);
}
$right = self::parseExpression($tokens, $declarations);
/** @psalm-suppress ImplicitToStringCast */
$right = self::assertExpressionType($right, $left->getType(), sprintf(
'The expressions of both sides of === must be of the same type. Left: %s, right: %s',
$left->getType(),
Expand All @@ -128,14 +126,12 @@ private static function parseLazy(Expression|null $left, Peekable $tokens, Decla
if ($left === null) {
self::unexpectedToken($parsedToken);
}
/** @psalm-suppress ImplicitToStringCast */
$left = self::assertExpressionType($left, Type::bool(), sprintf(
'The expression on the left side of %s must be boolean, got %s',
Token::print($token),
$left->getType(),
));
$right = self::parseExpression($tokens, $declarations);
/** @psalm-suppress ImplicitToStringCast */
$right = self::assertExpressionType($right, Type::bool(), sprintf(
'The expression on the right side of %s must be boolean, got %s',
Token::print($token),
Expand All @@ -153,7 +149,6 @@ private static function parseLazy(Expression|null $left, Peekable $tokens, Decla
throw SyntaxError::create('Unexpected end of input', Span::char($parsedToken->line, $parsedToken->column + 1));
}
if (!$right->matchesType(Type::int()) && !$right->matchesType(Type::float())) {
/** @psalm-suppress ImplicitToStringCast */
throw TypeError::create(
$left === null
? sprintf('Can\'t negate %s', $right->getType())
Expand All @@ -165,7 +160,6 @@ private static function parseLazy(Expression|null $left, Peekable $tokens, Decla
return Expr::negative($right, $parsedToken->location()->to($right->location()));
}
if (!$left->getType()->equals($right->getType())) {
/** @psalm-suppress ImplicitToStringCast */
throw TypeError::create(
sprintf('Can\'t subtract %s from %s', $right->getType(), $left->getType()),
$left->location(),
Expand All @@ -180,11 +174,9 @@ private static function parseLazy(Expression|null $left, Peekable $tokens, Decla
$tokens->next();
$right = self::parseExpression($tokens, $declarations);
if (!$right->matchesType(Type::int()) && !$right->matchesType(Type::float())) {
/** @psalm-suppress ImplicitToStringCast */
throw TypeError::create(sprintf('Can\'t compare %s to %s', $right->getType(), $left->getType()), $right->location());
}
if (!$left->matchesType($right->getType())) {
/** @psalm-suppress ImplicitToStringCast */
throw TypeError::create(sprintf('Can\'t compare %s to %s', $left->getType(), $right->getType()), $left->location()->to($right->location()));
}
return $left->gt($right);
Expand Down Expand Up @@ -364,14 +356,9 @@ private static function parseParam(Peekable $tokens): string|null

private static function assertExpressionType(Expression $expr, Type $type, string $errorMessage): Expression
{
/** @psalm-suppress RedundantCondition False positive. This check is _not_ redundant. */
if ($expr->matchesType($type)) {
return $expr;
}
/**
* @psalm-suppress MixedArgument False positive
* @psalm-suppress MixedMethodCall False positive
*/
throw TypeError::create($errorMessage, $expr->location());
}

Expand Down
5 changes: 0 additions & 5 deletions src/Parser/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ private static function noArgs(Type $type, TypeNode $node): Type|TypeError
return $type;
}
$location = $node->args[0]->location->to($node->args[count($node->args) - 1]->location);
/** @psalm-suppress ImplicitToStringCast */
return TypeError::create(sprintf('Invalid type "%s": %s does not accept arguments', $node, $type), $location);
}

Expand Down Expand Up @@ -91,7 +90,6 @@ private function resolveList(TypeNode $node): Type|TypeError
$nArgs = count($args);
if ($nArgs > 1) {
$location = $args[0]->location->to($args[count($args) - 1]->location);
/** @psalm-suppress ImplicitToStringCast */
return TypeError::create(
sprintf(
'Invalid type "%s": list expects exactly one argument, got %d',
Expand All @@ -116,9 +114,7 @@ private function resolveMap(TypeNode $node): Type|TypeError
}
$nArgs = count($args);
if ($nArgs !== 2) {
/** @psalm-suppress TypeDoesNotContainNull False positive */
$location = $args[0]->location->to($args[count($args) - 1]->location);
/** @psalm-suppress ImplicitToStringCast */
return TypeError::create(
sprintf(
'Invalid type "%s": map expects exactly two arguments, got %d',
Expand All @@ -133,7 +129,6 @@ private function resolveMap(TypeNode $node): Type|TypeError
return $keyType;
}
if (!$keyType->equals(Type::int()) && !$keyType->equals(Type::string())) {
/** @psalm-suppress ImplicitToStringCast */
return TypeError::create(
sprintf(
'Invalid type "%s": map expects the key type to be int or string, got %s',
Expand Down
4 changes: 0 additions & 4 deletions src/Subtract.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@ public function __construct(public readonly Expression $minuend, public readonly

public function __toString(): string
{
/** @psalm-suppress ImplicitToStringCast */
return sprintf('%s - %s', $this->minuend, $this->subtrahend);
}

/**
* @psalm-suppress InvalidReturnType False positive
*/
public function evaluate(Scope $scope): int|float
{
/** @var mixed $minuend */
Expand Down
9 changes: 0 additions & 9 deletions src/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ public static function bool(): self

public static function listOf(self $item): self
{
/** @psalm-suppress ImplicitToStringCast */
return new self('list', [$item]);
}

public static function mapOf(self $keys, self $values): self
{
/** @psalm-suppress ImplicitToStringCast */
return new self('map', [$keys, $values]);
}

Expand All @@ -84,9 +82,6 @@ public static function func(self $return, array $parameters = []): self
return new self('Func', [$return, ...$parameters]);
}

/**
* @psalm-suppress InvalidReturnType False positive
*/
public static function fromValue(mixed $value): self
{
if (is_array($value)) {
Expand Down Expand Up @@ -168,10 +163,6 @@ public function equals(self $type): bool
return true;
}
foreach ($this->args as $i => $arg) {
/**
* @psalm-suppress RedundantCondition I think it's complaining about Type<mixed> being equal to Type<mixed>,
* but I don't know how to fix it.
*/
if ($arg->equals($type->args[$i])) {
continue;
}
Expand Down
7 changes: 0 additions & 7 deletions tests/unit/ExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,6 @@ functions: [
['ints3:list<int>.tail:list<int>()', new Scope(['ints3' => []]), []],
['foo:float - bar:float', new Scope(['foo' => 5.5, 'bar' => 3.4]), 2.1],
];
/**
* @psalm-suppress PossiblyUndefinedArrayOffset The runtime behavior is well-defined: `$declarations` is just null
*/
foreach ($cases as $tuple) {
[$expr, $scope, $expected] = $tuple;
$declarations = $tuple[3] ?? null;
Expand Down Expand Up @@ -463,10 +460,6 @@ public function testType(Expression|string $expression, Type $expected): void
$expression = ExpressionParser::parse($expression);
}

/**
* @psalm-suppress ImplicitToStringCast
* @psalm-suppress RedundantCondition I have no idea how to type this better
*/
self::assertTrue(
$expression->matchesType($expected),
sprintf('Expected %s, got %s', $expected, $expression->getType()),
Expand Down
1 change: 0 additions & 1 deletion tests/unit/Parser/ExpressionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@ public function testParse(string $str, Expression $expected): void
{
$actual = ExpressionParser::parse($str);

/** @psalm-suppress ImplicitToStringCast */
self::assertTrue($actual->equals($expected), sprintf(
"Expected:\n%s\nActual:\n%s",
$expected,
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/TypeEqualityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ public function testEqual(string $a, string $b): void
$typeA = self::fromString($a);
$typeB = self::fromString($b);

/** @psalm-suppress RedundantCondition */
self::assertTrue($typeA->equals($typeB));
/** @psalm-suppress RedundantCondition */
self::assertTrue($typeB->equals($typeA));
}

Expand Down
4 changes: 0 additions & 4 deletions tests/unit/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public function testAliasTypeEqualsAliasTarget(): void
$concrete = Type::listOf(Type::string());
$alias = Type::alias('Foo', $concrete);

/** @psalm-suppress RedundantCondition */
self::assertTrue($alias->equals($concrete));
/** @psalm-suppress RedundantCondition */
self::assertTrue($concrete->equals($alias));
}

Expand All @@ -60,9 +58,7 @@ public function testDifferentAliasesForTheSameTypeAreEqual(): void
$foo = Type::alias('Foo', $concrete);
$bar = Type::alias('Bar', $concrete);

/** @psalm-suppress RedundantCondition */
self::assertTrue($foo->equals($bar));
/** @psalm-suppress RedundantCondition */
self::assertTrue($bar->equals($foo));
}

Expand Down

0 comments on commit b6fba9e

Please sign in to comment.