Skip to content

Conversation

@fain182
Copy link
Collaborator

@fain182 fain182 commented Dec 27, 2025

Fixes #424

This commit implements the feature requested in issue #424, enabling
enforcement of trait usage policies across classes.

Changes:
- Extended ClassDescription to track traits used by classes
- Added getTraits() and hasTrait() methods to ClassDescription
- Updated ClassDescriptionBuilder with addTrait() method and trait tracking
- Modified FileVisitor to parse trait usage statements (TraitUse nodes)
- Implemented HaveTrait rule to require specific traits on classes
- Implemented NotHaveTrait rule to prohibit specific traits on classes
- Added comprehensive test coverage for both new rules

The implementation follows existing patterns for attribute and interface
checking, providing consistent API and behavior.

Example usage:
```php
// Require DatabaseTransactions trait on all Feature tests
Rule::allClasses()
    ->that(new ResideInOneOfTheseNamespaces('Tests\\Feature'))
    ->should(new HaveTrait(DatabaseTransactions::class))
    ->because('All Feature tests should run transactions');

// Prohibit RefreshDatabase trait for performance
Rule::allClasses()
    ->that(new ResideInOneOfTheseNamespaces('Tests\\Feature'))
    ->should(new NotHaveTrait(RefreshDatabase::class))
    ->because('Feature tests should never refresh the database');
```

All existing tests continue to pass, ensuring backward compatibility.

Fixes #424
@codecov
Copy link

codecov bot commented Dec 27, 2025

Codecov Report

❌ Patch coverage is 91.07143% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.54%. Comparing base (730d429) to head (fc5f611).

Files with missing lines Patch % Lines
src/Analyzer/FileVisitor.php 57.14% 3 Missing ⚠️
src/Expression/ForClasses/NotHaveTrait.php 90.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main     #555      +/-   ##
============================================
- Coverage     97.74%   97.54%   -0.21%     
- Complexity      614      633      +19     
============================================
  Files            79       81       +2     
  Lines          1775     1831      +56     
============================================
+ Hits           1735     1786      +51     
- Misses           40       45       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Add new HasTrait/NotHasTrait Rules

3 participants