<?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 Mantas Donelavicius on Medium]]></title>
        <description><![CDATA[Stories by Mantas Donelavicius on Medium]]></description>
        <link>https://medium.com/@mantasd?source=rss-5aca9256779------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*8Io8Ijyvf1G2OQ6q.</url>
            <title>Stories by Mantas Donelavicius on Medium</title>
            <link>https://medium.com/@mantasd?source=rss-5aca9256779------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 10:12:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mantasd/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[Are Google engineers paid for LoC?]]></title>
            <link>https://medium.com/@mantasd/are-google-engineers-paid-for-loc-9f50c40f73f9?source=rss-5aca9256779------2</link>
            <guid isPermaLink="false">https://medium.com/p/9f50c40f73f9</guid>
            <category><![CDATA[google]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[lol]]></category>
            <dc:creator><![CDATA[Mantas Donelavicius]]></dc:creator>
            <pubDate>Sun, 16 Feb 2020 11:49:51 GMT</pubDate>
            <atom:updated>2020-02-16T11:49:51.078Z</atom:updated>
            <content:encoded><![CDATA[<p>Instead of “microtime(true);” they wrote this gem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*uQC8eCTT3nawDblF6cUYLQ.png" /></figure><p>Code from: <a href="https://github.com/GoogleCloudPlatform/grpc-gcp-php/blob/master/cloudprober/grpc_gpc_prober/spanner_probes.php#L24">https://github.com/GoogleCloudPlatform/grpc-gcp-php/blob/master/cloudprober/grpc_gpc_prober/spanner_probes.php#L24</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f50c40f73f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Laravel APM (performance monitor)]]></title>
            <link>https://medium.com/@mantasd/laravel-apm-performance-monitor-b0dad8e9fd13?source=rss-5aca9256779------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0dad8e9fd13</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[apm]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Mantas Donelavicius]]></dc:creator>
            <pubDate>Mon, 27 Jan 2020 19:59:23 GMT</pubDate>
            <atom:updated>2023-07-20T13:47:07.914Z</atom:updated>
            <content:encoded><![CDATA[<h3>Enhance Your Laravel Application’s Performance with Laravel-APM</h3><p>In the ever-evolving world of web development, the key to success lies in delivering optimal performance and a seamless user experience. Laravel, being one of the most popular PHP frameworks, empowers developers to build robust and feature-rich applications. However, as your Laravel project grows, identifying areas for optimization becomes essential. This is where Laravel APM (Application Performance Monitoring) comes into play. In this article, we will explore how Laravel APM can revolutionize the way you monitor and optimize your Laravel applications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/705/0*u4wh7vTcvb8BNn1T" /></figure><p>What we see above — is one page that is generating almost 50% load on the live website server. By optimizing it, we can theoretically reduce the server load by 50%.</p><p>Package: <a href="https://github.com/mantas-done/laravel-apm">https://github.com/mantas-done/laravel-apm</a></p><h4>What is Laravel APM?</h4><p>Laravel APM is a free performance monitoring package crafted specifically for Laravel applications. Its primary objective is to enable developers to monitor the execution times of requests, cron jobs, and queue tasks in production environments. By doing so, Laravel APM empowers developers to gain valuable insights into their application’s performance, identify bottlenecks, and optimize resource-intensive pages</p><h4>Seamless Integration and Installation</h4><p>One of the major attractions of Laravel APM is its simplicity in terms of installation and integration. With support for Laravel versions 5.5 and above, getting started is incredibly straightforward:</p><p>Begin by installing the package using Composer:</p><pre>composer require mantas-done/laravel-apm</pre><p>Securely add the APM route to your routes/web.php file:</p><pre>Route::get(&#39;/apm&#39;, &#39;\Done\LaravelAPM\ApmController@index&#39;)-&gt;name(&#39;apm&#39;);</pre><p>To keep log files organized, add a scheduled job to clear logs daily in App/Console/Kernel.php:</p><pre>$schedule-&gt;command(&#39;apm:clear&#39;)-&gt;daily();</pre><p>Enable APM in your .env file:</p><pre>APM=1</pre><h4>Real-time Monitoring and Optimization</h4><p>Laravel APM boasts a highly efficient logging system that records every user request to a dedicated file (storage/app/apm/apm-2020-01-01.txt). This design decision ensures that monitoring adds a negligible overhead of less than 1 ms to each request, guaranteeing minimal impact on your application&#39;s performance.</p><p>The package’s ability to parse log files in real-time enables developers to gain quick insights into the resource usage of different pages. By correlating page views and resource utilization, you can identify which pages contribute most significantly to the server load. Armed with this information, you can prioritize optimization efforts and make data-driven decisions to enhance your application’s efficiency.</p><h4>Data-Driven Performance Improvement</h4><p>At the heart of Laravel APM lies the power of data. With continuous logging of request times, developers gain access to real-time performance metrics. This not only helps in spotting trends but also enables quick identification of anomalies. By grouping all requests by URL and viewing overall statistics, you can efficiently pinpoint areas that require attention, leading to a substantial boost in your application’s performance.</p><h4>Conclusion</h4><p>In the competitive realm of web development, optimizing your Laravel application’s performance is of paramount importance. Laravel APM provides a powerful solution for developers to identify and rectify performance bottlenecks with ease. With its seamless installation, real-time monitoring capabilities, and minimal impact on production, Laravel APM is the go-to choice for web developers looking to elevate their application’s performance.</p><p>Don’t wait to reduce your website’s load time and server load! Embrace the power of Laravel APM and unlock the true potential of your Laravel applications. Start integrating Laravel APM into your project today and experience the transformative impact it can bring.</p><p>For more information and to get started with Laravel APM, visit the official GitHub repository: <a href="https://github.com/mantas-done/laravel-apm">https://github.com/mantas-done/laravel-apm</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0dad8e9fd13" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Laravel performance overhead]]></title>
            <link>https://medium.com/@mantasd/laravel-performance-overhead-46109c1d08e0?source=rss-5aca9256779------2</link>
            <guid isPermaLink="false">https://medium.com/p/46109c1d08e0</guid>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Mantas Donelavicius]]></dc:creator>
            <pubDate>Wed, 08 Jan 2020 21:11:09 GMT</pubDate>
            <atom:updated>2020-01-08T21:11:09.568Z</atom:updated>
            <content:encoded><![CDATA[<p>Today I will try to investigate and illuminate (pun intended) me and a few curious people how much overhead does Laravel framework cause on the website performance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*j9k_9ZlEIP8IAUZO7br4VQ.png" /></figure><p>When using Homestead on Windows the website loads in about 2 seconds. During this time I could probably walk a dog, make dinner, wash the dishes… Oh, I don’t have a dog. Oh well, let’s get back to the website. If just finished loading. 2s is an eternity to wait.</p><h4>1. enable NFS on Homestead + Windows</h4><p>As you probably know you need to enable NFS<strong> </strong>or you will be in a lot of pain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*HhbdVsdzZSKNHIprksOJBA.png" /></figure><p>After enabling NFS, the load time is reduced to 1s. By the way, this website has 1000 routes and a lot of code. So it is not a typical demo website.</p><h4>2. disable debugbar</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*BLU6tRXexEs6L7WQyh8vIg.png" /></figure><p>How could I forgot disabling it before? Now we have shaved another 0.3s. By the way, did I mentioned that the homepage doesn’t make any database queries? For this experiment session and cache drivers a set to file.</p><h4><strong>3. artisan optimize</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*kOUdto_MZzE0GQEMaLX2Cw.png" /></figure><p>400 ms — not to bad. I feel that we are getting somewhere. But it is still to slow. The limiting factor is currently my PC setup. What I mean is Windows and Linux NFS setup is slowing us down. If only there were a simple way to use a different server? Actually there is!</p><p><strong>4. php artisan serve — executed from Windows</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*fAfFYns3ZfEP8VSf6tzefg.png" /></figure><p>0.2s — nice. Only Borat probably would say that. But what the freaking runny red nose? I thought that running a server directly on Windows and by reading files without transferring over NFS would increase performance much more. I definitely saw websites running in production much faster.</p><p>So what could be wrong? I have downloaded the official PHP 7.2 64-bit build from the PHP website. Extracted it to a folder and run it. Let’s try using php.ini-production file. Strange, but it makes no difference.</p><h4>5. enable opcache</h4><p>Somehow I had assumed that opcache is enabled by default. Just give me a second, I will go and google that.</p><p>A few moments later…</p><p>Seems that it should be enabled by default from PHP 5.5. But whatever. Let’s enable it.</p><p>A few years later…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/309/1*6LbWGVHM_IBOEPYlXxPc0g.png" /></figure><p>50 ms. I should be happy. But enabling opcache wasn’t fun. How should I have known that I need to add “zend_extension” instead of “extension”? That is probably the biggest mystery to me.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/280/1*glMp2cV3kgRYaGcdJSVs4w.png" /></figure><p>But let’s get back to results. It is still not enough. You probably know me by this time. I always want more.</p><h4>6. php artisan optimize 2</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/246/1*9J9RZQtBOQE8-IF_poSi4Q.png" /></figure><p>I thought that we already did that? Yes and no at the same time. We did it on Homestead. When I started the server on windows I used <em>artisan optimize:clear</em>. I have run few tests and the medium of load times are 48 ms without <em>optimize and </em><strong><em>43 ms</em></strong><em> with optimize. So we gained 5 ms. I wouldn’t call it a huge success, but every millisecond counts. I was expecting more from caching routes and cache files. Live and learn, live and learn… And don’t believe everything you read on the internet.<br>By the way, do you remember that I mentioned that this website has 1000 routes. Fun fact nr. 2: it started on Laravel 5.0 and was keeping up with updates and currently runs the newest version — 6.0.</em></p><h4>7. new laravel instalation</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/354/1*eUzTvA-HWczfPB4FU27oIg.png" /></figure><p>Mmm, there is nothing better than a fresh smell of Laravel. The median of load times is 17 ms.</p><p><strong>8. plain PHP</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/343/1*2w6JXOStkIDHo02N8dELDg.png" /></figure><p>Speed. More speed. I want it all! <br>By running a single index.php file with output saying just “ok” we finally reached a holy grail — 4 ms. And I want to sadly announce that our fun journey must end here.</p><h4>Here lies our answer: 13 ms.</h4><p>What I mean is, Laravel probably causes an overhead of 13 ms (17 — 4) on a new installation.</p><p>On an old website, Laravel takes 20 ms to get to the controller. But you also need to take into account that there is some custom middleware. And one caveat, not all pages can be rendered without the database connection.</p><p>No fireworks? No smart words? Yeah, probably there is nothing more to say here. I expected to find some enlightenment on this journey. But as it usually happens, when you stand on the top of a hill, all the roads seem clearly visible. I hope that you won’t miss our next dungeon and dragons meeting. Next time it is your turn to bring snacks. Don’t forget that!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=46109c1d08e0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Letter to Santa from PHP developer]]></title>
            <link>https://medium.com/@mantasd/letter-to-santa-from-php-developer-76047e246316?source=rss-5aca9256779------2</link>
            <guid isPermaLink="false">https://medium.com/p/76047e246316</guid>
            <category><![CDATA[developer]]></category>
            <category><![CDATA[new-features]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[laravel]]></category>
            <dc:creator><![CDATA[Mantas Donelavicius]]></dc:creator>
            <pubDate>Sun, 17 Dec 2017 12:53:48 GMT</pubDate>
            <atom:updated>2017-12-17T12:53:48.282Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello Santa,</p><p>This year I was a good boy. I haven’t tested code in production. Tried to fix bugs as fast as they arise and not leaving them for later. I have crashed the server only once!</p><p>Recently I saw a boy named Java who was playing with a new hot toy — Short Closures. I asked my parents to buy it. But unfortunately in our city this toy isn’t available yet.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pBdhI8QAS95c43SN." /></figure><p>So I put a little elbow grease and from sticks and duct tape I made a similar thing for PHP. I know that not every boy saw this new toy yet, so let me tell you a bit more about it.</p><h4>When Short Closures are useful?</h4><p>If you have watched <a href="https://youtu.be/crSUWtRYw-M?t=33m53s">Adam Wathan talk on YouTube</a> there he is refactoring code from loops to collections. His final code looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*5IUq356hmPGer6VmSoCn8Q.png" /></figure><p>To me it is still not good enough. Probably that’s why I rarely write it like so. The main problem is that code is cluttered with anonymous function syntax. This syntax doesn’t add any value to code readability. What if we could use Short Closures in PHP?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/524/1*dVcog7tGEtg-bndmXMwerw.png" /></figure><p>That would be nice, right?</p><p>Actually I have created a small library to show that it is possible: <a href="https://github.com/mantas-done/short-closures">https://github.com/mantas-done/short-closure</a>s</p><p>I don’t want much this year for Christmas. My only wish is that every year would be a bit easier for every developer than the previous one. I heard that some of them aren’t making much and probably are starving. If you can, bring them some vegetables, because some say that eating “spaghetti” is not very healthy. And about this new toy and my wishlist Santa, if you really really exist, I would really want:</p><ol><li>Implementation of my proposed library in Laravel Collection</li><li>Implementation of real <a href="https://wiki.php.net/rfc/short_closures">short closures in PHP</a></li></ol><p>Yours good boy,<br>Mantas</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=76047e246316" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why is my User class so fat?]]></title>
            <link>https://medium.com/@mantasd/why-is-my-user-class-so-fat-23f991b443d6?source=rss-5aca9256779------2</link>
            <guid isPermaLink="false">https://medium.com/p/23f991b443d6</guid>
            <category><![CDATA[refactoring]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[mvc]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[laravel]]></category>
            <dc:creator><![CDATA[Mantas Donelavicius]]></dc:creator>
            <pubDate>Wed, 15 Nov 2017 20:19:14 GMT</pubDate>
            <atom:updated>2017-11-15T20:19:14.319Z</atom:updated>
            <content:encoded><![CDATA[<ul><li>Yo User class is so fat, when it went to the beach, all the whales started singing “We Are Family.”</li><li>Yo User class is so fat, it took me two trains, a plane, and a bus to get to it’s good side.</li><li>Yo User class is so fat, when God said “Let there be light,” he asked it to move out of the way.</li></ul><p>Enough with mama jokes. Answer is quite simple. Most of things in your website are connected to User class. That’s why it is very convenient to put methods and logic into it.</p><p>Is it bad to have a big User class? Probably no, if you are comfortable with it, there is no problem. <br>Let’s examine how User class becomes fat and ways to prevent it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/0*xU8829_-81MosWi3.jpg" /></figure><p><strong>How User class becomes fat?</strong></p><p>One day at a time. Sorry, couldn’t hold the joke. But really, how User class becomes big? Reality is that you add one method. After some time you will add another method. After several months editor is counting lines in hundreds. Few years go by and by that time you already counting methods by hundreds instead of lines. Then you start to wonder what happened? Where things went wrong? How I became fat...?</p><h4><strong>1. Example</strong></h4><p>Let’s imagine that we have new Laravel project. Inside of it there is empty User class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/1*h55r7vssvq_K0QUOXfFohg.png" /></figure><p>Everything seems so peaceful and perfect. One day somebody decides that your website needs admin. Let’s add that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*JWLomsgxYb-Kjq9pzuCDzg.png" /></figure><p>One additional kilogram on the scale, nothing to worry about, I am still fit and can enjoy potato chips. One method in User class. Everything is still good. But after some time you need moderator on your website.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/1*c-VqHfTtxIQBTAmRVnwFYg.png" /></figure><p>And now you need to sent to admin email if moderator edited something.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/409/1*OHepHbUPk_3YNZOgEbXw0Q.png" /></figure><p>You see how we are getting fat?</p><h4>2. What to do?</h4><p>The answer is simple, but not easy to implement. You need to go on a diet! Probably you have heard it hundred of times. But you say it doesn’t work. <br>Of-course it is a lot of hard work, sweat and discipline. This is how you lose weight or lines from your file.</p><p>Do you really need to have everything in User class? If sendEmailToAdmin() is called only once from somewhere, can we extract it to another class?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/471/1*hTvynSBtHrveZdQCjVEpNg.png" /></figure><p>Maybe some logic can be abstracted? Like combining similar methods?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/438/1*QNSofN1dawcT4PHy2K5z3g.png" /></figure><p>That looks a bit better but still not perfect. Maybe we can extract that logic to separate class also?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*QcbCevO3LEuR5cMNDRvkxA.png" /></figure><p>Now User class is much shorter. Good job. Let’s have a pizza. While you have eated that deliciuos slice of pizza, you are fat again. That’s what they call Jo-Jo dieting. Keeping User class slim is constant work. It is quite easy to add new methods. But it is much harder to remove them.</p><p>How we made User class slim? Basically we moved rarely used methods to separate classes. Also we combined logic of similar functions.</p><h4>Skinny Fat</h4><p>So in conclusion I can only say that you need to watch your belly fat constantly. I am just kidding, you need to watch only your User class. Belly fat doesn’t matter while you are programming. Unless you can’t reach your keyboard. But that’s not my expertise.</p><p>If you are happy how you User class looks like, be proud of it! Fat is not bad, it’s just different. The most important thing is keeping balance. Too fat or too skinny isn’t good.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23f991b443d6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Proper way to use Laravel Eloquent]]></title>
            <link>https://medium.com/@mantasd/proper-way-to-use-laravel-eloquent-3ca194e2b766?source=rss-5aca9256779------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ca194e2b766</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[model]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[eloquent]]></category>
            <category><![CDATA[clean-code]]></category>
            <dc:creator><![CDATA[Mantas Donelavicius]]></dc:creator>
            <pubDate>Wed, 15 Nov 2017 16:19:19 GMT</pubDate>
            <atom:updated>2017-11-15T17:23:11.506Z</atom:updated>
            <content:encoded><![CDATA[<p>Organizing code is always hard, especially Models. When you are reading Laravel documentation it can be a bit misleading. It is easy to follow, but it doesn’t mention how to organize your code. Those who are programming for several years should already know all the things that will be said in this article.</p><p>Usually most MVC frameworks have 3 folders: Models, Views and Controllers. In Laravel 5 Taylor removed Models folder. He thought that people are misusing it. In my opinion he made an error. Those who know how to organize code - will organize it. And those who don’t - they will use the default settings (models are generated in app/ folder).</p><p>So the main question you should have: <strong>What is model in MVC?</strong></p><p>Let’s look at 3 ways how Eloquent can be used.</p><h4>1. Fast and Dirty</h4><p>Easiest way to write code is to put it directly in Controller. It is used for simple code where there is not much logic. Let’s take a simple blog example. You can create post and later you can publish it to be visible to everybody.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/1*8FSMJuCwIXHoKHqMbq4V_w.png" /></figure><p>This code is simple, fast to write and best suited when you are making simple things. When you logic becomes more complicated it can quickly become spaghetti code.</p><p>When doing things this way, your Eloquent Post class doesn’t contain any logic.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/186/1*Hyu2c29QvwHobak1xnhIYA.png" /></figure><h4>2. Optimal</h4><p>This time we will move all the logic into Eloquent class. Now controller code is simpler.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*eYyPNa92aYbs_WKKwc4HEw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/1*EDiltbD3LbhTmorWfpa8Ww.png" /></figure><p>This way of doing things is optimal. It removes your logic from controller and moves it into the model.</p><p>But there is one small disadvantage, you can still call Eloquent methods in Controller:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*3bkKU_qrURAx0kOT2118zQ.png" /></figure><p>Of-course I know that I shouldn’t do like this if I chose the second method. But new developer can easily do that.</p><h4>3. Clean and Slow</h4><p>This method sometimes is called Service pattern. It is mostly used for objects that have a lot of complex logic. Controller<strong> </strong>will be totally separated from Eloquent. We will need one more file for that. But let’s look at the controller first.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*xUnLI2h5EfKlq2XtqNNFLQ.png" /></figure><p>I have created new plain PHP class and called it BlogPost. Controller can access only BlogPost class methods. Now controller doesn’t know about Eloquent existence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/398/1*Regey9ZE4pFMl8V9gVDKrg.png" /></figure><p>BlogPost class now contains all the logic. It also directly talks to Eloquent Post class. The most important thing is that BlogPost doesn’t extend anything.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/186/1*Hyu2c29QvwHobak1xnhIYA.png" /></figure><p>Eloquent Post class is left empty. You can still use it for relationships and scopes.</p><h3>File Structure</h3><p>Another question you might have is <strong>where to put your classes?</strong></p><p>For the 1 and 2 examples I would put Post class in Models folder</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/275/1*97V3w0Xq76fw8sU5rqA0TA.png" /></figure><p>For the 3 example, file structure will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*3EmythpHzYyAxVP3t5Q5ZQ.png" /></figure><p>Models folder file structure is similar to how /vendor folder is organized. Usually you have one main file per package that you can access. All other package files doesn’t matter to you. So in my opinion Models folder should be structured the same way.</p><h3><strong>Conclusion</strong></h3><p>First method is usually used for small projects. Third method is used when you don’t want Eloquent to leak into your controller.<br>I am mostly using 1 and 2 methods. 3 method is used quite rarely in my code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ca194e2b766" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Refactoring refactored code…]]></title>
            <link>https://medium.com/@mantasd/refactoring-refactored-code-d54e801001b4?source=rss-5aca9256779------2</link>
            <guid isPermaLink="false">https://medium.com/p/d54e801001b4</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[mvc]]></category>
            <dc:creator><![CDATA[Mantas Donelavicius]]></dc:creator>
            <pubDate>Fri, 03 Nov 2017 07:53:18 GMT</pubDate>
            <atom:updated>2017-11-03T07:53:18.277Z</atom:updated>
            <content:encoded><![CDATA[<p>There is an article series with examples how to refactor one code piece. But something seemed not right with this code. Maybe it was because the code wasn’t refactored yet? But no, last version of that code still seemed not right.</p><p><a href="https://hackernoon.com/php-software-architecture-part-4-refactoring-fd577eb6fe3f">Read original article here.</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*JyXm1GUy2p703nt5zOm5Gg.jpeg" /></figure><p>So let’s start with the problems of that code. This is author’s controller:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/705/1*Zna-ZKEOZHRt0sJB_-WSoQ.png" /><figcaption>Author’s final controller</figcaption></figure><p>If I am a new programmer in that company (or if the author comes to the same code after a year) will somebody remember how the code works? My favorite code to read is code that right away explains what it is doing.</p><p>What we see from author’s controller is only that we call some method that returns results. New programmer will need to dig dipper to understand what is happening.</p><p>I am suggesting doing this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*EMHYLMKk8xZLKbwPt3BkEw.png" /></figure><p>When viewing this code new programmer will know what his boss requirements were:<br>1. search API<br>2. log results to DB<br>3. log results to file</p><h4>1. Question: <strong>where is dependency injection?</strong></h4><p>Dependency injection is mostly used for unit tests. Author didn’t mentioned that he tests his code. But event if he tested, check this example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/525/1*YyGk7PBaCfIPBS-HpSL1Eg.png" /></figure><p>Does author know what his code returns? Probably not, he just dumps it into Database and hopes that it is correct. How can you use it later if you haven’t validated what you have saved?</p><p>So I am assuming that there are no unit tests. And for a new programmer it will be much easier to understand without all this indirection. Moreover you can easily add it back through constructor injection.</p><h4>2. Question: What if I want to log every time API is called in other places?</h4><p>Ah, good question, good question.</p><p>Instead of logging in controller, I would raise event in FlightApi class. Then DB logger and File logger would need to listen for that event and log the data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*sCebAE7XRzfy8Be1GT86Aw.png" /></figure><h3>Conclusion</h3><p>“Everything should be as simple as possible but no simpler” — that is one of my favorite quotes.</p><p>So for simple code like authors example I would not use repositories and adapters. In my opinion static method calls are the way to go.</p><p>Of-course, if at some time in future code becomes more complex, you will need to add abstraction. But for now, I don’t think it is necessary.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d54e801001b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Comparison of framework pagination]]></title>
            <link>https://medium.com/@mantasd/comparison-of-framework-pagination-661f7ee36715?source=rss-5aca9256779------2</link>
            <guid isPermaLink="false">https://medium.com/p/661f7ee36715</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[symfony]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[pagination]]></category>
            <dc:creator><![CDATA[Mantas Donelavicius]]></dc:creator>
            <pubDate>Mon, 30 Oct 2017 17:23:42 GMT</pubDate>
            <atom:updated>2017-10-30T17:23:42.432Z</atom:updated>
            <content:encoded><![CDATA[<p>It is quite interesting how different frameworks (ORM’s) implemented pagination. Frameworks exist to allows developers to save time by re-using generic modules in order to focus on other areas. So when one framework implements something in a better way, logical person would think that other frameworks will follow by implementing it in the same way to save you time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*_yL5T4DgISNJ3tPZQCEptg.jpeg" /></figure><p>I have looked at these frameworks: Laravel (Eloquent), Ruby on Rails (Active Record), CodeIgniter (<a href="https://codeigniter.com/userguide2/database/active_record.html">Active Record</a>), Symfony (Doctrine), Spring (Hibernate).</p><p>Usually pagination consists of 2 parts:<br>1. Query builder must have method that automatically paginates SQL query<br>2. And there must be a way to retrieve HTML of links to show them on a page</p><h4>Laravel</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*Y14snEFf5UP70GjgYFSI4A.png" /><figcaption>Controller</figcaption></figure><p>Laravel’s pagination method by default doesn’t need additional parameters. It automatically checks page URL and retrieves page number. So you just write one method and you are done with pagination.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*toRGrtVsBrEtNqWZjXG4hw.png" /><figcaption>View</figcaption></figure><p>In HTML you call links() method on your result set. Quite straightforward.</p><h4>Ruby on Rails</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/344/1*yue9s1S1QFIcoTJjkA4v0A.png" /><figcaption>Current RoR pagination</figcaption></figure><p>It is similar to Laravel pagination. RoR was the first to release pagination looking like this. The only complaint is that you need to install additional package to be able to use it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/1*B4m_eSnYkJ7jHQuJ7LH7Zg.png" /><figcaption>old RoR pagination</figcaption></figure><p>First version of RoR had built in pagination. But it used 2 separate parameters: one for results and another one for containing HTML links.</p><h4>CodeIgniter</h4><p>CodeIgniter never was a modern framework. But at least in version 4, which is still not completed, they started copying some good features.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/274/1*WtbfzIWnIwMUETIm1wv6eg.png" /><figcaption>Controller</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/169/1*C-7nk1ZI-SBqu8gyDo8xMQ.png" /><figcaption>View</figcaption></figure><p>The only problem is that they copied first version of RoR instead of current. They now need 2 variables. But it is still much better than what they had in 3 version of CodeIgniter:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*VyKy6oBtRTXI4Hil7MDPJw.png" /><figcaption>CodeIgniter 3</figcaption></figure><p>Old pagination was horrible. Example above can be made a bit simpler, but there is no point, it is still too complicated.</p><p><strong>Symfony</strong></p><p>Let’s take a look at Symfony’s documentation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*DFAeaqOS55S0gehxKLXxkw.png" /><figcaption>From Symfony documentation</figcaption></figure><p>Oh, for fuck’s sake Symfony, be a man and tell the truth. The truth is that it is not much better than old CodeIgniter code. You need to manually calculate how many results you need and on what page you are currently.</p><p><strong>Spring</strong></p><p>In Hibernate 5 they finally have this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/738/1*NwFKoslVQ5LK4fcmAXV2gw.png" /></figure><p>Until release of Hibernate 5 in 2015 year Hibernate loaded all SQL rows into Java. After that it filtered results using Java. Performance was not good.</p><h3>Conclusion</h3><p>So as you see the same functionality can be implemented in 1 word or it can be implemented as a little horror story for programmers before bed.</p><p>Of-course we can’t compare frameworks just by one parameter. But on the other hand you can see how much effort was put into making your framework. And if simple things aren’t simple in some frameworks, then what about complex things?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=661f7ee36715" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SOLID is a lie]]></title>
            <link>https://medium.com/@mantasd/solid-is-a-lie-3e89ab3813a?source=rss-5aca9256779------2</link>
            <guid isPermaLink="false">https://medium.com/p/3e89ab3813a</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[solid]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Mantas Donelavicius]]></dc:creator>
            <pubDate>Sun, 29 Oct 2017 10:25:45 GMT</pubDate>
            <atom:updated>2017-10-29T10:25:45.228Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*qtmbn18cBlB9rbU8mi_bDg.jpeg" /></figure><p>You have probably heard a lot of times that programmers have common wisdom that says you need to write SOLID code, use TDD, Dependency Injection… Basically by that they mean that you need more abstraction. But abstractions makes code more complex. When code is complex it is much easier to introduce bugs. What I strive for:</p><blockquote>Everything Should Be Made as Simple as Possible, But Not Simpler</blockquote><p>If you look at the code on the left part of top image, it’s author assumes that you will need to change count of lines. But exercise probably didn’t mentioned that. The same happens at work. You have one task, but you assume that in the future there will be a need to change some things and you make your code harder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/328/1*aFTiQiSwHV9920NxN8aE8w.png" /></figure><p>This code is from real project where users can make orders. Website admin can configure some variables how price is calculated. But this price is hardcoded, because I didn’t thought that it will be changed often. By hardcoding it I didn’t had to make a page for admin to edit it. If in the future price will need to change it will be easy to change it just in one method. And if admin will need to change it often, then I will make a page for that. So by not making assumptions about the change I am saving time.</p><p>Of-course sometimes you know in advance that some things will need to change, then it will be foolish not to make that part changeable.</p><h3>Origins of SOLID</h3><p>So back to the topic. Do you know who invented the term SOLID? I will tell you, it’s <a href="https://en.wikipedia.org/wiki/Robert_C._Martin">Robert C. Martin</a> aka Uncle Bob. He is a motivational speaker for programmers. His talks usually have nice titles like: <br><a href="https://www.youtube.com/watch?v=TMuno5RZNeE"><strong>SOLID Principles of Object Oriented and Agile Design</strong></a><br><a href="https://www.youtube.com/watch?v=ecIWPzGEbFc"><strong>The Future of Programming</strong></a><br><a href="https://www.youtube.com/watch?v=o_TH-Y78tt4"><strong>The Principles of Clean Architecture</strong></a></p><p>If you listen for his talks, they are certainly entertaining, but will you get some knowledge? Nope. It is like cereal for breakfast, a lot of carbs but you still feel hungry after few hours. What I mean is that his principles are like hype words, loud, but almost no value. For example “single responsibility principle” says that a class should have only a single responsibility. I will paraphrase that, it says that programmer must decide what belongs in one class. Really? The rule says that I must decide? It’s like we don’t know that only we can decide were to put our code.</p><p>Don’t get me wrong, sometimes we need abstraction. But sometimes doesn’t mean all the time.</p><p>Also when listening for someone advice I am usually trying to understand how much that person knows. For programmers the easiest thing is to check their GitHub account to see if they apply their principles in practice. Go check Uncle Bob’s GitHub. He is programming over 40 years.</p><h3>Right tool for the job</h3><p>Usually guys like cars and sometimes we need to unscrew a bolt. What do you use for that?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*tjPhsd4dD8Z_znKJM74Ptw.png" /></figure><p>As you see there a several tools that can unscrew bolts. Some of them are more universal and some a better at their job. I bet half of you don’t know that the red tool is a torque wrench that beeps when you reach certain torque.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/887/0*s__WAjP-gADabjIN.jpg" /></figure><p>Have you ever used your foot to screw or unscrew bolts. Do you know that all car manufacturers specify torque for tire bolts? Ideally you should use torque wrench for everything. But in real life we use what is easier. Of-course there is a very small chance that you will break the bolt, but somehow cars don’t have torque wrenches in their boots for tire changes.</p><p>But why programmers got the idea that everything must be done perfectly?</p><h3>Simplicity</h3><p>My recommendation would be — write code as simple as possible. You will always be able to make it harder.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3e89ab3813a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ORM Is an Offensive Anti-Pattern. Really?]]></title>
            <link>https://medium.com/@mantasd/orm-is-an-offensive-anti-pattern-really-42269673d54d?source=rss-5aca9256779------2</link>
            <guid isPermaLink="false">https://medium.com/p/42269673d54d</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[orm]]></category>
            <dc:creator><![CDATA[Mantas Donelavicius]]></dc:creator>
            <pubDate>Tue, 06 Jun 2017 17:41:24 GMT</pubDate>
            <atom:updated>2017-06-06T17:41:24.739Z</atom:updated>
            <content:encoded><![CDATA[<p>There is an <a href="http://www.yegor256.com/2014/12/01/orm-offensive-anti-pattern.html">article (click to view it)</a> written by Yegor. Basically what it says: <strong>don’t use ORM.</strong></p><p>So let’s compare one of the best ORMs to Yegor’s SQL speaking objects.</p><h4>Usage</h4><p>Down below is the code from the article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/659/1*A7aULRK2D9CTFuMkiV8oZw.png" /><figcaption>Yegor’s code</figcaption></figure><p>vs</p><p>Below is the code I would write using ORM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/426/1*sD4MlmVpJUYMaFOG6aJGdA.png" /><figcaption>ORM</figcaption></figure><p>There is not much difference here. ORM code is very similar to Yegor’s code. From developers viewpoint who will use this code, both are equally good.</p><h4>Class definition</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/1*BGC8pJgrMK_9YSN0WL6TQg.png" /><figcaption>Yegor’s code</figcaption></figure><p>vs</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/335/1*3D2zr_8Od7Bhjs6MSOLx3Q.png" /><figcaption>ORM</figcaption></figure><p>This comparison is much more interesting. Basically we are extending ORM and we don’t need to write boilerplate code. Yegor on the other hand is reinventing the wheel. Quite clear which code is the winner.</p><h4>Performance</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*TMbS73oh9OmNKqS8QnQ29A.png" /><figcaption>Yegor’s code</figcaption></figure><p>We don’t need to write any additional code here. Everything works just fine when using ORM. Big minus for reinventing caching which is not needed in the first place in this example.</p><h4>Transactions</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/771/1*2J-GHOuIqt_Fcqt0RERWhw.png" /><figcaption>Yegor’s code</figcaption></figure><p>vs</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/457/1*vEbpgyFAUFAaATKlkMhplg.png" /><figcaption>“best ORM”</figcaption></figure><p>Again, we are using ORM and Yegor is writing code by hand.</p><h3>Conclusion</h3><p>In both cases usage of classes is almost the same, but by using ORM you don’t need to write all the boilerplate code inside of them. Yegor needs to write <strong>130 lines</strong> of code. By using ORM we can reduce it to <strong>20 lines</strong>.</p><p>The only downside of using ORM — you need to learn if first.</p><p>Moreover ORM has relationships and powerful query builder. If you think you can roll that by hand, probably you aren’t the brightest crayon in the box.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42269673d54d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>