PHPackages                             miaoxing/phpmnd - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. miaoxing/phpmnd

ActiveApplication[Utility &amp; Helpers](/categories/utility)

miaoxing/phpmnd
===============

A tool to detect Magic numbers in codebase

v2.3.2(5y ago)0813MITPHPPHP ^7.1

Since Apr 20Pushed 5y agoCompare

[ Source](https://github.com/miaoxing/phpmnd)[ Packagist](https://packagist.org/packages/miaoxing/phpmnd)[ RSS](/packages/miaoxing-phpmnd/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (7)Versions (13)Used By (0)

PHP Magic Number Detector (PHPMND)
==================================

[](#php-magic-number-detector-phpmnd)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/11e7d931ff8ec931b6eb304d5c193194f1e63f450afd5719bc3a8e8ea4fe2953/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f706f76696c732f7068706d6e642f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/povils/phpmnd/?branch=master)[![License](https://camo.githubusercontent.com/022561372ebf7015b8d57c2ce62065922e97c0eee211a8be12a2085fa1aa63f1/68747470733a2f2f706f7365722e707567782e6f72672f706f76696c732f7068706d6e642f6c6963656e7365)](https://packagist.org/packages/povils/phpmnd)[![Build Status](https://camo.githubusercontent.com/a17b732a81d51dfa3871cfcff3842e24c7e58627d395697d96c1096c26d5929d/68747470733a2f2f7472617669732d63692e6f72672f706f76696c732f7068706d6e642e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/povils/phpmnd)[![Build Status](https://camo.githubusercontent.com/6d10b7b36e00038e921cd62d4d39717f3bd7c4b6ed27928ba079cd08155d11bf/68747470733a2f2f63692e6170707665796f722e636f6d2f6170692f70726f6a656374732f7374617475732f6769746875622f706f76696c732f7068706d6e643f7376673d74727565)](https://ci.appveyor.com/project/povils/phpmnd)

`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?
-----------------------

[](#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
------------

[](#installation)

### Locally

[](#locally)

You can add this tool as a local, per-project, development dependency to your project by using [Composer](https://getcomposer.org/):

```
$ composer require --dev povils/phpmnd
```

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

### Globally

[](#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
-------------

[](#usage-example)

Demo:
[![demo](./demo.gif)](./demo.gif)

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 `--non-zero-exit-on-violation` option will return a non zero exit code, when there are any magic numbers in your codebase.

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.**

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 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
-------------------------------

[](#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
------------

[](#contributing)

Please see [CONTRIBUTING.md](CONTRIBUTING.md) for more information.

License
-------

[](#license)

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

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 54.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~110 days

Recently: every ~140 days

Total

12

Last Release

2101d ago

Major Versions

v1.1.1 → v2.0.02018-03-17

PHP version history (2 changes)v1.0.0PHP ^5.6|^7.0

v2.0.0PHP ^7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/8a09e00cb028f695bc3e5a4319c26b3955f546c4d7a0aa7cab553e4de1f3efab?d=identicon)[twin](/maintainers/twin)

---

Top Contributors

[![povils](https://avatars.githubusercontent.com/u/11535217?v=4)](https://github.com/povils "povils (89 commits)")[![exussum12](https://avatars.githubusercontent.com/u/1102850?v=4)](https://github.com/exussum12 "exussum12 (15 commits)")[![raphaelstolt](https://avatars.githubusercontent.com/u/48225?v=4)](https://github.com/raphaelstolt "raphaelstolt (12 commits)")[![padraic](https://avatars.githubusercontent.com/u/19780?v=4)](https://github.com/padraic "padraic (11 commits)")[![richardhughes](https://avatars.githubusercontent.com/u/4634220?v=4)](https://github.com/richardhughes "richardhughes (9 commits)")[![ravage84](https://avatars.githubusercontent.com/u/625761?v=4)](https://github.com/ravage84 "ravage84 (5 commits)")[![twinh](https://avatars.githubusercontent.com/u/594436?v=4)](https://github.com/twinh "twinh (4 commits)")[![MarkVaughn](https://avatars.githubusercontent.com/u/39970?v=4)](https://github.com/MarkVaughn "MarkVaughn (4 commits)")[![kubawerlos](https://avatars.githubusercontent.com/u/9282069?v=4)](https://github.com/kubawerlos "kubawerlos (3 commits)")[![devinpearson](https://avatars.githubusercontent.com/u/1645428?v=4)](https://github.com/devinpearson "devinpearson (3 commits)")[![bcremer](https://avatars.githubusercontent.com/u/55820?v=4)](https://github.com/bcremer "bcremer (3 commits)")[![jyggen](https://avatars.githubusercontent.com/u/264300?v=4)](https://github.com/jyggen "jyggen (2 commits)")[![chapeupreto](https://avatars.githubusercontent.com/u/834048?v=4)](https://github.com/chapeupreto "chapeupreto (1 commits)")[![MacFJA](https://avatars.githubusercontent.com/u/1475671?v=4)](https://github.com/MacFJA "MacFJA (1 commits)")[![RichVRed](https://avatars.githubusercontent.com/u/1980719?v=4)](https://github.com/RichVRed "RichVRed (1 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/miaoxing-phpmnd/health.svg)

```
[![Health](https://phpackages.com/badges/miaoxing-phpmnd/health.svg)](https://phpackages.com/packages/miaoxing-phpmnd)
```

###  Alternatives

[symfony/maker-bundle

Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.

3.4k111.1M568](/packages/symfony-maker-bundle)[coenjacobs/mozart

Composes all dependencies as a package inside a WordPress plugin

4723.6M20](/packages/coenjacobs-mozart)[symplify/monorepo-builder

Not only Composer tools to build a Monorepo.

5205.3M82](/packages/symplify-monorepo-builder)[roave/backward-compatibility-check

Tool to compare two revisions of a public API to check for BC breaks

5953.3M56](/packages/roave-backward-compatibility-check)[psalm/plugin-laravel

Psalm plugin for Laravel

3274.9M308](/packages/psalm-plugin-laravel)[friendsoftypo3/content-blocks

TYPO3 CMS Content Blocks - Content Types API | Define reusable components via YAML

96374.6k23](/packages/friendsoftypo3-content-blocks)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
