<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Meathanjay Baidya</title>
    <description>The latest articles on DEV Community by Meathanjay Baidya (@meathanjay).</description>
    <link>https://dev.to/meathanjay</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F46725%2F75ece6d0-704c-4609-b14f-c9fd5ddccf4d.jpeg</url>
      <title>DEV Community: Meathanjay Baidya</title>
      <link>https://dev.to/meathanjay</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/meathanjay"/>
    <language>en</language>
    <item>
      <title>Improve Laravel code readability using PHPDoc</title>
      <dc:creator>Meathanjay Baidya</dc:creator>
      <pubDate>Fri, 17 Apr 2020 21:36:04 +0000</pubDate>
      <link>https://dev.to/meathanjay/improve-laravel-code-readability-using-phpdoc-2670</link>
      <guid>https://dev.to/meathanjay/improve-laravel-code-readability-using-phpdoc-2670</guid>
      <description>&lt;p&gt;If you're writing PHP lately, very likely to know the Laravel framework, very easy to get started, and more likely, you can develop the same MVP app at the lowest time in compared to other frameworks. It has rich built-in and ready-to-use packages and easy configuration and huge community support, and all while you don't have to compromise the performance.&lt;/p&gt;

&lt;p&gt;If you're writing Laravel applications, you know how extensively Laravel uses PHP's magic methods, especially in Eloquent. &lt;/p&gt;

&lt;p&gt;You never explicitly mention a Model's properties except making some fields guarded and mass-assignable. When you try to access that property, the &lt;code&gt;__get&lt;/code&gt; method looks for database columns with the same name or return null; you access any defined relation as property in the same way.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GxxroaXP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mx1zu0h895rtwioo98fb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GxxroaXP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mx1zu0h895rtwioo98fb.png" alt="Property accessed via magic method"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even &lt;code&gt;$user-&amp;gt;is_avaiable&lt;/code&gt; is a boolean property, IDE shows mixed because you never declared that and your IDE can't help with auto-completion or if you mistyped a character.&lt;/p&gt;

&lt;p&gt;And, how you know a property is available in that model class? Well, in your head, because you created the migration, database schema; What about the new developers or your future-self?&lt;/p&gt;

&lt;p&gt;Your model is not so readable, and no one can guess &lt;code&gt;$user-&amp;gt;is_available&lt;/code&gt; or &lt;code&gt;$users-&amp;gt;projects&lt;/code&gt; exists or type until looking into columns and parent classes.&lt;/p&gt;

&lt;p&gt;So, why not make your code self-explanatory and let your IDE help you avoid making a mistake and help your future self?&lt;/p&gt;

&lt;p&gt;PHPDoc can help you add a piece of information in a code block like file, class, properties, methods, and variables.&lt;/p&gt;

&lt;p&gt;PHPDoc is the same as multiline comments but starts with a forward-slash and two asterisks(&lt;code&gt;/**&lt;/code&gt;) and ends with an asterisk and forward-slash(&lt;code&gt;*/&lt;/code&gt;), in-between you add those missing information in PHPDoc DSL language.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Domain\User&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;Illuminate\Database\Eloquent\Relations\HasMany&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;Illuminate\Foundation\Auth\User&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;Authenticatable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * User Model
 *
 * @property bool $is_available
 * @property-read Project[] $projects
 * @method static User create(array $attributes = [])
 * @method static \Illuminate\Database\Eloquent\Builder where($column, $operator = null, $value = null, $boolean = 'and')
 */&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Authenticatable&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$guarded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'email_verified_at'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'remember_token'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$hidden&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'password'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'remember_token'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;projects&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HasMany&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;hasMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Project&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, those missing properties and methods are self-explanatory, and you know precisely what their type and return value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9pK5rR1D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ua5izk328lkvsf9lm4mc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9pK5rR1D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ua5izk328lkvsf9lm4mc.png" alt="IDE suggestion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Declaring return type for &lt;code&gt;create&lt;/code&gt; method in the User model, let you avoid re-declaring return type every-time you call &lt;code&gt;create&lt;/code&gt; and it makes your code clean and maintainable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;
&lt;span class="nv"&gt;$user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// instead&lt;/span&gt;

&lt;span class="cd"&gt;/** @var User $user */&lt;/span&gt;
&lt;span class="nv"&gt;$user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>php</category>
      <category>laravel</category>
      <category>phpdoc</category>
      <category>typehint</category>
    </item>
  </channel>
</rss>
