Skip to content

Commit

Permalink
Merge pull request #7 from Innmind/fix-transitions
Browse files Browse the repository at this point in the history
Fix Psalm losing types of contraints when composing them
  • Loading branch information
Baptouuuu authored Nov 11, 2024
2 parents 129ddec + 9fcb55c commit 900f290
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Fixed

- Psalm losing the types of contraints when composing them via `and`, `or`, `map` and `asPredicate`

## 1.6.0 - 2024-11-11

### Added
Expand Down
24 changes: 24 additions & 0 deletions src/AndConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,45 @@ public static function of(Constraint $a, Constraint $b): self
return new self($a, $b);
}

/**
* @template T
*
* @param Constraint<C, T> $constraint
*
* @return self<A, C, T>
*/
public function and(Constraint $constraint): self
{
return new self($this, $constraint);
}

/**
* @template T
*
* @param Constraint<A, T> $constraint
*
* @return Constraint<A, C|T>
*/
public function or(Constraint $constraint): Constraint
{
return OrConstraint::of($this, $constraint);
}

/**
* @template T
*
* @param pure-callable(C): T $map
*
* @return Constraint<A, T>
*/
public function map(callable $map): Constraint
{
return Map::of($this, $map);
}

/**
* @return PredicateInterface<C>
*/
public function asPredicate(): PredicateInterface
{
return Predicate::of($this);
Expand Down
24 changes: 24 additions & 0 deletions src/AssociativeArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,45 @@ public static function of(Constraint $key, Constraint $value): self
return new self($key, $value);
}

/**
* @template T
*
* @param Constraint<Map<K, V>, T> $constraint
*
* @return Constraint<mixed, T>
*/
public function and(Constraint $constraint): Constraint
{
return AndConstraint::of($this, $constraint);
}

/**
* @template T
*
* @param Constraint<mixed, T> $constraint
*
* @return Constraint<mixed, Map<K, V>|T>
*/
public function or(Constraint $constraint): Constraint
{
return OrConstraint::of($this, $constraint);
}

/**
* @template T
*
* @param pure-callable(Map<K, V>): T $map
*
* @return Constraint<mixed, T>
*/
public function map(callable $map): Constraint
{
return namespace\Map::of($this, $map);
}

/**
* @return Predicate<Map<K, V>>
*/
public function asPredicate(): Predicate
{
return namespace\Predicate::of($this);
Expand Down
24 changes: 24 additions & 0 deletions src/Each.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,45 @@ public static function of(Constraint $constraint): self
return new self($constraint);
}

/**
* @template V
*
* @param Constraint<list<T>, V> $constraint
*
* @return Constraint<list, V>
*/
public function and(Constraint $constraint): Constraint
{
return AndConstraint::of($this, $constraint);
}

/**
* @template V
*
* @param Constraint<list, V> $constraint
*
* @return Constraint<list, list<T>|V>
*/
public function or(Constraint $constraint): Constraint
{
return OrConstraint::of($this, $constraint);
}

/**
* @template V
*
* @param pure-callable(list<T>): V $map
*
* @return Constraint<list, V>
*/
public function map(callable $map): Constraint
{
return Map::of($this, $map);
}

/**
* @return PredicateInterface<list<T>>
*/
public function asPredicate(): PredicateInterface
{
return Predicate::of($this);
Expand Down
24 changes: 24 additions & 0 deletions src/Has.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,45 @@ public function withFailure(callable $message): self
return new self($this->key, $message);
}

/**
* @template T
*
* @param Constraint<mixed, T> $constraint
*
* @return Constraint<array, T>
*/
public function and(Constraint $constraint): Constraint
{
return AndConstraint::of($this, $constraint);
}

/**
* @template T
*
* @param Constraint<array, T> $constraint
*
* @return Constraint<array, mixed|T>
*/
public function or(Constraint $constraint): Constraint
{
return OrConstraint::of($this, $constraint);
}

