Skip to content

Commit

Permalink
chore: update codebase to PHP 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol authored and marcosh committed Mar 21, 2023
1 parent d49cab6 commit 5b3e5ff
Show file tree
Hide file tree
Showing 83 changed files with 254 additions and 364 deletions.
4 changes: 2 additions & 2 deletions docs/typeclasses/Applicative.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface Applicative extends Apply
* @param A $a
* @return F<A>
*/
public function pure($a);
public function pure(mixed $a);
}
```

Expand All @@ -34,7 +34,7 @@ The `Applicative` typeclass needs to satisfy four laws in addition to the `Apply
### Identity

```php
$applicative->apply($applicative->pure(fn($x) => $x), $y) == $y
$applicative->apply($applicative->pure(fn(mixed $x): mixed => $x), $y) == $y
```

### Composition
Expand Down
4 changes: 2 additions & 2 deletions docs/typeclasses/Bifunctor.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ variables
### Identity

```php
$bifunctor->biMap(fn($x) => $x, fn($x) => $x, $a) == $a
$bifunctor->biMap(fn(mixed $x): mixed => $x, fn(mixed $x): mixed => $x, $a) == $a
```

### Composition

```php
$bifunctor->biMap(fn($x) => $f($g($x)), fn($x) => $h($k($x)), $a) == $bifunctor->biMap(fn($x) => $f($x), fn($x) => $h($x), $bifunctor->biMap(fn($x) => $g($x), fn($x) => $k($x)), $a)
$bifunctor->biMap(fn(mixed $x): mixed => $f($g($x)), fn(mixed $x): mixed => $h($k($x)), $a) == $bifunctor->biMap(fn(mixed $x): mixed => $f($x), fn(mixed $x): mixed => $h($x), $bifunctor->biMap(fn(mixed $x): mixed => $g($x), fn(mixed $x): mixed => $k($x)), $a)
```

## Implemented instances
Expand Down
4 changes: 2 additions & 2 deletions docs/typeclasses/Functor.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ A `Functor` instance needs to abide by two laws:
The identity function should be mapped to the identity function

```php
$functor->map(fn($x) => $x, $y) == $y
$functor->map(fn(mixed $x): mixed => $x, $y) == $y
```

### Composition

Mapping the composition of two functions or composing the mapping of those functions should be the same thing

```php
$functor->map(fn($x) => $f($g($x)), $y) == $functor->map(fn($x) => $f($x), $functor->map(fn($x) => $g($x), $y))
$functor->map(fn(mixed $x): mixed => $f($g($x)), $y) == $functor->map(fn(mixed $x): mixed => $f($x), $functor->map(fn(mixed $x): mixed => $g($x), $y))
```

## Implemented instances
Expand Down
4 changes: 2 additions & 2 deletions docs/typeclasses/Monad.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ $monad->bind($monad->pure($a), $f) == $f($a)
### Right identity

```php
$monad->bind($a, fn($x) => $monad->pure($x)) == $a
$monad->bind($a, fn(mixed $x): mixed => $monad->pure($x)) == $a
```

### Associativity

```php
$monad->bind($a, fn($x) => $monad->bind($f($x), $g)) == $monad->bind($monad->bind($a, $g), $g)
$monad->bind($a, fn(mixed $x): mixed => $monad->bind($f($x), $g)) == $monad->bind($monad->bind($a, $g), $g)
```

## Implemented instances
Expand Down
4 changes: 2 additions & 2 deletions docs/typeclasses/Profunctor.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ contravariant type variable.
### Identity

```php
$profunctor->diMap(fn($x) => $x, fn($x) => $x, $a) == $a
$profunctor->diMap(fn(mixed $x): mixed => $x, fn(mixed $x): mixed => $x, $a) == $a
```

### Composition

```php
$profunctor->diMap(fn($x) => $f($g($x)), fn($x) => $h($k($x)), $a) == $profunctor->diMap(fn($x) => $g($x), fn($x) => $h($x), $profunctor->diMap(fn($x) => $f($x), fn($x) => $k($x)), $a)
$profunctor->diMap(fn(mixed $x): mixed => $f($g($x)), fn(mixed $x): mixed => $h($k($x)), $a) == $profunctor->diMap(fn(mixed $x): mixed => $g($x), fn(mixed $x): mixed => $h($x), $profunctor->diMap(fn(mixed $x): mixed => $f($x), fn(mixed $x): mixed => $k($x)), $a)
```

