Skip to content

Commit ba03ae8

Browse files
committed
gram
1 parent 0232644 commit ba03ae8

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

resources/posts/2025/2025-01-06-unused-definitions-with-behat-static-analysis.md

+18-15
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
id: 423
33
title: "Unused Definitions with Behat Static Analysis"
44
perex: |
5-
Recently I've been working with projects with Behat tests. There are hundreds of definitions that can used in feature files scenarios.
5+
Recently, I've been working on projects with Behat tests. There are hundreds of definitions that can be used in feature file scenarios.
66
7-
I've noticed by accident one of the definitions is not used at all and could be removed. Less code to maintain, less code to read, less code to upgrade.
7+
I accidentally noticed that one of the definitions is not used at all and could be removed. This would result in less code to maintain, less code to read, and less code to upgrade.
88
9-
But I thought: that's weird, **why did not Behat reported this definition** in our CI? Oh, because there is no Behat static analysis report out of the box. Let's fix that.
9+
But I thought, "That's weird." **Why did not Behat report this definition** in our CI? Oh, because there is no Behat static analysis report out of the box. Let's fix that.
1010
---
1111

1212
Behat definitions are marked with annotations:
@@ -33,14 +33,14 @@ public function seeTheLight(): void
3333
}
3434
```
3535

36-
There definitions are used in `*.feature` files:
36+
These definitions can be used in `*.feature` files:
3737

3838
```bash
3939
Given I do something
4040
Then I see light
4141
```
4242

43-
But as project develops, there feature files can change:
43+
But as the project develops, there feature files can change:
4444

4545

4646
```diff
@@ -49,26 +49,26 @@ But as project develops, there feature files can change:
4949
+Then I see green
5050
```
5151

52-
Now we should remove the "I see light" definition, because it's not used anymore. But we often focus on business code and testing and don't have time for checking this.
52+
Now we should remove the "I see light" definition because it's not used anymore. But we often focus on business code and testing and don't have time to check this.
5353

5454
This is ideal work for static analysis!
5555

5656
## Behat Static Analysis
5757

58-
**Why static analysis?** We could have a Behat extension, that would run tests, compare used definitions and report issues in the end. The problem is Behat tests are extremely slow and we don't want to bound tool with specific Behat version.
58+
**Why static analysis?** We could have a Behat extension, that would run tests, compare used definitions, and report issues in the end. The problem is that Behat tests are extremely slow and we don't want to bind the tool with a specific Behat version.
5959

60-
We're lazy. We want to run single command on any Behat version and **get fast reliable feedback within couple seconds**.
60+
We're lazy. We want to run a single command on any Behat version and **get fast reliable feedback within a couple of seconds**.
6161

62-
## What do we actually analyse?
62+
## What do we actually analyze?
6363

6464
The process is simple:
6565

6666
* we collect all definitions from PHP files, both annotations and attributes
67-
* we separate full string match, regex and :mask
67+
* we separate full string match, regex, and :mask
6868
* we look into `*.feature` files and check if the definition is still used
6969

7070
```bash
71-
Checking static, named and regex masks from 100 *Feature files
71+
Checking static, named, and regex masks from 100 *Feature files
7272
==============================================================
7373

7474
Found 1036 masks:
@@ -92,20 +92,23 @@ tests/Behat/OrderContext.php
9292

9393
That's it! We can now remove the unused definitions and keep the codebase clean.
9494

95-
Add this command to you CI and you'll never have to worry about unused Behat definitions again. "What command" you ask?
95+
Add this command to your CI and you'll never have to worry about unused Behat definitions again. "What command?" you ask.
9696

97+
## Add Behastan to your Project
9798

98-
## How to Use It?
99+
Instead of adding yet another CLI tool you have to update, we made it part of [Rector Swiss Knife](https://github.com/rectorphp/swiss-knife/).
99100

100-
Instead of adding yet another CLI tool you have to update, we made it part of [Rector Swiss Knife](https://github.com/rectorphp/swiss-knife/). It's a tool that can [do many things](/blog/cool-features-of-swiss-knife), but it's not a jack of all trades. It's a master of one - maintainable code.
101+
It's a tool that can [do many useful things](/blog/cool-features-of-swiss-knife), but it's not a jack of all trades. It's a master of one - maintainable code.
101102

102103
```bash
103104
composer require rector/swiss-knife --dev
104105
```
105106

106107
It's downgraded to **PHP 7.2**, so even older projects can use it.
107108

108-
To run static analysis, just provide directory with your Behat definitions and feature files:
109+
<br>
110+
111+
To run static analysis on your Behat definitions, just provide a directory with your Behat definitions and feature files:
109112

110113
```bash
111114
vendor/bin/swiss-knife behastan tests

0 commit comments

Comments
 (0)