<?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: Camilo Payan</title>
    <description>The latest articles on DEV Community by Camilo Payan (@camilo).</description>
    <link>https://dev.to/camilo</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%2F182521%2Ff6911655-3a3c-495f-950f-790da79b7fb9.jpeg</url>
      <title>DEV Community: Camilo Payan</title>
      <link>https://dev.to/camilo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/camilo"/>
    <language>en</language>
    <item>
      <title>In Chess and Code, Move Past the Shiny</title>
      <dc:creator>Camilo Payan</dc:creator>
      <pubDate>Wed, 18 Nov 2020 20:36:29 +0000</pubDate>
      <link>https://dev.to/camilo/in-chess-and-code-move-past-the-shiny-2h16</link>
      <guid>https://dev.to/camilo/in-chess-and-code-move-past-the-shiny-2h16</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;TLDR: Stressing over tools and languages isn't useful for the new coder. Beginners should focus on learning reusable concepts like iteration, logic, and data structures.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Queen's Gambit is a great show on Netflix that has been exploding chess' popularity. I love it! But!... one scene in the first episode frustrated me. Right after the first few games, Beth's chess friend tells her to learn the Sicilian opening and gives her a book of chess openings!&lt;/p&gt;

&lt;p&gt;That doesn't deter her genius play, but it wouldn't work for most beginning chess players. Beginners need to study tactics, not openings.&lt;/p&gt;

&lt;h2&gt;
  
  
  The shiny
&lt;/h2&gt;

&lt;p&gt;In chess, openings are the shiny objects. They have attractive names like Sicilian Dragon, the Spanish game, or the Caro-Kann defense. People will go passionately instruct you to learn one or the other to win. They'll call other openings "boring" or "lazy" or not for "real players".&lt;/p&gt;

&lt;p&gt;Sound familiar?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The same thing happens to beginner programmers.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;New coders jump into a bewildering landscape of languages and code tools. "Should I learn Ruby or React?" "Should I start with one kubernet?" "Is JavaScript the same as Java?"&lt;/p&gt;

&lt;p&gt;Without context, it's overwhelming. There's a ton of literature and a lot of it is actually quite entertaining. People pick fights constantly on Twitter to push their favorite tool or language.&lt;/p&gt;

&lt;p&gt;But these are the shiny objects of programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Move past the shiny
&lt;/h2&gt;

&lt;p&gt;I once heard a chess coach say that they teach new players two openings, one for white and one for black. Then players do tactics. Why? Because you're going to spend most of your game either trying to cause or defend against tactics. The openings are &lt;em&gt;just enough&lt;/em&gt; to get a player safely to the middlegame, where tactics and strategy are more important.&lt;/p&gt;

&lt;p&gt;For new coders, I say &lt;strong&gt;move past the shiny&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Learn enough of a language to get started, then move on to problems and projects. Follow a tutorial and make changes to it. Do problems on a site like &lt;a href="//exercism.io/"&gt;Exercism&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The core concepts for beginning programmers are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Logic, like if/else conditionals&lt;/li&gt;
&lt;li&gt;Iteration, like loops&lt;/li&gt;
&lt;li&gt;Data structures&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Get enough knowledge to get to the core concepts and you'll find that the language itself doesn't matter as much. Many in the industry switch languages multiple times a day, and that's because their foundation in the core concepts is solid.&lt;/p&gt;

&lt;p&gt;If you liked this, find me &lt;a href="https://twitter.com/CamAsMetaphor"&gt;on Twitter&lt;/a&gt; I'd love to chat about it. Also you can always challenge me to a game of correspondence chess &lt;a href="https://lichess.org/@/zerocamilo"&gt;on Lichess&lt;/a&gt; or on &lt;a href="https://www.chess.com/member/zerocamilo"&gt;Chess.com&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>beginners</category>
      <category>chess</category>
    </item>
    <item>
      <title>Eager Loading In Laravel Route Models</title>
      <dc:creator>Camilo Payan</dc:creator>
      <pubDate>Mon, 19 Oct 2020 15:18:47 +0000</pubDate>
      <link>https://dev.to/camilo/eager-loading-in-laravel-route-models-5ch2</link>
      <guid>https://dev.to/camilo/eager-loading-in-laravel-route-models-5ch2</guid>
      <description>&lt;p&gt;TLDR: Override the &lt;code&gt;resolveRouteBinding&lt;/code&gt; method on your Eloquent model in order to eager-load relations that you know will be used.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem: Limits To Route Model Binding
