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 96.42857% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.70%. Comparing base (730d429) to head (0c1941c).
⚠️ Report is 14 commits behind head on main.

Files with missing lines Patch % Lines
src/Expression/ForClasses/NotHaveTrait.php 90.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main     #555      +/-   ##
============================================
- Coverage     97.74%   97.70%   -0.05%     
- Complexity      614      633      +19     
============================================
  Files            79       81       +2     
  Lines          1775     1831      +56     
============================================
+ Hits           1735     1789      +54     
- Misses           40       42       +2     

☔ 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.

claude added 2 commits January 1, 2026 16:57
…criptionBuilder

This commit adds comprehensive test coverage for the new trait tracking
functionality introduced in the previous commit.

Changes:
- Added test_it_should_add_traits() in ClassDescriptionBuilderTest to verify
  the addTrait() method correctly stores and retrieves traits
- Added test_should_return_true_if_has_trait() in ClassDescriptionTest to
  verify hasTrait() method with exact and wildcard pattern matching
- Added test_should_return_false_if_not_has_trait() in ClassDescriptionTest
  to verify hasTrait() returns false for non-matching traits

All tests pass (337 tests, 563 assertions). Coverage now includes:
- ClassDescriptionBuilder::addTrait()
- ClassDescription::getTraits()
- ClassDescription::hasTrait()
This commit adds comprehensive integration tests that verify the complete
workflow from parsing PHP files to validating trait usage rules.

Integration tests added:
- test_feature_tests_should_use_database_transactions_trait(): Verifies
  that classes in Tests\Feature namespace are required to use the
  DatabaseTransactions trait
- test_feature_tests_should_not_use_refresh_database_trait(): Verifies
  that classes in Tests\Feature namespace are prohibited from using the
  RefreshDatabase trait
- test_classes_with_uuid_should_have_uuid_trait(): Verifies that all
  classes in App\Models namespace use the HasUuid trait

These tests use vfsStream to create virtual PHP files and TestRunner to
execute the complete parsing and validation pipeline, ensuring that:
1. FileVisitor correctly parses trait usage from PHP code
2. ClassDescription correctly stores and retrieves traits
3. HaveTrait and NotHaveTrait rules correctly validate violations
4. The entire system works end-to-end

All tests pass (340 tests, 571 assertions).
@fain182 fain182 marked this pull request as ready for review January 1, 2026 17:17
@fain182 fain182 requested a review from micheleorselli January 1, 2026 17:17
The PHPDoc parameter annotations were not in the same order as the actual
constructor parameters. This fixes the type checking issue reported by
Scrutinizer.

Changed the order in PHPDoc from:
- , , , , ,

To match the actual constructor order:
- , , , , ,
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