tetsunosukeのnotebook

tetsunosukeのメモです

phpcpd だとCopy Paste Detectをあまりしてくれない件、でjenkinsで困った。

phpcpdとpmd

とりあえずコピペした下記のような2つのクソコードを作ってみる。
Dollar.php

<?php

class Dollar
{
    private $prefix;
    private $value;
    public function __construct($value)
    {
        $this->prefix = "$";    
        $this->value = $value;
    }

    public function __toString()
    {
        return $this->prefix . $this->value;
    }
}

Yen.php

<?php

class Yen 
{
    private $prefix;
    private $value;
    public function __construct($value)
    {
        $this->prefix = "\\";   
        $this->value = $value;
    }

    public function __toString()
    {
        return $this->prefix . $this->value;
    }
}

これをチェックしよう、と思うがとても残念なことになった。むしろ重複しまくっていると検出してくれないのか??

$ phpcpd .
phpcpd 1.3.5 by Sebastian Bergmann.

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

Time: 0 seconds, Memory: 1.25Mb

というわけで、本家pmdを使うことにする。

$ run.sh cpd --files . --minimum-tokens 75 --language php
Found a 7 line (78 tokens) duplication in the following files: 
Starting at line 9 of /var/lib/jenkins/jobs/test/workspace/svn/src/Test/./Dollar.php
Starting at line 9 of /var/lib/jenkins/jobs/test/workspace/svn/src/Test/./Yen.php

        $this->prefix = "$";    
        $this->value = $value;
    }

    public function __toString()
    {
        return $this->prefix . $this->value;
=====================================================================
Found a 5 line (75 tokens) duplication in the following files: 
Starting at line 5 of /var/lib/jenkins/jobs/test/workspace/svn/src/Test/./Dollar.php
Starting at line 5 of /var/lib/jenkins/jobs/test/workspace/svn/src/Test/./Yen.php

    private $prefix;
    private $value;
    public function __construct($value)
    {
        $this->prefix = "$";    

jenkinsのタスクに入れよう

で、これをjenkins経由で入れようとして、build.xmlを変更。

 <target name="cpd" description="Find duplicate code using PMD-CPD">
  <exec executable="pmd/bin/run.sh">
   <arg value="cpd" />
   <arg value="--language php" />
   <arg value="--minimum-tokens 75" />
   <arg value="--files ${basedir}/svn/src" />
   <arg value="--format xml" />
   <arg value=" &gt; ${basedir}/build/logs/pmd-cpd.xml" />
  </exec>
 </target>

ところが、これだと、 "> ファイル" の部分がうまく動いてくれない。
(pmdにそもそもファイルに出力するオプションがあればいいのに標準出力をパイプしないといけない)

おそらくその原因はpmd以下のrun.sh が原因。

java "${HEAPSIZE}" -cp "${classpath}" "${CLASSNAME}" ${@}

こんな具合にしたら通った

command="java ${HEAPSIZE} -cp ${classpath} ${CLASSNAME} ${@}"
$command
$ ant cpd
Buildfile: build.xml

cpd:

BUILD SUCCESSFUL
Total time: 0 seconds