Skip to content

The VS Code extensions I use for PHP development in 2019

Dear Reader,

I hate IDEs. I always have, I probably always will. I don’t always want to edit a file that is in a project and I don’t like any of the ‘project-less’ kludges that are out there.  So for the majority of my programming career, I’ve used “program editors”.  Programs just smart enough to know that I’m working in code and give me tools to do that properly, but not so smart that they get in my way. For the last few years I’ve been a huge fan of “Sublime Text”. I’ve owned a license at the last three places I’ve worked and even maintained my own personal license.  All that changed last year when I became one of the last developers on earth to notice VS Code by Microsoft.

Back in April, since I found myself with some time on my hands, I decided I would spend a day actually learning how to use VS Code. Maybe if I had bothered to do that with Sublime Text, I’d still be using it. but that’s a discussion for another day.

Once I got the basics setup, I started exploring Extensions. I’ve loaded more than 40 of them now at one time or another but I’ve paired my list down to my “Essential 12”. Here they are for you.

WAIT! Before we get started though, let me describe my setup.

I use Windows 10, but I use the Windows Subsystem for Linux (WSL) for all of my development. My production servers are all Linux (CentOS if you MUST know) All of my projects are stored on either GitLab or GitHub.  You may be using MacOS or Linux, but since all my development is Linux, almost everything I talk about here will apply.

“Absolutely must have or it’s not worth using” extensions

Visual Studio Code Remote Development Extension Pack

This is the grandaddy of all the extensions. This is what made VSC actually useful to me on a day to day basis. It was only released in May, so I have the least amount of experience with this one but trust me, this is the ONE you have to install if you don’t install anything else.

This is actually an extension pack, not a single extension.

  • Remote – WSL
  • Remote – Containers
  • Remote – SSH: Explorer
  • Remote – SSH: Editing Configuration Files

The first three are the secret sauce. They allow you to edit files natively inside of the WSL, inside of running Docker containers, or on any machine that you can ssh into.

From a command prompt inside the WSL (I use bash so I’m going to say bash prompt form now on. You can translate into your favorite command prompt) developers can open a single file  or a directory by simply typing code.

e.g.

code .

This will open VCS, instantiate a running instance of it’s server piece inside the WSL, and open the current directory ready for us to begin working.

It’s that middle task that makes the magic happen. VSC Remote Development Extension Pack actually has a server piece that it runs in Containers, WSL, and over SSH to allow it to seamlessly manipulate files and directories.

This group of extensions also integrate the remote file system into VSC so you can use File->New/Save/SaveAs/Delete etc on remote systems, WSL, and inside containers. That did away with the need for an extension solely for that purpose.

The fourth item on the list above is a convenience extension that makes editing remote ssh config files easier by giving keyword intellisense and syntax coloring.

BONUS: When you open the terminal window while connected to WSL, a container, or SSH, you get a command prompt in that environment. This means a bash shell in WSL for me, not the PowerShell. (which I still hate) The shell it presents is better and more compatible than any shell I’ve used in Windows so far. If you want, you can even setup forwarders. This could be your complete ssh solution if you so desired.

If you don’t have this extension, you are missing the power of VSC, especially if you work in Win/WSL.

PHP IntelliSense

Since the vast majority of my work in VSC is PHP code, I wanted more than the basic intellisense that is built into VSC. (It is actually recommended that when you install this extension, you turn off the native PHP intellisense) There are a couple of Extensions that do this, I tried them all and this is by far the best and least intrusive.

At least one of them tried to format things as I pasted them in. This caused me to have to do a lot of correcting, removing spaces, and reformatting before I could use the code I just pasted in. This plugin does not do anything too wild. It gives you basic keyword intellisense, tab completion, advanced searching, etc. One of my favorite things about this extension is that if you click on a use statement, it will locate the file that class is defined in and open it for you.

As I said, this plugin does not attempt to format your code for you. It recommends that you use the PHPCS extension for that. (Another reason I love it)

“Sugar coating that make the tool very nice to use” Extensions

