PHPackages                             povils/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. [CLI &amp; Console](/categories/cli)
4. /
5. povils/phpmnd

ActiveApplication[CLI &amp; Console](/categories/cli)

povils/phpmnd
=============

A tool to detect Magic numbers in codebase

v3.6.1(2mo ago)5795.6M—5.1%46[12 issues](https://github.com/povils/phpmnd/issues)[9 PRs](https://github.com/povils/phpmnd/pulls)20MITPHPPHP ^7.4 || ^8.0CI passing

Since Apr 20Pushed 2mo ago11 watchersCompare

[ Source](https://github.com/povils/phpmnd)[ Packagist](https://packagist.org/packages/povils/phpmnd)[ RSS](/packages/povils-phpmnd/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (25)Used By (20)

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

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

[![Minimum PHP version: 7.4.0](https://camo.githubusercontent.com/89e54687773afb25f5943cfbd66376d73e2ce50874c54429a78aac45cb6bcf84/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372e342e302532422d626c75652e737667)](https://packagist.org/packages/povils/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)[![CI](https://github.com/povils/phpmnd/workflows/CI/badge.svg?branch=master)](https://github.com/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](./demo.gif)](./demo.gif)

#### Basic usage

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

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

71

—

ExcellentBetter than 100% of packages

Maintenance83

Actively maintained with recent releases

Popularity65

Solid adoption and visibility

Community47

Growing community involvement

Maturity78

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~153 days

Recently: every ~193 days

Total

22

Last Release

86d ago

Major Versions

v1.1.1 → v2.0.02018-03-17

v2.5.0 → v3.0.02022-07-06

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

v2.0.0PHP ^7.1

v2.4.0PHP ^7.1|^8.0

v3.0.0PHP ^7.4 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/16eb4ea08011cf5a2d441d0f9304bb17decd7dd7cc29366ec1aeb002ee7a7352?d=identicon)[povils](/maintainers/povils)

---

Top Contributors

[![povils](https://avatars.githubusercontent.com/u/11535217?v=4)](https://github.com/povils "povils (104 commits)")[![exussum12](https://avatars.githubusercontent.com/u/1102850?v=4)](https://github.com/exussum12 "exussum12 (30 commits)")[![sidz](https://avatars.githubusercontent.com/u/1302230?v=4)](https://github.com/sidz "sidz (25 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)")[![sasezaki](https://avatars.githubusercontent.com/u/42755?v=4)](https://github.com/sasezaki "sasezaki (8 commits)")[![ravage84](https://avatars.githubusercontent.com/u/625761?v=4)](https://github.com/ravage84 "ravage84 (5 commits)")[![kubawerlos](https://avatars.githubusercontent.com/u/9282069?v=4)](https://github.com/kubawerlos "kubawerlos (4 commits)")[![MarkVaughn](https://avatars.githubusercontent.com/u/39970?v=4)](https://github.com/MarkVaughn "MarkVaughn (4 commits)")[![twinh](https://avatars.githubusercontent.com/u/594436?v=4)](https://github.com/twinh "twinh (3 commits)")[![bcremer](https://avatars.githubusercontent.com/u/55820?v=4)](https://github.com/bcremer "bcremer (3 commits)")[![devinpearson](https://avatars.githubusercontent.com/u/1645428?v=4)](https://github.com/devinpearson "devinpearson (3 commits)")[![llaville](https://avatars.githubusercontent.com/u/364342?v=4)](https://github.com/llaville "llaville (3 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (2 commits)")[![jyggen](https://avatars.githubusercontent.com/u/264300?v=4)](https://github.com/jyggen "jyggen (2 commits)")[![ksowa](https://avatars.githubusercontent.com/u/1027333?v=4)](https://github.com/ksowa "ksowa (1 commits)")[![RichVRed](https://avatars.githubusercontent.com/u/1980719?v=4)](https://github.com/RichVRed "RichVRed (1 commits)")[![juliangut](https://avatars.githubusercontent.com/u/1104131?v=4)](https://github.com/juliangut "juliangut (1 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

---

Tags

analysisautomationcheckerclean-codeclidetectormagic-numbersphpstatic-analysis

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[psy/psysh

An interactive shell for modern PHP.

9.8k545.6M719](/packages/psy-psysh)[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)[infection/infection

Infection is a Mutation Testing framework for PHP. The mutation adequacy score can be used to measure the effectiveness of a test set in terms of its ability to detect faults.

2.2k26.2M1.8k](/packages/infection-infection)[drush/drush

Drush is a command line shell and scripting interface for Drupal, a veritable Swiss Army knife designed to make life easier for those of us who spend some of our working hours hacking away at the command prompt.

2.4k57.4M685](/packages/drush-drush)[humbug/php-scoper

Prefixes all PHP namespaces in a file or directory.

7963.0M35](/packages/humbug-php-scoper)[silverstripe/framework

The SilverStripe framework

7213.5M2.5k](/packages/silverstripe-framework)

PHPackages © 2026

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