All Projects → povils → Phpmnd

povils / Phpmnd

Licence: mit
PHP Magic Number Detector

Projects that are alternatives of or similar to Phpmnd

Horusec
Horusec is an open source tool that improves identification of vulnerabilities in your project with just one command.
Stars: ✭ 311 (-27.84%)
Mutual labels:  static-analysis, analysis, cli
Php codesniffer
PHP_CodeSniffer is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations. PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.
Stars: ✭ 9,004 (+1989.1%)
Mutual labels:  automation, static-analysis, cli
lints
Lint all your JavaScript, CSS, HTML, Markdown and Dockerfiles with a single command
Stars: ✭ 14 (-96.75%)
Mutual labels:  analysis, checker, static-analysis
Structured Acceptance Test
An open format definition for static analysis tools
Stars: ✭ 10 (-97.68%)
Mutual labels:  static-analysis, analysis, checker
go-mnd
Magic number detector for Go.
Stars: ✭ 153 (-64.5%)
Mutual labels:  analysis, static-analysis, detector
Fw
workspace productivity booster
Stars: ✭ 269 (-37.59%)
Mutual labels:  automation, cli
Chronos
Chronos - A static race detector for the go language
Stars: ✭ 272 (-36.89%)
Mutual labels:  static-analysis, analysis
Rascal
The implementation of the Rascal meta-programming language (including interpreter, type checker, parser generator, compiler and JVM based run-time system)
Stars: ✭ 284 (-34.11%)
Mutual labels:  static-analysis, checker
Git Delete Merged Branches
Command-line tool to delete merged Git branches
Stars: ✭ 293 (-32.02%)
Mutual labels:  automation, cli
Reviewdog
🐶 Automated code review tool integrated with any code analysis tools regardless of programming language
Stars: ✭ 4,541 (+953.6%)
Mutual labels:  static-analysis, cli
U3d
U3d is a cross-platform set of tools to interact with Unity3D from command line.
Stars: ✭ 309 (-28.31%)
Mutual labels:  automation, cli
Krane
Kubernetes RBAC static Analysis & visualisation tool
Stars: ✭ 254 (-41.07%)
Mutual labels:  static-analysis, analysis
MalScan
A Simple PE File Heuristics Scanners
Stars: ✭ 41 (-90.49%)
Mutual labels:  analysis, static-analysis
Wallace Cli
Pretty CSS analytics on the CLI
Stars: ✭ 281 (-34.8%)
Mutual labels:  analysis, cli
static file analysis
Analysis of file (doc, pdf, exe, ...) in deep (emmbedded file(s)) with clamscan and yara rules
Stars: ✭ 34 (-92.11%)
Mutual labels:  analysis, static-analysis
analysis-net
Static analysis framework for .NET programs.
Stars: ✭ 19 (-95.59%)
Mutual labels:  analysis, static-analysis
Dss
📄 Documented Style Sheets Parser
Stars: ✭ 375 (-12.99%)
Mutual labels:  static-analysis, detector
Detekt
Static code analysis for Kotlin
Stars: ✭ 4,169 (+867.29%)
Mutual labels:  analysis, static-analysis
Go Ruleguard
Define and run pattern-based custom linting rules.
Stars: ✭ 402 (-6.73%)
Mutual labels:  static-analysis, analysis
Binee
Binee: binary emulation environment
Stars: ✭ 408 (-5.34%)
Mutual labels:  static-analysis, analysis

PHP Magic Number Detector (PHPMND)

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

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].