Polacode

This one really should be in a category all it’s own called “This is so cool” Extensions. Polacode allows you to create “snapshots” of your code. You activate it, you highlight the code you want snapshoted, and you click the shutter button. BOOK, you’ve got a png of your code ready to share on social media or incorporate into your next slide presentation. It really is that simple. (Well, when you click the button, it asks you where to save it and what to name it. Other than that it’s that simple.)

Outside of the Remote Development Extension Pack, this is probably the coolest extension I use.

vscode-php-cs-fixer

If you do want your code auto-formatted, this is the extension to use. You have complete control over which standards to implement and those can even change on a project by project basis. If you use phpcs in your development process this is a must-have extension. If you install it though, take the time necessary to configure it properly for your base configuration and then modify that as needed on a per-project basis. Since I work in several communities, this allows me to say that my base config is PSR1/1, but my WordPress plugins adhere to the WordPress coding standards. (Mostly) The investment of 30 minutes to get everything setup and configured is time well spent.

Twig Language 2

Some of the work I do requires views and when I use a view, I always use Twig as my templating engine. This extension provides syntax highlighting and hovers for my twig templates. Syntax highlighting might not sound important but once you get used to it, and then you open a file that does not have it, you begin to wonder if your program is broke or if you’ve suddenly been transported into a black & white movie.

Code Spell Checker

Anyone who has ever received an email from me (or read one of my blog posts that pre-dates spellcheckers built into browsers) knows that I seriously suck at spelling. Even when I do have spell checkers they rarely work in code because $somethingImportant will never pass a spell checker. So I have this plugin. This one is hit or miss. It almost always identifies misspelled words in strings or comments (YEA COMMENTS!) but it used to be that I could have it fix them. That stopped working in the latest update. Hopefully they get that fixed soon.

Bracket Pair Colorizer

First, you should not have a lot of nested IF and FOR statements. Down that road is madness. However there are a lot of times where we have function calls nested inside other function calls, nested inside an IF. When you do find yourself in that situation, you want this plugin. This plugin assigns a color to each pair of brackets, braces, and parentheses.  This makes tracing down missing ones trivial and helps you understand the actual nesting of your code.

 

“You may not needs these but I sure do” Extensions

markdownlint

I write a lot. Of late I’ve been writing two books “Using the WordPress REST API” and “Extending the WordPress REST API”. These books are written in MarkDown and stored in private repos on github. (Leanpub refuses to support GitLab, my preferred site. ) Most of the time, MarkDown is easy to do, however, there are some rules that have to be followed. This plugin keeps all of my markdown clean and parse-able.

Prettify JSON

JSON is the lingua franca of APIs. It is heads and shoulders better than XML in every respect except one, readability. XML is human readable. This plugin basically does what PHP’s json_encode($payload, JSON_PRETTY_PRINT) does. It puts linefeeds where we need them and indents where we need them. It also does syntax highlighting and linting for JSON. If you find yourself pasting JSON into a text editor and making it readable by hand, you want this extension.  If on the other hand, you prefer XML, well, you have problems beyond the scope of this article. :)

Word Count

Again, another writing tool. Not even a tool, just a simple little plugin that tells me how many words are in a given MarkDown file. It is not important if you don’t do a lot of writing, but without it I couldn’t tweet out my “how many words I wrote today” tweets.

“I’m keeping an eye on this” Extensions

SQLTools

I’m an old database programmer. I still hand code every line of SQL I write. So I am always looking for new ways to access and manipulate data. I had high hopes for this extension because it looked like it would give me a powerful command line to access my database. To date I’ve not been able to do anything other than get a list of tables on a server.

Still, I have not uninstalled it. Every time there is an update, I try again to see if it is working better.

 

fClose()

That’s it, that’s my current extension list. I hope I’ve introduced you to a new friend or two. If you are new to VS Code, I hope I’ve given you a few new toys to play with.

Until next time,
I <3 |<
=C=

WordPress, REST, and RegEx

Dear Reader,