&lt;/h2&gt;

&lt;p&gt;In a Laravel application I'm writing, I have a few dozen routes that all have basically the same format: &lt;code&gt;/jobs/{job}&lt;/code&gt; as a prefix and then particular actions for that job. In Laravel, we have route model binding, which is to say that by convention, you can use the name of a model (the Job model, in this case) in the braces of your route and when your application reaches the controller method, the model is&lt;br&gt;
sent in as a parameter.&lt;/p&gt;

&lt;p&gt;For example, with &lt;code&gt;/jobs/{job}&lt;/code&gt; this means my controller method signature would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function show(Job $job){ /* ... */ }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a very powerful function and it saves us a lot of boilerplate Eloquent code searching for the model from the database explicitly in our controller methods.&lt;/p&gt;

&lt;p&gt;I'm working on adding notes to each job, which is something I created using a new model &lt;code&gt;JobNote&lt;/code&gt; which has a one to many relationship with each &lt;code&gt;Job&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now one thing that I want to display in my sub-navigation for all these Jobs is a count of how many JobNotes each Job has. In Laravel, we can eager-load the count by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$job-&amp;gt;loadCount('notes')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;on the job model I receive as a parameter.&lt;/p&gt;

&lt;p&gt;Here's the issue, &lt;em&gt;I have 40+ routes that use this same job model prefix&lt;/em&gt; and they &lt;em&gt;all&lt;/em&gt; need the count since they all share the same sub-navigation where the count will be used. This would mean I'd have to add that line to 40+ spots in the code (once for each controller method) &lt;em&gt;as well as&lt;/em&gt; remember to write it in when I write new controller methods in this route group.&lt;/p&gt;

&lt;p&gt;Not my idea of a simple or maintainable coding experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  The First Idea: Default Eager Loading
&lt;/h2&gt;

&lt;p&gt;Eloquent has the ability to &lt;em&gt;always&lt;/em&gt; load a relationship by default using the &lt;code&gt;protected $with&lt;/code&gt; variable on my Eloquent model, but there are two issues here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I only need the count, not a collection of all the related notes.&lt;/li&gt;
&lt;li&gt;The Job index route loads lots of jobs, and doesn't use the single Job model binding, so I don't want to get this relationship there.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That second point got me thinking, "Is there a way to hook into a model when it's being resolved from a route?"&lt;/p&gt;

&lt;p&gt;The answer: Yes!&lt;/p&gt;

&lt;h2&gt;
  
  
  The Real Solution: resolveRouteBinding
&lt;/h2&gt;

&lt;p&gt;Eloquent models have a function, &lt;code&gt;resolveRouteBinding&lt;/code&gt;, which can be overriden to add extra functionality.&lt;/p&gt;

&lt;p&gt;Since this would only happen when a specific job is being asked for in the route, it would cleanly leave out the job index page, resolving the second problem I had with default eager loading.&lt;/p&gt;

