Skip to content

Conversation

@olivernybroe
Copy link
Member

@olivernybroe olivernybroe commented Nov 20, 2024

What:

  • Bug Fix
  • New Feature

Description:

Allow adding custom arch expectations into pest.

A custom arch expectation needs to have typehinted the return type to the ArchExpectation for it to work with this implementation.

An example could be the following added as a custom expectation

expect()->extend('toHaveMethodsDocumentedMatching', function (string $pattern): ArchExpectation {
    return Targeted::make(
        $this, // @phpstan-ignore-line
        fn (ObjectDescription $object): bool => isset($object->reflectionClass) === false
            || (
                array_filter(
                Reflection::getMethodsFromReflectionClass($object->reflectionClass),
                fn(ReflectionMethod $method): bool => (enum_exists($object->name) === false || in_array($method->name, ['from', 'tryFrom', 'cases'], true) === false)
                    && realpath($method->getFileName() ?: '/') === realpath($object->path)
                    && !Str::isMatch($pattern, $method->getDocComment()),
                ) === []
            ),
        'to have methods with documentation / annotations matching pattern',
        FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class'))
    );
});

Related:

@Lukasss93
Copy link

I would love to have this feature in pest

@nunomaduro nunomaduro changed the base branch from 3.x to 4.x July 26, 2025 03:23
@nunomaduro nunomaduro merged commit ee70a3c into 4.x Jul 26, 2025
36 of 40 checks passed
@nunomaduro nunomaduro deleted the allow-custom-arch-expectations branch July 26, 2025 03:23
@Lukasss93
Copy link

Can you add this feature to the documentation?

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.

5 participants