I’m going to add this to “Using the WordPress REST API“, but I thought I would blog it here as well.

I have a tendency to over think things.

Today, I was working on a REST API endpoint for a client and it needed to have RegEx in it. I hate RegEx with a passion usually reserved for XML, but unlike XML, it’s a necessary evil, so I dove into it.

1
/item/(?P<itemId>\d+)

That’s an example.

For the uninitiated, when you are defining a custom WordPress REST endpoint, one of the things you can do is put in Regex into the route definition and WordPress will use that to pull out content and make it a parameter. The code above defines an endpoint for

https://example.com/wp-json/my-namespace/v1/item/4

When run, it will make a parameter named itemId whose value is the 4 from the URI. It’s incredibly handy. They are very easy to work with, especially if you are using numbers like 4, or even 294875.  Strings…well, strings get tricky.

The above example expect a number (so no alpha, just numeric) and numbers are contiguous.  You don’t have numbers with a space in them. Phrases however, have spaces. And what I needed to pull out was a phrase. So, I did what I always do, I pulled out Regex 101, and started figuring this out. This is where the overthinking part comes in.

I got it working in short order, then I started thinking. “What if…”

  • What if There’s a query string at the end
  • What if there’s more to the URI
  • What if there’s a slash at the end
  • …what if

This is where I got into trouble. I lost a good 2-3 hours designing a beautiful piece of RegEx that handled every situation I could think of. It was art, if I do say so myself. The only problem was that once I pasted it into my WordPress REST Controller, it did not work.

So I did what every developer does, I assumed the problem had to be in WordPress. I rolled up my sleeves and found out how WordPress matches routes.

What WordPress does

WordPress matches REST routes in WP_REST_Server::dispatch() (wp-includes/rest-api/class-wp-rest-server.php)

1
$match = preg_match( '@^' . $route . '$@i', $path, $matches );
$path is the URI. IN my case
https://example.com/wp-json/my-namespace/v1/item/this%20item%20name
$route was the route I defined in regex.
1
item/(?P<itemName>[w+].*)[?|/|\$]
(I’m working from memory but I think that was it.)
If – and only if – I could set some pattern modifiers I could make it work…but I couldn’t.
Then I began doing the other thing that PHP developers do a lot, I began throwing var_dump();die(); into WP_REST_Server. I thought I needed to see what was going on. Turns out, the answer was there in front of me all the time.
I was assuming that WordPress was applying my RegEx standalone from everything else. If you look at the line above though, you can see that is uses the route that I define in it’s entirety.
  • It puts a ‘@’ at the beginning of it. This tells PHP that ‘@’ is the regex delimiter, not ‘/’ like usual
  • It adds the caret ‘^’ to match the beginning of the line
  • It concatenates the route I defined
  • It adds the $ to match the end of the line
  • It puts the ‘@’ to signify that this is the end of the RegEx
  • It adds the ‘i’ pattern modifier (the things I needed to tinker with) to indicate that all matches should be case insensitive
WordPress doesn’t worry about the query string, or anything after that because it’s already stripped it off. I don’t have to overthink this thing with subgroups and special characters, WordPress has got my back.
My finished product ended up looking like this:
1
item/(?P<itemName>w+.*)
This gives me a parameter named itemNamethat includes everything past item/ to the end of the line.

Conclusion

Stop over-thinking things. Sometimes just let the framework do it’s job. :)

Did I meantion I hate Regex? :)

Until next time,
I <3 |<
=C=

p.s. the section in the book will be more coherant. I’ve spent the day with RegEx so I’m a bit scatterbrained now. :)

Community

Dear Reader,

At Sunshine PHP ’19, it was my privilege to make many new friends and re-connect with old ones. One of the new friends I made was Nara Kasbergen.

Nara did a great talk on Community – which of course made me nervous because, well, that’s MY shtick. Never the less Nara delivered a very good talk with some good points.