&lt;p&gt;The function as I needed it, looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function resolveRouteBinding($value, $field = null)
{
    return $this-&amp;gt;where('id', $value)
              -&amp;gt;withCount('notes')
              -&amp;gt;firstOrFail();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First we search for the actual model, since the &lt;code&gt;id&lt;/code&gt; is what's passed in, then we make sure to load it with the count we want, and make sure we only get one model! Now the model is loaded with the field &lt;code&gt;notes_count&lt;/code&gt; available, with the data we need by default. We haven't loaded all the notes, which solves the 1st problem I had with the default eager loading.&lt;/p&gt;

&lt;p&gt;It's a regular PHP block so I could imagine adding more functionality to support extra loads depending on the route.&lt;/p&gt;

&lt;p&gt;Going this route meant that instead of having to add the&lt;br&gt;
&lt;code&gt;$job-&amp;gt;loadCount('notes')&lt;/code&gt; to each controller method (40+ times just to start!) I just added a few lines to my model code. Much easier to maintain.&lt;/p&gt;

&lt;p&gt;If you like this and found it useful, please let me know on &lt;a href="https://twitter.com/camasmetaphor"&gt;Twitter&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>codequality</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Setting Up Vim and the PHP Language Server Protocol</title>
      <dc:creator>Camilo Payan</dc:creator>
      <pubDate>Fri, 04 Sep 2020 18:33:47 +0000</pubDate>
      <link>https://dev.to/camilo/setting-up-vim-and-the-php-language-server-protocol-5fmh</link>
      <guid>https://dev.to/camilo/setting-up-vim-and-the-php-language-server-protocol-5fmh</guid>
      <description>&lt;p&gt;Vim is great at editing text, but doesn't have the full language features built&lt;br&gt;
in that devs love in IDEs. At the same time, the Vim plugin ecosystem has&lt;br&gt;
allowed for new languages to quickly ramp up in Vim instead of waiting for&lt;br&gt;
adoption to support a full IDE.&lt;/p&gt;

&lt;p&gt;Enter the Language Server Protocol. Originally to support another text editor,&lt;br&gt;
Visual Studio Code, LSP has been embraced by the Vim world as a way to get much&lt;br&gt;
richer language support than we're used to, and now that's available for PHP!&lt;/p&gt;

&lt;p&gt;Let's step through how to install Language Server Protocol support for PHP in&lt;br&gt;
Vim.&lt;/p&gt;
&lt;h2&gt;
  
  
  Assumptions
&lt;/h2&gt;

&lt;p&gt;Some silly assumptions to start!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have Vim installed and a method of installing plugins you prefer. Why?
Because there are multiple plugin managers and I'd like to gloss over
instructions for each.&lt;/li&gt;
&lt;li&gt;You have Composer installed for your PHP project&lt;/li&gt;
&lt;li&gt;You're comfortable on the command line! This seems likely if you're using Vim.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Step 1: Install the LSP client plugin
&lt;/h2&gt;

&lt;p&gt;The most popular Vim plugin for LSP integration is &lt;a href="https://github.com/prabirshrestha/vim-lsp"&gt;Prabir Shrestha's Vim-LSP&lt;br&gt;
plugin&lt;/a&gt;. Go ahead and install this&lt;br&gt;
plugin based on your plugin manager of choice and the instructions at the Github&lt;br&gt;
link. This is your LSP client for Vim!&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Install the LSP Server
&lt;/h2&gt;

&lt;p&gt;In your project, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./vendor/bin/composer require -D felixfbecker/language-server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will install the language server locally to your project, and add it to&lt;br&gt;
your &lt;code&gt;composer.json&lt;/code&gt; dev dependencies. Why do this per project? I have a few&lt;br&gt;
projects and generally avoid depending on a global install if I can help it. If&lt;br&gt;
you have different requirements, you could install it with a global composer&lt;br&gt;
install.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 3: Parse stubs
&lt;/h2&gt;

&lt;p&gt;There are plenty of parts of PHP that aren't defined in your code but the&lt;br&gt;
language server still needs to know about! These are things that are in the PHP&lt;br&gt;
standard library, like definitions for &lt;code&gt;DateTime&lt;/code&gt; or function signatures for built&lt;br&gt;
in functions like &lt;code&gt;array_map&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;From your project, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./vendor/bin/composer run-script --working-dir=vendor/felixfbecker/language-server parse-stubs
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Configure Vim to start and use the LSP
&lt;/h2&gt;

&lt;p&gt;Now we need to configure Vim to use the server. Add this to your &lt;code&gt;vimrc&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;au User lsp_setup call lsp#register_server({
    \ 'name': 'php',
    \ 'cmd': {server_info-&amp;gt;['php',
expand('[PATH TO YOUR COMPOSER VENDOR DIRECTORY]/vendor/bin/php-language-server.php')]},
    \ 'whitelist': ['php'],
    \ })

