Skip to content

Conversation

@t1sh0o
Copy link
Contributor

@t1sh0o t1sh0o commented Aug 13, 2025

Description

This PR fixes the issue described in #42 regarding PHPStan v1 errors with nullable callable parameters.

Problem

The callable parameters in Result methods were incorrectly marked as nullable (T=) when they should be required (T). This was causing PHPStan v1 to report errors about parameter mismatches.

Solution

Removed the = from all callable parameter type hints in the Result abstract class. Since these callables are only invoked when there's an actual value present (Ok for map/andThen, Err for mapErr/orElse), the parameters should not be nullable.

Changes

  • Updated all callable parameter type hints from callable(T=) to callable(T)
  • Updated all callable parameter type hints from callable(E=) to callable(E)

Testing

  • ✅ All unit tests pass
  • ✅ Static analysis with Psalm passes
  • ✅ Code style checks pass

Fixes #42

The callable parameters in Result methods were incorrectly marked as nullable (T=) when they should be required (T). This was causing PHPStan v1 to report errors about parameter mismatches.

Since these callables are only invoked when there's an actual value present (Ok for map/andThen, Err for mapErr/orElse), the parameters should not be nullable.

Fixes prewk#42
@t1sh0o t1sh0o marked this pull request as draft August 13, 2025 10:52
@t1sh0o t1sh0o force-pushed the fix-nullable-callable-parameters branch from ff4f03b to 94b6b6c Compare August 13, 2025 12:00
@t1sh0o t1sh0o marked this pull request as ready for review August 13, 2025 12:00
@tminich
Copy link

tminich commented Aug 18, 2025

Looks good, I would suggest though to make some of the methods more flexible:

        /**
         * Calls op if the result is Ok, otherwise returns the Err value of self.
         *
         * @template U
         * @template F
         *
         * @param callable(T):Result<U,F> $op
         * @return Result<U,E|F>
         */
        public function andThen(callable $op): self;
        /**
         * Calls op if the result is Err, otherwise returns the Ok value of self.
         *
         * @template U
         * @template F
         *
         * @param callable(E):Result<U,F> $op
         * @return Result<T|U,F>
         */
        public function orElse(callable $op): self;

        /**
         * Unwraps a result, yielding the content of an Ok. Else, it returns optb.
         *
         * @template U
         *
         * @param U $optb
         * @return T|U
         */
        public function unwrapOr($optb): mixed;

        /**
         * Unwraps a result, yielding the content of an Ok. If the value is an Err then it calls op with its value.
         *
         * @template U
         *
         * @param callable(E):U $op
         * @return T|U
         */
        public function unwrapOrElse(callable $op): mixed;

Updated Result class method signatures as suggested by @tminich:
- andThen(): Added template F, changed callable to Result<U,F> and return to Result<U,E|F>
- orElse(): Added template U, changed callable to Result<U,F> and return to Result<T|U,F>
- unwrapOr(): Added template U, changed return type to T|U
- unwrapOrElse(): Added template U, changed return type to T|U

These changes provide more flexible and accurate type annotations.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@t1sh0o
Copy link
Contributor Author

t1sh0o commented Aug 19, 2025

@tminich done

Updated actions/cache from v4.0.2 to v4 to resolve CI failures.
The specific version was causing automatic failures due to deprecation.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@prewk
Copy link
Owner

prewk commented Aug 19, 2025

Wow, you fixed the CI ✨

@prewk prewk merged commit aac58a5 into prewk:master Aug 19, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Why callable parameters are nullable

3 participants