Skip to content

Develop #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Additional rules for PHPStan, mostly focused on Clean Code and architecture conv

The rules help with enforcing certain method signatures, return types and dependency constraints in your codebase.

All controllers in your application should be `readonly`, no method should have more than 3 arguments, and no class should have more than 2 nested control structures.
For example, you can configure the rules so that all controllers in your application must be `readonly`, no method should have more than 3 arguments, and no class should have more than 2 nested control structures.

## Usage

Expand All @@ -21,6 +21,8 @@ See [Rules documentation](docs/Rules.md) for a list of available rules and confi
- [Readonly Class Rule](docs/Rules.md#readonly-class-rule)
- [Final Class Rule](docs/Rules.md#final-class-rule)
- [Namespace Class Pattern Rule](docs/Rules.md#namespace-class-pattern-rule)
- [Catch Exception of Type Not Allowed Rule](docs/Rules.md#catch-exception-of-type-not-allowed-rule)
- [Methods Returning Bool Must Follow Naming Convention Rule](docs/Rules.md#methods-returning-bool-must-follow-naming-convention-rule)
- [Method Signature Must Match Rule](docs/Rules.md#method-signature-must-match-rule)
- [Method Must Return Type Rule](docs/Rules.md#method-must-return-type-rule)
- Clean Code Rules:
Expand Down
8 changes: 8 additions & 0 deletions data/MethodSignatureMustMatch/TestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ class TestClass
public function testMethod(int $a)
{
}

public function testMethodNoType($x, string $y)
{
}

public function testMethodWithWrongType(int $x, int $y)
{
}
}
53 changes: 34 additions & 19 deletions docs/Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,49 @@

Add them to your `phpstan.neon` configuration file under the section `services`.

## Control Structure Nesting Rule
## Control Structure Nesting Rule {#control-structure-nesting-rule}

Ensures that the nesting level of `if` and `try-catch` statements does not exceed a specified limit.

**Configuration Example:**
```neon
-
class: Phauthentic\PhpstanRules\CleanCode\ControlStructureNestingRule
class: Phauthentic\PHPStanRules\CleanCode\ControlStructureNestingRule
arguments:
maxNestingLevel: 2
tags:
- phpstan.rules.rule
```

## Too Many Arguments Rule
## Too Many Arguments Rule {#too-many-arguments-rule}

Checks that methods do not have more than a specified number of arguments.

**Configuration Example:**
```neon
-
class: Phauthentic\PhpstanRules\CleanCode\TooManyArgumentsRule
class: Phauthentic\PHPStanRules\CleanCode\TooManyArgumentsRule
arguments:
maxArguments: 3
tags:
- phpstan.rules.rule
```

## Readonly Class Rule
## Readonly Class Rule {#readonly-class-rule}

Ensures that classes matching specified patterns are declared as `readonly`.

**Configuration Example:**
```neon
-
class: Phauthentic\PhpstanRules\Architecture\ReadonlyClassRule
class: Phauthentic\PHPStanRules\Architecture\ClassMustBeReadonlyRule
arguments:
patterns: ['/^App\\Controller\\/']
tags:
- phpstan.rules.rule
```

## Dependency Constraints Rule
## Dependency Constraints Rule {#dependency-constraints-rule}

Enforces dependency constraints between namespaces by checking `use` statements.

Expand All @@ -55,7 +55,7 @@ In the example below nothing from `App\Domain` can depend on anything from `App\
**Configuration Example:**
```neon
-
class: Phauthentic\PhpstanRules\Architecture\DependencyConstraintsRule
class: Phauthentic\PHPStanRules\Architecture\DependencyConstraintsRule
arguments:
forbiddenDependencies: [
'/^App\\Domain(?:\\\w+)*$/': ['/^App\\Controller\\/']
Expand All @@ -64,28 +64,28 @@ In the example below nothing from `App\Domain` can depend on anything from `App\
- phpstan.rules.rule
```

## Final Class Rule
## Final Class Rule {#final-class-rule}

Ensures that classes matching specified patterns are declared as `final`.

**Configuration Example:**
```neon
-
class: Phauthentic\PhpstanRules\Architecture\FinalClassRule
class: Phauthentic\PHPStanRules\Architecture\ClassMustBeFinalRule
arguments:
patterns: ['/^App\\Service\\/']
tags:
- phpstan.rules.rule
```

## Namespace Class Pattern Rule
## Namespace Class Pattern Rule {#namespace-class-pattern-rule}

Ensures that classes inside namespaces matching a given regex must have names matching at least one of the provided patterns.

**Configuration Example:**
```neon
-
class: Phauthentic\PhpstanRules\Architecture\NamespaceClassPatternRule
class: Phauthentic\PHPStanRules\Architecture\ClassnameMustMatchPatternRule
arguments:
namespaceClassPatterns: [
[
Expand All @@ -99,28 +99,42 @@ Ensures that classes inside namespaces matching a given regex must have names ma
- phpstan.rules.rule
```

## Catch Exception of Type Not Allowed Rule
## Catch Exception of Type Not Allowed Rule {#catch-exception-of-type-not-allowed-rule}

Ensures that specific exception types are not caught in catch blocks. This is useful for preventing the catching of overly broad exception types like `Exception`, `Error`, or `Throwable`.

**Configuration Example:**
```neon
-
class: Phauthentic\PhpstanRules\Architecture\CatchExceptionOfTypeNotAllowedRule
class: Phauthentic\PHPStanRules\Architecture\CatchExceptionOfTypeNotAllowedRule
arguments:
forbiddenExceptionTypes: ['Exception', 'Error', 'Throwable']
tags:
- phpstan.rules.rule
```

## Method Signature Must Match Rule
## Methods Returning Bool Must Follow Naming Convention Rule {#methods-returning-bool-must-follow-naming-convention-rule}

Ensures that methods returning boolean values follow a specific naming convention. By default, boolean methods should start with `is`, `has`, `can`, `should`, `was`, or `will`.

**Configuration Example:**
```neon
-
class: Phauthentic\PHPStanRules\Architecture\MethodsReturningBoolMustFollowNamingConventionRule
arguments:
regex: '/^(is|has|can|should|was|will)[A-Z_]/'
tags:
- phpstan.rules.rule
```

## Method Signature Must Match Rule {#method-signature-must-match-rule}

Ensures that methods matching a class and method name pattern have a specific signature, including parameter types, names, and count.

**Configuration Example:**
```neon
-
class: Phauthentic\PhpstanRules\Architecture\MethodSignatureMustMatchRule
class: Phauthentic\PHPStanRules\Architecture\MethodSignatureMustMatchRule
arguments:
signaturePatterns:
-
Expand All @@ -140,15 +154,16 @@ Ensures that methods matching a class and method name pattern have a specific si
- `pattern`: Regex for `ClassName::methodName`.
- `minParameters`/`maxParameters`: Minimum/maximum number of parameters.
- `signature`: List of expected parameter types and (optionally) name patterns.
- `visibilityScope`: Optional visibility scope (e.g., `public`, `protected`, `private`).

## Method Must Return Type Rule
## Method Must Return Type Rule {#method-must-return-type-rule}

Ensures that methods matching a class and method name pattern have a specific return type, nullability, or are void.

**Configuration Example:**
```neon
-
class: Phauthentic\PhpstanRules\Architecture\MethodMustReturnTypeRule
class: Phauthentic\PHPStanRules\Architecture\MethodMustReturnTypeRule
arguments:
returnTypePatterns:
-
Expand All @@ -162,7 +177,7 @@ Ensures that methods matching a class and method name pattern have a specific re
type: 'object'
nullable: true
void: false
objectTypePattern: '/^App\\\\Entity\\\\User$/'
objectTypePattern: '/^App\\Entity\\User$/'
-
pattern: '/^MyClass::reset$/'
type: 'void'
Expand Down
46 changes: 0 additions & 46 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,3 @@ parameters:
- src
parallel:
maximumNumberOfProcesses: 8

services:
-
class: Phauthentic\PHPStanRules\CleanCode\ControlStructureNestingRule
arguments:
maxNestingLevel: 2
tags:
- phpstan.rules.rule
-
class: Phauthentic\PHPStanRules\CleanCode\TooManyArgumentsRule
arguments:
maxArguments: 3
tags:
- phpstan.rules.rule
-
class: Phauthentic\PHPStanRules\Architecture\ClassMustBeReadonlyRule
arguments:
patterns: ['/^App\\Controller\\/']
tags:
- phpstan.rules.rule
-
class: Phauthentic\PHPStanRules\Architecture\DependencyConstraintsRule
arguments:
forbiddenDependencies: [
'/^App\\Domain(?:\\\w+)*$/': ['/^App\\Controller\\/']
]
tags:
- phpstan.rules.rule
-
class: Phauthentic\PHPStanRules\Architecture\ClassMustBeFinalRule
arguments:
patterns: ['/^App\\Service\\/']
tags:
- phpstan.rules.rule
-
class: Phauthentic\PHPStanRules\Architecture\ClassnameMustMatchPatternRule

arguments:
namespaceClassPatterns: [
[
namespace: '/^App\\Service$/',
classPatterns: ['/Class$/']
]
]
tags:
- phpstan.rules.rule
1 change: 1 addition & 0 deletions src/Architecture/MethodMustReturnTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

/**
* Specification:
*
* - Checks if the classname plus method name matches a given regex pattern.
* - Checks if the method returns the expected type or object, is nullable or void.
* - Check if the types of the parameters match the expected types.
Expand Down
Loading