1

Duplicated code below present in two different controller files but the copy paste detector in build.xml won't pick it up when running the phing. The report file it generates has just empty block as shown below. Can someone tell me what I'm missing?

Thanks in advance

DETECTOR's REPORT:

<?xml version="1.0" encoding="UTF-8"?>
<pmd-cpd/>


zzz@ubuntu:$ bin/phpcpd src/Foo/TeBundle/Controller/Create/LeController.php 
phpcpd 2.0.1-5-ga7488a0 by Sebastian Bergmann.

0.00% duplicated lines out of 134 total lines of code.

Time: 25 ms, Memory: 3.00Mb
  • CONTROLLER 1: src/Football/TeamBundle/Controller/Create/CountryController.php
  • CONTROLLER 2: src/Football/TeamBundle/Controller/Create/CityController.php

DUPLICATED CODE:

public function indexAction()
{
    $form = $this->getForm();

    if (is_null($form)) {
        echo 'form empty';
    } else {
        echo 'not empty';
    }
}

public function indexAction()
{
    $form = $this->getForm();

    if (is_null($form)) {
        echo 'form empty';
    } else {
        echo 'not empty';
    }
}

BUILD.XML

<?xml version="1.0" encoding="UTF-8"?>

<project name="Sport" default="detect-copy-paste" basedir=".">

    <!-- GLOBAL VARIABLES -->
    <property name="dir-source" value="${project.basedir}/src" />
    <property name="dir-report" value="test/report/phing" />
    <!-- END -->

    <!-- FILESET -->
    <fileset id="sourcecode" dir="${dir-source}">
        <include name="**/*.php" />
    </fileset>
    <!-- END -->


    <!-- COPY and PASTE DETECTOR -->
    <target name="detect-copy-paste" description="Checks similar code blocks.">
        <echo msg="Checking similar code blocks ..." />
        <phpcpd>
            <fileset refid="sourcecode" />
            <formatter type="pmd" outfile="${dir-report}/copypaste.xml" />
        </phpcpd>
    </target>
    <!-- END -->

</project>

COMPOSER.JSON

{
    "autoload": {
        "psr-0": {
            "": "src/",
            "SymfonyStandard": "app/"
        }
    },
"require": {
    "php": ">=5.3.3",
    "symfony/symfony": "2.5.*",
    "doctrine/orm": "~2.2,>=2.2.3",
    "doctrine/doctrine-bundle": "~1.2",
    "twig/extensions": "~1.0",
    "symfony/assetic-bundle": "~2.3",
    "symfony/swiftmailer-bundle": "~2.3",
    "symfony/monolog-bundle": "~2.4",
    "sensio/distribution-bundle": "~3.0",
    "sensio/framework-extra-bundle": "~3.0",
    "incenteev/composer-parameter-handler": "~2.0",
    "behat/behat": "2.5.*@stable",
    "behat/behat-bundle": "1.0.0",
    "behat/symfony2-extension": "1.1.2",
    "behat/mink": "1.5.0",
    "behat/mink-extension": "~1.3",
    "behat/mink-selenium2-driver": "1.1.1",
    "behat/mink-goutte-driver": "1.0.9",
    "phing/phing": "2.8.2",
    "squizlabs/php_codesniffer": "1.5.1",
    "doctrine/doctrine-fixtures-bundle": "2.2.*",
    "pdepend/pdepend": "2.0.*",
    "phpmd/phpmd" : "2.0.*",
    "sebastian/phpcpd": "@dev",
    "sebastian/finder-facade": "~1.1",
    "sebastian/version": "~1.0.3",
    "symfony/console": "~2.2",
    "phpunit/php-timer": "~1.0.4",
    "theseer/fdomdocument": "~1.4"
}
}
BentCoder
  • 12,257
  • 22
  • 93
  • 165
  • Do you have the same result when running the phpmd command manually? – COil Sep 21 '14 at 17:37
  • "0.00% duplicated lines out of 134 total lines of code." – BentCoder Sep 21 '14 at 17:49
  • It's strange because everything seems to be going well without any error but for some reason it doesn't recognise the duplications. – BentCoder Sep 21 '14 at 18:00
  • But `bin/phpcpd src/Foo/TeBundle/Controller/Create/LeController.php` is not correct. With this command you ony check the controller. What is the result of `bin/phpcpd src/` – COil Sep 21 '14 at 18:14
  • More lines checked this time: `0.00% duplicated lines out of 1692 total lines of code.` – BentCoder Sep 21 '14 at 18:23

1 Answers1

1

The duplicated code is not detected because the number of tokens is not enough is your code snippet, the following will be detected:

public function indexAction()
{
    $form = $this->getForm('kikoo');

    if (is_null($form)) {
        echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty';
        echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty';
        echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty';
        echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty';
        echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty';
        echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty';
        echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty';
        echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty'; echo 'form empty';
    } else {
        echo 'not empty';
    }
}

You can change the min-tokens parameter which is 70 by default:

phpcpd src/ --progress --min-lines=5 --min-tokens=70
COil
  • 7,201
  • 2
  • 50
  • 98