Certainly! The error message:
Call to an undefined method Pest\Expectation<mixed>::toBeValidUuid().
means that PHPStan doesn’t know about your custom expectation method (toBeValidUuid) on Pest’s Expectation class, even though it works at runtime.
This is a common situation when extending Pest with custom expectations. To fix it, you need to add a stub file or proper docblocks or configure PHPStan to recognize the custom method.
Solution
1. Create a PHPStan Stub File
Create a stub file (e.g., pest-extensions.stub) anywhere in your project (commonly in a /stubs directory).
<?php
namespace Pest;
class Expectation
{
/**
* @phpstan-assert self $this
*/
public function toBeValidUuid(): self {}
}
2. Register Stub File in phpstan.neon
In your phpstan.neon (or phpstan.neon.dist) config file, add the stub file path:
includes:
- ./vendor/phpstan/phpstan/conf/bleedingEdge.neon
parameters:
scanFiles:
- stubs/pest-extensions.stub
Make sure the path matches where you placed the stub file.
3. (Alternatively) Use Docblocks (less preferred for custom globals)
You can also document the method via docblocks if you are using your own custom class, but for Pest custom expectations, stub files offer a more robust solution.
4. Clear the Cache
After making these changes, it's a good idea to clear PHPStan’s cache:
php artisan clear-compiled
# or
rm -rf ./storage/framework/cache/data
# and
vendor/bin/phpstan clear-result-cache
Summary
Why does it work locally, but not on CI?
- Locally, you might have IDE helpers or cached analysis that mask the problem, but GitHub Actions runs with a fresh environment, making issues like missing stubs apparent.
Use the stub file approach for custom Pest expectations so PHPStan can recognize them! If you add more custom expectations, be sure to update your stub as needed.
If you need further help, let me know!