Slim PHP Framework PhpStorm IDE autocompletion Solution

I am a big fan of PHP Slim Framework. But sometimes, I realized that when I work with large and complex libraries with Slim as dependency. It just kills my productivity, it kills my valuable time too. It’s all because of I can’t remember all the method names, properties of my dependent class or libraries. And with Pimple dependencies Slim Framework don’t have any way to bypass referencing of my third party libraries which I add through it’s container as dependency. So I decided to figure out a solution for me.

First, I just installed and configured slim framework and here is my scripts.

<?php
//index.php
require "../vendor/autoload.php";
$app = new \Slim\App();
$container = $app->getContainer();

/**
 * @return \ShahariaAzam\Classes\HintsClass
 */
$container['hints'] = function () {
 return new \ShahariaAzam\Classes\HintsClass();
};

$app->get('/foo', function (\Slim\Http\Request $request, \Slim\Http\Response $response) {
 /** @var Dummy $this */
 $this->hints->printHelloWorld();
 $response->write("Hello World");
});

$app->get('/another-foo', function (\Slim\Http\Request $request, \Slim\Http\Response $response) {
 /** @var Dummy $this */
 $this->hints->printHelloWorld();
});
$app->run();

 

And I created a class HintsClass. Now the main trick is, I created a dummy _ide_autocomplete.php file in the project root and defined all the containers key as public property. Like this.

<?php
//src/HintsClass
namespace ShahariaAzam\Classes;

/**
 * Class HintsClass
 * @package ShahariaAzam\Classes
 */
class HintsClass
{
    /**
     * @var int
     */
    public $test = 1;

    /**
     * @return bool
     */
    public function testMethod()
    {
        return true;
    }

    /**
     * @return string
     */
    public function printHelloWorld()
    {
        return "HelloWorld";
    }
}

 

Now our goal is from inside route, our PHPStorm IDE will show the autocomplete and hinting for code completion when we will access this class with $this->hints->[ctrl+space to see autocompletion]. And in our _ide_autocomplete.php file we wrote the following codes. FYI, this _ide_autocomplete.php will never execute. It just used for giving IDE as a referential hinting for all the properties .

<?php

//_ide_autocomplete.php
class Dummy
{
    /**
     * @var \ShahariaAzam\Classes\HintsClass
     */
    public $hints;
}

 

You will see in this Dummy class file according to our slim containers[‘hints’], I just mentioned the same return reference here. And see my main index.php, in every route I just gave IDE hints about the $this context with

/** @var Dummy $this */

And after that from route context IDE will show the autocompletion of our dependencies like $this->hints[ctrl+space to see suggestions].

You can see my whole scripts are in GitHub https://github.com/shahariaazam/slim-phpstorm-autocomplete. And see this work in YouTube, https://www.youtube.com/watch?v=jyEvApLRofw .Please let me know if any issue found, write me in comments. Thank you.

Install Sublime Text 2 on Ubuntu and make application shortcut luncher

Step 1

Download Sublime Text 2 from their download location. To download it please type the following command :

sudo wget http://c758482.r82.cf2.rackcdn.com/Sublime%20Text%202.0.2.tar.bz2
Step 2

After downloading Sublime Text 2 you’ll need to extract the .tar.bz2 file. To extract tar.bz2 type the following command :

tar xf Sublime Text 2.0.1 x64.tar.bz2

Now you will get a extracted directory with all the files needed to run Sublime Text 2. So it’s better to move this directory to another appropriate place. To move this folder

sudo mv Sublime Text 2 /opt/

If you want to write sublime command only to run Sublime Text 2 then you need to link the file by typing the following command :

sudo ln -s /opt/Sublime Text 2/sublime_text /usr/bin/sublime

Now to create luncher in Ubuntu Unity we just need to create sublime.desktop file in /usr/share/applications with the following snippet :

Create sublime.desktop file by typing the following command :

sudo sublime /usr/share/applications/sublime.desktop

Then write the following code in newly created blank text file.

[Desktop Entry]
Version=1.0
Name=Sublime Text 2
# Only KDE 4 seems to use GenericName, so we reuse the KDE strings.
# From Ubuntu's language-pack-kde-XX-base packages, version 9.04-20090413.
GenericName=Text Editor

Exec=sublime
Terminal=false
Icon=/opt/Sublime Text 2/Icon/48x48/sublime_text.png
Type=Application
Categories=TextEditor;IDE;Development
X-Ayatana-Desktop-Shortcuts=NewWindow

[NewWindow Shortcut Group]
Name=New Window
Exec=sublime -n
TargetEnvironment=Unity

Save this file and restart your terminal. And type the following command to run sublime.

sudo sublime

Now enjoy the power of the world’s most popular editor for programmers. Happy coding!

PHPStorm Keyboard Shortcuts – For faster web PHP Coding

If you are a PHP coder just leave Dreamweaver. If you are novice website designer Dreamweaver is perfect. Now I can feel that how much bad the Dreamweaver is as an IDE! I prefer PhpStorm as my PHP IDE. Really PHPStorm is a great PHP editor for faster PHP application development. Since 3 years I used Adobe Dreamweaver but now I can be sure that Dreamweaver is such a novice playground. So if you want to be a PHP coder you should switch your IDE and you can consider PHPStorm. It’s really great to have a great coding environment.

Download PHPStorm Reference Card