<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mohamad Eldhemy on Medium]]></title>
        <description><![CDATA[Stories by Mohamad Eldhemy on Medium]]></description>
        <link>https://medium.com/@imdhemy?source=rss-5daa42a79273------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4Io6lG0K6EHDVvbETmU1ZA.jpeg</url>
            <title>Stories by Mohamad Eldhemy on Medium</title>
            <link>https://medium.com/@imdhemy?source=rss-5daa42a79273------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 06:29:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@imdhemy/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The What-Why-How Guide of PHP code quality tools]]></title>
            <link>https://medium.com/teknasyon-engineering/the-what-why-how-guide-of-php-code-quality-tools-6eaa6406859?source=rss-5daa42a79273------2</link>
            <guid isPermaLink="false">https://medium.com/p/6eaa6406859</guid>
            <category><![CDATA[psalms]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[code-quality]]></category>
            <category><![CDATA[php-cs-fixer]]></category>
            <dc:creator><![CDATA[Mohamad Eldhemy]]></dc:creator>
            <pubDate>Tue, 05 Apr 2022 12:46:55 GMT</pubDate>
            <atom:updated>2023-03-19T19:22:28.170Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8ZKOG9gKH5GdnwBTXrnd0A.png" /></figure><p>I can’t remove from my mind the memory of one of my PRs wasn’t approved to add an empty line before the return statement. It was a complex PR, I did hard work, added smart solutions, followed the best practices, submitted my PR, and started to wait for the applause 👏 Finally, what I got was:</p><blockquote>Please, add an empty line before the return statement! ~ Taras Andrusiak — somewhere in Ukraine</blockquote><p>Needless to say, the code styling should be automated to allow the programmers to focus on the code itself. That project was a legacy one and looks like they didn’t need that tools when they started, but over time the code base has grown, and the team members as well. This is why I submitted another PR to automate following the code standards of our team.</p><p>Not only the code style but there are also a million and one code quality tools out there (take for example <a href="https://github.com/exakat/php-static-analysis-tools">this list</a>) that are trying to analyse the code for different aspects. They all share a common goal though: to improve the code quality.</p><p>My advice to you is don’t think too much about which one to use, pick the one that you feel good with and fits with your project requirements. All of them have the same target, but only with different/same approaches. It doesn’t matter which one you use, using it the right way is what matters!</p><p>This advice doesn’t close the door to change your mind later, if you feel that you need to change your approach, or a tool that you are using is not working for you, you can always change it. It’s not a big deal, adapt to your needs.</p><p>Let me show you my picks.</p><h3>Discover Bugs</h3><p><strong>What are static code analysis tools?</strong></p><p>There are many static code analysis tools that help you to discover the bugs in your code. The big players are <a href="https://phpstan.org/">PHPStan</a>, <a href="https://psalm.dev/">Psalm</a>, and <a href="https://github.com/phan/phan">PHAN</a>.</p><p>Static code analyzers simply read the code and try to find errors in it. They can perform both very simple and obvious checks (for example, the existence of classes, methods, and functions, and more cunning ones (for example, looking for type mismatch, race conditions, or code vulnerabilities).</p><p>The key is that analyzers do not execute code, They analyze the text of the program and check it for typical errors.</p><p>My favorite one is Pslam, let me show you how to use it.</p><p><strong>How to use Psalm to find bugs in your code?</strong></p><p>You can add Psalm to your project by using Composer:</p><pre>composer require --dev vimeo/psalm</pre><p>Generate the config file:</p><pre>./vendor/bin/psalm --init</pre><p>This command will generate an XML file psalm.xml , it will guess an errorLevel after initially analyzing your code. You can <a href="https://psalm.dev/docs/running_psalm/configuration">tweak the configs</a> to suit your requirements and expectations.</p><p>Add the following command to your composer.json scripts to run Psalm:</p><pre>{<br>//...<br>    &quot;require-dev&quot;: {<br>        &quot;vimeo/psalm&quot;: &quot;^4.22&quot;<br>    },<br>    &quot;scripts&quot;: {<br>        &quot;psalm&quot;: &quot;./vendor/bin/psalm --show-info=true&quot;<br>    }<br>}</pre><p>I do care about warnings not only errors, so I added the show info flag. To run that script, use:</p><pre>composer psalm</pre><p><strong>So, Let’s answer the question of Why?</strong></p><p>Below is an output example:</p><pre>ERROR: UndefinedThisPropertyAssignment - src/Foo.php:7:9 - Instance property Foo::$foo is not defined (see <a href="https://psalm.dev/040">https://psalm.dev/040</a>)<br>        $this-&gt;foo = $foo;</pre><p>Your code will run, but bugs will appear somewhere on production. Especially for Psalm, it gives you a very good WHY Q/A. Do you notice that (see <a href="https://psalm.dev/040">https://psalm.dev/040</a>)? For any Error or Warning, it will give you a guide, that will help you know more about the problem and how to fix it.</p><h3>Code style tools</h3><p><strong>WHAT are the coding standards tools, and WHY do we use them?</strong></p><p>The motivation behind creating code style is to make sure developers write based on the guideline thus it will be able to be read by other developers. Besides, it also helps to avoid developers creating mistakes while writing code. Most programming languages have more than one code style, for PHP I prefer to follow the <a href="https://symfony.com/doc/current/contributing/code/standards.html">Symfony code standards</a> .</p><p>As usual, there are many tools to maintain the code style. The big players are <a href="https://github.com/squizlabs/PHP_CodeSniffer">PHP Code Sniffer</a> &amp; <a href="https://github.com/FriendsOfPHP/PHP-CS-Fixer">PHP CS Fixer</a>. Pick whatever you want!</p><p>My favorite tool is PHP CS Fixer.</p><p><strong>How to add PHP CS Fixer to your project?</strong></p><p>You can add CS Fixer to your project by using Composer:</p><pre>composer require --dev friendsofphp/php-cs-fixer</pre><p>To avoid the command-line options, you need to create a configuration file .php-cs-fixer.dist.php in the root directory of your project.</p><p>To make a long story short, I’m sharing my configuration file. It follows the Symfony code standards and checks both the src and tests directories. You can add more directories to this list, or exclude others.</p><pre>&lt;?php<br><br>use PhpCsFixer\Config;<br>use PhpCsFixer\Finder;</pre><pre>// Directories to check<br>$includedPatterns = [<br>    __DIR__ . &#39;/src&#39;,<br>    __DIR__ . &#39;/tests&#39;,<br>];</pre><pre>$rules = [<br>    &#39;@Symfony&#39; =&gt; true,<br>     // &gt; PHPUnit    <br>    &#39;php_unit_method_casing&#39; =&gt; [&#39;case&#39; =&gt; &#39;snake_case&#39;],    <br>    // &gt; Strict    <br>    &#39;declare_strict_types&#39; =&gt; true,    <br>    // &gt; Operator    <br>    &#39;not_operator_with_successor_space&#39; =&gt; true,    <br>    // &gt; Cast Notation    <br>    &#39;cast_spaces&#39; =&gt; [&#39;space&#39; =&gt; &#39;none&#39;],    <br>    // &gt; Import    <br>    &#39;global_namespace_import&#39; =&gt; [        <br>           &#39;import_classes&#39; =&gt; true,        <br>           &#39;import_constants&#39; =&gt; false,        <br>           &#39;import_functions&#39; =&gt; false,    <br>        ],<br>];</pre><pre>// Finder initialization<br>$finder = Finder::create()<br>    -&gt;in($includedPatterns)<br>    -&gt;name(&#39;*.php&#39;)<br>    -&gt;notName(&#39;.*.blade.php&#39;)<br>    -&gt;ignoreDotFiles(true)<br>    -&gt;ignoreVCS(true);</pre><pre>// Config initialization<br>$config = new Config();<br>$config-&gt;setRules($rules);<br>$config-&gt;setFinder($finder);<br><br>return $config;</pre><p>You can find <a href="https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/rules/index.rst">more rules</a> in the PHP CS Fixer docs.</p><p>Let’s add a script to our composer file the same as we did with Psalm.</p><pre>{<br>//...<br>    &quot;require-dev&quot;: {<br>        &quot;friendsofphp/php-cs-fixer&quot;: &quot;^3.0&quot;<br>    },<br>    &quot;scripts&quot;: {<br>        &quot;format&quot;: &quot;vendor/bin/php-cs-fixer fix --allow-risky=yes&quot;,<br>        &quot;check-style&quot;: &quot;vendor/bin/php-cs-fixer fix --dry-run&quot;<br>    }<br>}</pre><p>To run the script use:</p><pre>composer format</pre><p>If you need to check the coding style only without any fix, use:</p><pre>composer check-style</pre><p>You can add it to your pipelines to maintain the coding style.</p><p><strong>WHY should you automate the coding style?</strong></p><p>It’s more vital to automate the process of verifying (and sometimes even fixing) the style so you can focus on the code itself.</p><p>Trying to describe code style to someone who isn’t a coder frequently leads to blank looks and raised eyebrows. Arguing over trailing spaces appears to be a waste of time. And quite properly so!</p><p>Code style standards are essential, especially if you work in a team. But you don’t want to bring them up in every code review. Instead, you want to settle on a style once and then move on.</p><h3>Summary</h3><p>Code quality tools validate your code for consistency with a coding standard of your choice. You can appoint one of the predefined coding standards or use your own previously defined coding standard. If you are using the awesome PHPStorm and you should, you can easily <a href="https://www.jetbrains.com/help/phpstorm/php-code-quality-tools.html">integrate it</a> with the code quality tools.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6eaa6406859" width="1" height="1" alt=""><hr><p><a href="https://medium.com/teknasyon-engineering/the-what-why-how-guide-of-php-code-quality-tools-6eaa6406859">The What-Why-How Guide of PHP code quality tools</a> was originally published in <a href="https://medium.com/teknasyon-engineering">Teknasyon Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>