Repozytorium ustawień w PhpStorm / WebStorm

Cześć

Miał być post w niedzielę, jest w poniedziałek! 🙂

Dzisiaj krótko. Mało kto wie o wspaniałej wtyczce do naszego IDE – Settings Repository. Pozwala nam na zapisywanie ustawień aktualnego projektu do zewnętrznego repozytorium (polecam Bitbucket). Dzięki temu mamy te same ustawienia w domu, w pracy, czy w każdym innym miejscu. Ustawienia się synchronizują w pełni automatycznie. Więc w nowym miejscu możesz się poczuć jak w domu.

Konfiguracja

Czytaj dalej Repozytorium ustawień w PhpStorm / WebStorm

Kahlan – PHP test framework – dla wolności, prawdy i sprawiedliwości

TDD

Czyli coś co chyba każdy z nas robi – a przynajmniej powinien! Kto słyszał o PHPUnit? Albo o PHPSpec? A może Codeception? To dobre i sprawdzone narzędzia które mają jednak swoje minusy. Dzisiaj chcę wam zaprezentować całkiem świeże narzędzie – Kahlan.

Co to kurcze jest?

Jest to kolejny framework do pisania testów. Czym się różni od poprzednich? Na pewno składnią. Czy znacie RSpec albo jasmine? Tutaj mamy bardzo podobną implementację dla PHP, a więc używamy describe-it. Jest to cholernie wygodne i łatwe do zrozumienia. Chcecie przykład*?

Czytaj dalej Kahlan – PHP test framework – dla wolności, prawdy i sprawiedliwości

Złe strony JavaScriptu – new oraz this

Cześć!

Słowem wstępu…

Obiecałem wam ostatnio, że dodam wpis w ciągu dwóch tygodni. Jednak pisząc ostatniego posta zapomniałem, że za chwilę mam ślub i podróż poślubną…

Ah ten czas szybko leci! Wydarzenia te wydłużyły proces produkcji tego artykułu. Za co z góry przepraszam!

Co dzisiaj?

Rozważymy dwa problematyczne elementy JavaScriptu. Operator new oraz słowo kluczowe this.

Brak operatora new

Konstruktor to zwykła funkcja. Co się stanie gdy pominiesz operator new?

Czytaj dalej Złe strony JavaScriptu – new oraz this

ESLint – install and config – PhpStorm/WebStorm and git pre commit hook

Hello there! It’s been awhile since last post. Sorry 🙁

Let’s talk about JavaScript linting. If you don’t know what is it – go to wikipedia.

Quick Introduction

Currently we have four linting tools available:

Czytaj dalej ESLint – install and config – PhpStorm/WebStorm and git pre commit hook

Running and debugging Karma in PhpStorm / WebStorm

Hello!

Recently I wrote a post about configuring the Karma with RequireJS and PhantomJS (http://damian.dziaduch.pl/2015/09/12/karma-among-with-jasmine-requirejs-and-phantomjs/).

Today I’d like to move on and use the Karma inside the IDE instead of terminal. Before we start make sure you have installed & enabled Karma plugin and JavaScript debugger plugin in PhpStorm. I’m going to work on my previous example.

Czytaj dalej Running and debugging Karma in PhpStorm / WebStorm

PhpStorm Refactoring tutorial, part 3 – moving the class

Welcome in third part of my tutorial!

Today we are going to learn common thing while code refactor – class movement.

What I always did was to manually move the file in the Finder and then changed it’s namespace in the IDE. Finally I had to search whole project for class name etc. But PhpStorm can do all of that with one simple action.

I’m going to move the class \app\models\User to \common\models\User (Yii2 project).

Let me show you the beginning of my class:

First thing what you need to do is to move the cursor into class name and open refactor menu (right click -> refactor, or press ctrl + t) and choose Move.

Fill the target namespace. I always check two checkboxes, Search in comments and string and Search for text occurrences. This will allows us to refactor the PHPDoccomments and other places where the class name exists.

Before pressing the Refactor button, I recommend to Preview the changes. All found places you will see on the list like this:

class move refactor

With second mouse button you can exclude results from refactoring. When you’re done, press Do Refactor. Now run your tests. Everything should work like as a charm!

Thank you for reading. Have a good day!

 

PhpStorm refactoring tutorial – part 2 – method extraction

Hello again.

This will be quick. Let’s take an example:

This method is doing too much. We want to extract new method which will filter the data and return the result. Please highlight the marek lines and press Refactor This button (ctrl + t). Choose Extract method. New window will appear. You can now customise your new method –  parameters order, visibility, PHPDoc and many many more. Im most cases you only need to add new method name and press enter – IDE fills all needed data for you. In this example I named new method filterData. Here’s the code after quick refactor:

And that’s all :). Enjoy and tuned for next part. Cheers!

