Skip to content

fix: mutate the enums given to mutates() or --class - #50

Open
mechgw wants to merge 1 commit into
pestphp:5.xfrom
mechgw:fix/mutates-enums
Open

mechgw wants to merge 1 commit into
pestphp:5.xfrom
mechgw:fix/mutates-enums

Conversation

@mechgw

@mechgw mechgw commented Oct 18, 2026

Copy link
Copy Markdown

What:

  • Bug Fix
  • New Feature

Description:

mutates() and --class never select an enum, so its methods are never mutated. For example, with mutates(Urgency::class) and

enum Urgency: int
{
    case Low = 1;
    case High = 2;

    public function isHigh(): bool
    {
        return $this->value >= 2;
    }
}

pest --mutate creates no mutation for isHigh().

MutationGenerator::doesNotContainClassToMutate() recognizes a declaration by the regular expression (?:class|trait)\s+$class, so a file declaring an enum is always treated as not containing it.

The fix. Both regular expressions also accept enum. Nothing else changes in the way the classes are matched.

This changes the same line as #22, which adds a word boundary after the class name. The two changes do not contradict each other, so whichever is merged first, I will rebase this one on top of it. With #22, the namespace prefix case of the new test gives no mutation, exactly as #22 changes it for the class and trait tests.

This is part of the series described in pestphp/pest#1978 (point 2), but it is useful on its own and does not depend on #48 or #49.

Tests. it generates mutations for the given file if it contains the given enum in tests/Unit/MutationGeneratorTest.php uses the new fixture tests/Fixtures/Enums/Urgency.php, with the same cases as the existing class and trait tests: the enum by its full name, by its short name and by a namespace prefix gives one mutation; another class or another namespace gives none. Without the fix, the three positive cases fail.

CI. The workflows ran on a fork with the full matrix and fail-fast: false. COMPOSER_ROOT_VERSION=5.x-dev was set there only because Composer cannot guess the root version of a feature branch. The results were compared, job by job, with the same workflows on unmodified 5.x (d0c8711):

This branch passes the 5 new tests in every job (prefer-lowest: 324 passed, against 319 on 5.x). Apart from them, the set of failures is identical in every job:

  • the 3 Tests\Visual\* snapshots fail in the prefer-stable jobs, because current PHPUnit prints a deprecation warning for the cacheResult attribute of phpunit.xml;
  • PHPStan reports 2 errors, both in files this PR does not touch.

🤖 Generated with Claude Code

Only class and trait declarations were recognized, so an enum was never
selected for mutation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 18, 2026 06:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

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.

2 participants