autocmd FileType php setlocal omnifunc=lsp#complete
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Replace the path with your path. I use Vim's support for directory vimrc using&lt;br&gt;
&lt;code&gt;exrc&lt;/code&gt; so I replace that path portion with &lt;code&gt;.&lt;/code&gt;. But if you have a different&lt;br&gt;
setup, you'll need to make sure it points to the vendor directory where you&lt;br&gt;
installed the language server earlier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Test it out!
&lt;/h2&gt;

&lt;p&gt;Go into your project, and try autocompleting a class or function name using&lt;br&gt;
&lt;code&gt;Ctrl+X Ctrl+O&lt;/code&gt;, the keyboard shortcut for Vim omnicomplete.&lt;/p&gt;

</description>
      <category>php</category>
      <category>vim</category>
      <category>productivity</category>
      <category>vscode</category>
    </item>
    <item>
      <title>Backend or Frontend? Where should a fullstack dev start coding?</title>
      <dc:creator>Camilo Payan</dc:creator>
      <pubDate>Thu, 03 Sep 2020 15:13:13 +0000</pubDate>
      <link>https://dev.to/camilo/backend-or-frontend-where-should-a-fullstack-dev-start-coding-4e14</link>
      <guid>https://dev.to/camilo/backend-or-frontend-where-should-a-fullstack-dev-start-coding-4e14</guid>
      <description>&lt;p&gt;The other day, on Twitter, Angie Jones posted a very interesting poll about whether a full stack web developer would start writing a feature on the frontend or the backend.&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1299205848007286784-551" src="https://platform.twitter.com/embed/Tweet.html?id=1299205848007286784"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1299205848007286784-551');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1299205848007286784&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;Interestingly most people responded that they would start on the backend code, which is also my preference. For me, I prefer to mock up the UI quickly on paper to get an idea of what’s needed, then write the backend, then write the frontend.&lt;/p&gt;

&lt;p&gt;Either way, I think it’s interesting to see the pros and cons of each approach, so let’s start with the frontend.&lt;/p&gt;

&lt;h2&gt;
  
  
  Starting on the Frontend
&lt;/h2&gt;

&lt;p&gt;To be clear, we’re talking about writing the HTML/CSS/JS to display your feature on a page.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Clarify your ideas
&lt;/h3&gt;

&lt;p&gt;People mentioned being able to clarify the idea for what is needed. When you write the frontend, you will quickly get a very precise idea of exactly what data is going to be needed. This will clarify what database records you need, or what API calls you need, or if you need something new!&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Deal with frontend uncertainties sooner
&lt;/h3&gt;

&lt;p&gt;The frontend in web development can be a bit of a mess, with various browser differences and a large number of tools/dependencies that might be in use. It can be very useful to get those parts out of the way as quickly as possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Starting on the backend
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Develop in peace
&lt;/h3&gt;

&lt;p&gt;If you have trouble getting started, the backend can be a great place to start. Everything in that sandbox is usually under your control as the developer, so it may be easier to make progress.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. You can always come back
&lt;/h3&gt;

&lt;p&gt;Again, the generally controlled nature of the backend means that it’s easier to come back to the backend for tweaks if you realize something is missing as you develop the frontend later.&lt;/p&gt;

&lt;p&gt;All in all, it’s really not an either-or proposition. Some repliers, like Martin Fowler, mentioned deciding based on where they felt there was the most uncertainty. I practice a version where I mock up the frontend first, then do the backend, then frontend. There’s no hard and fast rule to follow, especially since so much coding is exploratory.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>fullstack</category>
    </item>
    <item>
      <title>Lint Your PHP in Vim</title>
      <dc:creator>Camilo Payan</dc:creator>
      <pubDate>Tue, 02 Jul 2019 15:40:50 +0000</pubDate>
      <link>https://dev.to/camilo/lint-your-php-in-vim-2kac</link>
      <guid>https://dev.to/camilo/lint-your-php-in-vim-2kac</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The objective of cleaning is not just to clean, but to feel happiness living&lt;br&gt;
within that environment.&lt;/p&gt;

