2017 © Pedro Peláez
 

application phpmnd

A tool to detect Magic numbers in codebase

image

povils/phpmnd

A tool to detect Magic numbers in codebase

  • Saturday, March 17, 2018
  • by povils
  • Repository
  • 15 Watchers
  • 228 Stars
  • 45,251 Installations
  • PHP
  • 43 Dependents
  • 2 Suggesters
  • 15 Forks
  • 10 Open issues
  • 8 Versions
  • 43 % Grown

The README.md

PHP Magic Number Detector (PHPMND)

Minimum PHP version: 7.4.0 Scrutinizer Code Quality License CI, (*1)

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., (*2)

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., (*3)

Consider the following hypothetical code:, (*4)

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

which should be refactored to:, (*5)

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., (*6)

Of course not every literal number is a magic number., (*7)

$is_even = $number % 2 === 0

Surely in this case the number 2 is not a magic number., (*8)

My rule of thumb:, (*9)

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:, (*10)

$ composer require --dev povils/phpmnd

Afterwards you can then invoke it using the vendor/bin/phpmnd executable., (*11)

Globally

To install it globally simply run:, (*12)

$ composer global require povils/phpmnd

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

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

Usage Example

Demo

demo, (*14)

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., (*15)

The --exclude-file option will exclude a file from the code analysis. Multiple values are allowed., (*16)

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

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)., (*18)

The --extensions option lets you extend the code analysis. The provided extensions must be comma separated., (*19)

The --hint option will suggest replacements for magic numbers based on your codebase constants., (*20)

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., (*21)

The --ignore-numbers option will exclude a list of comma separated numbers from the code analysis., (*22)

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

The --include-numeric-string option forces numeric strings such as "1234" to also be treated as a number., (*24)

The --progress option will display a progress bar., (*25)

The --strings option will include strings literal search in code analysis., (*26)

The --suffixes option will configure a comma separated list of valid source code filename extensions., (*27)

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

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., (*29)

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., (*30)

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., (*31)

eg, (*32)

$percent  = $number / 100;

would show 100 as a magic number, (*33)

$percent = $number / intval(100);

would mark 100 as not magic., (*34)

Contributing

Please see CONTRIBUTING.md for more information., (*35)

License

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

The Versions

27/04 2017

v1.0.3

1.0.3.0

A tool to detect Magic numbers in codebase

  Sources   Download

MIT

The Requires

 

The Development Requires

by Povilas Susinskas

25/04 2017

v1.0.2

1.0.2.0

A tool to detect Magic numbers in codebase

  Sources   Download

MIT

The Requires

 

The Development Requires

by Povilas Susinskas

21/04 2017

v1.0.1

1.0.1.0

A tool to detect Magic numbers in codebase

  Sources   Download

MIT

The Requires

 

The Development Requires

by Povilas Susinskas

21/04 2017

v1.0.0

1.0.0.0

A tool to detect Magic numbers in codebase

  Sources   Download

MIT

The Requires

 

The Development Requires

by Povilas Susinskas