A static php code analysis tool using the Graph Theory

Overview

Mondrian Build Status

Ok guyz, you have a master degree in Graph Theory, you follow Law of Demeter and you live on S.O.L.I.D principles ?

Let's have some Fun ! (^ω^)

What

It is a set of CLI tools to help you to analyse and refactor highly coupled classes. As you know Law of Demeter and S.O.L.I.D guidances, you need your classes loosely coupled. And for this, there is one only rule : ABSTRACTION

From Hell To Kiss

This app provides some helpers to add abstraction into concrete classes with minimum pain.

Getting started

Download the PHAR : mondrian.phar

Try to run a simple command with a few files (or a small part of a project)

$ mondrian.phar digraph /home/dev/project

This command produces a DOT file for GraphViz. Other formats are available :

  • html : interactive graph with the d3.js awesome library
  • svg : open standard for vector graphics (requires GraphViz)
  • json : format for d3.js for example

Example with html format

$ php mondrian.php d --ignore=tests --ignore=vendor --format=html ./MyPhpProject/

html-report

Note: The generated html file does not require any dependencies nor a connection.

Ecosystem

Running unit tests with PHPUnit

$ phpunit.phar

Building the documentation with phpDocumentor

$ phpDocumentor.phar

Building mondrian.phar from the sources with box2

$ box.phar b

Examples

Default digraph on trismegiste/dokudoki

digraph1

Which components are bottleneck ?

digraph2

Reduced graph to LSP violations on trismegiste/prolog

digraph3

How

Read the concept here

Read the online documentation here

And the API here (not up to date)

Extensible

You can easily add plugins to this tool to make your own algorithm, coloring scheme or new generator. I try to make the API mostly decoupled, re-usable and with great SRP and OCP concerns. Look into the "About" plugin to understand a basic stub of plugin.

Third party

Packages used in this project :

Special thanks

  • William Gibson
  • JS Bach
  • Dream Theater
