PHP code example of imsuperlative / phpstan-pest

1. Go to this page and download the library: Download imsuperlative/phpstan-pest library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

imsuperlative / phpstan-pest example snippets


beforeEach(function () {
    $this->user = User::factory()->create();
});

it('has a name', function () {
    // PHPStan knows $this->user is User
    expect($this->user->name)->toBeString();
});

// tests/Pest.php
pest()->extends(HasGreeting::class)->in('Inherited');

// tests/Inherited/FooTest.php
it('can call trait methods', function () {
    expect($this->greet('world'))->toBe('Hello, world!');
});

it('proxies and accesses properties', function () {
    expect($user)
        ->name->toBe('Test');

    expect(collect([1, 2, 3]))->first()->toBe(1);
});

// Union types are automatically narrowed to members that have the accessed property/method
it('narrows union types', function () {
    /** @var Order|Order[]|null $order */
    $order = Order::factory()->create();

    expect($order)
        ->id->toBeInt()          // narrows to Order (only Order has ->id)
        ->status->toBeString();
});
neon
parameters:
    PhpstanPest:
        testCaseClass: PHPUnit\Framework\TestCase  # Base test case class
        parseAssignments: true                     # $this->prop = expr inference
        parsePestPropertyTags: false               # @pest-property tags
        parsePhpDocProperties: false               # @property PHPDoc tags
        parseUses: true                            # Resolve uses()/pest()->extend() traits
        parseParentUses: true                      # Walk up for parent Pest.php files
        expectationPropertyAccess: true            # Higher-order property access on expect()
        expectationSequenceTypes: false            # Typed closures in ->sequence()
        expectationScopedTypes: false              # Typed closures in ->scoped()
        simplifyToBe: true                         # ->toBe(true/false) → ->toBeTrue/False()
        simplifyToBeNull: true                     # ->toBe(null) → ->toBeNull()
        simplifyToBeEmpty: true                    # ->toBe('')/->toBe([]) → ->toBeEmpty()
        simplifyToHaveCount: true                  # ->toHaveCount(0) → ->toBeEmpty()
        simplifyToHaveLength: true                 # ->toHaveLength(0) → ->toBeEmpty()