One of my takeaways was that you need to be an active part of several community. it’s not enough to be a part of your local PHP user group, you also need to be part of at least one conference community, (Sunshine, phptek, PHPBenlux, DPC, the list goes on)  and an easy win is to be part of an online community as well. (Whether that be Twitter, slack , Day Camp 4 Developers, or NomadPHP is up to you)

Take the time to get involved in the PHP community in the way that is best for you.

The connections you make will help you build a career.

The friends you make will help you build a life.

Until next time,
I <3 |<
=C=

Mautic Step 0 – Installing Mautic

Dear Reader,

Last time, in the post titled “My Journey into Mautic”, I explained that I was starting to play with the marketing automation package Mautic. Today’s post is about installing Mautic.

This post is not necessary for everyone. If you are using a hosted version of Mautic, it is probably safe to ignore this post. Even if you are hosting it with a provider like SiteGround or GoDaddy, the first part of these instructions won’t apply. You can skip the “Foundation” if you installed Mautic from a control panel and go straight to “Running the Mautic Installer”.

The Foundation

I use Digital Ocean as my hosting platform. For small businesses, Mautic WILL run on a $5/month droplet. However, I strongly suggest that if you are going this route to invest a little more and get at the minimum a $10/month droplet.

You will need to install:

  • Apache (or the web server of your choice)
  • PHP
  • MySQL/MariaDB

Additionally, Mautic makes this recommendation.

Mautic recommended configuration options.

You can also run Mautic in a sub-directory of your existing website. If you already have a website that is based on the LAMP stack then Mautic will either run without problems or it will tell you what the problem is and let you correct it before you install.

If you do not have a website, Mautic will not do you any good. Start with my friends over at SiteGround and get a WordPress installation up, running, and completely fleshed out before you even think the word Mautic.

Installing Mautic

Once you have your hosting platform up and ready to go, download and unzip Mautic into the proper directory. The proper directory will depend on how you decided to install it. If you decided to install it as a sub-domain, you will unzip Mautic into the root directory of the sub-domain. If you decided to install it in a sub-directory of your main domain, you will need to unzip it into that sub-directory.

Database

Before you begin, you will need a blank database for Mautic to use. Connect to your MySQL/MariaDB service with your favorite tool and create a database, and a user. Make sure you give the user access to the newly created database but ONLY access to that one database.

DO NOT us an existing database. Create a new database. if your hosting provider won’t let you create another database, get a better hosting provider. Start with a clean and empty database.

DO NOT user your root db or a root equivalent user account. Create a new account just for this database. Give it all the permissions except for GRANT.

If you are using a hosting provider with a tool like cPanel, you can use cPanel to create the database, and the user account.

Running the Mautic Installer

Installing Mautic is very straight forward. If you have your web server setup properly then you just point a browser to wherever you unzipped it and BOOM, it starts the install process.

Mautic install screen number one. This screen tells you if your platform is sane. it also makes optional recomendations.

This first screen tells you if your platform is sane. You can see from the green “Ready to Install” that Mautic likes my setup. However, the yellow section makes a few recommendations for how it could be better. Since I manage my own server, it was easy enough to fix the issues. If you are running on a platform where you do not have root access, it might not be as easy for you to fix them. In that case, I would recommend working with your hosting provider to see if they can resolve any or all of the recommendations.

Clicking “Next Step” takes you to the second screen.

This is the second Mautic install screen. This is where you enter all the information about your database.

On this screen, you enter all the information that Mautic needs to connect to your database server. If you have installed applications on a web server before (e.g. WordPress) then this will be familiar to you.

 

The only two things that might not ring a bell on this screen are:

  • Backup Existing Tables/Prefix for backup tables.
    This is set by default to YES. Since you are installing into a new database, it really makes no difference at all. I turn it to no as there should not be any existing tables. Either way, since no backup tables will be created, the prefix for the backup tables is irrelevant. It is safe to ignore this.
  • Database Table Prefix
    This is blank but I highly recommend you putting something here. three random letters and an underscore is plenty. This is a security feature. Anyone who reads the code for Mautic knows what the table names are. This gives an attacker a piece of information they can use in their favor. If you put a random prefix on the tables, it just makes it a little harder for an attacker to compromise your system. (Security s like an onion, it is made of of layers)

