Git Product home page Git Product logo

phpmnd's Introduction

PHP Magic Number Detector (PHPMND)

Minimum PHP version: 7.4.0 Scrutinizer Code Quality License CI

phpmnd is a tool that aims to help you to detect magic numbers in your PHP code. By default 0 and 1 are not considered to be magic numbers.

What is a magic number?

A magic number is a numeric literal that is not defined as a constant, but which may change at a later stage, and therefore can be hard to update. It's considered a bad programming practice to use numbers directly in any source code without an explanation. In most cases this makes programs harder to read, understand, and maintain.

Consider the following hypothetical code:

class Foo
{
    public function setPassword($password)
    {
         // don't do this
         if (mb_strlen($password) > 7) {
              throw new InvalidArgumentException("password");
         }
    }
}

which should be refactored to:

class Foo
{
    const MAX_PASSWORD_LENGTH = 7; // not const SEVEN = 7 :)

    public function setPassword($password)
    {
         if (mb_strlen($password) > self::MAX_PASSWORD_LENGTH) {
              throw new InvalidArgumentException("password");
         }
    }
}

This clearly improves the code readability and also reduces its maintenance cost.

Of course not every literal number is a magic number.

$is_even = $number % 2 === 0

Surely in this case the number 2 is not a magic number.

My rule of thumb:

If the number came from business specs and is used directly - it's a magic number.

Installation

Locally

You can add this tool as a local, per-project, development dependency to your project by using Composer:

$ composer require --dev povils/phpmnd

Afterwards you can then invoke it using the vendor/bin/phpmnd executable.

Globally

To install it globally simply run:

$ composer global require povils/phpmnd

Afterwards make sure you have the global Composer binaries directory in your PATH. Example for some Unix systems:

$ export PATH="$PATH:$HOME/.composer/vendor/bin"

Usage Example

Demo

demo

Basic usage

$ phpmnd wordpress --ignore-numbers=2,-1 --ignore-funcs=round,sleep --exclude=tests --progress \
--extensions=default_parameter,-return,argument

The --allow-array-mapping option allow keys as strings when using "array" extension.

The --exclude-file option will exclude a file from the code analysis. Multiple values are allowed.

The --exclude-path option will exclude a path, which must be relative to the source, from the code analysis. Multiple values are allowed.

The --exclude option will exclude a directory, which must be relative to the source, from the code analysis. Multiple values are allowed (e.g. --exclude=tests --exclude=examples).

The --extensions option lets you extend the code analysis. The provided extensions must be comma separated.

The --hint option will suggest replacements for magic numbers based on your codebase constants.

The --ignore-funcs option will exclude a list of comma separated functions from the code analysis, when using the "argument" extension. Defaults to intval, floatval, strval.

The --ignore-numbers option will exclude a list of comma separated numbers from the code analysis.

The --ignore-strings option will exclude strings from the code analysis, when using the "strings" option.

The --include-numeric-string option forces numeric strings such as "1234" to also be treated as a number.

The --progress option will display a progress bar.

The --strings option will include strings literal search in code analysis.

The --suffixes option will configure a comma separated list of valid source code filename extensions.

The --whitelist option will only process the files listed in the file specified. This is useful for incremental analysis.

The --xml-output option will generate an report in an Xml format to the path specified by the option. By default it analyses conditions, return statements, and switch cases.

Extensions

  • argument
round($number, 4);
  • array
$array = [200, 201];
  • assign
$var = 10;
  • default_parameter
function foo($default = 3);
  • operation
$bar = $foo * 20;
  • property
private $bar = 10;
  • return (default)
return 5;
  • condition (default)
$var < 7;
  • switch_case (default)
case 3;
  • all To include all extensions.

If extensions start with a minus, it means that these will be removed from the code analysis. I would recommend to clean up your code by using the default extension before using any of these extensions.

Ignoring a number from analysis

Sometimes magic numbers are required. For example implementing a known mathematical formula, by default intval, floatval and strval mark a number as not magic.

eg

$percent  = $number / 100;

would show 100 as a magic number

$percent = $number / intval(100);

would mark 100 as not magic.

Contributing

Please see CONTRIBUTING.md for more information.

License

The MIT License (MIT). Please see LICENSE for more information.

phpmnd's People

Contributors

