Skip to content

vantoozz/phpcdm

Repository files navigation

PHP Code Density Meter (PHPCDM)

phpcdm is a Code Density Meter for PHP

Build Status Codacy Badge Coverage Status Latest Stable Version License

What is code density?

Look at this code:

<?php

final class SomethingGreat
{
    public function doSomethingGreat($entity, $property, $type, $refProperties, $update, $delete)
    {
        $time = $this->startAction(" $type (" . implode(',', (array) $type) . ')');
        $this->createAction()->addExtraKey($entity, $entity, $property, $refProperties, $delete, $update)->execute();
        $this->finishAction($time);
        $this->createAction()->updateProperty($entity, $property, $type)->execute();
        if ($type instanceof InvalidArgumentException && $type->getMessage() !== null) {
            $this->createAction()->addDescriptionOnProperty($entity, $property, $type->description)->execute();
        }
        $this->createAction()->renameEntity($entity, $property)->execute();
        $this->finishAction($time);
        $time = $this->startAction(" $type (" . implode(',', (array) $type) . ')');
        $this->createAction()->addExtraKey($entity, $entity, $property, $refProperties, $delete, $update)->execute();
        if ($type instanceof InvalidArgumentException && $type->getMessage() !== $delete) {
            $this->finishAction($time);
            $this->createAction()->addDescriptionOnProperty($entity, $property, $type->description)->execute();
            $this->createAction()->renameEntity($entity, $property)->execute();
        }
        $this->createAction()->updateProperty($entity, $property, $type)->execute();
        $this->finishAction($time);
    }
}

It looks like a wall of characters. It actually is a wall of characters. No one can easily understand what the author of this code meant and definitely, no one will be happy trying to read it. Moreover, often such walls of characters are the result of bad application design.

Code density is a measure of how many characters are displayed on a single page.

PHP Code Density Meter aims to help a developer to prevent code density issues and eventually keep an application in a good shape.

The example code has density of 0.381 which is far above the default threshold (0.2)

Installation

You can add this tool as a local, per-project, development-time dependency to your project using Composer:

composer require --dev vantoozz/phpcdm

Also, PHPCDM may be installed globally like this:

composer global require --dev vantoozz/phpcdm

Running

Analyzing the src directory

 ./vendor/bin/phpcdm src

Exiting with an error code

 ./vendor/bin/phpcdm src --non-zero-exit-on-violation