Coder Social home page Coder Social logo

phpstan-rules's Introduction

phpstan-rules

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.

phpstan-rules's People

Contributors

maks-rafalko avatar sanhe avatar sidz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

phpstan-rules's Issues

Rename

Hello!
How about calling the package phpstan-magic-numbers?

"sidzIgnoreMagicNumbers" is not working for numeric-strings

Hi, and thanks for this phpstan addon. I use it in a legay php project and step-by-step the magic numbers will be replaced with constants that I can understand. 👍


The situation: I updated the php application from 7.4 to 8.3 and now I use vesion 0.4.3 but I see many new errors from numerc-strings. For now I will use sidzIgnoreNumericStrings: true but I think ignoring should be work also for numeric-strings.

The problem: ignoreNumber() will check the type of the config value but numeric-strings are not allowed here, I see messages like this: The item 'parameters › sidzIgnoreMagicNumbers › 2' expects to be number, '0' given. if I try to add '2' or "2" in for sidzIgnoreMagicNumbers.

Code:

    /**
     * @param LNumber|DNumber $scalar
     */
    private function ignoreNumber(Expr $scalar): bool
    {
        return in_array($scalar->value, $this->ignoreMagicNumbers, true);
    }

Possible fix: ?

    /**
     * @param LNumber|DNumber $scalar
     */
    private function ignoreNumber(Expr $scalar): bool
    {
        $ignoreMagicNumbersStrings = array_map(function($obj) {
            return (string) $obj;
        }, $this->ignoreMagicNumbers);

        return in_array($scalar->value, $this->ignoreMagicNumbers, true) || in_array($scalar->value, $ignoreMagicNumbersStrings, true);
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.