alfredkoncsag avatar bcremer avatar chapeupreto avatar devinpearson avatar exussum12 avatar juliangut avatar jyggen avatar ksowa avatar kubawerlos avatar llaville avatar macfja avatar markvaughn avatar padraic avatar peter279k avatar povils avatar raphaelstolt avatar ravage84 avatar richardhughes avatar richvred avatar sasezaki avatar scrutinizer-auto-fixer avatar sidz avatar szepeviktor avatar twinh 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  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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phpmnd's Issues

Extensions...many words, much typing

Just a quick question on whether having an "all" extensions option would be appropriate, or is there a deliberate choice to make that slightly hard (e.g. users not understanding it's not necessarily a good idea as Step 1).

PHP 8

Root composer.json requires php ^8.0 but your php version (7.4.14) does not satisfy that requirement.

Dependency phpunit/php-timer

The dependency phpunit/php-timer is 3 major revisions behind. It currently requires ^2.3 but the latest version is v5.0.3.
I'm not sure what the changes were but I doubt it is breaking.

phpunit v9.x requires phpunit/php-timer ^5.0.2.

(e.g. your package is not compatible with Laravel/Lumen v7.x)

Support for define() in argument extension

define( 'FOO__HTTPS_PORT', 80 );

That gets identified as a magic number as part of the argument extension.

Perhaps an allowance can be made if the magic number is the 2nd argument in a define() call?

query re: compatibility with symfony/console:^5

I've a general preference for using composer require --dev rather than using global installs/phar builds.

Are there any plans to nudge up compatibility with symfony/console, i.e. symfony/console:^4.0|^5.0 ?

Release a new version

Hello. I see some nice features in the master branch, however the last release is pretty old. Are you going to release a new version soon?

Provide a PHAR

While it is nice to be able to require phpmnd through Composer, it would be even better to have it as a downloadable PHAR file.

Like:

'0' and '1' (as strings) are considered to be magic numbers

The documentation says

By default 0 and 1 are not considered to be magic numbers.

But when I use the --include-numeric-string option I get warnings for strings '0' and '1':

tests/bootstrap.php:5. Magic number: 1
  > 5| ini_set('display_errors', '1');

tests/bootstrap.php:7. Magic number: 0
  > 7| ini_set('display_startup_errors', '0');

Shouldn't '0' and '1' be considered as non-magic?

PHPMND version: b720469

Informational Website

I guess the reach and proliferation of phpmnd could benefit from having a simple but sufficient informational website. At best under a simple domain such as phpmnd.org.

Like:

This website should contain the most important documentation (or link to it, if held externally, e.g. on readthedocs) and could have some marketing material (e.g. who uses phpmnd etc).

Setup complete documentation

Especially if this tools becomes bigger and gets more features, it would benefit from having a complete documentation.
This documentation should not live in the README.md, as it would bloat it over time.

Such a documentation could live in an informational website (#17), on an external host such as Read the Docs or simply as a collection of linked Markdown files in the docs folder or a mixture of them (like Composer).

Such a documentation should also document how to extend the current library, if necessary or possible.

Symfony 4 compatibility

I have a Symfony 4 project where i like to install phpmnd locally, i get the following error:

$ composer require --dev povils/phpmnd

Using version ^1.1 for povils/phpmnd
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - povils/phpmnd v1.1.1 requires symfony/console ^2.4 || ^3.0 -> satisfiable by symfony/console[2.4.x-dev, 2.5.x-dev, 2.6.x-dev, 2.7.x-dev, 2.8.x-dev, 3.0.x-dev, 3.1.x-dev, 3.2.x-dev, 3.3.x-dev, 3.4.x-dev, v2.4.0, v2.4.0-BETA1, v2.4.0-BETA2, v2.4.0-RC1, v2.4.1, v2.4.10, v2.4.2, v2.4.3, v2.4.4, v2.4.5, v2.4.6, v2.4.7, v2.4.8, v2.4.9, v2.5.0, v2.5.0-BETA1, v2.5.0-BETA2, v2.5.0-RC1, v2.5.1, v2.5.10, v2.5.11, v2.5.12, v2.5.2, v2.5.3, v2.5.4, v2.5.5, v2.5.6, v2.5.7, v2.5.8, v2.5.9, v2.6.0, v2.6.0-BETA1, v2.6.0-BETA2, v2.6.1, v2.6.10, v2.6.11, v2.6.12, v2.6.13, v2.6.2, v2.6.3, v2.6.4, v2.6.5, v2.6.6, v2.6.7, v2.6.8, v2.6.9, v2.7.0, v2.7.0-BETA1, v2.7.0-BETA2, v2.7.1, v2.7.10, v2.7.11, v2.7.12, v2.7.13, v2.7.14, v2.7.15, v2.7.16, v2.7.17, v2.7.18, v2.7.19, v2.7.2, v2.7.20, v2.7.21, v2.7.22, v2.7.23, v2.7.24, v2.7.25, v2.7.26, v2.7.27, v2.7.28, v2.7.29, v2.7.3, v2.7.30, v2.7.31, v2.7.32, v2.7.33, v2.7.34, v2.7.35, v2.7.36, v2.7.37, v2.7.38, v2.7.39, v2.7.4, v2.7.40, v2.7.5, v2.7.6, v2.7.7, v2.7.8, v2.7.9, v2.8.0, v2.8.0-BETA1, v2.8.1, v2.8.10, v2.8.11, v2.8.12, v2.8.13, v2.8.14, v2.8.15, v2.8.16, v2.8.17, v2.8.18, v2.8.19, v2.8.2, v2.8.20, v2.8.21, v2.8.22, v2.8.23, v2.8.24, v2.8.25, v2.8.26, v2.8.27, v2.8.28, v2.8.29, v2.8.3, v2.8.30, v2.8.31, v2.8.32, v2.8.33, v2.8.4, v2.8.5, v2.8.6, v2.8.7, v2.8.8, v2.8.9, v3.0.0, v3.0.0-BETA1, v3.0.1, v3.0.2, v3.0.3, v3.0.4, v3.0.5, v3.0.6, v3.0.7, v3.0.8, v3.0.9, v3.1.0, v3.1.0-BETA1, v3.1.0-RC1, v3.1.1, v3.1.10, v3.1.2, v3.1.3, v3.1.4, v3.1.5, v3.1.6, v3.1.7, v3.1.8, v3.1.9, v3.2.0, v3.2.0-BETA1, v3.2.0-RC1, v3.2.0-RC2, v3.2.1, v3.2.10, v3.2.11, v3.2.12, v3.2.13, v3.2.14, v3.2.2, v3.2.3, v3.2.4, v3.2.5, v3.2.6, v3.2.7, v3.2.8, v3.2.9, v3.3.0, v3.3.0-BETA1, v3.3.0-RC1, v3.3.1, v3.3.10, v3.3.11, v3.3.12, v3.3.13, v3.3.14, v3.3.15, v3.3.2, v3.3.3, v3.3.4, v3.3.5, v3.3.6, v3.3.7, v3.3.8, v3.3.9, v3.4.0, v3.4.0-BETA1, v3.4.0-BETA2, v3.4.0-BETA3, v3.4.0-BETA4, v3.4.0-RC1, v3.4.0-RC2, v3.4.1, v3.4.2, v3.4.3] but these conflict with your requirements or minimum-stability.
    - povils/phpmnd v1.1.0 requires symfony/console ^2.4 || ^3.0 -> satisfiable by symfony/console[2.4.x-dev, 2.5.x-dev, 2.6.x-dev, 2.7.x-dev, 2.8.x-dev, 3.0.x-dev, 3.1.x-dev, 3.2.x-dev, 3.3.x-dev, 3.4.x-dev, v2.4.0, v2.4.0-BETA1, v2.4.0-BETA2, v2.4.0-RC1, v2.4.1, v2.4.10, v2.4.2, v2.4.3, v2.4.4, v2.4.5, v2.4.6, v2.4.7, v2.4.8, v2.4.9, v2.5.0, v2.5.0-BETA1, v2.5.0-BETA2, v2.5.0-RC1, v2.5.1, v2.5.10, v2.5.11, v2.5.12, v2.5.2, v2.5.3, v2.5.4, v2.5.5, v2.5.6, v2.5.7, v2.5.8, v2.5.9, v2.6.0, v2.6.0-BETA1, v2.6.0-BETA2, v2.6.1, v2.6.10, v2.6.11, v2.6.12, v2.6.13, v2.6.2, v2.6.3, v2.6.4, v2.6.5, v2.6.6, v2.6.7, v2.6.8, v2.6.9, v2.7.0, v2.7.0-BETA1, v2.7.0-BETA2, v2.7.1, v2.7.10, v2.7.11, v2.7.12, v2.7.13, v2.7.14, v2.7.15, v2.7.16, v2.7.17, v2.7.18, v2.7.19, v2.7.2, v2.7.20, v2.7.21, v2.7.22, v2.7.23, v2.7.24, v2.7.25, v2.7.26, v2.7.27, v2.7.28, v2.7.29, v2.7.3, v2.7.30, v2.7.31, v2.7.32, v2.7.33, v2.7.34, v2.7.35, v2.7.36, v2.7.37, v2.7.38, v2.7.39, v2.7.4, v2.7.40, v2.7.5, v2.7.6, v2.7.7, v2.7.8, v2.7.9, v2.8.0, v2.8.0-BETA1, v2.8.1, v2.8.10, v2.8.11, v2.8.12, v2.8.13, v2.8.14, v2.8.15, v2.8.16, v2.8.17, v2.8.18, v2.8.19, v2.8.2, v2.8.20, v2.8.21, v2.8.22, v2.8.23, v2.8.24, v2.8.25, v2.8.26, v2.8.27, v2.8.28, v2.8.29, v2.8.3, v2.8.30, v2.8.31, v2.8.32, v2.8.33, v2.8.4, v2.8.5, v2.8.6, v2.8.7, v2.8.8, v2.8.9, v3.0.0, v3.0.0-BETA1, v3.0.1, v3.0.2, v3.0.3, v3.0.4, v3.0.5, v3.0.6, v3.0.7, v3.0.8, v3.0.9, v3.1.0, v3.1.0-BETA1, v3.1.0-RC1, v3.1.1, v3.1.10, v3.1.2, v3.1.3, v3.1.4, v3.1.5, v3.1.6, v3.1.7, v3.1.8, v3.1.9, v3.2.0, v3.2.0-BETA1, v3.2.0-RC1, v3.2.0-RC2, v3.2.1, v3.2.10, v3.2.11, v3.2.12, v3.2.13, v3.2.14, v3.2.2, v3.2.3, v3.2.4, v3.2.5, v3.2.6, v3.2.7, v3.2.8, v3.2.9, v3.3.0, v3.3.0-BETA1, v3.3.0-RC1, v3.3.1, v3.3.10, v3.3.11, v3.3.12, v3.3.13, v3.3.14, v3.3.15, v3.3.2, v3.3.3, v3.3.4, v3.3.5, v3.3.6, v3.3.7, v3.3.8, v3.3.9, v3.4.0, v3.4.0-BETA1, v3.4.0-BETA2, v3.4.0-BETA3, v3.4.0-BETA4, v3.4.0-RC1, v3.4.0-RC2, v3.4.1, v3.4.2, v3.4.3] but these conflict with your requirements or minimum-stability.
    - Installation request for povils/phpmnd ^1.1 -> satisfiable by povils/phpmnd[v1.1.0, v1.1.1].

I ran composer update but it changed nothing.

PHPUnit 7 wrong dependencies

Get the following message when trying to install PHPUnit 7 with this package.

Problem 1
    - Installation request for povils/phpmnd ^1.1 -> satisfiable by povils/phpmnd[1.1.x-dev, v1.1.0, v1.1.1].
    - Conclusion: don't install phpunit/php-timer 2.0.0
    - phpunit/phpunit 7.0.0 requires phpunit/php-timer ^2.0 -> satisfiable by phpunit/php-timer[2.0.0, 2.0.x-dev].
    - phpunit/phpunit 7.1.x-dev requires phpunit/php-timer ^2.0 -> satisfiable by phpunit/php-timer[2.0.0, 2.0.x-dev].
    - phpunit/phpunit 7.0.x-dev requires phpunit/php-timer ^2.0 -> satisfiable by phpunit/php-timer[2.0.0, 2.0.x-dev].
    - Conclusion: don't install phpunit/php-timer 2.0.x-dev
    - Installation request for phpunit/phpunit ^7 -> satisfiable by phpunit/phpunit[7.0.0, 7.0.x-dev, 7.1.x-dev].

Could we update the dependency to php-timer ^2.0? I'll submit a PR for this later tonight

Whitelist functions

Create an option ignore-func (comma separted) which will ignore functions in code analysis when using 'argument' extension.
Example:

phpmnd code --ignore-func=round,usleep,sleep --extensions=argument

Provide File Extension CLI parameter

It would be nice to be able to define which files get checked by providing a comma separated list of file extensions.

That could look like:

$ phpmnd cakephp-app --file-extensions=php,php5,ctp,bla.foo

The above would check files with the following file extension:

  • php
  • php5
  • ctp
  • bla.foo

This is relevant for every PHP project that has more than just the typical .php files.
For example, CakePHP has template files with the .ctp extension.

Syntax error when using class called `Match` in the code

Example file:

<?php
declare(strict_types=1);

namespace Test;

class Match
{
    public function compareTo(Match $other)
    {
        // do something
    }
}

Call:

phpmnd test.php

Expected result (more or less):

phpmnd 2.3.0 by Povilas Susinskas
--------------------------------------------------------------------------------
Total of Magic Numbers: 0
Time: 54 ms, Memory: 12.00 MB

Insted it returns:

phpmnd 2.3.0 by Povilas Susinskas
Syntax error, unexpected T_MATCH, expecting T_STRING on line 6
--------------------------------------------------------------------------------
Total of Magic Numbers: 0
Time: 65 ms, Memory: 12.00 MB

More info:

# php -v
PHP 7.4.11 (cli) (built: Oct 13 2020 10:09:45) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.11, Copyright (c), by Zend Technologies
    with Xdebug v2.9.8, Copyright (c) 2002-2020, by Derick Rethans
    with blackfire v1.42.0~linux-x64-non_zts74, https://blackfire.io, by Blackfire

CLI Usage GIF

It would be nice to have a GIF of a usage example.

Like those.

Such a GIF could be added to the info website from #17.

Exit with an evaluable return code

Currently a command run with n magic numbers found evaluates to 0 via echo $?. This should prolly should return a non zero exit code for usage in CI tooling.

Support configuration via configuration file

Since the number of options keeps growing it would be considerable to allow their configuration via a dedicated configuration file. Whether this happens via a programmtic approach, like done by the PHP Coding Standards Fixer or via a configuration format of choice e.g. yaml, json or ini is up to you.

Notice: Undefined property: PhpParser\Node\Expr\PropertyFetch::$value

povils/phpmnd (dev-master 0471854)

$phpmnd app

phpmnd 2.1.0 by Povilas Susinskas
PHP Notice: Undefined property: PhpParser\Node\Expr\PropertyFetch::$value in /Users/xxx/xxx/vendor/povils/phpmnd/src/Visitor/DetectorVisitor.php on line 55
PHP Stack trace:
PHP 1. {main}() /Users/xxx/xxx/vendor/povils/phpmnd/bin/phpmnd:0
PHP 2. Povils\PHPMND\Console\Application->run() /Users/xxx/xxx/vendor/povils/phpmnd/bin/phpmnd:40
PHP 3. Povils\PHPMND\Console\Application->doRun() /Users/xxx/xxx/vendor/symfony/console/Application.php:145
PHP 4. Povils\PHPMND\Console\Application->doRun() /Users/xxx/xxx/vendor/povils/phpmnd/src/Console/Application.php:66
PHP 5. Povils\PHPMND\Console\Application->doRunCommand() /Users/xxx/xxx/vendor/symfony/console/Application.php:262
PHP 6. Povils\PHPMND\Console\Command->run() /Users/xxx/xxx/vendor/symfony/console/Application.php:901
PHP 7. Povils\PHPMND\Console\Command->execute() /Users/xxx/xxx/vendor/symfony/console/Command/Command.php:255
PHP 8. Povils\PHPMND\Detector->detect() /Users/xxx/xxx/vendor/povils/phpmnd/src/Console/Command.php:172
PHP 9. PhpParser\NodeTraverser->traverse() /Users/xxx/xxx/vendor/povils/phpmnd/src/Detector.php:51
PHP 10. PhpParser\NodeTraverser->traverseArray() /Users/xxx/xxx/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:91
PHP 11. PhpParser\NodeTraverser->traverseNode() /Users/xxx/xxx/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:223
PHP 12. PhpParser\NodeTraverser->traverseArray() /Users/xxx/xxx/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:114
PHP 13. PhpParser\NodeTraverser->traverseNode() /Users/xxx/xxx/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:223
PHP 14. PhpParser\NodeTraverser->traverseArray() /Users/xxx/xxx/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:114
PHP 15. PhpParser\NodeTraverser->traverseNode() /Users/xxx/xxx/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:223
PHP 16. PhpParser\NodeTraverser->traverseArray() /Users/xxx/xxx/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:114
PHP 17. PhpParser\NodeTraverser->traverseNode() /Users/xxx/xxx/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:223
PHP 18. PhpParser\NodeTraverser->traverseNode() /Users/xxx/xxx/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:146
PHP 19. PhpParser\NodeTraverser->traverseNode() /Users/xxx/xxx/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:146
PHP 20. PhpParser\NodeTraverser->traverseArray() /Users/xxx/xxx/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:114
PHP 21. PhpParser\NodeTraverser->traverseNode() /Users/xxx/xxx/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:223
PHP 22. PhpParser\NodeTraverser->traverseNode() /Users/xxx/xxx/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:146
PHP 23. PhpParser\NodeTraverser->traverseNode() /Users/xxx/xxx/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:146
PHP 24. Povils\PHPMND\Visitor\DetectorVisitor->enterNode() /Users/xxx/xxx/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php:123

The rest of the checks work fine and gives the normal output
Total of Magic Numbers: 10
Time: 1.54 seconds, Memory: 124.00 MB

Extension support for Exceptions possible?

Hi, at first: I'm completely new to this package and I'm pleased about the possibilities! :-)

Currently I have this piece of code:

            throw new InvalidArgumentException(
                'Exception message',
                1583854980
            );

This position is found from phpmnd and apparently no extension is active here, tested with setting "all".

Is it possible and useful to add an extension for this? Or how would you handle this case?

Support ignore annotations

Hey,

I'm currently running mnd on a larger codebase (~1k classes) and noticed that the tools reports some warnings that I would not consider a problem in the specific context. This clutters the output and prevents a 0 exit code.

It would be great if we could introduce something like
https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#ignoring-parts-of-a-file
to mark certain exceptions as "okay".

Currently, I'd have to ignore the whole file via `--exclude-file=.." - which I'd like to avoid.

PS: This frequently happens on the null coalesce operator for setting default values. E.g.

function foo($arg = null){
   $arg = $arg ?? 1000;
}

Having that operator "removed" from the "conditions" extension (per config?) would also help a lot ;)