&lt;p&gt;~ Marie Kondo&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Everyone who has written PHP for a while ends up confronted with messy code. The gentle learning curve, the forgiving type system, and the easy execution model all lend themselves to writing PHP that's quick, dirty, and eventually frustrating.&lt;/p&gt;

&lt;p&gt;In 2013, the PHP Framework Interoperability Group (&lt;a href="https://www.php-fig.org"&gt;PHP-FIG&lt;/a&gt;) adopted PSR-2, a standard for code which was intended to "reduce cognitive friction when scanning code from different authors." The result? Reading PHP code by different authors in different projects has never been easier. Whether you're diving into your workplace's proprietary code or Laravel's Eloquent ORM code, you'll find that it's readable (even if not quite understandable!).&lt;/p&gt;

&lt;p&gt;Our goal here is going to be setting up automated tools in Vim to clean up your code as you write. By making it part of your regular flow and getting quick feedback, you'll be able to write your code with the confidence that in a year or two when you open up this file again, you'll still be able to read it!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you clean your bathroom every day, you never have to clean your bathroom.&lt;/p&gt;

&lt;p&gt;~ Lindy West's Mother, &lt;em&gt;Shrill: Notes from a Loud Woman&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We're going to take advantage of some existing tools: PHP-Code Sniffer&lt;br&gt;
(PHP-CS) and PHP-CS-Fixer.&lt;/p&gt;
&lt;h2&gt;
  
  
  Installing PHP-CS
&lt;/h2&gt;

&lt;p&gt;To add PHP-CS to your project using composer, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require --dev "squizlabs/php_codesniffer=\*"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;By default, PHP-CS checks against the PEAR coding standard. However it ships with several other configurations, including &lt;code&gt;PSR2&lt;/code&gt;. We can run PHP-CS to check for standards violations using the &lt;code&gt;phpcs&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./vendor/bin/phpcs --standard=PSR2 path/to/file
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However, since we're also looking to standardize and automate this in our&lt;br&gt;
project, we are going to create a file in our project that configures PHP-CS. The file that PHP-CS looks for is a &lt;code&gt;phpcs.xml&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;ruleset name="Custom Standard" namespace="MyProject\CS\Standard"&amp;gt;
  &amp;lt;description&amp;gt;Our custom coding standard based on PSR-2&amp;lt;/description&amp;gt;
  &amp;lt;rule ref="PSR2"/&amp;gt;
&amp;lt;/ruleset&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since we're using the PSR-2 standard, our ruleset is pretty simple. &lt;a href="https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-Ruleset"&gt;The PHP-CS documentation shows many more options, including the ability to exclude some files or rules, or include rules from other coding standards.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now when we run &lt;code&gt;.vendor/bin/phpcs&lt;/code&gt; we're going to get the PSR-2 coding standard checked against every one of our files based on our &lt;code&gt;phpcs.xml&lt;/code&gt; file!&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing PHP-CS-Fixer
&lt;/h2&gt;

&lt;p&gt;Generally we don't only want our computer to tell us when something is wrong, we can also get it to fix our coding issues for us! The tool for this is PHP-CS-Fixer.&lt;/p&gt;

&lt;p&gt;Again we'll use Composer to install PHP-CS-Fixer&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require --dev friendsofphp/php-cs-fixer
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And we'll be able to run it using &lt;code&gt;.vendor/bin/php-cs-fixer fix path/to/file&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We'll also create a &lt;code&gt;.php_cs.dist&lt;/code&gt; file in our project so we can standardize the rules we're going to enforce. The file will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

$finder = PhpCsFixer\Finder::create();

return PhpCsFixer\Config::create()
    -&amp;gt;setRules([
        '@PSR2' =&amp;gt; true,
    ])
    -&amp;gt;setFinder($finder)
;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Integrating With Vim
&lt;/h2&gt;

&lt;p&gt;To bring these tools into Vim, we're going to rely on &lt;a href="https://github.com/w0rp/ale"&gt;the plugin ALE&lt;/a&gt;. ALE is capable of a lot of things, including linting (with PHP-CS), fixing (with PHP-CS-Fixer), and even deeper IDE-like features using the Language Server Protocol (going to get into that in a later post).&lt;/p&gt;

