定制 macropage/phpstan-inspections 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

macropage/phpstan-inspections

Composer 安装命令:

composer require macropage/phpstan-inspections

包简介

A set of PHPStan rules inspired by PhpInspections (EA Extended) to help you find bugs and code smells in your PHP code.

README 文档

README

⚠️ Disclaimer: Use With Caution ⚠️

All PHPStan rules in this package were written with the assistance of AI. While they have been tested, it is highly likely that some rules may produce false positives or be otherwise incorrect. Please use this package with caution and thoroughly verify its findings.

This repository contains a set of PHPStan rules inspired by the popular PhpStorm plugin PhpInspections (EA Extended). Its goal is to bring the powerful static analysis capabilities of PhpInspections to the command line and your CI/CD pipeline through PHPStan.

This allows you to find potential bugs, performance issues, and code style violations automatically, ensuring a higher code quality across your projects.

Installation

To use these rules, require the package via Composer:

composer require --dev macropage/phpstan-inspections

PHPStan will automatically discover the extension via phpstan/extension-installer.

Usage

Once installed, the rules are active by default. If you need to configure it manually, you can include the rules.neon file in your project's phpstan.neon configuration:

includes:
    - vendor/macropage/phpstan-inspections/rules/rules.neon

Laravel Support

This package also includes rules specifically adapted for Laravel projects. For example, the StaticInvocationViaThisLaravelRule is optimized to work correctly with Laravel's Facades and Eloquent Models.

To use the Laravel-specific rules, include the rules.laravel.neon file in your phpstan.neon instead:

includes:
    - vendor/macropage/phpstan-inspections/rules/rules.laravel.neon

You can then run PHPStan as you normally would:

vendor/bin/phpstan analyse src tests

Origin of the Rule Logic

Each rule in this package is a direct port from a Java template found in the original PhpInspections (EA Extended) project. The corresponding Java source file for each rule is located in the dev-assets/java directory, providing a clear reference to the original logic.

Example:

  • Rule File: src/CodeStyle/AmbiguousMethodsCallsInArrayMappingRule.php
  • Ported From: dev-assets/java/codeStyle/AmbiguousMethodsCallsInArrayMappingInspector.java
  • Test Trigger File: dev-assets/triggers/AmbiguousMethodsCallsInArrayMappingRule_trigger.php

Rule Set

This package includes a wide variety of rules that check for:

  • Architecture-related issues
  • Potential bugs and logical errors
  • Performance bottlenecks
  • Code style and best practice violations
  • Security vulnerabilities

A complete list of all included rules can be found by browsing the src/ directory.

Acknowledgements

This project would not be possible without the incredible work done by Vladimir Reznichenko (@kalessil) on the original PhpInspections (EA Extended) plugin for PhpStorm. All the logic and inspiration for these rules come from that project.

Contributing

Contributions are welcome! Please feel free to submit a pull request or create an issue for any bugs or feature requests.

Testing

This project includes a test suite that validates all rules against their trigger files in a single PHPStan run (~3 seconds). See TESTING.md for full documentation.

docker run --rm -v $(pwd):/app -w /app michabbb/frankenphplaravel:8.5 \
  bash -c 'composer install --quiet 2>/dev/null; php test_rules.php'

Known Issues

PHPStan Core Bug with First-Class Callables in for Loops

There is a known bug in PHPStan's core (TypeSpecifier::specifyTypesInCondition()) that causes an internal error when analyzing for loops with First-Class Callable syntax in the condition:

// This will crash PHPStan (not this rule, but PHPStan itself)
for ($i = 0; $i < count(...); $i++) {
    // loop body
}

Error: Internal error: assert(!$this->isFirstClassCallable())

This crash occurs during PHPStan's internal type analysis before any custom rules are invoked. The ForeachInvariantsRule in this package has defensive guards to skip First-Class Callables, but the crash happens in PHPStan's core when it tries to analyze the loop condition.

Workarounds:

  • Use a variable to store the count: $count = count($array); for ($i = 0; $i < $count; $i++)
  • Use a normal function call: for ($i = 0; $i < count($array); $i++)
  • Use foreach instead of for loops

References:

License

This project is licensed under the MIT License - see the LICENSE file for details.

统计信息

  • 总下载量: 56
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 0
  • Forks: 0
  • 开发语言: Java

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固