Git Product home page Git Product logo

phpmnd's Introduction

PHP Magic Number Detector (PHPMND)

Scrutinizer Code Quality License Build Status Build Status

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

What is Magic Number?

Magic number is a numeric literal that is not defined as a constant that may change at a later stage, but that can be therefore hard to update. It is a bad programming practice of using numbers directly in source code without explanation. In most cases this makes programs harder to read, understand, and maintain.

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

This 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");
         }
    }
}

It improves readability of the code and it's easier to maintain. Of course not every literal number is magic number.

    $is_even = $number % 2 === 0

Surely number 2 is not a magic number.

My rule of thumb:

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

Installation

Composer

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

$ composer require --dev povils/phpmnd

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

Globally

To install globally:

    $ composer global require povils/phpmnd

Then 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

Basic usage:

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

The --ignore-numbers option will exclude numbers from code analysis.

The --ignore-funcs option will exclude functions from code analysis when using "argument" extension.

The --exclude option will exclude a directory from code analysis (must be relative to source) (multiple values allowed)

The --exclude-path option will exclude path from code analysis (must be relative to source) (multiple values allowed)

The --exclude-file option will exclude file from code analysis (multiple values allowed)

The --suffixes comma separated option of valid source code filename extensions.

The --progress option will display progress bar.

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

The --non-zero-exit-on-violation option will return non zero exit code when there are magic numbers in your codebase.

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

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

The --extensions option lets you extend code analysis (extensions must be separated by a comma).

By default it analyses conditions, return statements and switch cases.

Choose from the list of available 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 Extension starts with minus that means it will be removed from code analysis. I would recommend clean up code using default extension before using these extensions.

Contributing

Please see CONTRIBUTING for more information.

License

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

phpmnd's People

Contributors

exussum12 avatar kubawerlos avatar padraic avatar povils avatar raphaelstolt avatar ravage84 avatar scrutinizer-auto-fixer avatar

Watchers

 avatar  avatar

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.