Comments
  • Change in the .dot file label generation.

    Change in the .dot file label generation.

    Instead of new line characters it delimits the lables with "->" and "-->>" strings.

    The reason I did it because the graphviz did contain only the first line of the label.

    opened by tothimre-zz 8
  • Digraph building fails when Trait are involved

    Digraph building fails when Trait are involved

    Trait are not parsed but method within trait are parsed.

    Therefore they are added by ReflectionContext with an empty FQCN and generation fails at the digraph building (the second pass)

    Need to process trait, at least, skip them in the first time

    bug 
    opened by Trismegiste 4
  • Digraph::addEdge() must be an instance of Vertex, null given

    Digraph::addEdge() must be an instance of Vertex, null given

    Hi there,

    I'm getting the following error: PHP Catchable fatal error: Argument 2 passed to Trismegiste\Mondrian\Graph\Digraph::addEdge() must be an instance of Trismegiste\Mondrian\Graph\Vertex, null given, called in phar:///usr/local/bin/mondrian/src/Visitor/EdgeCollector.php on line 118 and defined in phar:///usr/local/bin/mondrian/src/Graph/Digraph.php on line 59

    Any idea why this might happen?

    opened by xtrasmal 3
  • Are

    Are "@var" for variables supported

    In my project a class factory is used to create objects. This means, that in the code I have Factory::create('someName') and Factory::create('someOtherName'), which indirectly maps to actual object class, that will be created.

    To make IDE auto-complete work I do this:

    /** @var ActualClassName $object */
    $object = Factory::create('someName');
    

    Are such @var annotations being processed?

    opened by aik099 2
  • warnings and notices

    warnings and notices

    I'm using php 5.5 and getting: Warning: Invalid argument supplied for foreach() in /Mondrian/Transform/ReflectionContext.php on line 65

    Warning: Invalid argument supplied for foreach() in /Mondrian/Transform/ReflectionContext.php on line 65

    Catchable fatal error: Argument 1 passed to Trismegiste\Mondrian\Graph\Digraph::addEdge() must be an instance of Trismegiste\Mondrian\Graph\Vertex, null given, called in /Mondrian/Visitor/EdgeCollector.php on line 107 and defined in /Mondrian/Graph/Digraph.php on line 59

    opened by dwenaus 2
  • Bug in FactoryGenerator

    Bug in FactoryGenerator

    The PhpFile is not Modified after modified() when in the method afterTraversing

    It does not make sense ?!? cloning in Traverser ? Otherwise, why other refactoring passes do work ?

    bug 
    opened by Trismegiste 2
  • Creation of a plugin system to add new Command

    Creation of a plugin system to add new Command

    To prevent this app to grow to its out-maintainbility point, it would be wise to create a system permitting to add third-party plugin.

    zero config pleaz

    enhancement 
    opened by Trismegiste 1
  • The phar does not work when called from a relative path

    The phar does not work when called from a relative path

    So far, there could be a problem with autoloading of PHPParser

    For reason I don't figure out, when you call

    $ ../mondrian.phar 
    

    autoloading fails for PHPParser_*

    bug 
    opened by Trismegiste 1
  • Generating default confg

    Generating default confg

    writing the .mondrian.yml is a boring task, make a default generation with a new command.

    Is that parsing or refactoring ? Depending on the issue #16 this command will be inherit from AbstractParse or AbstractRefactor...

    enhancement 
    opened by Trismegiste 1
  • Refactoring

    Refactoring

    • Better design for refactor pass interface : too many if in Contractor
    • Where to save the file ? in the "afterTraverse" visitor or in "leaveNode" for PhpFile node

    Abstraction for the persistence to the filesystem ?

    The 3 visitors for refactoring need to be fully unit and functional tested

    enhancement 
    opened by Trismegiste 0
  • Composer package PHP-Parser is not stable

    Composer package PHP-Parser is not stable

    When installing via composer, it currently complains that the package nikic/PHP-Parser is not stable. This forces you to set your minimum-stabilty to dev which is not good.

    This was attempted to be fixed here, but it now looks to be outdated: https://github.com/Trismegiste/Mondrian/pull/35.

    It's likely that we'll want to upgrade this package to version 1.1, as suggested by @Trismegiste in the PR.

    opened by lenton 0
  • Missing src/Transform/Format/template.html

    Missing src/Transform/Format/template.html

    I am using the PHP version 7 fork, and downloaded the bin/box/mondrian.phar. On executing mondrian.phar d --ignore=tests --ignore=vendor --format=html project-dir, I get the following warning, and the generated html is blank.

    PHP Warning: file_get_contents(phar:///mondrian.phar/src/Transform/Format/template.html): failed to open stream: phar error: "src/Transform/Format/template.html" is not a file in phar "mondrian.phar" in phar:///mondrian.phar/src/Transform/Format/Html.php on line 20

    opened by lokeb 0
  • Cannot use 'PhpParser\Node\Scalar\String' as class name as it is reserved in phar://...

    Cannot use 'PhpParser\Node\Scalar\String' as class name as it is reserved in phar://...

    Hello!

    It appears you have an older version of nikic/php-parser as a dependency. Versions earlier than 1.3 cause the following error under PHP 7:

    PHP Fatal error: Cannot use 'PhpParser\Node\Scalar\String' as class name as it is reserved in phar:///usr/local/bin/mondrian.phar/vendor/nikic/php-parser/lib/PhpParser/PrettyPrinter/Standard.php on line 86

    More info: https://github.com/nikic/PHP-Parser/issues/182#issuecomment-109368749

    Would it be possible to update to at least v1.3 of nikic/php-parser?

    Thanks!

    Ian

    opened by thinkspill 5
  • PHP Parse error:  parse error in phar:.../mondrian.phar/src/Transform/ReflectionContext.php on line 32

    PHP Parse error: parse error in phar:.../mondrian.phar/src/Transform/ReflectionContext.php on line 32

    Downloaded the .phar thinking/hoping this would be real easy to use.

    [39] 13:04:40 2585 .../htdocs/php.tools$ ./mondrian.phar digraph ../afp1
    #!/usr/bin/env php
    Parsing 3 files...
    PHP Parse error:  parse error in phar:///Applications/MAMP/htdocs/php.tools/mondrian.phar/src/Transform/ReflectionContext.php on line 32
    

    just a simple check to make sure the phar isn't corrupted or something:

    [40] 13:04:51 2586 .../htdocs/php.tools$ ./mondrian.phar digraph wtf?
    #!/usr/bin/env php
    
      [InvalidArgumentException]            
      The "wtf?" directory does not exist.  
    
    digraph [-i|--ignore="..."] [-f|--format="..."] dir [report]
    

    Is this a Mondrian problem or something in my environment? The message could be more informative.

    opened by toddkaufmann 1
Releases(v1.3.3)
  • v1.3.3(Sep 16, 2014)

    The 3 visitors for digraph building become big fatty classes. I have to split them to preserve SRP.

    No features added. Traits are not fully supported yet (method aliasing and coupling algorithm for exemple)

    Source code(tar.gz)
    Source code(zip)
  • v1.3.2(Sep 12, 2014)

    Thanks to D3.js, a report can be generated in html format (option --format=html )

    Download the PHAR here https://github.com/Trismegiste/Mondrian/raw/master/bin/box/mondrian.phar

    Source code(tar.gz)
    Source code(zip)
  • v1.3.1(Sep 11, 2014)

    Major update to clean and update this repo:

    • switching to PSR-4 autoloader
    • Moving sources to src/ and tests to tests/
    • updating and tagging version for all dependencies
    • adding export to SVG format
    • minimum-stability : stable

    Phar executable download here

    Not all traits features supported yet

    image

    Source code(tar.gz)
    Source code(zip)
  • v1.3.0-dev(Sep 10, 2014)

    This alpha release is supporting Trait declarations and the use in classes.

    Things that do NOT WORK :

    • method aliasing during use of trait
    • use traits in a trait

    Unit tests are OK despite Travis fail. Locally it works with PHPUnit 4.2.5 (No time to figure out why)

    PHAR package all-in-one at https://github.com/Trismegiste/Mondrian/blob/master/Resources/box/mondrian.phar

    Source code(tar.gz)
    Source code(zip)
PHP Static Analysis Tool - discover bugs in your code without running it!

PHPStan - PHP Static Analysis Tool PHPStan focuses on finding errors in your code without actually running it. It catches whole classes of bugs even b

PHPStan 11.6k Dec 30, 2022
Deptrac is a static code analysis tool for PHP that helps you communicate, visualize and enforce architectural decisions in your projects

Deptrac is a static code analysis tool for PHP that helps you communicate, visualize and enforce architectural decisions in your projects. You can freely define your architectural layers over classes and which rules should apply to them.

QOSSMIC GmbH 2.2k Dec 30, 2022
Beautiful and understandable static analysis tool for PHP

PhpMetrics PhpMetrics provides metrics about PHP project and classes, with beautiful and readable HTML report. Documentation | Twitter | Contributing

PhpMetrics 2.3k Dec 22, 2022
A static analysis tool for finding errors in PHP applications

Psalm Psalm is a static analysis tool for finding errors in PHP applications. Installation To get started, check out the installation guide. Live Demo

Vimeo 5k Jan 2, 2023
A static analysis tool for security

progpilot A static analyzer for security purposes Only PHP language is currently supported Installation Option 1: use standalone phar Download the lat

null 271 Dec 27, 2022
Performs advanced static analysis on PHP code

PHP Analyzer Please report bugs or feature requests via our website support system ? in bottom right or by emailing [email protected]. Contri

Continuous Inspection 443 Sep 23, 2022
The Exakat Engine : smart static analysis for PHP

Exakat The Exakat Engine is an automated code reviewing engine for PHP. Installation Installation with the phar Phar is the recommended installation p

Exakat 370 Dec 28, 2022
Static Analysis Results Baseliner

Static Analysis Results Baseliner (SARB) Why SARB Requirements Installing Using SARB Examples Further reading Why SARB? If you've tried to introduce a

Dave Liddament 151 Jan 3, 2023
Infection Static Analysis Plugin

Static analysis on top of mutation testing - prevents escaped mutants from being invalid according to static analysis

Roave, LLC 108 Jan 2, 2023
A set of tools for lexical and syntactical analysis written in pure PHP.

Welcome to Dissect! master - this branch always contains the last stable version. develop - the unstable development branch. Dissect is a set of tools

Jakub Lédl 221 Nov 29, 2022
A project to add Psalm support for Drupal for security testing, focused only on taint analysis.

psalm-plugin-drupal A Drupal integration for Psalm focused on security scanning (SAST) taint analysis. Features Stubs for sinks, sources, and sanitize

Samuel Mortenson 38 Aug 29, 2022
A PHP code-quality tool

GrumPHP Sick and tired of defending code quality over and over again? GrumPHP will do it for you! This composer plugin will register some git hooks in

PHPro 3.9k Jan 1, 2023
A tool to automatically fix PHP Coding Standards issues by Dragon Code.

A tool to automatically fix PHP Coding Standards issues by Dragon Code.

The Dragon Code 24 Aug 27, 2022
Phan is a static analyzer for PHP. Phan prefers to avoid false-positives and attempts to prove incorrectness rather than correctness.

Phan is a static analyzer for PHP that prefers to minimize false-positives. Phan attempts to prove incorrectness rather than correctness. Phan looks f

null 5.4k Jan 7, 2023
A static analyzer for PHP version migration

PHP Migration Readme in Chinese 中文 This is a static analyzer for PHP version migration and compatibility checking. It can suppose your current code ru

Yuchen Wang 194 Sep 27, 2022
SonarPHP: PHP static analyzer for SonarQube & SonarLint

Code Quality and Security for PHP This SonarSource project is a static code analyser for PHP language used as an extension for the SonarQube platform.

SonarSource 343 Dec 25, 2022
A full-scale PHP sandbox class that utilizes PHP-Parser to prevent sandboxed code from running unsafe code

A full-scale PHP 7.4+ sandbox class that utilizes PHP-Parser to prevent sandboxed code from running unsafe code. It also utilizes FunctionParser to di

Corveda 192 Dec 10, 2022
Parse: A Static Security Scanner

Parse: A PHP Security Scanner PLEASE NOTE: This tool is still in a very early stage. The work continues... The Parse scanner is a static scanning tool

psec.io 342 Jan 2, 2023
A full-scale PHP 5.3.2+ sandbox class that utilizes PHPParser to prevent sandboxed code from running unsafe code.

##DEPRECATED: The PHPSandbox project has transfered to Corveda/PHPSandbox and will be actively maintained there. This branch is no longer being active

Elijah Horton 219 Sep 2, 2022