Skip to content

phpstan/phpstan-strict-rules 導入 #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2025
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
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
"wiz-develop/php-cs-fixer-config": "^8.3",
"phpstan/phpstan": "^1.12",
"phpunit/php-code-coverage": "^11.0",
"phpunit/phpunit": "^11.3"
"phpunit/phpunit": "^11.3",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan-strict-rules": "^1.6"
},
"config": {
"allow-plugins": {
"phpstan/extension-installer": true
}
}
}
}
103 changes: 100 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ parameters:
level: max
paths:
- src
- tests
typeAliases:
BasicTypes: 'int|string|bool|null|float|array|iterable|callable|resource|object'
2 changes: 1 addition & 1 deletion src/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function and(self $right): self;
* @param Closure(T): Option<U> $right
* @return Option<U>
*/
public function andThen(Closure $right): self;
public function andThen(Closure $right): self; /** @phpstan-ignore method.childParameterType */

/**
* @see https://doc.rust-lang.org/std/option/enum.Option.html#method.or
Expand Down
3 changes: 3 additions & 0 deletions src/Option/None.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public static function unit(mixed $value): self
/**
* @return $this
*/
/**
* @phpstan-ignore method.childParameterType
*/
#[Override]
public function andThen(Closure $right): self
{
Expand Down
3 changes: 3 additions & 0 deletions src/Option/Some.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public static function unit(mixed $value): self
* @param Closure(T): Option<U> $right
* @return Option<U>
*/
/**
* @phpstan-ignore method.childParameterType
*/
#[Override]
public function andThen(Closure $right): Option
{
Expand Down
2 changes: 1 addition & 1 deletion src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function and(self $right): self;
* @param Closure(T): Result<U, F> $right
* @return (F is BasicTypes ? Result<U, E|F> : Result<U, E>)
*/
public function andThen(Closure $right): self;
public function andThen(Closure $right): self; /** @phpstan-ignore method.childParameterType */

/**
* @see https://doc.rust-lang.org/std/result/enum.Result.html#method.or
Expand Down
2 changes: 1 addition & 1 deletion src/Result/Err.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function unit(mixed $value): self
}

/**
* @return $this
* @phpstan-ignore method.childParameterType, missingType.generics
*/
#[Override]
public function andThen(Closure $right): self
Expand Down
4 changes: 4 additions & 0 deletions src/Result/Ok.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ public static function unit(mixed $value): self
* @param Closure(T) :Result<U, F> $right
* @return Result<U, F>
*/
/**
* @phpstan-ignore method.childParameterType
*/
#[Override]
public function andThen(Closure $right): Result
{
// @phpstan-ignore return.type
return $right($this->value);
}

Expand Down
11 changes: 7 additions & 4 deletions src/Result/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,18 @@ function transpose(Result $result): Option
}

/**
* @return Result<bool, non-empty-list<mixed>>
* @template T
* @template E
* @param (Result<T, E>|Result) ...$results
* @return Result<bool, non-empty-list<E>>
*/
/** @phpstan-ignore-next-line */
function combine(Result ...$results): Result
function combine(Result ...$results): Result // @phpstan-ignore-line
{
$errs = array_filter($results, static fn (Result $result) => $result->isErr());
if (count($errs) > 0) {
// @phpstan-ignore return.type
return Result\err(array_values(array_map(static fn (Result $result) => $result->unwrapErr(), $errs)));
}

return Result\ok(true);
return Result\ok();
}
3 changes: 3 additions & 0 deletions tests/Provider/OptionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ trait OptionProvider
/**
* @return iterable<array{Option<mixed>, mixed, mixed}>
*/
/**
* @phpstan-ignore missingType.iterableValue
*/
public static function fromValueMatrix(): iterable
{
$o = (object)[];
Expand Down
5 changes: 2 additions & 3 deletions tests/Unit/MonadTest.php → tests/Unit/MonadTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* @template TMonad of Monad
*/
abstract class MonadTest extends TestCase
abstract class MonadTestAbstract extends TestCase
{
/**
* @return iterable<array{TMonad<string>}>
Expand Down Expand Up @@ -100,8 +100,7 @@ private function get_caller_type(array $backtrace): ?ReflectionNamedType
$ref_class = new ReflectionClass($backtrace['class']);
$ref_method = $ref_class->getMethod($backtrace['function']);
$ref_type = $ref_method->getReturnType();

} elseif ($function = ($backtrace['function'] ?? null)) {
} elseif ($function = ($backtrace['function'] ?? null)) {/** @phpstan-ignore elseif.condNotBoolean */
$ref_function = new ReflectionFunction($function);
$ref_type = $ref_function->getReturnType();
}
Expand Down
2 changes: 0 additions & 2 deletions tests/Unit/Option/InterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@ public function instanceOfOption(): void
public function instanceOfNone(): void
{
Assert::assertInstanceOf(Option\None::class, Option\none());
// @phpstan-ignore-next-line
Assert::assertNotInstanceOf(Option\None::class, Option\some(null));
}

#[Test]
#[TestDox('instanceOfSome test')]
public function instanceOfSome(): void
{
// @phpstan-ignore-next-line
Assert::assertNotInstanceOf(Option\Some::class, Option\none());
Assert::assertInstanceOf(Option\Some::class, Option\some(null));
}
Expand Down
7 changes: 4 additions & 3 deletions tests/Unit/Option/OrThrowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPUnit\Framework\Attributes\TestDox;
use RuntimeException;
use WizDevelop\PhpMonad\Option;
use WizDevelop\PhpMonad\Tests\Assert;
use WizDevelop\PhpMonad\Tests\TestCase;

#[TestDox('Option - OrThrow メソッドのテスト')]
Expand All @@ -27,9 +28,9 @@ public function someReturnsItself(): void

$result = $some->orThrow($exception);

$this->assertSame($some, $result);
$this->assertTrue($result->isSome());
$this->assertSame(42, $result->unwrap());
Assert::assertSame($some, $result);
Assert::assertTrue($result->isSome());
Assert::assertSame(42, $result->unwrap());
}

#[Test]
Expand Down
15 changes: 8 additions & 7 deletions tests/Unit/Option/TransposeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPUnit\Framework\Attributes\TestDox;
use WizDevelop\PhpMonad\Option;
use WizDevelop\PhpMonad\Result;
use WizDevelop\PhpMonad\Tests\Assert;
use WizDevelop\PhpMonad\Tests\TestCase;

#[TestDox('Option - transpose関数のテスト')]
Expand All @@ -24,9 +25,9 @@ public function transposeSomeOk(): void
/** @phpstan-ignore-next-line */
$result = Option\transpose($option);

$this->assertTrue($result->isOk());
$this->assertTrue($result->unwrap()->isSome());
$this->assertSame(42, $result->unwrap()->unwrap());
Assert::assertTrue($result->isOk());
Assert::assertTrue($result->unwrap()->isSome());
Assert::assertSame(42, $result->unwrap()->unwrap());
}

