Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

mpmd

> 编程语言
Open source

Magento Project Mess Detector (for n98-magerun)

181 stars0 likes0 views
WebsiteGitHub

About

Magento Project Mess Detector (for n98-magerun)

Magento Project Mess Detector

Author: Fabrizio Branca (fbrnc.net / @fbrnc) Some additional commands for the excellent n98-magerun Magento command-line tool that will help you find out how messed up a Magento instance is :)

…

Table of Contents

  • Installation
  • Compare files
    • mpmd:corehacks
    • mpmd:codepooloverrides
  • Dependency Checker
    • How does it work?
    • Why?
    • Parsers
    • Handlers
    • Specifying sources
    • Commands: Tables
      • mpmd:dependencychecker
      • mpmd:dependencychecker:verify
      • mpmd:dependencychecker:configured
    • Commands: Graphs
      • mpmd:dependencychecker:graph:module
      • mpmd:dependencychecker:graph:class
      • mpmd:dependencychecker:graph:configured
    • How to run the unit tests
    • Interesting Graphviz commands

Installation

There are a few options. You can check out the different options in the MageRun docs.

Here's the easiest:

  1. Install n98-magerun if you haven't already. Find the instructions on the n98-magerun wiki.

  2. Create ~/.n98-magerun/modules/ if it doesn't already exist. (or /usr/local/share/n98-magerun/modules or put your modules inside your Magento instance in lib/n98-magerun/modules if you prefer that)

mkdir -p ~/.n98-magerun/modules/
  1. Clone the mpmd repository in there.
bash
git clone https://github.com/AOEpeople/mpmd.git ~/.n98-magerun/modules/mpmd
  1. It should be installed. To verify that it was installed correctly, check if the new commands show up in the command list:
n98-magerun.phar | grep mpmd

Commands

Command mpmd:corehacks

Usage:
 mpmd:corehacks [--format[="..."]] pathToVanillaCore [htmlReportOutputPath] [skipDirectories]

Arguments:
 pathToVanillaCore     Path to Vanilla Core used for comparison
 htmlReportOutputPath  Path to where the HTML report will be written
 skipDirectories       ':'-separated list of directories that will not be considered (defaults to '.svn:.git')

This command requires a vanilla version of Magento (same version and edition! Run n98-magerun.phar sys:info for more details) to be present somewhere in the filesystem. It will then traverse all project files and compare them with the original files. This command will also be able to tell the difference between whitespace or code comments changes and real code changes. It will generate a HTML report that also includes the diffs.

$ cd /var/www/magento/htdocs
$ n98-magerun.phar mpmd:corehacks /path/to/vanilla/magento /path/to/report.html
Comparing project files in 'var/www/magento/htdocs' to vanilla Magento code in '/path/to/vanilla/magento'...
+----------------------+-------+
| Type                 | Count |
+----------------------+-------+
| differentFileContent | 2     |
| identicalFiles       | 16049 |
| fileMissingInB       | 1     |
| sameFileButComments  | 0     |
+----------------------+-------+
Generating detailed HTML Report

Report preview:

Command: mpmd:codepooloverrides

Usage:
 mpmd:codepooloverrides [--format[="..."]] [htmlReportOutputPath] [skipDirectories]

Arguments:
 htmlReportOutputPath  Path to where the HTML report will be written
 skipDirectories       ':'-separated list of directories that will not be considered (defaults to '.svn:.git')

This command will compare all code pools with each other and detect files that are overriding each other. It will show identical files (What's the point of these? But yes, seen projects where this happened), copied files with changes in comments and whitespace only, and real changes. Of course with diff...

Report preview:

Dependency Checker

The dependency checker parses one or more files or directories and detects PHP classes that are being "used" there.

How does it work?

The dependency checker is a "semi" static code analysis tool. That means it does the job without actually executing any of the PHP code you're pointing it to, but it does need that module to be installed correctly and it will invoke the Magento framework to resolve classpaths (catalog/product -> Mage_Catalog_Model_Product)

Why?

While tools like pdepend exist those tools don't know anything about Magento in general, Magento's special classpaths and where they are being used. Also sometimes the numbers generated by pdepend are a little overwhelming and after all what are you going to do knowing that you're module has an avarage cyclomatic complexity of x?

The mpmd:dependencychecker will

  • help you to detect other Magento modules that the module you're currently looking at depends on.
  • check you module's configuration and let's you know if all actual dependencies are declared correctly and will also show if the module is declaring dependencies that this tool didn't detect (Note: this tool isn't perfect, so please double check before removing any dependencies)
  • show you the relations between modules and individual classes
  • produce pretty graphs that you can render with Graphviz
  • help you detect code that requires some refactoring and will help you create cleaner - less dependent - modules in the first place.

