承接 sidz/phpstan-rules 相关项目开发

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

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

sidz/phpstan-rules

Composer 安装命令:

composer require sidz/phpstan-rules

包简介

Set of PHPStan rules

README 文档

README

Continuous Integration

Provides additional rules for phpstan/phpstan.

Installation

Run

composer require --dev sidz/phpstan-rules

If you use PHPStan extension installer, you're all set. If not, you need to manually register all the rules in your phpstan.neon:

includes:
    - vendor/sidz/phpstan-rules/rules.neon

Each rule by default ignores the following numbers: 0 and 1. This can be configured by adding the following parameter to your phpstan.neon:

parameters:
	sidzIgnoreMagicNumbers: [0, 1, 100]

Each rule by default detects numeric strings like '12' in source code. This behavior could be disabled via parameter:

parameters:
	sidzIgnoreNumericStrings: true

Ignoring particular rules

If you need to ignore particular rule, for example NoMagicNumberInComparisonOperatorRule, you can do so by using built-in ignoreErrors parameter:

parameters:
    ignoreErrors:
        - '#^Do not use magic number in comparison operations\. Move to constant with a suitable name\.$#'

If you need to ignore this rule only for particular file or folder, this also can be done by using ignoreErrors parameter:

parameters:
    ignoreErrors:
        -
            message: '#^Do not use magic number in comparison operations\. Move to constant with a suitable name\.$#'
            path: src/SomeFolder/*

And finally, if you want to ignore all the rules from this package for particular files or folders, add this to phpstan.neon:

parameters:
    ignoreErrors:
        -
            message: '#Do not (use|return|assign) magic number (.)#'
            paths:
                - src/DataFixtures/*
                - tests/*

Rules

This package provides the following rules for use with phpstan/phpstan:

Classes

MagicNumber\NoMagicNumberAsFunctionArgumentRule

This rule reports an error when magic number is used as function argument:

<?php

some_function(10);

MagicNumber\NoMagicNumberAssignedToPropertyRule

This rule reports an error when magic number is assigned to class property:

<?php

class Test
{
    private $prop1 = 10;

    private $prop2 = -5.5;
}

MagicNumber\NoMagicNumberInArithmeticOperatorRule

This rule reports an error when magic number is used in various arithmetic operators:

<?php

$var1 + 2;

$var2 - .3;

$var3 * 2.2;

$var4 / 2;

$var5 % 1000;

$var6 ** 2;

2 + $var1;

1.1 - $var2;

2 * $var3;

-2 / $var4;

1000 % $var5;

MagicNumber\NoMagicNumberInBitwiseOperatorRule

This rule reports an error when magic number is used in various bitwise operators:

<?php

$a & 1;

$b | 2;

$c ^ 3;

$a << 4;

$b >> 5;

1 & $a;

2 | $b;

3 ^ $c;

4 << $a;

5 >> $b;

6 >> 7;

MagicNumber\NoMagicNumberInComparisonOperatorRule

This rule reports an error when magic number is used in comparison operator:

<?php

$var1 === 1;

$var2 !== 2;

$var3 !== 3;

$var4 === 4.4;

$var5 !== -5;

$var6 < 6;

$var7 <= 7;

$var8 > .8;

$var9 >= 9;

$var10 <=> 0.1;

$var11 === 11;

MagicNumber\NoMagicNumberInDefaultParameterRule

This rule reports an error when magic number is used as default parameter:

<?php

class Test
{
    public function testMethod($param = 3): string
    {
        return 'string';
    }
}

MagicNumber\NoMagicNumberInLogicalOperatorRule

This rule reports an error when magic number is used as part of logical operation:

<?php

$a and 1;

1 and $a;

$b or 2;

2 or $b;

$c xor 3;

3 xor $c;

MagicNumber\NoMagicNumberInMatchRule

This rule reports an error when magic number is used in arms or conditions:

<?php

match (3) {
    1 => 'Hi',
    2, 4 => 'There',
    default => throw new LogicException(),
};

MagicNumber\NoMagicNumberInReturnStatementRule

This rule reports an error when magic number is used in return statement:

<?php

class Test
{

    public function getNegativeValue(): float
    {
        return -20.5;
    }
}

MagicNumber\NoMagicNumberInSwitchCaseRule

This rule reports an error when magic number is used in condition or cases:

<?php

switch (100) {
    case 5:
        break;
}

MagicNumber\NoMagicNumberInTernaryOperatorRule

This rule reports an error when magic number is used in ternary operator:

<?php

$a = $b ? 2 : 'string';

$c = $b ?: -3.5;

$d = $b ? 'string' : 6;

MagicNumber\NoMagicNumberVariableAssignmentRule

This rule reports an error when magic number is assigned to some variable:

<?php

$var1 = 4;

$var2 = -2;

function test_func($var4 = 3): void
{
    $var5 = 0;
}

$var6 = .1;

$var7 = 3.5;

$var8 = -2.3;

License

This package is licensed using the MIT License.

Please have a look at LICENSE.md.

sidz/phpstan-rules 适用场景与选型建议

sidz/phpstan-rules 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 521.44k 次下载、GitHub Stars 达 31, 最近一次更新时间为 2023 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「PHPStan」 「phpstan-rules」 「phpstan-extreme-rules」 「phpstan-magic-numbers」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 sidz/phpstan-rules 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sidz/phpstan-rules 我们能提供哪些服务?
定制开发 / 二次开发

基于 sidz/phpstan-rules 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 521.44k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 31
  • 点击次数: 15
  • 依赖项目数: 12
  • 推荐数: 0

GitHub 信息

  • Stars: 31
  • Watchers: 3
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-10