GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - Phauthentic/phpstan-rules: PHPStan rules focused on architectural & clean code rules

BranchesTags
Open more actions menu

Latest commit

 

History

63 Commits
63 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
data
data
 
 
docs
docs
 
 
src
src
 
 
tests/TestCases
tests/TestCases
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
composer.json
composer.json
 
 
composer.lock
composer.lock
 
 
phpcs.xml
phpcs.xml
 
 
phpstan.neon
phpstan.neon
 
 
phpunit.xml.dist
phpunit.xml.dist
 
 
rule-builder.html
rule-builder.html
 
 

Repository files navigation

PHPStan Rules

Additional rules for PHPStan, mostly focused on Clean Code and architecture conventions.

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

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

composer require phauthentic/phpstan-rules --dev

Rules

See individual rule documentation for detailed configuration examples. A full configuration example is also available.

Architecture Rules

  • Attribute Rule
  • Catch Exception Of Type Not Allowed Rule
  • Circular Module Dependency Rule
  • Class Must Be Final Rule
  • Class Must Be Readonly Rule
  • Class Must Have Specification Docblock Rule
  • Classname Must Match Pattern Rule
  • Dependency Constraints Rule (deprecated, use Forbidden Dependencies Rule)
  • Forbidden Accessors Rule
  • Forbidden Business Logic Rule
  • Forbidden Dependencies Rule
  • Forbidden Date Time Comparison Rule
  • Forbidden Namespaces Rule
  • Forbidden Static Methods Rule
  • Method Must Return Type Rule
  • Method Signature Must Match Rule
  • Methods Returning Bool Must Follow Naming Convention Rule
  • Modular Architecture Rule
  • Property Must Match Rule

Clean Code Rules

  • Control Structure Nesting Rule
  • Forbidden Else Statements Rule
  • Too Many Arguments Rule
  • Max Line Length Rule

Domain Specific Rules / Extending Rules

The rules in this package can be extended at project level to create self-documenting, domain-specific rules. Instead of configuring complex regex patterns in your neon file, you can create custom rule classes that encapsulate the configuration. For example, a class named DomainClassesMustBeFinalRule immediately communicates its purpose. See the Extending Rules documentation for examples and best practices.

Using Regex in Rules

A lot of the rules use regex patterns to match things. Many people are not good at writing them but thankfully there is AI today.

If you struggle to write the regex patterns you need, you can use AI agents to help you generate them. Just describe what you want to match, and it can provide you with a regex pattern that fits your needs. The regex can be tested using online tools like regex101.

Why PHPStan to enforce Architectural Rules?

Because PHPStan is a widely used static analysis tool in the PHP community. It already provides a solid foundation for code quality checks, and adding custom rules allows you to enforce specific coding standards and architectural constraints is just a logical choice. You won't need more 3rd party tools to enforce your architectural constraints.

It is also more or less easy to write your own rules if you need to enforce something specific that is not covered by the existing rules.

What if I need to ignore a Rule in a certain Place?

Use the inline annotations PHPStan provides and add a comment explaining why in this case the rule is allowed to be broken. An inline annotation is the best because you should keep the information close to where it happens, visible right at the root of the problem.

Alternative Tools

If you don't like this library, you can also check out other tools. Some of them provide a fluent interface instead of a Regex. If this feels more comfortable for you, you might want to check them out:

  • Deptrac - Checks dependencies between namespaces and classes.
  • PHP Architecture Tester - A tool to enforce architectural rules in PHP applications.
  • PHPArkitect - A tool to enforce architectural rules in PHP applications.
  • PHPMD - A tool that scans PHP source code and looks for potential problems.

License

This library is under the MIT license.

Copyright Florian Krämer

About

PHPStan rules focused on architectural & clean code rules

Topics

architecturecheckerclean-architectureclean-codephpstanphpstan-rules

Resources

Readme
MIT license
Activity
Custom properties

Stars

3 stars

Watchers

1 watching

Forks

2 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.