Skip to content

Add Behat context support#267

Merged
janedbal merged 3 commits intoshipmonk-rnd:masterfrom
devbanana:feature/add-behat-support
Nov 25, 2025
Merged

Add Behat context support#267
janedbal merged 3 commits intoshipmonk-rnd:masterfrom
devbanana:feature/add-behat-support

Conversation

@devbanana
Copy link
Copy Markdown
Contributor

This adds support for detecting Behat context classes and marking their step definitions and hooks as used. It supports both annotation-based and attribute-based methods.

  • Supports annotation-based step definitions (@given, @when, @then)
  • Supports PHP 8 attribute-based step definitions (#[Given], #[When], #[Then])
  • Supports all Behat hooks (BeforeScenario, AfterScenario, BeforeStep, AfterStep, etc.)
  • Supports @Transform/#[Transform] for parameter transformations
  • Auto-enables when behat/behat package is detected via Composer
  • Configurable via shipmonkDeadCode.usageProviders.behat.enabled parameter

@devbanana devbanana force-pushed the feature/add-behat-support branch from 56d1830 to 0b70997 Compare November 22, 2025 21:05
Copy link
Copy Markdown
Member

@janedbal janedbal left a comment

Choose a reason for hiding this comment

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

Please also rebase.

Comment thread tests/Rule/data/providers/behat.php
Comment thread tests/Rule/DeadCodeRuleTest.php Outdated
@janedbal
Copy link
Copy Markdown
Member

Also please add short readme mention.

@janedbal
Copy link
Copy Markdown
Member

It would be also nice if you could confirm it actually work on some real-world project using Behat. Thank you

@devbanana devbanana force-pushed the feature/add-behat-support branch from 0b70997 to bd85ad5 Compare November 25, 2025 05:59
@devbanana
Copy link
Copy Markdown
Contributor Author

Thanks @janedbal . As requested I added additional dead methods and added the feature to the README.

Yes I verified it on a personal project I am using it on along with behat. That's why I wanted to add this feature since I was getting a lot of false positives. If you need me to link to the project, I can.

Let me know if I need to make any other changes.

@devbanana devbanana requested a review from janedbal November 25, 2025 06:08
Copy link
Copy Markdown
Member

@janedbal janedbal left a comment

Choose a reason for hiding this comment

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

Thank you!

This commit adds support for detecting Behat context classes and marking their step definitions and hooks as used, preventing them from being incorrectly flagged as dead code.

Features:
- New BehatUsageProvider that detects classes implementing Behat\Behat\Context\Context
- Supports annotation-based step definitions (@given, @when, @then)
- Supports PHP 8 attribute-based step definitions (#[Given], #[When], #[Then])
- Supports all Behat hooks (BeforeScenario, AfterScenario, BeforeStep, AfterStep, etc.)
- Supports @Transform/#[Transform] for parameter transformations
- Auto-enables when behat/behat package is detected via Composer
- Configurable via shipmonkDeadCode.usageProviders.behat.enabled parameter

Test coverage:
- Added comprehensive test data with both annotation and attribute examples
- Achieved 100% code coverage (6/6 methods, 49/49 lines)
- Added 4 new test cases to the test suite
Behat contexts often have constructors for dependency injection. These constructors are called by the Behat framework when instantiating contexts, so they should not be marked as dead code.

Changes:
- Updated BehatUsageProvider to detect and mark constructors as used
- Added test cases with constructor examples
- Maintained 100% code coverage
- Add helper methods without Behat annotations to test files to verify the provider doesn't incorrectly mark all methods as used
- Document Behat support in README including constructors, step definitions, hooks, and transformations
@janedbal janedbal force-pushed the feature/add-behat-support branch from bd85ad5 to 0127fe8 Compare November 25, 2025 08:04
@janedbal janedbal merged commit 4ebdee6 into shipmonk-rnd:master Nov 25, 2025
35 checks passed
@janedbal
Copy link
Copy Markdown
Member

Released as 0.14

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