PHPackages                             simtel/phpstan-rules - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. simtel/phpstan-rules

ActiveLibrary

simtel/phpstan-rules
====================

Extended rules for phpstan

2.0.3(7mo ago)0302↓100%MITPHPPHP &gt;8.3CI passing

Since Jan 15Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/Simtel/phpstan-rules)[ Packagist](https://packagist.org/packages/simtel/phpstan-rules)[ RSS](/packages/simtel-phpstan-rules/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (4)Versions (19)Used By (0)

PHPStan Extended Rules
======================

[](#phpstan-extended-rules)

[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](LICENSE)[![PHP Version](https://camo.githubusercontent.com/e991cd302c60ee959c2fbcce3026475408e5b513be27ac68dc132e07211a95f1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345382e332d626c75652e737667)](https://php.net/)[![PHPStan](https://camo.githubusercontent.com/fd2468b51182aff9487c673cd17930e12f3944181015f35787c7aa391cfd7f45/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d253545322e302d626c75652e737667)](https://phpstan.org/)

A collection of custom PHPStan rules that enforce coding standards and improve code quality in PHP projects. This extension provides additional static analysis rules focused on naming conventions, annotations, and PHPDoc consistency.

🎯 Features
----------

[](#-features)

This package includes three powerful rules that help maintain high code quality:

### 1. Command-Handler Relationship Rule

[](#1-command-handler-relationship-rule)

**Rule**: `CommandClassShouldBeHelpCommandHandlerClass`

Enforces that classes ending with "Command" must have a `@see` PHPDoc tag pointing to their corresponding CommandHandler class.

**Example**:

```
/**
 * @see CreateUserCommandHandler
 */
class CreateUserCommand
{
    // Command implementation
}
```

**Exception**: Classes with an `__invoke` method are exempt from this rule.

### 2. Event Listener Attribute Rule

[](#2-event-listener-attribute-rule)

**Rule**: `EventListenerClassShouldBeIncludeAsListenerAttribute`

Ensures that classes ending with "EventListener" are properly annotated with the `#[AsEventListener]` attribute.

**Example**:

```
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

#[AsEventListener]
class UserRegisteredEventListener
{
    // Event listener implementation
}
```

### 3. Redundant PHPDoc Return Type Rule

[](#3-redundant-phpdoc-return-type-rule)

**Rule**: `NotShouldPhpdocReturnIfExistTypeHint`

Prevents redundant or conflicting `@return` PHPDoc annotations when native return type hints are already declared.

**Good**:

```
public function getUser(): User
{
    return $this->user;
}
```

**Bad**:

```
/**
 * @return User
 */
public function getUser(): User  // Redundant @return annotation
{
    return $this->user;
}
```

📦 Installation
--------------

[](#-installation)

### System Requirements

[](#system-requirements)

- **PHP**: &gt;8.3
- **PHPStan**: ^2.0
- **PHPStan PHPDoc Parser**: ^2.2

### Install via Composer

[](#install-via-composer)

Install the package as a development dependency:

```
composer require --dev simtel/phpstan-rules
```

⚙️ Configuration
----------------

[](#️-configuration)

### Option 1: Include All Rules (Recommended)

[](#option-1-include-all-rules-recommended)

Add the bundled configuration to your `phpstan.neon` or `phpstan.dist.neon`:

```
includes:
    - vendor/simtel/phpstan-rules/rules.neon
```

### Option 2: Manual Rule Registration

[](#option-2-manual-rule-registration)

For granular control, register specific rules:

```
parameters:
    rules:
        - Simtel\PHPStanRules\Rule\CommandClassShouldBeHelpCommandHandlerClass
        - Simtel\PHPStanRules\Rule\EventListenerClassShouldBeIncludeAsListenerAttribute
        - Simtel\PHPStanRules\Rule\NotShouldPhpdocReturnIfExistTypeHint
```

### Complete Configuration Example

[](#complete-configuration-example)

```
includes:
    - vendor/simtel/phpstan-rules/rules.neon

parameters:
    level: 8
    paths:
        - src/
    excludePaths:
        - tests/
    ignoreErrors:
        # Exclude specific patterns if needed
        - '#Command class should be include phpDoc with @see attribute#'
          path: src/Deprecated/
```

🔧 Development
-------------

[](#-development)

### Setting Up Development Environment

[](#setting-up-development-environment)

1. Clone the repository:

    ```
    git clone  phpstan-rules
    cd phpstan-rules
    ```
2. Install dependencies:

    ```
    composer install
    ```

### Development Commands

[](#development-commands)

#### Running Tests

[](#running-tests)

```
# Run all tests
vendor/bin/phpunit

# Run specific test class
vendor/bin/phpunit tests/Rules/CommandClassShouldBeHelpCommandHandlerClassTest.php
```

#### Code Style

[](#code-style)

```
# Check coding standards
vendor/bin/ecs check

# Fix coding standards automatically
vendor/bin/ecs fix
```

#### Static Analysis

[](#static-analysis)

```
# Run PHPStan on the project itself
vendor/bin/phpstan analyse
```

### Project Structure

[](#project-structure)

```
.
├── src/Rule/                          # Rule implementations
│   ├── CommandClassShouldBeHelpCommandHandlerClass.php
│   ├── EventListenerClassShouldBeIncludeAsListenerAttribute.php
│   └── NotShouldPhpdocReturnIfExistTypeHint.php
├── tests/
│   ├── Fixture/                        # Test code samples
│   │   ├── EventListener/
│   │   └── Return/
│   ├── Rules/                          # Unit tests for rules
│   └── data/                           # Additional test data
├── composer.json                       # Package configuration
├── ecs.php                            # ECS configuration
└── README.md                          # This file

```

### Creating New Rules

[](#creating-new-rules)

1. **Implement the Rule Interface**:

    ```
