-
-
Notifications
You must be signed in to change notification settings - Fork 47
Adds HaveTrait and NotHaveTrait rules #555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fain182
wants to merge
4
commits into
main
Choose a base branch
from
claude/create-rules-issue-424-2jMAP
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+502
−1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
…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).
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #424