Skip to content

add toHaveMethodWithReturnType arch expectation#1740

Open
damiogunsiji wants to merge 1 commit into
pestphp:4.xfrom
damiogunsiji:4.x
Open

add toHaveMethodWithReturnType arch expectation#1740
damiogunsiji wants to merge 1 commit into
pestphp:4.xfrom
damiogunsiji:4.x

Conversation

@damiogunsiji

@damiogunsiji damiogunsiji commented Jun 26, 2026

Copy link
Copy Markdown

Adds toHaveMethodWithReturnType to check a method's return type.

// exact match - string must match the return type exactly
arch('services return correct types')
    ->expect('App\Services\PaymentService')
    ->toHaveMethodWithReturnType('process', 'array');

// subset match - checks all given types are present in the union
arch('repositories can return null')
    ->expect('App\Repositories\UserRepository')
    ->toHaveMethodWithReturnType('find', ['User', 'null']);  // matches ?User or User|null

// negated - passes when not all of the given types are present
arch('repositories not returning strings')
    ->expect('App\Repositories\UserRepository')
    ->not->toHaveMethodWithReturnType('find', ['string']);

// show() returns ?array (array|null)
arch('show does not return array, string and null')
    ->expect('App\Models\User')
    ->not->toHaveMethodWithReturnType('show', ['array', 'string', 'null']); // passes (string missing)

arch('show does not return array and null')
    ->expect('App\Models\User')
    ->not->toHaveMethodWithReturnType('show', ['array', 'null']); // fails

arch('show does not return array')
    ->expect('App\Models\User')
    ->not->toHaveMethodWithReturnType('show', ['array']); // fails

When $returnType is a string it matches exactly (e.g. 'array' only matches array). When an array it checks the method's return type contains all the given types (e.g. ['User', 'null'] matches ?User or User|null). PHP's ?Type shorthand is normalized so ?array is treated as ['array', 'null']. The not modifier inverts the assertion — it passes only when at least one of the given types is missing from the method's return type.

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.

1 participant