2 changes: 1 addition & 1 deletion docs/typeclasses/Traversable.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ $t($traversable->traverse($applicative, $f, $x)) == $traversable->traverse($appl
### Identity

```php
$traversable->traverse(new IdentityApplicative(), fn($x) => new Identity($x), $y) == new Identity($y)
$traversable->traverse(new IdentityApplicative(), fn(mixed $x): mixed => new Identity($x), $y) == new Identity($y)
```

### Composition
Expand Down
57 changes: 24 additions & 33 deletions src/Either.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Marcosh\LamPHPda\Typeclass\Functor;
use Marcosh\LamPHPda\Typeclass\Monad;
use Marcosh\LamPHPda\Typeclass\Traversable;
use Throwable;

/**
* @see https://github.com/marcosh/lamphpda/tree/master/docs/data-structures/Either.md
Expand All @@ -41,25 +40,17 @@
*/
final class Either implements DefaultMonad, DefaultTraversable, HK2Covariant
{
private bool $isRight;

/** @var null|A */
private $leftValue;

/** @var null|B */
private $rightValue;

/**
* @param null|A $leftValue
* @param null|B $rightValue
*
* @psalm-mutation-free
*/
private function __construct(bool $isRight, $leftValue = null, $rightValue = null)
{
$this->isRight = $isRight;
$this->leftValue = $leftValue;
$this->rightValue = $rightValue;
private function __construct(
private readonly bool $isRight,
private mixed $leftValue = null,
private mixed $rightValue = null
) {
}

/**
Expand All @@ -70,7 +61,7 @@ private function __construct(bool $isRight, $leftValue = null, $rightValue = nul
*
* @psalm-pure
*/
public static function left($value): self
public static function left(mixed $value): self
{
return new self(false, $value);
}
Expand All @@ -83,7 +74,7 @@ public static function left($value): self
*
* @psalm-pure
*/
public static function right($value): self
public static function right(mixed $value): self
{
return new self(true, null, $value);
}
Expand Down Expand Up @@ -147,62 +138,62 @@ public function eval(
* @param A $a
* @return A
*/
public function fromLeft($a)
public function fromLeft(mixed $a): mixed
{
return $this->eval(
/**
* @param A $aa
* @return A
*/
static fn ($aa) => $aa,
static fn (mixed $aa): mixed => $aa,
/**
* @param B $_
* @return A
*/
static fn ($_) => $a
static fn (mixed $_): mixed => $a
);
}

/**
* @param B $b
* @return B
*/
public function fromRight($b)
public function fromRight(mixed $b): mixed
{
return $this->eval(
/**
* @param A $_
* @return B
*/
static fn ($_) => $b,
static fn (mixed $_): mixed => $b,
/**
* @param B $bb
* @return B
*/
static fn ($bb) => $bb
static fn (mixed $bb): mixed => $bb
);
}

/**
* @throws Throwable
* @return B
* @throws \Throwable
*/
public function fromRightThrow(Throwable $e)
public function fromRightThrow(\Throwable $e): mixed
{
return $this->eval(
/**
* @param A $_
* @throws Throwable
* @return B
* @throws \Throwable
*/
static function ($_) use ($e) {
static function (mixed $_) use ($e): never {
throw $e;
},
/**
* @param B $b
* @return B
*/
static fn ($b) => $b
static fn (mixed $b): mixed => $b
);
}

Expand All @@ -213,7 +204,7 @@ public function toMaybe(): Maybe
{
return $this->eval(
static fn (): Maybe => Maybe::nothing(),
static fn ($b): Maybe => Maybe::just($b)
static fn (mixed $b): Maybe => Maybe::just($b)
);
}

Expand Down Expand Up @@ -289,7 +280,7 @@ public function mapLeft(callable $f): self
* @param B $b
* @return B
*/
static fn ($b) => $b
static fn (mixed $b): mixed => $b
);
}

Expand Down Expand Up @@ -330,7 +321,7 @@ public function apply(HK1 $f): self
*
* @psalm-pure
*/
public static function ipure(Applicative $applicative, $a): self
public static function ipure(Applicative $applicative, mixed $a): self
{
return self::fromBrand($applicative->pure($a));
}
Expand All @@ -345,7 +336,7 @@ public static function ipure(Applicative $applicative, $a): self
*
* @psalm-suppress LessSpecificImplementedReturnType
*/
public static function pure($a): self
public static function pure(mixed $a): self
{
return self::ipure(new EitherApplicative(), $a);
}
Expand Down Expand Up @@ -387,7 +378,7 @@ public function bind(callable $f): self
*
* @psalm-mutation-free
*/
public function ifoldr(Foldable $foldable, callable $f, $b)
public function ifoldr(Foldable $foldable, callable $f, mixed $b): mixed
{
/** @psalm-suppress ArgumentTypeCoercion */
return $foldable->foldr($f, $b, $this);
Expand All @@ -401,7 +392,7 @@ public function ifoldr(Foldable $foldable, callable $f, $b)
*
* @psalm-mutation-free
*/
public function foldr(callable $f, $b)
public function foldr(callable $f, mixed $b): mixed
{
return $this->ifoldr(new EitherFoldable(), $f, $b);
}
Expand Down
6 changes: 3 additions & 3 deletions src/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function fromBrand(HK1 $hk): self
/**
* @return A
*/
public function eval()
public function eval(): mixed
{
/** @psalm-suppress ImpureFunctionCall */
return ($this->action)();
Expand Down Expand Up @@ -122,7 +122,7 @@ public function apply(HK1 $f): self
*
* @psalm-pure
*/
public static function ipure(Applicative $applicative, $a): self
public static function ipure(Applicative $applicative, mixed $a): self
{
return self::fromBrand($applicative->pure($a));
}
Expand All @@ -134,7 +134,7 @@ public static function ipure(Applicative $applicative, $a): self
*
* @psalm-pure
*/
public static function pure($a): self
public static function pure(mixed $a): self
{
return self::ipure(new IOApplicative(), $a);
}
Expand Down
20 changes: 7 additions & 13 deletions src/Identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,11 @@
*/
final class Identity implements DefaultMonad, DefaultTraversable
{
/**
* @var A
*/
private $value;

/**
* @param A $value
*/
private function __construct($value)
private function __construct(private readonly mixed $value)
{
$this->value = $value;
}

/**
Expand All @@ -54,7 +48,7 @@ private function __construct($value)
*
* @psalm-pure
*/
public static function wrap($value): self
public static function wrap(mixed $value): self
{
return new self($value);
}
Expand All @@ -75,7 +69,7 @@ public static function fromBrand(HK1 $b): self
/**
* @return A
*/
public function unwrap()
public function unwrap(): mixed
{
return $this->value;
}
Expand Down Expand Up @@ -138,7 +132,7 @@ public function apply(HK1 $f): self
*
* @psalm-pure
*/
public static function ipure(Applicative $applicative, $a): self
public static function ipure(Applicative $applicative, mixed $a): self
{
return self::fromBrand($applicative->pure($a));
}
Expand All @@ -152,7 +146,7 @@ public static function ipure(Applicative $applicative, $a): self
*
* @psalm-suppress LessSpecificImplementedReturnType
*/
public static function pure($a): self
public static function pure(mixed $a): self
{
return self::ipure(new IdentityApplicative(), $a);
}
Expand Down Expand Up @@ -191,7 +185,7 @@ public function bind(callable $f): self
*
* @psalm-suppress ArgumentTypeCoercion
*/
public function ifoldr(Foldable $foldable, callable $f, $b)
public function ifoldr(Foldable $foldable, callable $f, mixed $b): mixed
{
return $foldable->foldr($f, $b, $this);
}
Expand All @@ -202,7 +196,7 @@ public function ifoldr(Foldable $foldable, callable $f, $b)
* @param B $b
* @return B
*/
public function foldr(callable $f, $b)
public function foldr(callable $f, mixed $b): mixed
{
return $this->ifoldr(new IdentityFoldable(), $f, $b);
}
Expand Down
Loading

0 comments on commit 5b3e5ff

Please sign in to comment.