Czołem!
Całkiem nie dawno dorzuciłem swoje 3 grosze do projektu Kahlan. W tym wpisie przedstawię co nowego dodałem.
Czytaj dalej Kahlan – skróty podczas tworzenia instancji Double
Czołem!
Całkiem nie dawno dorzuciłem swoje 3 grosze do projektu Kahlan. W tym wpisie przedstawię co nowego dodałem.
Czytaj dalej Kahlan – skróty podczas tworzenia instancji Double
Cześć! Dzisiaj na tapecie wzór projektowy wstrzykiwanie zależności. Co znajdziesz w tym wpisie:
Czytaj dalej Dependency Injection na prostym przykładzie w PHP
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.
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
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:
1 2 3 4 5 6 7 8 9 10 |
<?php namespace app\models; use yii\base\Exception; use yii\base\Model; use yii\base\NotSupportedException; use yii\web\IdentityInterface; class User extends Model implements IdentityInterface { |
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 PHPDoc, comments 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:
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!
Hello again.
This will be quick. Let’s take an example:
1 2 3 4 5 6 7 8 9 10 11 12 |
public function testAction($firstName, $lastName, $address) { $data = ['firstName' => $firstName, 'lastName' => $lastName, 'address' => $address]; $filteredData = []; foreach ($data as $key => $value) { $filteredData[$key] = str_replace('!@#$%^&*(', '', $value); } return $this->render('test', [ 'data' => $filteredData ]); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public function testAction($firstName, $lastName, $address) { $data = ['firstName' => $firstName, 'lastName' => $lastName, 'address' => $address]; $filteredData = $this->filterData($data); return $this->render('test', [ 'data' => $filteredData ]); } /** * @param $data * * @return array */ private function filterData($data) { $filteredData = []; foreach ($data as $key => $value) { $filteredData[$key] = str_replace('!@#$%^&*(', '', $value); } return $filteredData; } |
And that’s all :). Enjoy and tuned for next part. Cheers!