{"id":27113,"date":"2016-12-12T10:47:04","date_gmt":"2016-12-12T08:47:04","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=27113"},"modified":"2016-12-12T12:35:49","modified_gmt":"2016-12-12T10:35:49","slug":"quick-and-easy-introduction-into-php-mess-detector-phpmd","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2016\/12\/12\/quick-and-easy-introduction-into-php-mess-detector-phpmd\/","title":{"rendered":"Quick and easy introduction into PHP Mess Detector (PHPMD)"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p><a href=\"https:\/\/phpmd.org\/\">PHP Mess Detector<\/a> is yet another one of those tools that help to keep the code base manageable and clean. \u00a0Here&#8217;s the description straight from the site:<\/p>\n<blockquote><p>What PHPMD does is: It takes a given PHP source code base and look for several potential problems within that source. These problems can be things like:<\/p>\n<ul>\n<li>Possible bugs<\/li>\n<li>Suboptimal code<\/li>\n<li>Overcomplicated expressions<\/li>\n<li>Unused parameters, methods, properties<\/li>\n<\/ul>\n<\/blockquote>\n<p>Here is how you can jump right in. \u00a0It&#8217;s super easy. \u00a0It only takes 6 steps.<\/p>\n<p><strong>Step 1: Pick a project to try it on.<\/strong><\/p>\n<p>You can use any of your own PHP projects, or grab one from GitHub. \u00a0It doesn&#8217;t matter. \u00a0You&#8217;ll know better where to apply it once you get comfortable with the tool. \u00a0For sake of this quick guide, I&#8217;ll use one of our Open Source repositories &#8211; <a href=\"https:\/\/github.com\/QoboLtd\/cakephp-groups\">cakephp-groups plugin<\/a>.<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\ncd \/tmp\r\ngit clone\u00a0git@github.com:QoboLtd\/cakephp-groups.git\r\ncd cakephp-groups\r\n<\/pre>\n<p><strong>Step 2: Install PHPMD with composer.<\/strong><\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\ncomposer require phpmd\/phpmd\r\n<\/pre>\n<p><strong>Step 3: Run PHPMD.<\/strong><\/p>\n<p>If you run &#8220;<em>.\/vendor\/bin\/phpmd<\/em>&#8220;, you&#8217;ll see a help screen. But what&#8217;s the purpose of this blog post if you have to read the manual, right? So, let me simplify it for you. PHPMD needs three parameters:<\/p>\n<ol>\n<li>Path to the PHP source code that it will be examining. \u00a0We&#8217;ll use &#8220;<em>src\/<\/em>&#8220;.<\/li>\n<li>Report format &#8211; one of: <em>xml<\/em>, <em>text<\/em>, or <em>html<\/em>. \u00a0We&#8217;ll use &#8220;<em>html<\/em>&#8220;.<\/li>\n<li>A choice of mess detection rules that you want it to apply. \u00a0You can create your own, or you can pick one from:\u00a0<em>cleancode<\/em>, <em>codesize<\/em>, <em>controversial<\/em>, <em>design<\/em>, <em>naming<\/em>, <em>unusedcode<\/em>. \u00a0We&#8217;ll use &#8220;<em>unusedcode<\/em>&#8220;.<\/li>\n<\/ol>\n<p>Also, we&#8217;ll give it an extra one: &#8220;<em>&#8211;reportfile<\/em>&#8220;, because by default PHPMD will spit everything to the standard output. \u00a0So, let&#8217;s put it together and see what we&#8217;ve got.<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\nphpmd src\/ html unusedcode --reportfile phpmd.html\r\n<\/pre>\n<p><strong>Step 4: Examine the report.<\/strong><\/p>\n<p>After running PHPMD command above, you&#8217;ll find a <em>phpmd.html<\/em> file in the same folder. Here&#8217;s how it looked for me, when open in the browser.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"27118\" data-permalink=\"https:\/\/mamchenkov.net\/wordpress\/2016\/12\/12\/quick-and-easy-introduction-into-php-mess-detector-phpmd\/mess-detector\/\" data-orig-file=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/12\/mess-detector.png?fit=772%2C153&amp;ssl=1\" data-orig-size=\"772,153\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"PHP mess detector\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/12\/mess-detector.png?fit=660%2C131&amp;ssl=1\" class=\"aligncenter size-medium wp-image-27118\" src=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/12\/mess-detector-500x99.png?resize=500%2C99&#038;ssl=1\" alt=\"PHP mess detector\" width=\"500\" height=\"99\" srcset=\"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/12\/mess-detector.png?resize=500%2C99&amp;ssl=1 500w, https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/12\/mess-detector.png?resize=768%2C152&amp;ssl=1 768w, https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2016\/12\/mess-detector.png?w=772&amp;ssl=1 772w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>So, PHPMD found one problem in the &#8220;<em>src\/Shell\/Task\/ImportTask.php<\/em>&#8221; file on line 93. \u00a0Here&#8217;s the relevant piece of code:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n    protected function _getImportErrors($entity)\r\n    {\r\n        $result = &#x5B;]; \r\n        if (!empty($entity-&gt;errors())) {\r\n            foreach ($entity-&gt;errors() as $field =&gt; $error) {\r\n                if (is_array($error)) {\r\n                    $msg = implode(', ', $error);\r\n                } else {\r\n                    $msg = $errors;\r\n                }\r\n                $result&#x5B;] = $msg . ' &#x5B;' . $field . ']';\r\n            }\r\n        }\r\n\r\n        return $result;\r\n    }\r\n<\/pre>\n<p>As you can see (line 09 above is line 93 in the report), the issue reported by the PHPMD is a typo in the variable name. It should be <em>$error<\/em>, not <em>$errors<\/em>.<\/p>\n<p><strong>Step 5: Fix the problem.<\/strong><\/p>\n<ul>\n<li>Rename the <em>$errors<\/em> variable to <em>$error<\/em>.<\/li>\n<li>Rerun the PHPMD report as per Step 3.<\/li>\n<li>Examine report as per Step 4 to make sure that the problem is fixed and no new issues were introduced.<\/li>\n<li>Create a new branch.<\/li>\n<li>Commit the code.<\/li>\n<li>Push the branch to GitHub.<\/li>\n<li>Create the <a href=\"https:\/\/github.com\/QoboLtd\/cakephp-groups\/pull\/24\">Pull Request<\/a>.<\/li>\n<\/ul>\n<p>All of the above mini\u00a0steps took\u00a0about 7 seconds.<\/p>\n<p><strong>Step 6: Pour yourself a drink.<\/strong><\/p>\n<p>You&#8217;ve just learned how to use a new tool, found a bug, and submitted a patch to the Open Source project. \u00a0At least I hope you did.<\/p>\n<p>Not bad at all.<\/p>\n<p>If you are wondering what to do next, here are a few suggestions:<\/p>\n<ul>\n<li>Try running PHPMD for other types of issues. \u00a0As I said, it supports\u00a0cleancode, codesize, controversial, design, naming, unusedcode, and we&#8217;ve only ran it for the &#8220;unusedcode&#8221;. \u00a0See what else is there.<\/li>\n<li>Integrate PHPMD into your projects, to run automatically, together with your unit tests. \u00a0You do have automated unit tests, right?<\/li>\n<li>Customize the ruleset that PHPMD is using to find more\/less issues, which are maybe more specific to your project.<\/li>\n<li>Use your newly acquired knowledge to fix issues with more Open Source projects. \u00a0You&#8217;ll make a name for yourself and you&#8217;ll make a world a better place.<\/li>\n<\/ul>\n<p>Let me know how it goes.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>PHP Mess Detector is yet another one of those tools that help to keep the code base manageable and clean. \u00a0Here&#8217;s the description straight from the site: What PHPMD does is: It takes a given PHP source code base and look for several potential problems within that source. These problems can be things like: Possible &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2016\/12\/12\/quick-and-easy-introduction-into-php-mess-detector-phpmd\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Quick and easy introduction into PHP Mess Detector (PHPMD)<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Quick and easy introduction into PHP Mess Detector (PHPMD) #PHP #WebDev #BestPractices #testing #QA","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,18,62,1334],"tags":[3069,38,3074,1108,1330],"keyring_services":[],"class_list":["post-27113","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","category-technology","category-web-work","tag-best-practices","tag-php","tag-quality-assurance","tag-testing","tag-web-development"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":27454,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/26\/phpqa-all-in-one-analyzer-cli-tool\/","url_meta":{"origin":27113,"position":0},"title":"PHPQA all-in-one Analyzer CLI tool","author":"Leonid Mamchenkov","date":"March 26, 2017","format":false,"excerpt":"PHPQA all-in-one Analyzer CLI tool. \u00a0This project bundles together all the usual PHP quality control tools, and then some. \u00a0It simplifies the installation and configuration of the tools and helps developers to push up the quality control bar on their projects. The tools currently included are: PHP Parallel Lint PHP\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2017\/03\/phplint-500x390.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":27105,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/12\/11\/php-static-analysis-tool-discover-bugs-in-your-code-without-running-it\/","url_meta":{"origin":27113,"position":1},"title":"PHP Static Analysis Tool &#8211; discover bugs in your code without running it!","author":"Leonid Mamchenkov","date":"December 11, 2016","format":false,"excerpt":"Ond\u0159ej Mirtes shares the idea behind the creation of\u00a0PHPStan - a static analysis tool for PHP: Compiled languages need to know about the type of every variable, return type of every method etc. before the program runs. This is why the compiler needs to make sure that the program is\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":43022,"url":"https:\/\/mamchenkov.net\/wordpress\/2019\/08\/21\/php-vs-p\/","url_meta":{"origin":27113,"position":2},"title":"PHP vs P++","author":"Leonid Mamchenkov","date":"August 21, 2019","format":false,"excerpt":"If you haven't heard the news yet and are wondering what the heck P++ is, here's a quick update for you. There's been some noise around the idea of creating a separate dialect of PHP, code-named P++. The idea is an attempt to find a compromise between two different schools\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":29219,"url":"https:\/\/mamchenkov.net\/wordpress\/2019\/01\/14\/new-in-php-7-4\/","url_meta":{"origin":27113,"position":3},"title":"New in PHP 7.4","author":"Leonid Mamchenkov","date":"January 14, 2019","format":false,"excerpt":"\"New in PHP 7.4\" is a quick and simple introduction into changes and new features of the upcoming PHP 7.4, which is planned for release in December of 2019. In brief: Preloading (parsed source code caching)Typed propertiesImproved type varianceFFI (Foreign Function Interface) - a way to run C code from\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":28559,"url":"https:\/\/mamchenkov.net\/wordpress\/2018\/05\/08\/composer-plugin-qa-composer-plugin-for-php-quality-assurance-tools\/","url_meta":{"origin":27113,"position":4},"title":"composer-plugin-qa &#8211; Composer Plugin for PHP Quality Assurance Tools","author":"Leonid Mamchenkov","date":"May 8, 2018","format":false,"excerpt":"composer-plugin-qa is a Composer plugin which adds all the most popular PHP quality assurance tools as composer scripts, so that you don't have to install and set them up one by one.\u00a0 The list of tools includes the following: PHPUnit: Testing Framework PHPCOV: CLI frontend for the\u00a0PHP_CodeCoverage Paratest: Parallel testing\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2018\/05\/composer-plugin-qa-500x296.jpeg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":27879,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/08\/28\/clean-code-solid-principles-applied-to-php\/","url_meta":{"origin":27113,"position":5},"title":"Clean Code SOLID principles applied to PHP","author":"Leonid Mamchenkov","date":"August 28, 2017","format":false,"excerpt":"clean-code-php is an excellent set of examples for the SOLID principles as applied to PHP programming: Software engineering principles, from Robert C. Martin's book\u00a0Clean Code, adapted for PHP. This is not a style guide. It's a guide to producing readable, reusable, and refactorable software in PHP. Not every principle herein\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/27113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=27113"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/27113\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=27113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=27113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=27113"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=27113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}