Any thoughts?

Version noted in master

Console\Application version reads 1.1.0, current release is 1.1.1. Not anything critical, obviously ๐Ÿ˜€ . Just noticed it merging master into the phar self-update work (where it would impact assuming the updater stuff is merged).

New release needed !

Hi everybody,

Thanks for this great work.

I wondering if it would be possible to release a new minor version that embeds this commit 310eb6c. Actually we are forced to pull the dev-master on our Satis and it could be great to create a release instead ;-)

Thanks per advance,

Possible constants hinting

I thought about implementing of showing possible constants for magic numbers and strings. I will not mention strings below, as I think the principles for numbers and strings are similar. Below I will describe possible interface implementation and some thougts about probability of constants hinting.
Below is the example of an interface implementation for console output:

--------------------------------------------------------------------------------

src/SomeTestFile.php:34. Magic number: 1121
Possible constants:
	SomeTestFile::SOME_NUMBER
  > 34|         if (($x == 1121) || ($x == 2351)) {

src/SomeTestFile.php:34. Magic number: 2351
Possible constants:
	SomeTestFile::SOME_OTHER_NUMBER
  > 34|         if (($x == 1121) || ($x == 2351)) {

--------------------------------------------------------------------------------

Constant hinting:
I was thinking about this idea for some time, and found out that all the use-cases of magic numbers can be divided into two big groups:

  • There are constants for some of the magic numbers, but we don't use them for some reason(e.g. we don't know about their existence or we refactor legacy code after constants appearance)
  • There are no constants in our project at all, so we use magic numbers
    First group can be covered easily by indexing all project constants(and, maybe, PHP constants) and comparing each new found constant with indexed list. All matching constants will be shown as possible constants.
    I didn't find good implementation ideas for the second group as we cannot predict what meaning a number has and whether two equal numbers have equal meaning or not.

This description does not covers some interesting topics like constant order in hints, number of constants shown in hints etc. But I think they can be specified later.

Negative numbers

Good day, I get an issue for such case. Package doesn't recognize this like a magic number

$limit = Carbon::now()->addYears(-123123);

Interesting that with the second example works fine

$limit = Carbon::now()->addYears(123123);

As I understand it's bug or I'm wrong

Move to phpmnd namespace

If you plan to support & maintain this project over many years, it could be beneficial to move this tool away from your personal namespace/GitHub user to a phpmnd specific namespace/GitHub organisation.

This could also lower the barriers for others to co-maintain it (if that's an option anyway).

Add PHP-Parser 4 support

PHP-Parser 4.0.0 has been released 17 days ago.

When I'm trying to use PHP Magic Number Detector and PHP-Parser 4 together, I run into error:

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - povils/phpmnd dev-master requires nikic/php-parser ^3.0 -> satisfiable by nikic/php-parser[3.x-dev, v3.0.0, v3.0.0alpha1, v3.0.0beta1, v3.0.0beta2, v3.0.1, v3.0.2, v3.0.3, v3.0.4, v3.0.5, v3.0.6, v3.1.0, v3.1.1, v3.1.2, v3.1.3, v3.1.4, v3.1.5] but these conflict with your requirements or minimum-stability.
    - povils/phpmnd dev-master requires nikic/php-parser ^3.0 -> satisfiable by nikic/php-parser[3.x-dev, v3.0.0, v3.0.0alpha1, v3.0.0beta1, v3.0.0beta2, v3.0.1, v3.0.2, v3.0.3, v3.0.4, v3.0.5, v3.0.6, v3.1.0, v3.1.1, v3.1.2, v3.1.3, v3.1.4, v3.1.5] but these conflict with your requirements or minimum-stability.
    - Installation request for povils/phpmnd dev-master -> satisfiable by povils/phpmnd[dev-master].

Filter out checks for Test files

It would be nice to have a way of passing an argument to the command which allows you to filter out file names with a given string.

For example: My tests are always in the format of ****Test.php and if we could filter out these from the checks it would be good.

I don't think the exclude directory argument would be suitable due to the location of all our test files (Tests are alongside the code).

I'm happy to take a look at this.

Cheers

Exclude specific files

Make an option --exclude-file (multiple) which will exclude specific files from code analysis.

Please update to support newer versions of the symfony components that are requirements

When trying to upgrade illuminate/database to ^7.18 povils/phpmnd requires an older version of symfony/console ^4.0. It looks like I need to remove povils/phpmnd to use a newer version of illuminate/database.

  Problem 1
    - povils/phpmnd v2.2.0 requires symfony/console ^4.0 -> satisfiable by symfony/console[4.0.x-dev, 4.1.x-dev, 4.2.x-dev, 4.3.x-dev, 4.4.x-dev, v4.0.0, v4.0.0-BETA1, v4.0.0-BETA2, v4.0.0-BETA3, v4.0.0-BETA4, v4.0.0-RC1, v4.0.0-RC2, v4.0.1, v4.0.10, v4.0.11, v4.0.12, v4.0.13, v4.0.14, v4.0.15, v4.0.2, v4.0.3, v4.0.4, v4.0.5, v4.0.6, v4.0.7, v4.0.8, v4.0.9, v4.1.0, v4.1.0-BETA1, v4.1.0-BETA2, v4.1.0-BETA3, v4.1.1, v4.1.10, v4.1.11, v4.1.12, v4.1.2, v4.1.3, v4.1.4, v4.1.5, v4.1.6, v4.1.7, v4.1.8, v4.1.9, v4.2.0, v4.2.0-BETA1, v4.2.0-BETA2, v4.2.0-RC1, v4.2.1, v4.2.10, v4.2.11, v4.2.12, v4.2.2, v4.2.3, v4.2.4, v4.2.5, v4.2.6, v4.2.7, v4.2.8, v4.2.9, v4.3.0, v4.3.0-BETA1, v4.3.0-BETA2, v4.3.0-RC1, v4.3.1, v4.3.10, v4.3.11, v4.3.2, v4.3.3, v4.3.4, v4.3.5, v4.3.6, v4.3.7, v4.3.8, v4.3.9, v4.4.0, v4.4.0-BETA1, v4.4.0-BETA2, v4.4.0-RC1, v4.4.1, v4.4.10, v4.4.2, v4.4.3, v4.4.4, v4.4.5, v4.4.6, v4.4.7, v4.4.8, v4.4.9] but these conflict with your requirements or minimum-stability.
    - povils/phpmnd v2.2.0 requires symfony/console ^4.0 -> satisfiable by symfony/console[4.0.x-dev, 4.1.x-dev, 4.2.x-dev, 4.3.x-dev, 4.4.x-dev, v4.0.0, v4.0.0-BETA1, v4.0.0-BETA2, v4.0.0-BETA3, v4.0.0-BETA4, v4.0.0-RC1, v4.0.0-RC2, v4.0.1, v4.0.10, v4.0.11, v4.0.12, v4.0.13, v4.0.14, v4.0.15, v4.0.2, v4.0.3, v4.0.4, v4.0.5, v4.0.6, v4.0.7, v4.0.8, v4.0.9, v4.1.0, v4.1.0-BETA1, v4.1.0-BETA2, v4.1.0-BETA3, v4.1.1, v4.1.10, v4.1.11, v4.1.12, v4.1.2, v4.1.3, v4.1.4, v4.1.5, v4.1.6, v4.1.7, v4.1.8, v4.1.9, v4.2.0, v4.2.0-BETA1, v4.2.0-BETA2, v4.2.0-RC1, v4.2.1, v4.2.10, v4.2.11, v4.2.12, v4.2.2, v4.2.3, v4.2.4, v4.2.5, v4.2.6, v4.2.7, v4.2.8, v4.2.9, v4.3.0, v4.3.0-BETA1, v4.3.0-BETA2, v4.3.0-RC1, v4.3.1, v4.3.10, v4.3.11, v4.3.2, v4.3.3, v4.3.4, v4.3.5, v4.3.6, v4.3.7, v4.3.8, v4.3.9, v4.4.0, v4.4.0-BETA1, v4.4.0-BETA2, v4.4.0-RC1, v4.4.1, v4.4.10, v4.4.2, v4.4.3, v4.4.4, v4.4.5, v4.4.6, v4.4.7, v4.4.8, v4.4.9] but these conflict with your requirements or minimum-stability.
    - povils/phpmnd v2.2.0 requires symfony/console ^4.0 -> satisfiable by symfony/console[4.0.x-dev, 4.1.x-dev, 4.2.x-dev, 4.3.x-dev, 4.4.x-dev, v4.0.0, v4.0.0-BETA1, v4.0.0-BETA2, v4.0.0-BETA3, v4.0.0-BETA4, v4.0.0-RC1, v4.0.0-RC2, v4.0.1, v4.0.10, v4.0.11, v4.0.12, v4.0.13, v4.0.14, v4.0.15, v4.0.2, v4.0.3, v4.0.4, v4.0.5, v4.0.6, v4.0.7, v4.0.8, v4.0.9, v4.1.0, v4.1.0-BETA1, v4.1.0-BETA2, v4.1.0-BETA3, v4.1.1, v4.1.10, v4.1.11, v4.1.12, v4.1.2, v4.1.3, v4.1.4, v4.1.5, v4.1.6, v4.1.7, v4.1.8, v4.1.9, v4.2.0, v4.2.0-BETA1, v4.2.0-BETA2, v4.2.0-RC1, v4.2.1, v4.2.10, v4.2.11, v4.2.12, v4.2.2, v4.2.3, v4.2.4, v4.2.5, v4.2.6, v4.2.7, v4.2.8, v4.2.9, v4.3.0, v4.3.0-BETA1, v4.3.0-BETA2, v4.3.0-RC1, v4.3.1, v4.3.10, v4.3.11, v4.3.2, v4.3.3, v4.3.4, v4.3.5, v4.3.6, v4.3.7, v4.3.8, v4.3.9, v4.4.0, v4.4.0-BETA1, v4.4.0-BETA2, v4.4.0-RC1, v4.4.1, v4.4.10, v4.4.2, v4.4.3, v4.4.4, v4.4.5, v4.4.6, v4.4.7, v4.4.8, v4.4.9] but these conflict with your requirements or minimum-stability.
    - Installation request for povils/phpmnd ^2.2 -> satisfiable by povils/phpmnd[v2.2.0].

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.