Skip to content

Commit c55b015

Browse files
author
Florian Krämer
committed
Adding a rule for doc blocks containing specifications
1 parent 5b70051 commit c55b015

24 files changed

+1124
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\SpecificationDocblock;
6+
7+
/**
8+
* Specification:
9+
*
10+
* - Removes an item from the recommendation engine
11+
* - Updates the cache accordingly
12+
*/
13+
class InvalidMissingPeriodsClass
14+
{
15+
public function execute(): void
16+
{
17+
}
18+
}
19+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\SpecificationDocblock;
6+
7+
/**
8+
* Specification:
9+
*
10+
* - Removes an item from the recommendation engine
11+
* and updates all related caches
12+
* - Validates the input data
13+
*/
14+
class InvalidMultiLineNoPeriodClass
15+
{
16+
public function execute(): void
17+
{
18+
}
19+
}
20+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\SpecificationDocblock;
6+
7+
/**
8+
* Specification:
9+
* - Missing blank line after header.
10+
*/
11+
class InvalidSpecificationNoBlankLineClass
12+
{
13+
public function execute(): void
14+
{
15+
}
16+
}
17+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\SpecificationDocblock;
6+
7+
/**
8+
* Specification:
9+
*
10+
* This has no list items.
11+
*/
12+
class InvalidSpecificationNoListItemClass
13+
{
14+
public function execute(): void
15+
{
16+
}
17+
}
18+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\SpecificationDocblock;
6+
7+
class MissingDocblockClass
8+
{
9+
public function execute(): void
10+
{
11+
}
12+
}
13+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\SpecificationDocblock;
6+
7+
/**
8+
* This is just a regular docblock.
9+
*
10+
* - Some item here.
11+
*/
12+
class MissingSpecificationHeaderClass
13+
{
14+
public function execute(): void
15+
{
16+
}
17+
}
18+
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Specification Docblock Rule
2+
3+
This directory contains test data for the `ClassMustHaveSpecificationDocblockRule`.
4+
5+
## Rule Configuration Example
6+
7+
```neon
8+
# phpstan.neon
9+
services:
10+
-
11+
class: Phauthentic\PHPStanRules\Architecture\ClassMustHaveSpecificationDocblockRule
12+
arguments:
13+
patterns:
14+
- '/.*Facade$/' # All classes ending with "Facade"
15+
- '/.*Command$/' # All classes ending with "Command"
16+
- '/.*Handler$/' # All classes ending with "Handler"
17+
specificationHeader: 'Specification:' # Optional: customize the header
18+
requireBlankLineAfterHeader: true # Optional: require blank line (default: true)
19+
requireListItemsEndWithPeriod: false # Optional: require periods (default: false)
20+
tags:
21+
- phpstan.rules.rule
22+
```
23+
24+
## Configuration Options
25+
26+
- `patterns`: Array of regex patterns to match class names (required)
27+
- `specificationHeader`: Header text to look for (default: `'Specification:'`)
28+
- `requireBlankLineAfterHeader`: Require blank line after header (default: `true`)
29+
- `requireListItemsEndWithPeriod`: Require list items end with period (default: `false`)
30+
31+
## Valid Specification Format
32+
33+
### Minimum Required Format
34+
```php
35+
/**
36+
* Specification:
37+
*
38+
* - Removes an item from the recommendation engine.
39+
*/
40+
class MyClass {}
41+
```
42+
43+
### Multi-Line List Items
44+
List items can span multiple lines:
45+
```php
46+
/**
47+
* Specification:
48+
*
49+
* - Removes an item from the recommendation engine
50+
* and updates all related caches including user
51+
* preferences and global recommendations.
52+
* - Validates the input data before processing
53+
* and throws an exception if validation fails.
54+
*/
55+
class MyClass {}
56+
```
57+
58+
### With Annotations
59+
```php
60+
/**
61+
* Specification:
62+
*
63+
* - Removes an item from the recommendation engine.
64+
*
65+
* @throws \Exception
66+
*/
67+
class MyClass {}
68+
```
69+
70+
### With Additional Description and Annotations
71+
```php
72+
/**
73+
* Specification:
74+
*
75+
* - Removes an item from the recommendation engine.
76+
* - Updates the cache accordingly.
77+
*
78+
* Some additional description goes here.
79+
*
80+
* @throws \Exception
81+
* @return void
82+
*/
83+
class MyClass {}
84+
```
85+
86+
## Invalid Formats
87+
88+
### Missing Specification Header
89+
```php
90+
/**
91+
* This is just a regular docblock.
92+
*
93+
* - Some item here.
94+
*/
95+
class MyClass {} // ERROR
96+
```
97+
98+
### Missing Blank Line After Header
99+
```php
100+
/**
101+
* Specification:
102+
* - Missing blank line after header.
103+
*/
104+
class MyClass {} // ERROR
105+
```
106+
107+
### Missing List Items
108+
```php
109+
/**
110+
* Specification:
111+
*
112+
* This has no list items.
113+
*/
114+
class MyClass {} // ERROR
115+
```
116+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\SpecificationDocblock;
6+
7+
/**
8+
* Requirements:
9+
*
10+
* - Removes an item from the recommendation engine.
11+
* - Updates the cache accordingly.
12+
*/
13+
class ValidCustomHeaderClass
14+
{
15+
public function execute(): void
16+
{
17+
}
18+
}
19+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\SpecificationDocblock;
6+
7+
/**
8+
* Specification:
9+
*
10+
* - Removes an item from the recommendation engine
11+
* and updates all related caches including:
12+
* - User preferences cache
13+
* - Global recommendations cache
14+
* This ensures consistency across the system.
15+
* - Validates the input data before processing.
16+
*
17+
* @param array $data
18+
* @throws \InvalidArgumentException
19+
*/
20+
class ValidMultiLineComplexClass
21+
{
22+
public function execute(): void
23+
{
24+
}
25+
}
26+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\SpecificationDocblock;
6+
7+
/**
8+
* Specification:
9+
*
10+
* - Removes an item from the recommendation engine
11+
* and updates all related caches.
12+
* - Validates the input data before processing
13+
* and throws an exception if validation fails.
14+
*/
15+
class ValidMultiLineListItemClass
16+
{
17+
public function execute(): void
18+
{
19+
}
20+
}
21+

0 commit comments

Comments
 (0)