<?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 João Brandão on Medium]]></title>
        <description><![CDATA[Stories by João Brandão on Medium]]></description>
        <link>https://medium.com/@joaorbrandao?source=rss-b120ee1bb1cb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dDeGf2VIULaXM-Zz-vfptQ.jpeg</url>
            <title>Stories by João Brandão on Medium</title>
            <link>https://medium.com/@joaorbrandao?source=rss-b120ee1bb1cb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 02:45:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@joaorbrandao/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[ Happy Teams with Coding Dojos]]></title>
            <link>https://medium.com/geekculture/happy-teams-with-coding-dojos-de1ff74de23?source=rss-b120ee1bb1cb------2</link>
            <guid isPermaLink="false">https://medium.com/p/de1ff74de23</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[dojo]]></category>
            <category><![CDATA[developer]]></category>
            <dc:creator><![CDATA[João Brandão]]></dc:creator>
            <pubDate>Tue, 22 Mar 2022 18:33:02 GMT</pubDate>
            <atom:updated>2022-03-23T05:31:36.270Z</atom:updated>
            <content:encoded><![CDATA[<p>Building and powering up software development teams is not just about hiring good developers. It’s also about keeping them happy, challenged and engaged.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jet9DMpgoArmSWu2" /><figcaption>Photo by <a href="https://unsplash.com/@h4x0r3?utm_source=medium&amp;utm_medium=referral">Thao Le Hoang</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Over the past years, I’ve been pushing for the teams I work with to share the knowledge between their members. I’ve done it through <em>Knowledge Sharing Sessions</em>, <em>Hands-On Sessions</em> and more recently with <em>Coding Dojos</em>. All of these have had great advantages for teams but I noticed that <em>Coding Dojos</em> have not only a great technical and social impact but they’re also a great contribution to the team member’s happiness.</p><p>Let’s see two of the <em>Coding Dojos</em> fundamental pieces and my favourite tools to help with them.</p><blockquote><em>“A Coding Dojo is a meeting where a bunch of coders get together to work on a programming challenge.“ — </em><a href="http://codingdojo.org/"><em>codingdojo.org</em></a></blockquote><h3>Challenges</h3><p>As per definition, the main goal is to tackle a programming challenge. <strong>The challenge is what you want to solve.</strong> There are so many different ones that, for me usually the most difficult part is choosing one.</p><p>Choosing a challenge always depends on what exactly you want to <strong>technically tackle</strong>. I tend to choose according to what I see and understand from the team’s daily work: difficulties, improvements or new topics to keep the learning going.</p><p>Here are a couple of topics that usually challenges want to tackle:</p><ul><li>SOLID principles</li><li>Pair-programming</li><li>Refactoring</li><li>Test-Driven Development (TDD)</li><li>…</li></ul><p>And here are a couple of <em>Web</em> sites to help you pick them up:<br><a href="https://leetcode.com/">LeetCode</a><br><a href="https://dojopuzzles.com/">Dojo Puzzles</a><br><a href="https://kata-log.rocks/index.html">Kata-Log</a></p><h3>Constraints</h3><p>As I said initially, <em>Coding Dojos</em> are not just about the technical impact but social as well. <strong>Constraints are how you want the challenge to be solved.</strong> I like to call this the fun part of Dojos. They are a set of rules that participants must follow so the result is valid.</p><p>Here are a couple of constraints that I usually choose for Dojos:</p><ul><li>1 pilot (coding) + 1 co-pilot (helping) + rest in silence.</li><li>No passing of primitives between methods as parameters or return types.</li><li>Do not use “else”.</li><li>No more than 2 parameters per method.</li></ul><p>By adding constraints to the challenge we can:</p><ul><li>Enforce participants to choose specific technical topics to overcome the challenge.</li><li>Enforce collaboration and communication between participants.</li></ul><h3>Tools</h3><p>Currently, more and more teams work in remote environments so we should use the right tools to help us out. I’ve been mostly using a combination of an IDE/Code Editor and whatever provider the team uses for calls. The goal here is to have everyone writing and reading code at the same time and in the same place while they’re able to speak to one another.</p><p><a href="https://code.visualstudio.com/">VS Code</a> has been my number 1 choice for this. It allows multiple people to work on the code at the same time by using the <a href="https://marketplace.visualstudio.com/items?itemName=MS-vsliveshare.vsliveshare">Live Share</a> extension with very decent performance! Plus different developers use different IDEs and code editors and VS Code can run in a <em>Web</em> browser just by going to <a href="http://vscode.dev/">vscode.dev</a>. I mean, anyone can be up and running for the <em>Coding Dojo</em> in less than 5 minutes with pretty much everything they need.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q0wYufe3ax27n6QxlRwULQ.png" /><figcaption>VS Code — Host</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sC1vvS3l4D21a2ztlJAS1A.png" /><figcaption>VS Code — Participant in a <em>Web</em> browser</figcaption></figure><p>I have been using <em>Coding Dojos</em> mainly for three reasons:</p><ul><li>technical skills improvement</li><li>engaging the team to socialize and have fun together</li><li>bring community trends and best practices in</li></ul><p>We pick up a challenge which is about what we want to tackle on the technical side. The challenge we choose is usually related to topics we want the team to improve or learn based on topics like TDD, SOLID principles, refactoring techniques and many more!</p><p>Solving problems to technically improve people and teams is great. But there’s nothing like having fun doing it! To do that, we usually add constraints to the challenge to impact how people interact with each other.</p><p>So now go and try it yourself! If you do, let me know about the outcome. 👋</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de1ff74de23" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/happy-teams-with-coding-dojos-de1ff74de23">🥋 Happy Teams with Coding Dojos</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Given, When, Then]]></title>
            <link>https://medium.com/geekculture/given-when-then-326d86a3c165?source=rss-b120ee1bb1cb------2</link>
            <guid isPermaLink="false">https://medium.com/p/326d86a3c165</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[qa]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[João Brandão]]></dc:creator>
            <pubDate>Wed, 22 Sep 2021 08:11:48 GMT</pubDate>
            <atom:updated>2021-09-24T07:06:24.444Z</atom:updated>
            <content:encoded><![CDATA[<p>In this post, let’s check <em>Given, When, Then</em>. A sequence of words that not only helped me to improve my way of thinking about coding but also has been helping me in my personal life.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MV0bKT4Ziqajw-8t" /><figcaption>Photo by <a href="https://unsplash.com/@alex_tsl?utm_source=medium&amp;utm_medium=referral">Alexandra</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Background</h3><p>I started to think more about <em>Given, When, Then</em> when I got into software testing. In <a href="https://joaorbrandao.medium.com/hero-to-zero-ba8de7f47828">this post</a>, I told you that used to spend a lot of time improving my coding skills by watching videos in <a href="https://laracasts.com/">Laracasts</a>. There are a couple of testing video series in there and in almost all of them, they write the tests in the same way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2789ec9a40352605f178503bf8cd340f/href">https://medium.com/media/2789ec9a40352605f178503bf8cd340f/href</a></iframe><p>If you think about this, you can cover so much ground with such a simple way of thinking. And if you structure your tests in the same way, you end up with code that is very simple and easy to understand, making them very straight to the point.</p><p>I started to think and use more and more this concept. Not only for testing but pretty much for all steps of a feature’s lifecycle.</p><p>Let’s jump into a technical example.</p><h3>Technical Example</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2c9H0OJKkIoDmuUt" /><figcaption>Photo by <a href="https://unsplash.com/@mikepetrucci?utm_source=medium&amp;utm_medium=referral">Mike Petrucci</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Let’s imagine that you and I have a Pet Store website and, during this month, we have a 10% discount on our Premium subscription. So, every time a free account user login we want to show them a message saying:</p><blockquote>Hey 👋 Subscribe to Premium now with a 10% discount!</blockquote><p>At this point and technically speaking, we want to create an endpoint from where we can get a subscription discount for a given user: GET /v1/discounts</p><h4>Planning</h4><p>You’re the one creating this endpoint! We start by getting together and creating a working ticket. In there, we want to ensure that we include something called <strong>Acceptance Criteria</strong>. When you finish your work, you’ll pass the ticket to me. I’ll be responsible to test it and ensuring that what you implemented covers every use case that we wrote on that Acceptance Criteria. Let’s check the ticket!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f3abc7e559106e7ed9d03b0c3a3cec3/href">https://medium.com/media/5f3abc7e559106e7ed9d03b0c3a3cec3/href</a></iframe><h4>Development &amp; Testing</h4><p>You picked up the ticket from the queue and put your hands to work! You ended up developing a pretty simple logic.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5979279d65ece431ec7fb9883cf1541a/href">https://medium.com/media/5979279d65ece431ec7fb9883cf1541a/href</a></iframe><p>In the DiscountsController, you added a getSubscriptionDiscountForUser method. That method receives the User object resolved from a token passed in the request (@UserToken() decorator will do that for you). You return undefined when the user.subscription is already premium or a new instance of Discount with the value.</p><p>To ensure that this works as expected, you created two tests for the new code:</p><ul><li>The first one verifies that you get the 10% discount when a user subscription is free.</li><li>The second one verifies that you get no discount when a user subscription is a premium.</li></ul><p>Let’s see those tests!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46ca5d3e522bff29e56ffd6c3540ad0d/href">https://medium.com/media/46ca5d3e522bff29e56ffd6c3540ad0d/href</a></iframe><p>Nice one! I noticed that you used a similar approach to the one in the Acceptance Criteria of the ticket to create your tests.</p><h4>QA</h4><p>You finished your work and passed me the ticket for some QA. Let’s bring that ticket back!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f3abc7e559106e7ed9d03b0c3a3cec3/href">https://medium.com/media/5f3abc7e559106e7ed9d03b0c3a3cec3/href</a></iframe><p>Ok! So we need 2 users: one in a free account and a second in a premium account. After getting those, I’ll use an HTTP client tool and execute two requests:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c4e16d973767b3612cf907e1517886a/href">https://medium.com/media/3c4e16d973767b3612cf907e1517886a/href</a></iframe><p>Perfect! Now I got the 10% discount for the user in a free account and got nothing for the user in the premium account.</p><p>As you probably noticed, we used this <em>Given, When, Then</em> concept in different steps: planning, development, testing and QA. This is something that is not oriented to testing but a way of thinking that can help you through many steps.</p><p>Let’s take a look at this concept applied to non-technical matters.</p><h3>Applied to Personal Life</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wGPNLHE_wm0wcrvVqwzYMQ.jpeg" /><figcaption>Pookie &amp; Quino</figcaption></figure><p>As I started to use this Given, When, Then daily at work, I noticed that I was also applying this in my personal life.</p><p>If you pay a little attention to your thoughts over your day, you’ll notice that, without even planning on it, probably your brain is already behaving in that way.</p><p>I’ll give you one of my examples! A couple of months ago, my partner and I were thinking about adopting a second cat. At first, I was very reluctant about it as I was afraid of <em>Pookie</em> (first cat) not receiving the kitten well. As for any important decision, I started to think more about it: upsides and downsides. As I was listing all of these points in my head I started to structure them in a Given, When, Then way, like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5bdf7c68813a7386495fefab8ad3cb8a/href">https://medium.com/media/5bdf7c68813a7386495fefab8ad3cb8a/href</a></iframe><h3>Conclusion</h3><p><em>Given, When, Then</em> are just three simple words that combined create this simple and logical way of thinking. It is something that your brain does every day without you even noticing. You just need to be more aware. As it naturally occurs, If you take advantage of that flow of thinking you can easily apply it to pretty much anything.</p><p>As a software engineer, this has been helping me through most of the development cycle steps of a feature. It is also useful to improve team members’ communication by creating this kind of standard way of defining what is expected to happen. This way, developers can take advantage of that information to guide them through the coding process. Testers take that statement as their source of truth for what’s expected when testing a feature.</p><p>I hope you’ve enjoyed it, cheers! 👋</p><p>P.S. The cats are now each other besties! 🐈🐈</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=326d86a3c165" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/given-when-then-326d86a3c165">🧪 Given, When, Then</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hero to Zero]]></title>
            <link>https://joaorbrandao.medium.com/hero-to-zero-ba8de7f47828?source=rss-b120ee1bb1cb------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba8de7f47828</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[self-improvement]]></category>
            <category><![CDATA[career-development]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[João Brandão]]></dc:creator>
            <pubDate>Sun, 21 Mar 2021 20:37:30 GMT</pubDate>
            <atom:updated>2021-03-22T09:25:33.327Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’re learning something new within the programming world, most likely you’ll be able to find some tutorials with the title <strong>“Zero to Hero…”</strong>. It means that you’ll be learning everything you need to deliver some awesome work with it, from the scratch!</p><p>Now, let me tell you an opposite story: from hero to zero!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ArjR9f9mWLGhwZs_" /><figcaption>Photo by <a href="https://unsplash.com/@isaacmsmith?utm_source=medium&amp;utm_medium=referral">Isaac Smith</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>About a year and a half ago, I was working as a developer for almost 3 years at a big company. By that time, the team was building the new version of its main software. You can probably see what this means: we were redesigning the whole architecture for the system. What a challenge!</p><p>At the same time, we wanted to migrate from the legacy no framework PHP 5.4 to a brand new monolithic Laravel application. Right from the beginning, I fell in love with Laravel and started to push myself to learn more about it day after day. This made me subscribe to <a href="https://laracasts.com/">Laracasts</a>, a platform to push your web development skills to the next level. At night, I started watching <a href="https://twitter.com/jeffrey_way">Jeffrey Way</a> diving into Laravel while washing dishes. By day, I was applying the concepts that I was learning: design patterns, tips and tricks, everything!</p><p>A year later, I was working on every step of a feature’s life cycle: requirements, architecture design, coding, deploying and sometimes presenting it to clients. This hero kind of feeling started to arise — doing anything with a small amount of effort. In other words, it felt like I wasn’t learning and improving my skills and myself as much as I wanted. Pure coincidence, a recruiter reached out to me with this: You’ll be designing, building and maintaining APIs and workers in a microservices architecture, using Node.js and Kotlin, plus deploying your code through the CI/CD process. Quoting our beloved friend Yoda: <em>“Much to learn, you still have”</em>!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3ohuAxV0DfcLTxVh6w%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3ohuAxV0DfcLTxVh6w%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F3ohuAxV0DfcLTxVh6w%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="188" frameborder="0" scrolling="no"><a href="https://medium.com/media/14a34b03360c761ec0680ee5b8a34834/href">https://medium.com/media/14a34b03360c761ec0680ee5b8a34834/href</a></iframe><p>I have to admit: I was so scared of just thinking of moving to a different job. Even more at the time that Covid made us all go into lockdown. Everything felt so uncertain. Even though, I went for it. It was tough, but at the end of March 2020, I joined a small company, which meant: moving from a ~4k people working place and always working at the office to a completely remote startup environment. I felt I was <strong>back to zero</strong> — learning everything from scratch: new programming languages, frameworks and DevOps stuff that I had never worked before in my life. But everyone on the team was so available to share their knowledge that it kept me going, and most importantly, wanting to keep on this learning journey. The feeling of learning new things day after day and my brain having difficulties executing some tasks… It was such a good feeling! At the same time, the impostor syndrome hitting me. I think that at some point, we all go through this — time passes and you feel like you should be better by then. I still feel it sometimes.</p><p>After a year in this role, I’m back at architecture design, coding, deploying. Node.js and TypeScript became the default for my brain and I started to explore more of the JVM world with Kotlin and Spring. I learned so much in the past year and I still feel that I have so much to learn.</p><p>It’s not the road from zero to hero that makes you improve. Neither the other way around. All of those different situations have a certain weight on your life’s learning journey. On yourself! I’m still very eager to keep going, learning, improving but I’m also so grateful for everything and everyone. After all, learning is an ongoing process. We’ll never know everything.</p><p>This is my retrospective. It doesn’t mean that you have to go through all of that to feel the same. You are responsible for your path, needs and happiness.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba8de7f47828" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Self-reference Laravel Model]]></title>
            <link>https://joaorbrandao.medium.com/self-reference-laravel-model-fa8a7b37360d?source=rss-b120ee1bb1cb------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa8a7b37360d</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[relationships]]></category>
            <category><![CDATA[model]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[laravel]]></category>
            <dc:creator><![CDATA[João Brandão]]></dc:creator>
            <pubDate>Sun, 14 Feb 2021 15:38:00 GMT</pubDate>
            <atom:updated>2021-07-19T16:21:07.476Z</atom:updated>
            <content:encoded><![CDATA[<h3>First things first!</h3><p>A year or two ago, when I was a Software Engineer at Bosch a colleague of mine came with this amazing solution of a self-reference trait to map relationships inside a single SQL database table. He wrote a small but very effective piece of code and then I just added the ability to change the name of the database column responsible to keep that relationship.<br><strong>So, all props to </strong><a href="https://github.com/sergiog95"><strong>Sérgio</strong></a><strong> here!</strong> 👊👏</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*l8rHyJnaaLZiu3Eu" /><figcaption>Photo by <a href="https://unsplash.com/@daiga_ellaby?utm_source=medium&amp;utm_medium=referral">Daiga Ellaby</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Use case</h3><p>Big companies tend to have big nested levels of hierarchy organization. So the main goal here was to create a departments database table to allow people to manage how many nested levels of departments they want. Let&#39;s see the example.</p><pre>- Department<br>  - Section 1<br>    - Team 1<br>    - Team 2<br>  - Section 2<br>...</pre><p>You can imagine this scaling up to Country Department, Europe Department, or even if they’d like to create groups of people inside a team with a large number of people. Well, we wanted to provide our end users the freedom to organize their teams as they wish!</p><p>Let’s use the next set of data for future examples!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*anvSAQsANmLYIr-sPLSwgA.png" /><figcaption>Departments Table</figcaption></figure><h3>Show me the code!</h3><p>Let’s dive into the code then! The following Gist contains the Trait that will be used in the models on which we want to apply the self-reference relationship. Take a look! 👀</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ad842c290d34e0e7bfb792b3f4ed1dc/href">https://medium.com/media/4ad842c290d34e0e7bfb792b3f4ed1dc/href</a></iframe><p>As you can see, it is a small piece of code but powerful enough to get the job done.</p><p>When using Laravel’s model <a href="https://laravel.com/docs/8.x/eloquent-relationships#defining-relationships">relationship methods</a> they will return the relationship itself (BelongsTo, HasMany, ...). But if we access it like a property, Laravel will automatically get us an instance of the related entity!</p><p>Let’s make use of our brand new Trait in our Department model.</p><pre>class Department extends Model {<br>	use SelfReferenceTrait;<br>}</pre><h3>The parent method</h3><p>The parent method defines a belongsTo relationship. This means that a department can belong to another one.</p><p>parent will return a Department instance, when the current department has another department above or it will return null when there are no more departments above.</p><pre>// Let&#39;s get &#39;Development&#39;<br>$department = Department::find(3);</pre><pre>$parent = $department-&gt;parent; // Product</pre><h3>The children method</h3><p>The children method defines a hasMany relationship. This means that a department can have many other departments below it.</p><p>children will return a Collection containing all the departments on the next level below. In other words, a list of departments that parent_id equals the id of the current model. If there are no departments above, then the collection returned will be empty.</p><pre>// Let&#39;s get &#39;Development&#39;<br>$department = Department::find(3);</pre><pre>$children = $department-&gt;children; // Backend and Frontend</pre><h3>The allChildren method</h3><p>The allChildren method takes advantage of Laravel&#39;s relationship method with to eager load a nested relationship. But in this case, instead of redefining the relationship inside our new method, we use the already defined children method to get the next nested level and then eager load our own allChildren relationship, again! <em>&quot;Say whaaat?!&quot;</em> 🙃</p><p>Well, let me make it easier: we loop through all nested levels until all children are loaded! 😄</p><pre>// Let&#39;s get &#39;Backend&#39;<br>$department = Department::find(5);</pre><pre>$allChildren = $department-&gt;allChildren; // Develpment &gt; Backend and Frontend</pre><h3>The root method</h3><p>The root method is pretty simple: it checks if the current department belongs to any other and if yes, then it repeats that process until it finds the record on that tree that has a parent_id value of null.</p><pre>// Let&#39;s get &#39;Backend&#39;<br>$department = Department::find(5);</pre><pre>$root = $department-&gt;root; // Product</pre><p>I hope that this is as useful for you as it was for our team! Feel free to use it! 🚀</p><p>Cheers! 👋</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa8a7b37360d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Automatically Format PHP Code]]></title>
            <link>https://joaorbrandao.medium.com/automatically-format-php-code-af6d495b3823?source=rss-b120ee1bb1cb------2</link>
            <guid isPermaLink="false">https://medium.com/p/af6d495b3823</guid>
            <category><![CDATA[pipeline]]></category>
            <category><![CDATA[ci]]></category>
            <category><![CDATA[linter]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[João Brandão]]></dc:creator>
            <pubDate>Thu, 28 Jan 2021 19:35:10 GMT</pubDate>
            <atom:updated>2021-07-13T23:05:30.741Z</atom:updated>
            <content:encoded><![CDATA[<p>I love to have everything organized and to keep the code as clean as possible. And to do this by hand it’s just really boring… And let’s be honest it’s also something from the past century because there are a lot of useful tools to beautifully help us keeping our beautiful code organized! 🙌</p><p>In this post, I’ll show you how do I setup my PHP projects to be automatically formatted. 👀</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pbS73ZvoXirnpaIl" /><figcaption>Photo by <a href="https://unsplash.com/@rush_intime?utm_source=medium&amp;utm_medium=referral">rashid khreiss</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>1. Setup Formatter</h3><h4>1.1. Install the package <a href="https://github.com/FriendsOfPHP/PHP-CS-Fixer">php-cs-fixer</a></h4><pre>composer require friendsofphp/php-cs-fixer</pre><h4>1.2. Create a .php_cs file on the root of your project</h4><pre>&lt;?php<br><br>$finder = \PhpCsFixer\Finder::create()<br>    -&gt;in(__DIR__)<br>    -&gt;exclude([&#39;bootstrap&#39;, &#39;storage&#39;, &#39;vendor&#39;])<br>    -&gt;name(&#39;*.php&#39;)<br>    -&gt;ignoreDotFiles(true)<br>    -&gt;ignoreVCS(true);<br><br>return PhpCsFixer\Config::create()<br>    -&gt;setRules([<br>        &#39;@PSR2&#39; =&gt; true,<br>        &#39;array_syntax&#39; =&gt; [&#39;syntax&#39; =&gt; &#39;short&#39;],<br>        &#39;ordered_imports&#39; =&gt; [&#39;sortAlgorithm&#39; =&gt; &#39;length&#39;],<br>        &#39;no_unused_imports&#39; =&gt; true,<br>    ])<br>    -&gt;setFinder($finder);</pre><p>In this example, here’s what will happen:</p><ul><li>bootstrap, storage and vendor directories will not be formatted.</li><li>All *.php files will be considered.</li><li><a href="https://www.php-fig.org/psr/psr-2/">PRS2</a> rules are the ones that are going to be used!</li><li>Arrays like array() will be transformed to [].</li><li>Import statements will be ordered by their length to give that pyramid effect.</li><li>Unused imports will be removed.</li></ul><p><strong>Check the package documentation and change according your needs!</strong></p><h4>1.3. Add a script to your composer.json file</h4><pre>&quot;scripts&quot;: {<br>	&quot;format&quot;: &quot;vendor/bin/php-cs-fixer fix&quot;<br>}</pre><h4>1.4. Run it</h4><pre>composer format</pre><p>Check the following basic example!</p><pre>&lt;?php</pre><pre>namespace Package\\Services;</pre><pre>// Unwanted space here!<br>use Package\\Box;<br>// This should be the last!<br>use Illuminate\\Support\\Facades\\Session as LaravelSession; <br>use Package\\Contracts\\SomeInterface;</pre><pre>class SessionService implements SomeInterface<br>{<br>    // Unwanted space here!<br>    public function load(): Box<br>    {<br>        return LaravelSession::has(Box::class)<br>          ? LaravelSession::get(Box::class)<br>          : new Box();<br>    }<br>}</pre><ul><li>Only 1 space after namespace.</li><li>LaravelSession use line moved to the final import because of its length.</li><li>Removed an empty line before load function.</li><li>Added an empty line to the end of the file.</li></ul><pre>&lt;?php</pre><pre>namespace Package\\Services;</pre><pre>use Package\\Box;<br>use Package\\Contracts\\SomeInterface;<br>use Illuminate\\Support\\Facades\\Session as LaravelSession;</pre><pre>class SessionService implements SomeInterface<br>{<br>    public function load(): Box<br>    {<br>        return LaravelSession::has(Box::class)<br>          ? LaravelSession::get(Box::class)<br>          : new Box();<br>    }<br>}</pre><h3>2. Make it run automatically</h3><p>Now you can run it using composer. But that only means you can run the command… by hand! Ideally would be to add this to your CI/CD pipeline alongside your tests! But most of the times, at least for me, I don’t have a pipeline setup for really small projects like packages.</p><p>So I just use husky-php! This is a package for PHP to work with git hooks just like NPM’s husky.</p><h4>2.1. Install <a href="https://github.com/joaorbrandao/husky-php">husky-php</a></h4><pre>composer require --dev ccinn/composer-husky-plugin ccinn/husky-php</pre><h4>2.2. Add the hooks to composer.json</h4><p>Check the package’s documentation if you want to use other hooks! Normally I just use pre-commit for formatting and pre-push for testing.</p><pre>{<br>  &quot;hooks&quot;: {<br>    &quot;pre-commit&quot;: &quot;composer format&quot;,<br>  }<br>}</pre><p>Now when you commit your code, composer format will run automatically! 🚀</p><p>I really hope that this is as useful for you as it was for me! 👋</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af6d495b3823" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>