<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>php quality phpmd phpcs phpcpd on random thoughts</title><link>https://awesomeprogrammer.com/categories/php-quality-phpmd-phpcs-phpcpd/</link><description>Recent content in php quality phpmd phpcs phpcpd on random thoughts</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Sat, 11 Aug 2012 00:00:00 +0000</lastBuildDate><atom:link href="https://awesomeprogrammer.com/categories/php-quality-phpmd-phpcs-phpcpd/index.xml" rel="self" type="application/rss+xml"/><item><title>PHP Code Quality / refactoring messy code tips</title><link>https://awesomeprogrammer.com/blog/2012/08/11/php-code-quality-slash-refactoring-messy-code-tips/</link><pubDate>Sat, 11 Aug 2012 00:00:00 +0000</pubDate><guid>https://awesomeprogrammer.com/blog/2012/08/11/php-code-quality-slash-refactoring-messy-code-tips/</guid><description><![CDATA[<p>Lately I have been adding new and new features to existing application and now came time when I have to play with some seriously old code. I mean - really old, forgotten and never refactored code. It&rsquo;s just there, it&rsquo;s working and nobody dares touching it. So before I started I need to prepare myself for the battle.</p>
<ul>
<li>
<p>if you are in need of refactoring some critical part of the system the big chance is that this critical part doesn&rsquo;t came with any tests (life, so to say). There is also a big chance that the code is basically impossible to test with unit testing - write some integration tests instead. As they say - any kind of test are better that none tests. You can use anything that works for you (for example BDD tool like <a href="http://behat.org/">behat</a>).</p>
</li>
<li>
<p>install <a href="http://pear.php.net/package/PHP_CodeSniffer">PHP_CodeSniffer</a>, <a href="http://phpmd.org/">PHP Mess Detector</a>, <a href="https://github.com/sebastianbergmann/phpcpd/">PHP Copy/Paste Detector</a> and <a href="http://pdepend.org/">PHP_Depend</a> (phpmd requires it so probably at this point you&rsquo;ll have it installed anyway) - these tools will be very useful - trust me</p>
</li>
<li>
<p>you can integrate your tests/code checks with <a href="http://phpundercontrol.org/">phpUnderControl</a> that is extending <a href="http://cruisecontrol.sourceforge.net/">CruiseControl</a> - prepare your <em>build</em> file according to manual (you may want to exclude some directories for each tool, also if you code base is old I would stick with PSR-1 for the start as the code standard because PSR-2 check will likely to kill you on sight)</p>
</li>
</ul>
<p>Bonus: some interesting results of <em>phpmd</em> ran just for two files :) (methods/class names blanked).</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">The</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">some_class</span> <span style="color:#a6e22e">has</span> <span style="color:#ae81ff">41</span> <span style="color:#a6e22e">methods</span><span style="color:#f92672">.</span> <span style="color:#a6e22e">Consider</span> <span style="color:#a6e22e">refactoring</span> <span style="color:#a6e22e">user_art</span> <span style="color:#a6e22e">to</span> <span style="color:#a6e22e">keep</span> <span style="color:#a6e22e">number</span> <span style="color:#a6e22e">of</span> <span style="color:#a6e22e">methods</span> <span style="color:#a6e22e">under</span> <span style="color:#ae81ff">10.</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">The</span> <span style="color:#a6e22e">method</span> <span style="color:#a6e22e">someMethod</span>() <span style="color:#a6e22e">has</span> <span style="color:#a6e22e">an</span> <span style="color:#a6e22e">NPath</span> <span style="color:#a6e22e">complexity</span> <span style="color:#a6e22e">of</span> <span style="color:#ae81ff">15211861552761400491758084218783915899329999864762835394560.</span> <span style="color:#a6e22e">The</span> <span style="color:#a6e22e">configured</span> <span style="color:#a6e22e">NPath</span> <span style="color:#a6e22e">complexity</span> <span style="color:#a6e22e">threshold</span> <span style="color:#a6e22e">is</span> <span style="color:#ae81ff">200.</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">The</span> <span style="color:#a6e22e">method</span> <span style="color:#a6e22e">someMethod2</span>() <span style="color:#a6e22e">has</span> <span style="color:#a6e22e">an</span> <span style="color:#a6e22e">NPath</span> <span style="color:#a6e22e">complexity</span> <span style="color:#a6e22e">of</span> <span style="color:#ae81ff">900000.</span> <span style="color:#a6e22e">The</span> <span style="color:#a6e22e">configured</span> <span style="color:#a6e22e">NPath</span> <span style="color:#a6e22e">complexity</span> <span style="color:#a6e22e">threshold</span> <span style="color:#a6e22e">is</span> <span style="color:#ae81ff">200.</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">The</span> <span style="color:#a6e22e">method</span> <span style="color:#a6e22e">someMethod3</span>() <span style="color:#a6e22e">has</span> <span style="color:#a6e22e">an</span> <span style="color:#a6e22e">NPath</span> <span style="color:#a6e22e">complexity</span> <span style="color:#a6e22e">of</span> <span style="color:#ae81ff">119430531454390553552.</span> <span style="color:#a6e22e">The</span> <span style="color:#a6e22e">configured</span> <span style="color:#a6e22e">NPath</span> <span style="color:#a6e22e">complexity</span> <span style="color:#a6e22e">threshold</span> <span style="color:#a6e22e">is</span> <span style="color:#ae81ff">200.</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">The</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">some_class2</span> <span style="color:#a6e22e">has</span> <span style="color:#ae81ff">3442</span> <span style="color:#a6e22e">lines</span> <span style="color:#a6e22e">of</span> <span style="color:#a6e22e">code</span><span style="color:#f92672">.</span> <span style="color:#a6e22e">Current</span> <span style="color:#a6e22e">threshold</span> <span style="color:#a6e22e">is</span> <span style="color:#ae81ff">1000.</span> <span style="color:#a6e22e">Avoid</span> <span style="color:#a6e22e">really</span> <span style="color:#a6e22e">long</span> <span style="color:#a6e22e">classes</span><span style="color:#f92672">.</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">The</span> <span style="color:#a6e22e">method</span> <span style="color:#a6e22e">someMethod</span>() <span style="color:#a6e22e">has</span> <span style="color:#a6e22e">an</span> <span style="color:#a6e22e">NPath</span> <span style="color:#a6e22e">complexity</span> <span style="color:#a6e22e">of</span> <span style="color:#ae81ff">151619238808128.</span> <span style="color:#a6e22e">The</span> <span style="color:#a6e22e">configured</span> <span style="color:#a6e22e">NPath</span> <span style="color:#a6e22e">complexity</span> <span style="color:#a6e22e">threshold</span> <span style="color:#a6e22e">is</span> <span style="color:#ae81ff">200.</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">...</span>
</span></span></code></pre></div><p>Happy refactoring!</p>]]></description><content:encoded><![CDATA[<p>Lately I have been adding new and new features to existing application and now came time when I have to play with some seriously old code. I mean - really old, forgotten and never refactored code. It&rsquo;s just there, it&rsquo;s working and nobody dares touching it. So before I started I need to prepare myself for the battle.</p>
<ul>
<li>
<p>if you are in need of refactoring some critical part of the system the big chance is that this critical part doesn&rsquo;t came with any tests (life, so to say). There is also a big chance that the code is basically impossible to test with unit testing - write some integration tests instead. As they say - any kind of test are better that none tests. You can use anything that works for you (for example BDD tool like <a href="http://behat.org/">behat</a>).</p>
</li>
<li>
<p>install <a href="http://pear.php.net/package/PHP_CodeSniffer">PHP_CodeSniffer</a>, <a href="http://phpmd.org/">PHP Mess Detector</a>, <a href="https://github.com/sebastianbergmann/phpcpd/">PHP Copy/Paste Detector</a> and <a href="http://pdepend.org/">PHP_Depend</a> (phpmd requires it so probably at this point you&rsquo;ll have it installed anyway) - these tools will be very useful - trust me</p>
</li>
<li>
<p>you can integrate your tests/code checks with <a href="http://phpundercontrol.org/">phpUnderControl</a> that is extending <a href="http://cruisecontrol.sourceforge.net/">CruiseControl</a> - prepare your <em>build</em> file according to manual (you may want to exclude some directories for each tool, also if you code base is old I would stick with PSR-1 for the start as the code standard because PSR-2 check will likely to kill you on sight)</p>
</li>
</ul>
<p>Bonus: some interesting results of <em>phpmd</em> ran just for two files :) (methods/class names blanked).</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">The</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">some_class</span> <span style="color:#a6e22e">has</span> <span style="color:#ae81ff">41</span> <span style="color:#a6e22e">methods</span><span style="color:#f92672">.</span> <span style="color:#a6e22e">Consider</span> <span style="color:#a6e22e">refactoring</span> <span style="color:#a6e22e">user_art</span> <span style="color:#a6e22e">to</span> <span style="color:#a6e22e">keep</span> <span style="color:#a6e22e">number</span> <span style="color:#a6e22e">of</span> <span style="color:#a6e22e">methods</span> <span style="color:#a6e22e">under</span> <span style="color:#ae81ff">10.</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">The</span> <span style="color:#a6e22e">method</span> <span style="color:#a6e22e">someMethod</span>() <span style="color:#a6e22e">has</span> <span style="color:#a6e22e">an</span> <span style="color:#a6e22e">NPath</span> <span style="color:#a6e22e">complexity</span> <span style="color:#a6e22e">of</span> <span style="color:#ae81ff">15211861552761400491758084218783915899329999864762835394560.</span> <span style="color:#a6e22e">The</span> <span style="color:#a6e22e">configured</span> <span style="color:#a6e22e">NPath</span> <span style="color:#a6e22e">complexity</span> <span style="color:#a6e22e">threshold</span> <span style="color:#a6e22e">is</span> <span style="color:#ae81ff">200.</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">The</span> <span style="color:#a6e22e">method</span> <span style="color:#a6e22e">someMethod2</span>() <span style="color:#a6e22e">has</span> <span style="color:#a6e22e">an</span> <span style="color:#a6e22e">NPath</span> <span style="color:#a6e22e">complexity</span> <span style="color:#a6e22e">of</span> <span style="color:#ae81ff">900000.</span> <span style="color:#a6e22e">The</span> <span style="color:#a6e22e">configured</span> <span style="color:#a6e22e">NPath</span> <span style="color:#a6e22e">complexity</span> <span style="color:#a6e22e">threshold</span> <span style="color:#a6e22e">is</span> <span style="color:#ae81ff">200.</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">The</span> <span style="color:#a6e22e">method</span> <span style="color:#a6e22e">someMethod3</span>() <span style="color:#a6e22e">has</span> <span style="color:#a6e22e">an</span> <span style="color:#a6e22e">NPath</span> <span style="color:#a6e22e">complexity</span> <span style="color:#a6e22e">of</span> <span style="color:#ae81ff">119430531454390553552.</span> <span style="color:#a6e22e">The</span> <span style="color:#a6e22e">configured</span> <span style="color:#a6e22e">NPath</span> <span style="color:#a6e22e">complexity</span> <span style="color:#a6e22e">threshold</span> <span style="color:#a6e22e">is</span> <span style="color:#ae81ff">200.</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">The</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">some_class2</span> <span style="color:#a6e22e">has</span> <span style="color:#ae81ff">3442</span> <span style="color:#a6e22e">lines</span> <span style="color:#a6e22e">of</span> <span style="color:#a6e22e">code</span><span style="color:#f92672">.</span> <span style="color:#a6e22e">Current</span> <span style="color:#a6e22e">threshold</span> <span style="color:#a6e22e">is</span> <span style="color:#ae81ff">1000.</span> <span style="color:#a6e22e">Avoid</span> <span style="color:#a6e22e">really</span> <span style="color:#a6e22e">long</span> <span style="color:#a6e22e">classes</span><span style="color:#f92672">.</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">The</span> <span style="color:#a6e22e">method</span> <span style="color:#a6e22e">someMethod</span>() <span style="color:#a6e22e">has</span> <span style="color:#a6e22e">an</span> <span style="color:#a6e22e">NPath</span> <span style="color:#a6e22e">complexity</span> <span style="color:#a6e22e">of</span> <span style="color:#ae81ff">151619238808128.</span> <span style="color:#a6e22e">The</span> <span style="color:#a6e22e">configured</span> <span style="color:#a6e22e">NPath</span> <span style="color:#a6e22e">complexity</span> <span style="color:#a6e22e">threshold</span> <span style="color:#a6e22e">is</span> <span style="color:#ae81ff">200.</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">...</span>
</span></span></code></pre></div><p>Happy refactoring!</p>]]></content:encoded></item></channel></rss>