#[Test]
Expand All @@ -38,8 +39,8 @@ public function transposeSomeErr(): void
/** @phpstan-ignore-next-line */
$result = Option\transpose($option);

$this->assertTrue($result->isErr());
$this->assertSame('error', $result->unwrapErr());
Assert::assertTrue($result->isErr());
Assert::assertSame('error', $result->unwrapErr());
}

#[Test]
Expand All @@ -51,7 +52,7 @@ public function transposeNone(): void
/** @phpstan-ignore-next-line */
$result = Option\transpose($option);

$this->assertTrue($result->isOk());
$this->assertTrue($result->unwrap()->isNone());
Assert::assertTrue($result->isOk());
Assert::assertTrue($result->unwrap()->isNone());
}
}
3 changes: 2 additions & 1 deletion tests/Unit/Option/UnwrapOrThrowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPUnit\Framework\TestCase;
use RuntimeException;
use WizDevelop\PhpMonad\Option;
use WizDevelop\PhpMonad\Tests\Assert;

use function WizDevelop\PhpMonad\Option\none;
use function WizDevelop\PhpMonad\Option\some;
Expand All @@ -27,7 +28,7 @@ public function some値の場合は値を返す(): void
$option = some($value);
$exception = new Exception('This exception should not be thrown');

$this->assertSame($value, $option->unwrapOrThrow($exception));
Assert::assertSame($value, $option->unwrapOrThrow($exception));
}

#[Test]
Expand Down
Loading
Loading