PhpStorm refactoring tutorial , part 1

PhpStorm like all JetBrains IDEs has o lot of built in features. And you can add even more with plugins. It provides many refactoring features.

This will be a first part of refactoring tutorial.

We will start from the simple things.

Lesson 1: Extracting a variable and renaming it.

Example code to be refactored:

This code is bad. If you use jQuery object more than once, assign it to variable.

First let’s extract variable. Move cursor to any occurrence of $(‚#container’) in the code and press ctrl+r. Menu will appear, now select Extract variable. IDE gives you ability to replace all occurrences or just the highlighted one. We want to replace all of them. Choose it. Now we have ability to name the variable. IDE suggest names by the code, in most cases it gives  correct name, but we can change it :).

Code after refactor:

Next thing. We have extracted the variable but later we decide that current name is not obvious. Naming is very important so we want to change it. Move cursor to any occurrence of variable $container and again press +r (on Mac) or ctrl+r (on other os). In the menu choose first position, Rename. Place a new name in the popup and do refactor.

You have ability to search the whole project for occurrences of the variable name in the string / comment and text . Try it by yourself, it’s very handy :).

That’s all for today. See you in the next part.

Karma among with Jasmine, RequireJS and PhantomJS

This time something about JavaScript. Everybody likes JavaScript :P.

I’d like to write quick guide how to set up working tests environment.

First, what we need:

Let get all required things by NPM. Create a package.json with content:

Now run the command:

Great. We have now all required dependencies.

Next thing is to configure Karma. There are two ways of doing this, manually by creating a karma config file or using karma creator (karma init command). I prefer doing it manually. Create a new file and name it karma.conf.js. Here’s my file with comments:

Basically what you need to change is basePath and files / exclude. But that’s not all. We need to create another file which will run asynchronous our test with RequireJS. Let’s place this file in the tests and name it main.js. This is the only file which is included by karma. Content of the file:

Variable window.__karma__.files comes from karma.conf.js files section. We need to filter them as we want to require only tests files. The source files will be declared as dependencies of each test file.

Let’s create a example test file tests/exampleSpec.js and put in it:

We are defining a spec which has source file as a dependency. RequireJS injects it’s to our spec and we can now test it with Jasmine. How cool is that?

Let’s define our example via src/example.js:

The last thing which need to be done is to export the variable which will tell Karma where PhantomJS binary is:

Now let’s run Karma:

Finally you should see something like this:

karma

If you have any problems with PhantomJS – maybe you need to install some libs required by it. Check it’s website for more info. Other solution is to try different browser – Chrome, Firefox etc. But this can’t be done via SSH… 🙂

Any problems, suggestions, feel free to comment.

 

SSH + XDebug + PhpStorm

Hi.

CLI debugging. I never did that. Until today. My tests started to fail and I had no idea why.

So that’s my story why I started to debug tests in the CLI via SSH.

Here’s a list of ingredients:

  • PhpStorm,
  • remote SSH (I’m using Vagrant),
  • PHP + XDebug.

First thing what we need to do is to configure the PhpStorm.

Open your setting and go to Languages & FrameworksPHPDebugDBGp Proxy.

dbgp proxy

  1. Fill the field IDE key with key you choose (I chose PHPSTORM and I recommend it).
  2. In the Host field put the IP which is seen by remote (run echo $SSH_CLIENT on the remote to see it).
  3. Default Port is 9000 and you should not change it.

Next thing to configure is Language & FrameworksPHPServers.

server

  1. Put the server Name.
  2. In the Host field type the IP address, same as on DBGp Proxy.
  3. If you set other Port for DBGp Proxy then change it here.
  4. As we are working on XDebug you don’t have to change the Debugger :).
  5. My remote has different folders structure so I had to set path mapping, probably you will have to do the same.

Ok PhpStorm is ready to go.

Now go to SSH and run this command:

In the first line put your Server Name which you’ve just set. In the second just update the remote_host with the IP address and change the idekey to yours.

The last thing to do is to enable listening for debug connections in the PhpStorm. Just click the button in the top right corner:

enable debug

Ah I forgot, please add some breakpoints! And then you can run your tests / scripts etc.

 

Enjoy 😀