Skip to content

Commit d839bcc

Browse files
authored
Readme: simplify (#12)
1 parent a434523 commit d839bcc

File tree

1 file changed

+22
-153
lines changed

1 file changed

+22
-153
lines changed

README.md

Lines changed: 22 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,206 +1,75 @@
1-
# PHPStan Development Utilities
1+
# PHPStan Rules Development Utilities
22

3-
Development utilities for PHPStan rules testing, extracted from [shipmonk/phpstan-rules](https://github.com/shipmonk-rnd/phpstan-rules).
4-
5-
This package provides the `RuleTestCase` class - an enhanced testing framework specifically designed for testing PHPStan rules with additional validation and convenience features.
3+
Inplace fixture asserts and autofix for PHPStan rules.
4+
No more manual line adjustments in tests when new code is added to rule fixtures.
65

76
## Installation
87

98
```bash
109
composer require --dev shipmonk/phpstan-dev
1110
```
1211

13-
## Quick Start
14-
15-
### 1. Create Your Rule Test Class
12+
## Usage
1613

1714
```php
1815
<?php declare(strict_types = 1);
1916

20-
namespace YourNamespace;
21-
22-
use ShipMonk\PHPStanDev\RuleTestCase;
2317
use PHPStan\Rules\Rule;
2418

2519
/**
2620
* @extends RuleTestCase<YourRule>
2721
*/
28-
class YourRuleTest extends RuleTestCase
22+
class YourRuleTest extends \ShipMonk\PHPStanDev\RuleTestCase
2923
{
30-
protected function getRule(): Rule
31-
{
32-
return new YourRule();
33-
}
34-
3524
public function testRule(): void
3625
{
37-
$this->analyzeFiles([__DIR__ . '/Data/YourRule/code.php']);
26+
$this->analyzeFiles([__DIR__ . '/Data/code.php']);
3827
}
3928
}
4029
```
4130

42-
### 2. Create Test Data File
43-
44-
Create `tests/Rule/Data/YourRule/code.php`:
31+
Create test fixture at `code.php`:
4532

4633
```php
47-
<?php declare(strict_types = 1);
48-
49-
namespace YourRule;
50-
51-
function test() {
52-
$valid = 'This is valid code';
53-
$invalid = something(); // error: Your custom error message here
54-
}
55-
```
56-
57-
### 3. Run Your Test
34+
<?php
5835

59-
```bash
60-
vendor/bin/phpunit tests/Rule/YourRuleTest.php
36+
$valid = 'This is valid code';
37+
$invalid = something(); // error: Rule error message
6138
```
6239

6340
## Key Features
6441

65-
### 🎯 Error Comment System
42+
### In-fixture error asserts of via `// error:`
6643

67-
Use `// error: <message>` comments in test files to specify expected errors:
44+
Mark expected errors directly in test files via PHP comments:
6845

6946
```php
7047
<?php
7148

7249
$validCode = 'No error expected here';
73-
$invalidCode = forbidden(); // error: This is forbidden
74-
$alsoInvalid = another(); // error: Another error message
50+
$invalidCode = forbidden(); // error: Rule error message
51+
$alsoInvalid = another(); // error: Rule error message // error: Same-line multi errors
7552
```
7653

77-
### 🔧 Autofix Mode
54+
### Autofix
7855

79-
During development, automatically generate error comments:
56+
Automatically generate inplace error comments during development:
8057

8158
```php
8259
public function testRule(): void
8360
{
84-
// Set to true temporarily to generate error comments
85-
$this->analyzeFiles([__DIR__ . '/Data/code.php'], autofix: true);
61+
$this->analyzeFiles([...], autofix: true);
8662
}
8763
```
8864

8965
**⚠️ Important**: Remove `autofix: true` before committing - tests will fail if autofix is enabled.
9066

91-
### 🛡️ Automatic Error Validation
92-
93-
Every error is automatically validated:
94-
- ✅ Must have an identifier
95-
- ✅ Errors are matched to specific line numbers
96-
97-
## Advanced Usage
98-
99-
### Multiple Test Scenarios
100-
101-
```php
102-
class ComplexRuleTest extends RuleTestCase
103-
{
104-
private bool $strictMode = false;
105-
106-
protected function getRule(): Rule
107-
{
108-
return new ComplexRule($this->strictMode);
109-
}
110-
111-
public function testDefault(): void
112-
{
113-
$this->analyzeFiles([__DIR__ . '/Data/ComplexRule/default.php']);
114-
}
115-
116-
public function testStrict(): void
117-
{
118-
$this->strictMode = true;
119-
$this->analyzeFiles([__DIR__ . '/Data/ComplexRule/strict.php']);
120-
}
121-
}
122-
```
123-
124-
### PHP Version-Specific Tests
125-
126-
```php
127-
public function testPhp82Features(): void
128-
{
129-
$this->phpVersion = $this->createPhpVersion(80_200);
130-
$this->analyzeFiles([__DIR__ . '/Data/Rule/php82-features.php']);
131-
}
132-
```
133-
134-
### Custom PHPStan Configuration
67+
## Contributing
68+
- Check your code by `composer check`
69+
- Autofix coding-style by `composer fix:cs`
70+
- All functionality must be tested
13571

136-
Create `tests/Rule/Data/YourRule/config.neon`:
137-
138-
```neon
139-
parameters:
140-
customParameter: value
141-
```
142-
143-
Then reference it in your test:
144-
145-
```php
146-
public static function getAdditionalConfigFiles(): array
147-
{
148-
return array_merge(
149-
parent::getAdditionalConfigFiles(),
150-
[__DIR__ . '/Data/YourRule/config.neon'],
151-
);
152-
}
153-
```
154-
155-
### Rules with Dependencies
156-
157-
```php
158-
protected function getRule(): Rule
159-
{
160-
$dependency = self::getContainer()->getByType(SomeService::class);
161-
return new RuleWithDependencies($dependency);
162-
}
163-
```
164-
165-
## File Organization
166-
167-
Recommended directory structure:
168-
169-
```
170-
tests/
171-
├── Rule/
172-
│ ├── YourRuleTest.php
173-
│ ├── AnotherRuleTest.php
174-
│ └── Data/
175-
│ ├── YourRule/
176-
│ │ ├── code.php # Main test file
177-
│ │ ├── edge-cases.php # Additional scenarios
178-
│ │ └── config.neon # Optional PHPStan config
179-
│ └── AnotherRule/
180-
│ └── code.php
181-
```
182-
183-
## Development
184-
185-
```bash
186-
# Install dependencies
187-
composer install
188-
189-
# Run all checks
190-
composer check
191-
192-
# Individual checks
193-
composer check:composer # Validate composer.json
194-
composer check:ec # Check EditorConfig compliance
195-
composer check:cs # Check coding standards (PHPCS)
196-
composer check:types # Run PHPStan analysis
197-
composer check:dependencies # Analyze dependencies
198-
composer check:collisions # Check for name collisions
199-
200-
# Fix coding standards
201-
composer fix:cs
202-
```
20372

20473
## License
20574

206-
MIT License - see [LICENSE](LICENSE) file.
75+
MIT

0 commit comments

Comments
 (0)