Clicking “Next Step” takes you to the screen where you will create the admin user for Mautic.

 

This screen will let you enter in the Mautic admin user name and password. Since this account will have God like powers in your application, do not simply reuse a long and password that you use for other websites. Make the user name something meaningful and use a password manager to create a strong password.

Once you have filled this form out, click “Next Step” to take you to the “Email Configuration” screen.

This is the Mautic email configuration screen. This is where you setup your Email Server provider information.

Enter your name and email address here. Don’t worry about the rest, we are going to properly configure all of this in a future post. Honestly, I’m not really sure why this information is asked for here. I’m sure there is a good reason though.

Success!

Mautic log in screen

If all goes well – and honestly, the installer is designed so well that if something is not going to go well, it won’t let you get to this point – then the next thing you see is the Mautic log in screen. Go ahead, enter your email address and password, you’ve earned it.

As you can see, even from scratch, Mautic is very easy to install. It rivals the WordPress “5 Minute Install” for ease of use and completeness.

Next, we will talk about Email Service Providers.

Until next time,
I <3 |<
=C=

Thank you Derick Rethans for 15 years of XDebug

Dear Reader,

There are a handful of tools that have actually changed how many of us code PHP. XDebug is one of those tools[1]. There is no doubt of the impact that XDebug has has on PHP developers and PHP projects.

Recently, XDebug turned 15. (If you don’t know what XDebug is, start with this SitePoint article about Xdebug) This means that the man responsible for XDebug, Derick Rethans, has been supporting XDebug for 15 years, for free. XDebug is open source. Derick maintains it, answers questions about it, spoke at conference about it, and generally done everything he can to help anyone who is having an issue with it.

FIFTEEN YEARS!

Thank about that. How much code do you have that has lasted fifteen years?

So on this the (close to) anniversary of this product, many of us in the PHP community decided to do something to show Derick how much we appreciate it. Those that know him know that Derick loves a good Scotch. So we decided to buy him some. Originally, I was just going to buy him the most expensive bottle I could find and be done with it. However, my friend James Titcumb stepped in. He knew the owner of the shop that Derick buys his Scotch. He contacted the owner and we gt a quick education in Scotch. tl;dr, expensive doesn’t always mean good. He picked out a selection of bottles that he knew met Derick’s high standards.

On April 26th, 2017, James met Derick at the retailer and here is the video.

Thank you!

  • First and foremost, thank you Derick.
  • Second, thank you James Titcumb for going above and beyond on this project. Y’all really have no idea what he did to make this happen.
  • Third, I gotta say it. Thank you to the lovely and talented Kathy. It is hard to describe how difficult it is to live with someone like me who randomly sit up and shouts “I HAVE AN IDEA”. Yes, sometimes, she buries her face in her hands and weeps. Most of the time though, she supports me, she encourages me, and she helps me. This time, she worked with James to get the Scotch paid for. (Moving that amount of money across the pond is not as easy as you might think it would be.)
  • Finally, but not nearly the least important, thank you to the entire PHP community. When I setup the GoFundMe, I never expected to actually hit the $5,000. I would have been happy with hitting $1,500. As of right now we have hit $5,100! (I’m trying to figure out how to close the GoFundMe!) :)

    Part of the deal I offered companies was that if anyone donated at least $100, I would list their logo and link in this blog post. Here are the people and companies that rose to that level.

    Make sure you say thank you to these individuals and companies.[2]

All told, 151 people donated to make this happen!

So that’s it. We had some fun, we raised some money, we said thank you to someone who has given so much to all of us. Once again, thanks Derick. :)

Until next time,
I <3 |<
=C=

p.s. I will provide a full accounting of all funds raised when I get home from traveling.

[1] The other two, IMHO, are PHPUnit and Composer.
[2] If you are on this list, you probably noticed there re no logos. This is my fault, not yours. Basically, I lost them in all my recent travels. PLEASE send to me again via email and I will update your entry.