Skip to content

Commit

Permalink
fix psalm errors (#11)
Browse files Browse the repository at this point in the history
* fix phpdoc type of $pass

You have to specify the type of the value

* Specify psalm type of Option::fromKey()'s $key argument

* none: return new instances instead of returning $this

phpdoc say it should return a Option<U>, but $this is of type Option<T>, so psalm detect this as a type mismatch
  • Loading branch information
Lctrs committed Mar 24, 2021
1 parent e060cfe commit fa938d7
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 21 deletions.
15 changes: 0 additions & 15 deletions psalm-baseline.xml
@@ -1,21 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.6.4@97fe86c4e158b5a57c5150aa5055c38b5a809aab">
<file src="src/Option.php">
<MixedArgument occurrences="1">
<code>$key</code>
</MixedArgument>
</file>
<file src="src/Option/None.php">
<InvalidReturnStatement occurrences="3">
<code>$this</code>
<code>$this</code>
<code>$this</code>
</InvalidReturnStatement>
<InvalidReturnType occurrences="3">
<code>Option&lt;U&gt;</code>
<code>Option&lt;U&gt;</code>
<code>Option&lt;U&gt;</code>
</InvalidReturnType>
<MixedArgument occurrences="2">
<code>$this-&gt;pass</code>
<code>$this-&gt;pass</code>
Expand Down
2 changes: 1 addition & 1 deletion spec/Option/NoneSpec.php
Expand Up @@ -51,7 +51,7 @@ function it_unwrapOrElses()

function it_doesnt_map()
{
$this->map(function() {})->shouldBe($this);
$this->map(function() {})->shouldHaveType(None::class);
}

function it_mapOrs()
Expand Down
1 change: 1 addition & 0 deletions src/Option.php
Expand Up @@ -223,6 +223,7 @@ public static function fromNullable($thing): Option
*
* @param array $coll C
* @param mixed $key
* @psalm-param array-key $key
* @return Option Option<V>
*/
public static function fromKey(array $coll, $key): Option
Expand Down
9 changes: 5 additions & 4 deletions src/Option/None.php
Expand Up @@ -28,13 +28,14 @@ class None extends Option
{
/**
* @var array
* @psalm-var list<mixed>
*/
private $pass;

/**
* None constructor
*
* @param array ...$pass
* @param mixed ...$pass
*/
public function __construct(...$pass) {
$this->pass = $pass;
Expand Down Expand Up @@ -123,7 +124,7 @@ public function unwrapOrElse(Closure $op)
*/
public function map(Closure $mapper): Option
{
return $this;
return new self(...$this->pass);
}

/**
Expand Down Expand Up @@ -184,7 +185,7 @@ public function iter(): array
*/
public function and(Option $optb): Option
{
return $this;
return new self(...$this->pass);
}

/**
Expand All @@ -200,7 +201,7 @@ public function and(Option $optb): Option
*/
public function andThen(Closure $op): Option
{
return $this;
return new self(...$this->pass);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Option/Some.php
Expand Up @@ -34,6 +34,7 @@ class Some extends Option

/**
* @var array
* @psalm-var list<mixed>
*/
private $pass;

Expand All @@ -42,7 +43,7 @@ class Some extends Option
*
* @param mixed $value
* @psalm-param T $value
* @param array ...$pass
* @param mixed ...$pass
*/
public function __construct($value, ...$pass)
{
Expand Down

0 comments on commit fa938d7

Please sign in to comment.