Setting up Sublime Text with phpcs

R_JR_J Cheerleader & TroubleshooterMunich Moderator

Although a PHP IDE is the best way to develop, I never fell in love with one. I still prefer using a programmers editor with some comfortable extensions. I run a Windows machine and I would like to share what steps are needed to set up Sublime Text to be a great tool for writing Vanilla code.

First of all: I don't like installers. Therefore, whenever available, I use portable version of all programs. There are downsides with that approach and I know that. It's just my preference. I just want to share my setup so that you can

a) rebuild it if you have nothing and want to start coding Vanilla plugins or

b) take it as an inspiration if you see something that can be used to enhance your development setup or 

c) give you an opportunity to share your best practices so that I can benefit from such comments

 

Here is an overview of what I have:

  • Sublime Text with the following packages DocBlockr, Function Name Display, SFTP, SublimeLinter, SublimeLinter-phpcs, SideBarTools, TrailingSpaces, TabsExtra
  • PHP
  • Composer
  • php-cs

And I have a copy of the Vanilla files from my web server and Vanillas coding standards.

 

Setting up the editor requires the most descriptions and explanations. Therefore we start with prerequisites:

 

PHP

You can get PHP from here. There is a description for the difference between "Thread Safe" and "Non-Thread Safe". For my local machine I use 64-bit NTS ("VC15 x64 Non Thread Safe"), but I guess there's not much difference for our purpose. I unzip this into "C:\Program Files\php", although at some other places it is adviced to have php in c:\php but that doesn't feel right for me in a Windows environment.

As a Windows user you might be tried to unzip that to "php-7.4.2-nts-Win32-vc15-x64" or whatever the zip name might be. I name that folder only "php" because I replace the files in there from time to time with newer php versions and so I do not have a discrepancy between folder name and php version and I don't have to change references that I made to my php file before.

 

In the php folder there is a file called "php.ini-development". Copy that and rename the copy to "php.ini". Edit this file with a text editor. Search for a line ;extension=openssl and remove the ";" sign. After that, search for ;extension_dir = "ext". You also have to remove the ";" sign in this line.

Ready, let's test. Open the windows command line and type in the following "c:\program files\php\php" -v. You should see php telling its version number.

 

Composer

I will use composer for installing php-cs, but there are other ways to do so. Composer is a handy tool, so I think it might come in helpful some time later in your hobby developer career.

If you visit composers download page, you see several options. I have a filtered perception and therefore the first thing I see are some php lines describing how to install composer with php. Those steps do not work for me. Above them there is a Windows setup file, but I don't like setup files.

I prefer what is shown if you scroll down a little bit: the manual installation. You can download the latest stable version with this link. I recommend to download it and save it as "C:\Program Files\php\composer.phar"

To test that, enter "c:\program files\php\php" "c:\program files\php\composer.phar" -V and again, there should be a version number displayer.

 

PHP Code Sniffer

Time for using composer the first time! In the Windows command line, change to the php folder (cd "c:\program files\php"), we will have to do less typing then. Now simply enter php composer.phar global require "squizlabs/php_codesniffer=*" which then will install the code sniffer to your windows users folder.

Everything you install with composer global will be downloaded to the subfolder "\AppData\Roaming\Composer\vendor" of your user profile.

We want to use Vanillas coding standards with phpcs so we need to get them from somewhere. They are available as composer packages, so we can simply write php composer.phar global require vanilla/standards.