&lt;p&gt;First, &lt;a href="https://github.com/w0rp/ale#installation"&gt;install ALE based on your own Vim setup&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Then we want to add some configuration so ALE knows where to look for our tools. Add the following to your project's &lt;code&gt;.vimrc&lt;/code&gt; assuming you're using &lt;code&gt;:exrc&lt;/code&gt; to allow for project-based vim configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let g:ale_php_phpcs_executable='./vendor/bin/phpcs'
let g:ale_php_php_cs_fixer_executable='./vendor/bin/php-cs-fixer'
let g:ale_fixers = {'php': ['php-cs-fixer']}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And that should set up ALE with PHP-CS and PHP-CS-Fixer! PHP-CS should be running asynchronously, and the issues it finds will pop up as you type.&lt;/p&gt;

&lt;p&gt;In order to run PHP-CS-Fixer, use the command &lt;code&gt;:ALEFix&lt;/code&gt; in a file, which will run the fixer and write those changes.&lt;/p&gt;

&lt;p&gt;You can also run the fixers every time you save by adding &lt;code&gt;let g:ale_fix_on_save&lt;/code&gt; in your &lt;code&gt;.vimrc&lt;/code&gt; file.&lt;/p&gt;

</description>
      <category>vim</category>
      <category>productivity</category>
      <category>beginners</category>
      <category>php</category>
    </item>
    <item>
      <title>Moving in Vim</title>
      <dc:creator>Camilo Payan</dc:creator>
      <pubDate>Tue, 25 Jun 2019 15:10:55 +0000</pubDate>
      <link>https://dev.to/camilo/moving-in-vim-248a</link>
      <guid>https://dev.to/camilo/moving-in-vim-248a</guid>
      <description>&lt;h1&gt;
  
  
  Moving in Vim
&lt;/h1&gt;

&lt;p&gt;Everyone who’s tried Vim learns about the wonky &lt;code&gt;hjkl&lt;/code&gt; keys to move around a file. These usually make a new user feel one of the only two emotions we need: anger and confusion.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcamilopayan.com%2Fimg%2Fthegoodplace-anger-confusion.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcamilopayan.com%2Fimg%2Fthegoodplace-anger-confusion.gif" alt="A scene from the sitcom The Good Place where a character says "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What if I told you that there was a way to make moving around your code feel as smooth as a swan dive and as fast as thought, while making editing a cinch? That’s where Vim movements come in.&lt;/p&gt;

&lt;p&gt;Vim movements are commands that move the cursor in some direction. The &lt;code&gt;hjkl&lt;/code&gt; keys are all examples of movement commands. In their case, movements that go left, down, up, and right. Vim also has other motions built around text objects as well as the direction you want to go in. If you’d like to go to your next code block, instead of mashing the &lt;code&gt;j&lt;/code&gt; key a few times, you can hit &lt;code&gt;}&lt;/code&gt; to get to the next empty line, landing you right before your next block of code (you are spacing out your code for readability, right?). Swapping out several hits on the &lt;code&gt;j&lt;/code&gt; button with a single &lt;code&gt;}&lt;/code&gt; is both a time saver and a finger saver. It helps you get to where you need to be in the code as quickly as you can think.&lt;/p&gt;

&lt;p&gt;You can learn about all the available Vim motions in the Vim help file &lt;code&gt;motion.txt&lt;/code&gt; accessible by running &lt;code&gt;:help motion.txt&lt;/code&gt; in command mode.&lt;/p&gt;

&lt;p&gt;If we were only eliminating keystrokes, it would already be worth your time to learn. But Vim motions are composable. This means they can combine movements with Vim’s editing actions to make changes to your file.&lt;/p&gt;

&lt;p&gt;Let's go back to our example about moving to a new code block. What if you wanted to delete everything up until your next code block? We already know that moving to the next blank line is &lt;code&gt;}&lt;/code&gt;, so we can combine that with the delete command &lt;code&gt;d&lt;/code&gt; to create &lt;code&gt;d}&lt;/code&gt;. This will delete every line from where our cursor is up until the next blank line.&lt;/p&gt;

