Skip to content

Commit

Permalink
add static analyzer and ci
Browse files Browse the repository at this point in the history
  • Loading branch information
yceruto committed Apr 18, 2024
1 parent 3f08aa8 commit ab69903
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,22 @@ jobs:

- name: Run tests
run: vendor/bin/phpunit tests

psalm:
name: Static Analyzer
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}

- name: Download dependencies
run: composer install --classmap-authoritative

- name: Phpstan
run: vendor/bin/phpstan analyze src --level=max
32 changes: 23 additions & 9 deletions src/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Type;

use function Type\Option\some;

/**
* The Option type represents an optional value: every Option
* is either Some and contains a value, or None, and does not.
Expand Down Expand Up @@ -176,15 +178,19 @@ public function unwrapOrElse(\Closure $fn): mixed
*
* @param \Closure(T): U $fn
*
* @return self<U>
* @return self<null>|self<U>
*/
public function map(\Closure $fn): self
{
if ($this->isNone()) {
return self::None();
}

return self::Some($fn($this->value));
if (null === $value = $fn($this->value)) {
return self::None();
}

return some($value);
}

/**
Expand Down Expand Up @@ -320,7 +326,7 @@ public function orElse(\Closure $fn): self
*
* @param self<T> $option The option to compare with
*
* @return self<T>
* @return self<T>|self<null>
*/
public function xor(self $option): self
{
Expand All @@ -347,7 +353,7 @@ public function xor(self $option): self
*
* @param self<T> $option The option to compare with
*
* @return self<T>
* @return self<T>|self<null>
*/
public function and(self $option): self
{
Expand Down Expand Up @@ -375,7 +381,7 @@ public function and(self $option): self
*
* @param \Closure(T): self<U> $fn
*
* @return self<U>
* @return self<null>|self<U>
*/
public function andThen(\Closure $fn): self
{
Expand All @@ -400,11 +406,12 @@ public function andThen(\Closure $fn): self
* assert([] === iterator_to_array($x->iterate()), 'Expected to be [].');
* ```
*
* @return \ArrayIterator<T>
* @return \ArrayIterator<int|string, T>
*/
public function iterate(): \ArrayIterator
{
if ($this->isSome()) {
/** @var \ArrayIterator<int|string, T> */
return new \ArrayIterator((array) $this->value);
}

Expand All @@ -429,7 +436,7 @@ public function iterate(): \ArrayIterator
*
* @param \Closure(T): bool $predicate A closure that returns a boolean
*
* @return self<T>
* @return self<T>|self<null>
*/
public function filter(\Closure $predicate): Option
{
Expand Down Expand Up @@ -485,15 +492,19 @@ public function equals(self $option): bool
* assert(Option::Some(2) == $x->flatten()->flatten(), 'Expected to be Option<T>.');
* ```
*
* @return self<T>
* @return self<null>|self<T>
*/
public function flatten(): self
{
if ($this->isNone()) {
return self::None();
}

return $this->value;
if ($this->value instanceof self) {
return $this->value;
}

throw new \LogicException('Cannot flatten a non-Option value.');
}

/**
Expand All @@ -506,6 +517,9 @@ public function clone(): self
return new self($this->value);
}

/**
* @param T $value
*/
private function __construct(
private mixed $value,
) {
Expand Down
1 change: 1 addition & 0 deletions src/Option/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
function some(mixed $value): Option
{
/** @var Option<T> */
return Option::Some($value);
}
}
Expand Down

0 comments on commit ab69903

Please sign in to comment.