Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 22 additions & 153 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,206 +1,75 @@
# PHPStan Development Utilities
# PHPStan Rules Development Utilities

Development utilities for PHPStan rules testing, extracted from [shipmonk/phpstan-rules](https://github.com/shipmonk-rnd/phpstan-rules).

This package provides the `RuleTestCase` class - an enhanced testing framework specifically designed for testing PHPStan rules with additional validation and convenience features.
Inplace fixture asserts and autofix for PHPStan rules.
No more manual line adjustments in tests when new code is added to rule fixtures.

## Installation

```bash
composer require --dev shipmonk/phpstan-dev
```

## Quick Start

### 1. Create Your Rule Test Class
## Usage

```php
<?php declare(strict_types = 1);

namespace YourNamespace;

use ShipMonk\PHPStanDev\RuleTestCase;
use PHPStan\Rules\Rule;

/**
* @extends RuleTestCase<YourRule>
*/
class YourRuleTest extends RuleTestCase
class YourRuleTest extends \ShipMonk\PHPStanDev\RuleTestCase
{
protected function getRule(): Rule
{
return new YourRule();
}

public function testRule(): void
{
$this->analyzeFiles([__DIR__ . '/Data/YourRule/code.php']);
$this->analyzeFiles([__DIR__ . '/Data/code.php']);
}
}
```

### 2. Create Test Data File

Create `tests/Rule/Data/YourRule/code.php`:
Create test fixture at `code.php`:

```php
<?php declare(strict_types = 1);

namespace YourRule;

function test() {
$valid = 'This is valid code';
$invalid = something(); // error: Your custom error message here
}
```

### 3. Run Your Test
<?php

```bash
vendor/bin/phpunit tests/Rule/YourRuleTest.php
$valid = 'This is valid code';
$invalid = something(); // error: Rule error message
```

## Key Features

### 🎯 Error Comment System
### In-fixture error asserts of via `// error:`

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

```php
<?php

$validCode = 'No error expected here';
$invalidCode = forbidden(); // error: This is forbidden
$alsoInvalid = another(); // error: Another error message
$invalidCode = forbidden(); // error: Rule error message
$alsoInvalid = another(); // error: Rule error message // error: Same-line multi errors
```

### 🔧 Autofix Mode
### Autofix

During development, automatically generate error comments:
Automatically generate inplace error comments during development:

```php
public function testRule(): void
{
// Set to true temporarily to generate error comments
$this->analyzeFiles([__DIR__ . '/Data/code.php'], autofix: true);
$this->analyzeFiles([...], autofix: true);
}
```

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

### 🛡️ Automatic Error Validation

Every error is automatically validated:
- ✅ Must have an identifier
- ✅ Errors are matched to specific line numbers

## Advanced Usage

### Multiple Test Scenarios

```php
class ComplexRuleTest extends RuleTestCase
{
private bool $strictMode = false;

protected function getRule(): Rule
{
return new ComplexRule($this->strictMode);
}

public function testDefault(): void
{
$this->analyzeFiles([__DIR__ . '/Data/ComplexRule/default.php']);
}

public function testStrict(): void
{
$this->strictMode = true;
$this->analyzeFiles([__DIR__ . '/Data/ComplexRule/strict.php']);
}
}
```

### PHP Version-Specific Tests

```php
public function testPhp82Features(): void
{
$this->phpVersion = $this->createPhpVersion(80_200);
$this->analyzeFiles([__DIR__ . '/Data/Rule/php82-features.php']);
}
```

### Custom PHPStan Configuration
## Contributing
- Check your code by `composer check`
- Autofix coding-style by `composer fix:cs`
- All functionality must be tested

Create `tests/Rule/Data/YourRule/config.neon`:

```neon
parameters:
customParameter: value
```

Then reference it in your test:

```php
public static function getAdditionalConfigFiles(): array
{
return array_merge(
parent::getAdditionalConfigFiles(),
[__DIR__ . '/Data/YourRule/config.neon'],
);
}
```

### Rules with Dependencies

```php
protected function getRule(): Rule
{
$dependency = self::getContainer()->getByType(SomeService::class);
return new RuleWithDependencies($dependency);
}
```

## File Organization

Recommended directory structure:

```
tests/
├── Rule/
│ ├── YourRuleTest.php
│ ├── AnotherRuleTest.php
│ └── Data/
│ ├── YourRule/
│ │ ├── code.php # Main test file
│ │ ├── edge-cases.php # Additional scenarios
│ │ └── config.neon # Optional PHPStan config
│ └── AnotherRule/
│ └── code.php
```

## Development

```bash
# Install dependencies
composer install

# Run all checks
composer check

# Individual checks
composer check:composer # Validate composer.json
composer check:ec # Check EditorConfig compliance
composer check:cs # Check coding standards (PHPCS)
composer check:types # Run PHPStan analysis
composer check:dependencies # Analyze dependencies
composer check:collisions # Check for name collisions

# Fix coding standards
composer fix:cs
```

## License

MIT License - see [LICENSE](LICENSE) file.
MIT