&lt;p&gt;Now you aren’t only moving faster, you’re editing your file faster, too!&lt;/p&gt;

&lt;p&gt;Motions and commands aren't only Vim and Neovim. Vim emulation layers like the &lt;a href="https://plugins.jetbrains.com/plugin/164-ideavim" rel="noopener noreferrer"&gt;IDEAVim&lt;/a&gt; plugin for JetBrains IDEs also have motions. The support for all motions might not always be there. My last experience with PHPStorm, for example, meant losing out on some motions. I missed those motions, and I went back to using Vim.&lt;/p&gt;

&lt;p&gt;Vim motions and commands in normal mode are the language of Vim. When you learn to speak it, you’ll find that you can jump around files quicker than you ever thought possible.&lt;/p&gt;

</description>
      <category>vim</category>
      <category>beginners</category>
      <category>explainlikeimfive</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Netlify Form Submissions With Golang Lambda Functions</title>
      <dc:creator>Camilo Payan</dc:creator>
      <pubDate>Tue, 18 Jun 2019 16:44:20 +0000</pubDate>
      <link>https://dev.to/camilo/netlify-form-submissions-with-golang-lambda-functions-537l</link>
      <guid>https://dev.to/camilo/netlify-form-submissions-with-golang-lambda-functions-537l</guid>
      <description>&lt;p&gt;Static websites are a great way to recapture some of the best parts of the history of the web. They provide low overhead and simple deployments. One of the things that a static website loses, however, is the ability to use your backend to handle form data. Netlify, a static site hosting service, solves this problem by leveraging AWS Lambda. AWS Lambda lets you run backend code without needing to host a whole application: you only run the code you need.&lt;/p&gt;

&lt;p&gt;This tutorial will start by setting up a Netlify form. Next we’ll create the Lambda function that processes our form submission. Finally, we’ll deploy that on Netlify. Our use case is going to be a simple contact form that emails the submitter that their contact form was received.&lt;/p&gt;

&lt;h3&gt;
  
  
  Form Setup
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.netlify.com/docs/form-handling/"&gt;Netlify Forms&lt;/a&gt; are forms that Netlify will handle automatically and they are already plenty powerful. By default, a form handled by Netlify has its data dumped into the Netlify admin area for your website, then that data is accessible either through the admin area or through the Netlify API. If you don’t need to do immediate work on your form data, that might be a good way to go!&lt;/p&gt;

&lt;p&gt;Our contact form is going to need some basic information: name, email, and a message. To make a form a Netlify form, all we have to do is add &lt;code&gt;data-netlify=true&lt;/code&gt; to our form element.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form name="contact" method="POST" data-netlify="true"&amp;gt;
  &amp;lt;p&amp;gt;
    &amp;lt;label&amp;gt;Your Name: &amp;lt;input type="text" name="name" /&amp;gt;&amp;lt;/label&amp;gt;   
  &amp;lt;/p&amp;gt;
  &amp;lt;p&amp;gt;
    &amp;lt;label&amp;gt;Your Email: &amp;lt;input type="email" name="email" /&amp;gt;&amp;lt;/label&amp;gt;
  &amp;lt;/p&amp;gt;
  &amp;lt;p&amp;gt;
    &amp;lt;label&amp;gt;Message: &amp;lt;textarea name="message"&amp;gt;&amp;lt;/textarea&amp;gt;&amp;lt;/label&amp;gt;
  &amp;lt;/p&amp;gt;
  &amp;lt;p&amp;gt;
    &amp;lt;button type="submit"&amp;gt;Send&amp;lt;/button&amp;gt;
  &amp;lt;/p&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Pretty simple, right? Now this information will get dumped into our Netlify admin area without any changes, but we want to email the form to the submitter and ourselves as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Forms and Functions
&lt;/h3&gt;

&lt;p&gt;AWS Lambda functions give us the ability to run server-side code without needing to run a server. Netlify wraps up AWS Lambda for certain use cases with &lt;a href="https://www.netlify.com/docs/functions/#event-triggered-functions"&gt;Event Triggers&lt;/a&gt;, mostly around deployments and service coordination. However, there is also a hook for form submissions! That gives us an in. Netlify supports Javascript or Go for these functions; we’re going to use Go.&lt;/p&gt;