Parsers

The dependency checker comes with two different parsers (and allows you to add new ones:)

Parser Will process How it works
Tokenizer *.php, *.phtml The tokenizer parser will split the PHP file into tokens and traverses them. Handlers can subscribe to token to detect various class usages.
Xpath *.xml The xpath parser will read the file into a SimpleXMLElement object and will pass this to all the subscribed handlers

How to add your own parser

Add a new parser via n98-magerun's YAML configuration

commands:
  Mpmd\Magento\DependencyCheckCommand:
    parsers:
      - Mpmd\DependencyChecker\Parser\Tokenizer
      - Mpmd\DependencyChecker\Parser\Xpath
      - (... add your parser here ...)

All parsers need to implement Mpmd\DependencyChecker\Parser\ParserInterface. Also checkout the AbstractParser that implements that interface and might be a good starting point.

Handlers

Every parser comes with a number of handlers. Here's the list of default handlers that come with the dependency checker:

Parser Handler Will process What it does
Tokenizer Interfaces T_IMPLEMENTS Finds interfaces: class A implements B {}
Tokenizer WhitespaceString T_NEW, T_EXTENDS, T_CLASS Finds classes instantiated with 'new': $a = new B();
Finds extended classes: class A extends B {}
Tokenizer StaticCalls T_DOUBLE_COLON Finds static calls: A::B and A::B()
Tokenizer TypeHints T_FUNCTION Finds type hints: function a (B $b) {}
Tokenizer MagentoFactoryMethods T_STRING for specific keywords Finds classes instantiated with one of Magento's factory methods and resolves them to real PHP classes not taking rewrites into account:
Mage::getModel()
Mage::getSingleton()
Mage::getResourceModel()
Mage::getResourceSingleton()
$this->getLayout()->createBlock()
Mage::getBlockSingleton()
Mage::helper()
Mage::getResourceHelper()
Mage::getControllerInstance()
                                                                                              |

| Xpath | LayoutXml | All xml files | Finds blocks and resolves them into real PHP classes: `` | | Xpath | SystemXml | All xml files | Finds references to models in system.xml files: adminhtml/system_config_form_field_notification adminhtml/system_config_source_yesno adminhtml/system_config_backend_store |

Note: MagentoFactoryMethods, LayoutXml and SystemXml need to resolve Magento classpaths into real PHP classes. The challenge hereby is NOT to take rewrites into account since rewriting a class is a mechanism that was introduced to ALLOW decoupling without dependending on each other. In order to leverage Magento and it's configuration to resolve the class paths but not take the rewrite into accounts

  • the module that we're testing needs to be installed into a functioning Magento environment and all dependencies must be fulfilled
  • we need to "trick" something into being Mage_Core_Model_Config having access to the same data but doing things slightly differently. Mpmd\Util\MagentoFactory takes care of that and provides access to some of the original functions like getModelClassName() and getBlockClassName()...

How to add your own handler

Add a new handler via n98-magerun's YAML configuration (also checkout n98-magerun's documentation for custom commands)

commands:
  Mpmd\Magento\DependencyCheckCommand:
	Mpmd\DependencyChecker\Parser\Tokenizer:
      handlers:
        - Mpmd\DependencyChecker\Parser\Tokenizer\Handler\WhitespaceString
        - Mpmd\DependencyChecker\Parser\Tokenizer\Handler\Interfaces
        - (... add your tokenizer handler here ...)
    :
      handlers:
        - (... add your  handler here ...)    
``

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHP

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言