You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: resources/posts/2025/2025-01-06-unused-definitions-with-behat-static-analysis.md
+18-15
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,11 @@
2
2
id: 423
3
3
title: "Unused Definitions with Behat Static Analysis"
4
4
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.
6
6
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.
8
8
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.
10
10
---
11
11
12
12
Behat definitions are marked with annotations:
@@ -33,14 +33,14 @@ public function seeTheLight(): void
33
33
}
34
34
```
35
35
36
-
There definitions are used in `*.feature` files:
36
+
These definitions can be used in `*.feature` files:
37
37
38
38
```bash
39
39
Given I do something
40
40
Then I see light
41
41
```
42
42
43
-
But as project develops, there feature files can change:
43
+
But as the project develops, there feature files can change:
44
44
45
45
46
46
```diff
@@ -49,26 +49,26 @@ But as project develops, there feature files can change:
49
49
+Then I see green
50
50
```
51
51
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.
53
53
54
54
This is ideal work for static analysis!
55
55
56
56
## Behat Static Analysis
57
57
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.
59
59
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**.
61
61
62
-
## What do we actually analyse?
62
+
## What do we actually analyze?
63
63
64
64
The process is simple:
65
65
66
66
* 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
68
68
* we look into `*.feature` files and check if the definition is still used
69
69
70
70
```bash
71
-
Checking static, named and regex masks from 100 *Feature files
71
+
Checking static, named, and regex masks from 100 *Feature files
That's it! We can now remove the unused definitions and keep the codebase clean.
94
94
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.
96
96
97
+
## Add Behastan to your Project
97
98
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/).
99
100
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.
101
102
102
103
```bash
103
104
composer require rector/swiss-knife --dev
104
105
```
105
106
106
107
It's downgraded to **PHP 7.2**, so even older projects can use it.
107
108
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:
0 commit comments