Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Throwable;

/**
* @template T of mixed type of non-null value

Check notice on line 11 in src/Optional.php

View workflow job for this annotation

GitHub Actions / run

* [Disallow mixed type hint] Usage of "mixed" type hint is disallowed.
*
* @implements JavaSe8\Optional<T>
*/
Expand All @@ -22,7 +22,7 @@
final protected function __construct(
protected readonly mixed $value,
) {
if ($this->value !== null && !@static::isSupported($this->value)) {

Check notice on line 25 in src/Optional.php

View workflow job for this annotation

GitHub Actions / run

* [No silenced errors] Silencing errors is discouraged; found: @static::isSupported... * [Space after not] Expected 1 space(s) after NOT operator; 0 found
throw new InvalidArgumentException('Value is not supported.');
}
}
Expand Down Expand Up @@ -58,12 +58,12 @@
if (static::class === Optional::class) {
if ($value !== null) {
try {
/** @var static */

Check notice on line 61 in src/Optional.php

View workflow job for this annotation

GitHub Actions / run

* [Inline doc comment declaration] Invalid inline documentation comment format "@var static", expected "@var type $variableName Optional description".
return TypedOptional::of($value, Optional::class);
} catch (Exception\CouldNotFindTypedOptionalForValue) {

Check notice on line 63 in src/Optional.php

View workflow job for this annotation

GitHub Actions / run

* [Empty statement] Empty CATCH statement detected
}
}
/** @var static */

Check notice on line 66 in src/Optional.php

View workflow job for this annotation

GitHub Actions / run

* [Inline doc comment declaration] Invalid inline documentation comment format "@var static", expected "@var type $variableName Optional description".
return new class ($value) extends Optional {
protected static function isInstanceOfStatic(object $obj): bool
{
Expand Down Expand Up @@ -115,7 +115,7 @@
$obj->ifPresent(function (mixed $objValue) use (&$equals, $strict): void {
$equals = match (!is_object($this->value) || $strict) {
true => $this->value === $objValue,
false => $this->value == $objValue,

Check notice on line 118 in src/Optional.php

View workflow job for this annotation

GitHub Actions / run

* [Disallow equal operators] Operator == is disallowed, use === instead.
};
});
return $equals ?? $this->value === null;
Expand Down Expand Up @@ -148,13 +148,13 @@
public function flatMap(callable $mapper): self
{
if ($this->value === null) {
/** @var self<U> */

Check notice on line 151 in src/Optional.php

View workflow job for this annotation

GitHub Actions / run

* [Inline doc comment declaration] Invalid inline documentation comment format "@var self<U>", expected "@var type $variableName Optional description".
return Optional::empty();
}

/** @var mixed $mapped */
$mapped = $mapper($this->value);
/** @var self<U> */

Check notice on line 157 in src/Optional.php

View workflow job for this annotation

GitHub Actions / run

* [Inline doc comment declaration] Invalid inline documentation comment format "@var self<U>", expected "@var type $variableName Optional description".
return match (true) {
$mapped instanceof self => $mapped,
$mapped instanceof JavaSe8\Optional => $mapped->isPresent() ? Optional::of($mapped->get()) : Optional::empty(),
Expand Down Expand Up @@ -213,7 +213,9 @@
return $this->value;
}
$other = $otherSupplier();
return static::isSupported($other) ? $other : throw new InvalidArgumentException('Other supplier must return supported other.');
return $other === null || static::isSupported($other)
? $other
: throw new InvalidArgumentException('Other supplier must return supported other.');
}

/**
Expand All @@ -237,7 +239,7 @@
}

if (is_string($exceptionSupplier)) {
/** @var class-string<E> $exceptionSupplier */

Check notice on line 242 in src/Optional.php

View workflow job for this annotation

GitHub Actions / run

* [Inline doc comment declaration] Missing variable $exceptionSupplier before or after the documentation comment.
if (!class_exists($exceptionSupplier, autoload: true)) {
throw new InvalidArgumentException('Exception supplier must be existing class name.');
}
Expand All @@ -260,7 +262,7 @@
/**
* @deprecated use {@see self::orElse()}
*
* @todo BC remove it

Check notice on line 265 in src/Optional.php

View workflow job for this annotation

GitHub Actions / run

* [Todo] Comment refers to a TODO task
*
* Inverse of {@see self::ofNullable()}
*
Expand Down
24 changes: 14 additions & 10 deletions tests/OptionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,31 +308,35 @@ public static function dataMethodMapWorks(): array
}

#[DataProvider('dataMethodOrElseWorks')]
public function testMethodOrElseWorks(Optional $optional, string $expectedValue): void
public function testMethodOrElseWorks(Optional $optional, string|null $orValue, string|null $expectedValue): void
{
self::assertSame($expectedValue, $optional->orElse(self::OTHER));
self::assertSame($expectedValue, $optional->orElse($orValue));
}

public static function dataMethodOrElseWorks(): array
{
return self::makeDataSet([
[self::VALUE],
[self::OTHER],
]);
[self::OTHER, self::VALUE],
[self::OTHER, self::OTHER],
]) + [
'typed null' => [OptionalString::empty(), null, null],
];
}

#[DataProvider('dataMethodOrElseGetWorks')]
public function testMethodOrElseGetWorks(Optional $optional, string $expectedValue): void
public function testMethodOrElseGetWorks(Optional $optional, string|null $orValue, string|null $expectedValue): void
{
self::assertSame($expectedValue, $optional->orElseGet(static fn(): string => self::OTHER));
self::assertSame($expectedValue, $optional->orElseGet(static fn(): string|null => $orValue));
}

public static function dataMethodOrElseGetWorks(): array
{
return self::makeDataSet([
[self::VALUE],
[self::OTHER],
]);
[self::OTHER, self::VALUE],
[self::OTHER, self::OTHER],
]) + [
'typed null' => [OptionalString::empty(), null, null],
];
}

#[DataProvider('dataMethodOrElseThrowWorks')]
Expand Down