/**
* @template T
*
* @param pure-callable(mixed): T $map
*
* @return Constraint<array, T>
*/
public function map(callable $map): Constraint
{
return Map::of($this, $map);
}

/**
* @return PredicateInterface<mixed>
*/
public function asPredicate(): PredicateInterface
{
return Predicate::of($this);
Expand Down
24 changes: 24 additions & 0 deletions src/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,45 @@ public static function of(string $class): self
return new self(Predicate\Instance::of($class), $class);
}

/**
* @template V
*
* @param Constraint<T, V> $constraint
*
* @return Constraint<mixed, V>
*/
public function and(Constraint $constraint): Constraint
{
return AndConstraint::of($this, $constraint);
}

/**
* @template V
*
* @param Constraint<mixed, V> $constraint
*
* @return Constraint<mixed, T|V>
*/
public function or(Constraint $constraint): Constraint
{
return OrConstraint::of($this, $constraint);
}

/**
* @template V
*
* @param pure-callable(T): V $map
*
* @return Constraint<mixed, V>
*/
public function map(callable $map): Constraint
{
return Map::of($this, $map);
}

/**
* @return Predicate<T>
*/
public function asPredicate(): Predicate
{
return $this->assert;
Expand Down
24 changes: 24 additions & 0 deletions src/Is.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,45 @@ public function withFailure(string $message): self
return new self($this->assert, $this->type, $message);
}

/**
* @template V
*
* @param Constraint<U, V> $constraint
*
* @return Constraint<T, V>
*/
public function and(Constraint $constraint): Constraint
{
return AndConstraint::of($this, $constraint);
}

/**
* @template V
*
* @param Constraint<T, V> $constraint
*
* @return Constraint<T, U|V>
*/
public function or(Constraint $constraint): Constraint
{
return OrConstraint::of($this, $constraint);
}

/**
* @template V
*
* @param pure-callable(U): V $map
*
* @return Constraint<T, V>
*/
public function map(callable $map): Constraint
{
return Map::of($this, $map);
}

/**
* @return PredicateInterface<U>
*/
public function asPredicate(): PredicateInterface
{
return Predicate::of($this);
Expand Down
24 changes: 24 additions & 0 deletions src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,45 @@ public static function of(Constraint $constraint, callable $map): self
return new self($constraint, $map);
}

/**
* @template V
*
* @param Constraint<T, V> $constraint
*
* @return Constraint<I, V>
*/
public function and(Constraint $constraint): Constraint
{
return AndConstraint::of($this, $constraint);
}

/**
* @template V
*
* @param Constraint<I, V> $constraint
*
* @return Constraint<I, T|V>
*/
public function or(Constraint $constraint): Constraint
{
return OrConstraint::of($this, $constraint);
}

/**
* @template V
*
* @param pure-callable(T): V $map
*
* @return self<I, T, V>
*/
public function map(callable $map): self
{
return new self($this, $map);
}

/**
* @return PredicateInterface<T>
*/
public function asPredicate(): PredicateInterface
{
return Predicate::of($this);
Expand Down
24 changes: 24 additions & 0 deletions src/Of.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,45 @@ public static function callable(callable $assert): self
return new self($assert);
}

/**
* @template T
*
* @param Constraint<B, T> $constraint
*
* @return Constraint<A, T>
*/
public function and(Constraint $constraint): Constraint
{
return AndConstraint::of($this, $constraint);
}

/**
* @template T
*
* @param Constraint<A, T> $constraint
*
* @return Constraint<A, B|T>
*/
public function or(Constraint $constraint): Constraint
{
return OrConstraint::of($this, $constraint);
}

/**
* @template T
*
* @param pure-callable(B): T $map
*
* @return Constraint<A, T>
*/
public function map(callable $map): Constraint
{
return Map::of($this, $map);
}

/**
* @return PredicateInterface<B>
*/
public function asPredicate(): PredicateInterface
{
return Predicate::of($this);
Expand Down
Loading

0 comments on commit 900f290

Please sign in to comment.