Next line will config phpcs so that it knows that there is another path with "sniffs" in it (don't forget to replace "your_name"):

php C:\Users\your_name\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\bin\phpcs  --config-set installed_paths C:\Users\your_name\AppData\Roaming\Composer\vendor\vanilla\standards\code-sniffer

You should get the message "Config value "installed_paths" added successfully" in return.

 

Sublime Text

Fetch the portable version from here and unzip it to "C:\Program Files\Sublime Text". Sublime has a package manager called Package Control. You can install it through the menu: "Tools \ Install Package Control".

Open the package control: "Preferences \ Package Control \ Install Package". Then search for SublimeLinter, install it and do the same with SublimeLinter-phpcs afterwards.

Open "Preferences \ Package Settings \ SublimeLinter \ Settings". Replace everything you see (should be only the debug setting) with the following (replacing "your_name" with your windows user name again):

// SublimeLinter Settings - User
{
    "debug": true,
    "linters": {
        "phpcs": {
            "disable": false,
            "args": [
                "--standard=vanilla"
            ],
            "executable": "c:/Users/rj/AppData/Roaming/Composer/vendor/squizlabs/php_codesniffer/bin/phpcs.bat",
            "lint_mode": "background",
            "env": {
                "PATH": "c:/program files/php/",
            },
        },
    },
}

 

I had to restart Sublime before it started to work, but you might have more luck. Open e.g. \applications\conversations\class.conversationscontroller.php and you will see phpcs working:

The error can be seen when you hover over that red dot.


That's it! I have a local copy of my servers files which I am working on. SFTP helps me getting files in sync with my test server.

With the help of this setup, all lines of code that I am writing are directly linted. They look pretty, obvious errors are directly shown. Not as good as an IDE, but nevertheless nice.

Some day I'll try to set up XDebug with my remote server and sublime.


donshakespearerbrahmson

Comments

  • Awesome write up.

    Same here, I settled with Sublime Text for years until end of last year when Visual Studio Code grew in leaps and had everything I needed to jump.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Microsoft VS Code

    @donshakespeare May I ask what the advantages for you are, that made you change to VS Code?

    Based on its reputation, I tried it, but startup time is (from my feelings) instantanious vs. 4-5 seconds (without any extensions or projects) and HDD space is 35MB vs 250MB. I nevertheless would have considered the change if there were obvious advantages, but I found none.

    I have bought Sublime Text and the commercial ftp package SFTP years ago, so the price is at least for me of no importance any more. After using both with their nagging screen for quite a long while, I found that they will be really worth their price as I was using them that often.


    Notepad++

    Before Sublime, I was using Notepad++ and I still use it a lot as a note taking tool and for it's search(&replace) GUI. I have no grep-tool installed on my Windows machine. Notepad++ is my grep it does a hell of a job there.


    phpcbf

    There is another tool you have already installed, but without knowing about it: the "PHP Code Beautifier and Fixer". Its in the same path as phpcs. If you run phpcs from command line against "applications\conversations\controllers\api\MessagesApiController.php" you get this result:

    FILE: ...ications\conversations\controllers\api\MessagesApiController.php
    ----------------------------------------------------------------------
    FOUND 7 ERRORS AFFECTING 7 LINES
    ----------------------------------------------------------------------
     37 | ERROR | [ ] Doc comment for parameter
        |      |    $conversationMessageModel does not match actual
        |      |    variable name $conversationModel
     38 | ERROR | [ ] Doc comment for parameter $conversationModel does
        |      |    not match actual variable name
        |      |    $conversationMessageModel
     69 | ERROR | [ ] Doc comment short description must start with a
        |      |    capital letter
     137 | ERROR | [ ] Doc comment short description must start with a
        |      |    capital letter
     236 | ERROR | [x] Expected 1 space after FUNCTION keyword; 0 found
     256 | ERROR | [ ] Doc comment short description must start with a
        |      |    capital letter
     371 | ERROR | [x] The closing brace for the class must go on the
        |      |    next line after the body
    ----------------------------------------------------------------------
    PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------
    

     

    See that "PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY" at the end? That's true! phpcbf fixes your errors automagically. Look at both errors in Sublime. Open the file and use the power of Sublimes "goto anything": hit ctrl+p and type "messapicon" or "msgapictrl" or any part of the filename you like, you will see how it works and hit the enter key to open the file.

    Lines 236 and 371 are important for us, so jump to 236 by hitting ctrl+p and typing ":236" or by using the "goto line" shortcut (which opens goto anything prefilled with a colon) and entering only the line number 236, followed by enter key again. Hover over the red dot at the side and you will see this:


     

    And that for 371:


     

    Go to Windows command line and change to the folder where that file is saved. If you have Sublimes TabsExtra package installed, you can right click on the opened file tab and pick "Copy FilePath > Full Path". That would save yourself some work. To fix this code we have to run phpcbf which is a php file, like that (don't forget to set the coding standard=vanilla!

    "c:\Program Files\php\php.exe" %userprofile%\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\bin\phpcbf --standard=vanilla MessagesApiController.php

    Sublime will notice that the file has changed and reload it, yo that you can immediately see the changes in your new favorite editor.

     

    I do not recommend to use phpcbf on other peoples code. Use it on your own, existing, older files, to make them adhere to Vanillas standards.


    donshakespearerbrahmson
Sign In or Register to comment.