&lt;p&gt;For this we need some Go code to run, then a way for Netlify to build our Go code into the correctly named file, and then away we go!&lt;/p&gt;

&lt;p&gt;Our use case is simple. We want to take the input from the contact form and mirror it back to the submitter, with ourselves BCC’d.&lt;/p&gt;

&lt;p&gt;The requirements for a very basic Go function on Netlify are a main function that sets up your lambda handler, and that handler function. You can read the whole code for our function here: &lt;a href="https://github.com/camilopayan/netlify-go-function-example/blob/master/main.go" rel="me"&gt;&lt;/a&gt;&lt;a href="https://github.com/camilopayan/netlify-go-function-example/blob/master/main.go"&gt;https://github.com/camilopayan/netlify-go-function-example/blob/master/main.go&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting Form Data
&lt;/h3&gt;

&lt;p&gt;Netlify injects the form data into the request body as part of a JSON object. If you’d like a more detailed look at the entire JSON object, then check out &lt;a href="https://open-api.netlify.com/"&gt;https://open-api.netlify.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For our use case however, we’ll be using Go’s built-in JSON Unmarshaler to change our request body into Go structs.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Form struct {
    Name    string `json:"name"`
    Email   string `json:"email"`
    Message string `json:"message"`
}

type Payload struct {
    Form Form `json:"data"`
}

type Body struct {
    Payload Payload `json:"payload"`
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;By using nested structs, with fields tagged as their fields inside the JSON object, we can use json.Unmarshal to slurp our form data into a usable struct!&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting Secret Data
&lt;/h3&gt;

&lt;p&gt;If we’re going to send emails or plug into any 3rd party service (maybe you want to dump your customer information into Airtable!) then we need a secure way to get those secrets like API keys or SMTP credentials.&lt;/p&gt;

&lt;p&gt;For this, Netlify provides Environment Variables, which will come along with your Function. You can set some variables in your netlify.toml file, but this is meant for build variables (npm versions and so on). Secrets should be stored via your Netlify admin dashboard. &lt;a href="https://www.netlify.com/docs/continuous-deployment/#environment-variables"&gt;You can read more about environment vars in the Netlify docs.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To access those environment variables, you can use the built in Go &lt;code&gt;os&lt;/code&gt; library.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;port, _      := strconv.Atoi(os.Getenv("MAIL_PORT"))
host         := os.Getenv("MAIL_HOST")
senderemail  := os.Getenv("MAIL_SENDER_EMAIL")
smtppassword := os.Getenv("MAIL_PASSWORD")
smtpuser     := os.Getenv("MAIL_USERNAME")
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  Building Your Function
&lt;/h3&gt;

&lt;p&gt;One of the coolest things about Netlify is its built-in continuous deployment pipeline. If it’s possible for you (using the latest Go), I recommend keeping your life easy and using Go modules.&lt;/p&gt;

&lt;p&gt;First, locally you need to run &lt;code&gt;go mod init&lt;/code&gt; to create your go.mod file and bring in your dependencies. Commit that to your git repo.&lt;/p&gt;

&lt;p&gt;Next I use a Makefile with a very simple set of commands:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir -p functions
go build -o functions/submission-created ./...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This creates the functions directory, where Netlify will look for any functions and also build your go files and compile them into one file called &lt;code&gt;submission-created&lt;/code&gt;. You might remember from earlier that &lt;code&gt;submission-created&lt;/code&gt; is one of the supported event-triggered functions.&lt;/p&gt;

&lt;p&gt;You should also modify your netlify.toml file to run &lt;code&gt;make build&lt;/code&gt; as part of your deployment.&lt;/p&gt;

&lt;p&gt;With this, you should have enough base information to start writing your own form-crunching functions, all without having to lurch through the AWS Lambda documentation. Netlify Functions are more flexible than this, though, and you could even deploy Functions that implement a full API or just some RPC endpoints to hit from a JS frontend.&lt;/p&gt;

</description>
      <category>go</category>
      <category>webdev</category>
      <category>serverless</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
