承接 atournayre/phpstan-elegant-object 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

atournayre/phpstan-elegant-object

最新稳定版本:1.0.0

Composer 安装命令:

composer require atournayre/phpstan-elegant-object

包简介

Custom PHPStan rules for enforcing elegant object design

README 文档

README

This extension is a work in progress. Use at your own risk.

PHPStan Elegant Object Rules

This PHPStan extension adds custom rules to encourage and validate elegant object design principles in your PHP code.

It's based on Yegor Bugayenko's books :

  • Elegant Objects (Volume 1)
  • Elegant Objects (Volume 2)

Installation

composer require --dev atournayre/phpstan-elegant-object

Configuration

Include the extension in your phpstan.neon file:

# Include the extension config if you want extra strict rules
includes:
    - vendor/atournayre/phpstan-elegant-object/phpstan.neon

It is recommended to configure each rule separately.

Available Rules

See configuration for a list of available rules.

Creating Custom Rules

To create a new rule:

  1. Create a class in the src/Analyzer/ folder that implements Atournayre\PHPStan\ElegantObject\Analyzer\RuleAnalyzer
  2. Define the node type to analyze with getNodeType()
  3. Override shouldSkipAnalysis() if necessary
  4. Implement your analysis logic in analyze()
  5. Create a class in the src/Rules/ folder that extends Atournayre\PHPStan\ElegantObject\Rules\ComposableRule (see existing rules for examples)
  6. Register your rule in phpstan.neon

Tests

To run the tests:

vendor/bin/phpunit

Rules

From the book:

  • Never use -er names (chapter 1.1)
  • Keep constructors code-free (chapter 1.3)
  • Always use interface (chapter 2.3)
  • Don't use public constant (chapter 2.5)
  • Be immutable (chapter 2.6)
  • Keep interfaces short (chapter 2.9)
  • Expose fewer than 5 public methods (chapter 3.1)
  • Don't use static methods (chapter 3.2)
  • Never accept null arguments (chapter 3.3)
  • Never use getters and setters (chapter 3.5)
  • Don't use new outside of secondary constructors (chapter 3.6)
  • Avoid type introspection and casting (chapter 3.7)
  • Never return Null (chapter 4.1)
  • Throw only checked exceptions (chapter 4.2)
  • Be either final or abstract (chapter 4.3)
  • Private static literals (chapter 5.4)

Other:

  • All properties must be private
  • Constructor objects only

统计信息

  • 总下载量: 1.65k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 3
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-16