<?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: Daniele Lenares</title>
    <description>The latest articles on DEV Community by Daniele Lenares (@fatrex).</description>
    <link>https://dev.to/fatrex</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%2F25231%2F3b672f78-83d2-4ece-bfeb-c1d0b891f45b.jpg</url>
      <title>DEV Community: Daniele Lenares</title>
      <link>https://dev.to/fatrex</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fatrex"/>
    <language>en</language>
    <item>
      <title>Traits in Laravel: an elegant way to reuse code</title>
      <dc:creator>Daniele Lenares</dc:creator>
      <pubDate>Mon, 18 Mar 2024 08:02:55 +0000</pubDate>
      <link>https://dev.to/fatrex/traits-in-laravel-an-elegant-way-to-reuse-code-7kl</link>
      <guid>https://dev.to/fatrex/traits-in-laravel-an-elegant-way-to-reuse-code-7kl</guid>
      <description>&lt;p&gt;Traits offer an elegant way to reuse code, providing increased flexibility and ease of maintenance in your php and Laravel projects. In this article, we will explore what a trait is, how to use it, and the benefits it can offer.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Trait?
&lt;/h2&gt;

&lt;p&gt;A trait is a collection of methods that can be reused across different classes. Traits can contain methods, constants, or even property declarations. A class can use multiple traits, allowing you to combine functionality from different sources without having to extend a single parent class or implement an interface.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using a Trait
&lt;/h2&gt;

&lt;p&gt;To use a trait in any php project, simply use the &lt;code&gt;use&lt;/code&gt; keyword followed by the name of the trait within the class you want to enrich with its methods. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;ApiResponse&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="n"&gt;sendResponse&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="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;response&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;json&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Traits\ApiResponse&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;ApiResponse&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="n"&gt;someMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Rest of method that creates a $data variables&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="nf"&gt;sendReponse&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="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;In this example, the &lt;code&gt;Post&lt;/code&gt; class uses the &lt;code&gt;ApiResponse&lt;/code&gt; trait. Now, the &lt;code&gt;Post&lt;/code&gt; class will have access to all the methods defined inside the &lt;code&gt;ApiResponse&lt;/code&gt; trait as if they were defined directly within the class itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of Traits
&lt;/h2&gt;

&lt;p&gt;Traits offer numerous benefits, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Code Reusability&lt;/strong&gt;: Traits allow you to separate code logic into smaller, reusable units. This promotes better code organization and reduces duplication.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Modularity&lt;/strong&gt;: Traits enable you to add functionality to a class without creating a complex class hierarchy. You can combine multiple traits to achieve the desired combination of features.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Simplified Maintenance&lt;/strong&gt;: By using traits, you can make changes to a specific functionality in one place, rather than having to modify every class that implements that functionality. This simplifies maintenance and reduces the risk of errors.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Traits are a powerful tool that php provides to developers, making code more modular, reusable, and maintainable. They allow you to separate code logic into smaller units and combine them flexibly.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>DoNPP - A PHP Docker starter</title>
      <dc:creator>Daniele Lenares</dc:creator>
      <pubDate>Wed, 05 May 2021 17:25:31 +0000</pubDate>
      <link>https://dev.to/fatrex/donpp-a-php-docker-starter-3o3i</link>
      <guid>https://dev.to/fatrex/donpp-a-php-docker-starter-3o3i</guid>
      <description>&lt;p&gt;I started working as a web developer in 2010 using mostly PHP as main programming language. Gradually, as the years passed by, I completely switched to a full-stack JavaScript experience and by the end of 2017 I completely removed PHP from my stack.&lt;br&gt;
&lt;a href="https://i.giphy.com/media/m9eG1qVjvN56H0MXt8/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/m9eG1qVjvN56H0MXt8/source.gif" alt="bye"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But I'm a &lt;em&gt;curious person&lt;/em&gt; and a &lt;strong&gt;very curious dev&lt;/strong&gt;... I need to know what is going on in the whole dev ecosystem!&lt;br&gt;
So I wanted to try some shiny new or updated PHP framework or cms.&lt;/p&gt;

&lt;p&gt;The only downside is that I &lt;em&gt;do not want&lt;/em&gt; to install PHP directly on my machine and risk to mess up something with the &lt;em&gt;world of dependencies&lt;/em&gt; needed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/KWzzTbkhDvmQU/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/KWzzTbkhDvmQU/source.gif" alt="crazy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So I built a quick and simple docker ecosystem containing PHP, Nginx and Postgres without messing with my local setup.&lt;/p&gt;

&lt;p&gt;If you want to give it a spin, you can find it here&lt;br&gt;
&lt;a href="https://github.com/dnlnrs/donpp"&gt;https://github.com/dnlnrs/donpp&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>docker</category>
      <category>showdev</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Lint your commits</title>
      <dc:creator>Daniele Lenares</dc:creator>
      <pubDate>Mon, 19 Apr 2021 08:34:22 +0000</pubDate>
      <link>https://dev.to/fatrex/lint-your-commits-2p54</link>
      <guid>https://dev.to/fatrex/lint-your-commits-2p54</guid>
      <description>&lt;p&gt;If you are using &lt;a href="https://www.conventionalcommits.org"&gt;Conventional Commits&lt;/a&gt; (and if you don't, &lt;em&gt;you should&lt;/em&gt; 😜) it is very useful to have something that tells you if you are committing the right way.&lt;br&gt;
This topic will be focused on &lt;strong&gt;JavaScript projects&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As you know, conventional commits are composed this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type(scope?): subject
body?
footer?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's easy to do a commmit message that don't represent this standard so linting comes in our help!&lt;/p&gt;

&lt;p&gt;We are going to use two packages: &lt;code&gt;commitlint&lt;/code&gt; and &lt;code&gt;husky&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://commitlint.js.org"&gt;commitlint&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;This package checks if the commit messages are in the form showed above, or at least in a form &lt;code&gt;type: subject&lt;/code&gt;. It's easily configurable through file so its configuration is shareable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;extends&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@commitlint/config-conventional&lt;/span&gt;&lt;span class="dl"&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;With the package installed and configured we are telling the code that we would like our commits to be "conventional".&lt;br&gt;
But we need to ask commitlint to lint our messages.&lt;br&gt;
Can we do this automatically everytime we make a new commit?&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;a href="https://typicode.github.io/husky/#/"&gt;husky&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Husky is a package that interact with the hooks exposed by git to trigger some custom actions: linters, error checking, scripts running, etc...&lt;br&gt;
In this case we would like to trigger a &lt;code&gt;commitlint&lt;/code&gt; check everytime we do a commit wothout the needing of launch the lint manually.&lt;br&gt;
Fortunately the community comes in our help and we need to launch only two commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn husky install

npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;At the end this setup will make sure that everytime we commit, the linter gets called and all checks are made.&lt;/p&gt;

&lt;p&gt;And if we commit without following the rules, this happens (VSCode example)&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HXhFpD95--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/odex4wga2zapm2fowbaq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HXhFpD95--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/odex4wga2zapm2fowbaq.png" alt="alt text" width="800" height="586"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;⧗   input: &lt;span class="nb"&gt;test &lt;/span&gt;commit
✖   subject may not be empty &lt;span class="o"&gt;[&lt;/span&gt;subject-empty]
✖   &lt;span class="nb"&gt;type &lt;/span&gt;may not be empty &lt;span class="o"&gt;[&lt;/span&gt;type-empty]

✖   found 2 problems, 0 warnings
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>git</category>
      <category>tutorial</category>
      <category>javascript</category>
      <category>bestpractice</category>
    </item>
  </channel>
</rss>
