what is phpmd and how to use it?

15,382

Solution 1

I think the problem is you've installed phpmd in a local directory, but you're trying to use it as if it was installed globally.

Installation instruction on the referenced sites can't really be made any clearer. Since you've already installed phpcs and php-cs-fixer, and those work for you, just follow similar instructions for phpmd. These are all PHP projects and are installed in a similar way.

Anyway, to use phpmd as a global command you have several options.

Github

Clone the github repository just like you did and add the phpmd bin directory to your PATH variable.

Global composer installation

Use the composer global command to install phpmd globally. You will also need to make sure that composer's bin directory is in the PATH. By default it's ~/.composer/vendor/bin.

composer global require phpmd/phpmd

This command will install phpmd globally, and as soon as ~/.composer/vendor/bin is in your PATH you'll be able to call it by simply invoking phpmd.

It's very well explained in composer's documentation: https://getcomposer.org/doc/03-cli.md#global

Download the phar archive

This is the simplest thing you can do. Simply go the phpmd releases, choose the latest and download the phar archive.

Put the phar file to whatever place you'd like. Just remember that it needs to be in your PATH. You can also rename it, to skip the .phar extension.

For example:

wget http://static.phpmd.org/php/2.1.3/phpmd.phar
sudo mv phpmd.phar /usr/bin/phpmd
sudo chmod +x /usr/bin/phpmd

Docker container

First, fetch the docker image with static analysis tools for PHP:

docker pull jakzal/phpqa

One of the tools provided by the image is phpmd. The command below will run phpmd in a docker container and mount the current working directory as a /project.

docker run -it --rm -v $(pwd):/project -w /project jakzal/phpqa \
    phpmd src text cleancode,codesize,controversial,design,naming,unusedcode

Solution 2

When you use the composer-based install, it gets installed into the ./bin directory within the ./vendors directory. So for me, relative to my project's root directory, it was here:

./vendor/bin/phpmd

And I was able to run it from my project's root by running ./vendor/bin/phpmd . text codesize. (I'm not getting any useful output yet, but another issue)

Share:
15,382

Related videos on Youtube

Anton
Author by

Anton

Learn new things every day about anything Thanks for visiting my profile. Top 100 SO users in India

Updated on September 15, 2022

Comments

  • Anton
    Anton almost 2 years

    I have Ubuntu 14.04 + Sublime text 3 and installed phpcs packages

    additionalty I have installed phpcs and php-cs-fixer on my system

    From this blog

    I have found that phpmd (PHP Mess Detector) is also a required library, so installed phpmd as per given instructions on official php md page using alternative method From the github repository everything was finished.

    :~/phpmd$ curl -s http://getcomposer.org/installer | php
    #!/usr/bin/env php
    All settings correct for using Composer
    Downloading...
    
    Composer successfully installed to: /home/keshav/phpmd/composer.phar
    Use it: php composer.phar
    

    but now when I write on terminal

      phpmd /opt/lampp/htdocs/myproject, myfile.php 
    

    phpmd: command not found

    There is phpmd folder on Home directory and everything without any error.

    I have local project on core PHP create composer.json in project folder as per suggested on github .

    Please tell me what means by

    Then install Composer in your project (or download the composer.phar directly):