Go hacking Develop tooling.

我先假定大家都安裝好 PHPStorm 了

直接 new 一個專案來用吧

$ laravel new blog

如果沒有裝 laravel installer 或者是其他的自己處理,今天 focus 在 IDE 設定上

第一次打開的時候 PHPStorm 會做 indexing, 需要等待一下。

設定 Project PHP 版本

預設會是 PHP5.6,但我們要用 PHP7,要跟 PHPStorm 說

Preferences/Languages&Frameworks/PHP, 也可以順便設定 CLI

等等!我感覺不出來哪邊有差啊?

Return type declarations

http://php.net/manual/en/migration70.new-features.php#migration70.new-features.return-type-declarations

簡單來說就是回傳型別宣告

PHP5.6 沒有支援,所以會這樣:

PHP7 支援

樣式調整

我的 color & font

你可以到

http://www.phpstorm-themes.com/

http://daylerees.github.io/

裝 color IDE plugin

 

很多方法如果有需要再去改就好了。

Coding style

這很重要!

PHPStorm 幫你處理好了!

等等!PSR1/2/3/4/7 是什麼意思?

http://www.php-fig.org/psr/

 

每個 PSR 都制定了某個規範,剛好 PSR-2 是制定 coding style.

所以不要再以為越高版號越高大上了

http://www.php-fig.org/psr/psr-2/

原本程式是這個樣子

<?php

class Car {
    public function run() {
        if(0 > 1)
        {
            return true;
        }
    }
}

Alt + Command + L

class Car
{
    public function run()
    {
        if (0 > 1) {
            return true;
        }
    }
}

安全、快速的把格式調好,看起來就是賞心悅目,重點就是每個人寫出來的代碼就格式上來說長的都一樣,對於 debug 或 tracing 都有一定作用

常用 Live template

public method

pubf

tab

public function ()
{
    
}

切換下個 pramater: tab

prif, prof ...

__construct 也很常用啊,居然沒有!?

沒關係我們自己來

_c

tab

public function __construct()
{
    
}

tpubf 測試案例

提取代碼 tips

快速 rename

減少人工失誤

提取方法

(就是重構)

Result

if ($this->user->owns()) {
    $owner = $this->user->allow();
}
else {
    $owner = $this->user->NotAllow();
}
protected function policy()
{
    if ($this->user->owns()) {
        $owner = $this->user->allow();
    }
    else {
        $owner = $this->user->NotAllow();
    }
}

深層提取

public function index()
{
    $product = $this->user->products()->get();

    $storage = $this->storage->save($product);

    $user_policy = $this->policy();

    return $storage;
}

protected function policy(): void
{
    $author = $this->auth();

    return $author->owns() ? $author->allow() : $author->NotAllow();
}

LocalServiceProvider

什麼是 ServiceProvider?

Laravel 服務加載

https://laravel.com/docs/5.4/providers

情境

開發工具與正式環境透過 ServiceProvider 做隔離

需要的開發包

  • https://github.com/barryvdh/laravel-ide-helper
    • ide helper snippets.
  • https://github.com/barryvdh/laravel-debugbar
    • 頁面除錯工具
  •  https://github.com/recca0120/laravel-tracy
    • 社群朋友做的除錯工具

等等!--dev 意思是?

就是把包裝在 require-dev 當中

如果單純下

$ composer update

他會是 require + require-dev

的包全會安裝。

//裝 dev 的包或更新
$ composer install --dev
$ composer update --dev

//不裝
$ composer install --no-dev
$ composer update --no-dev

依序掛載需要的 dev tools

$ composer require barryvdh/laravel-debugbar --dev

新建 LocalServiceProvider

$ php artisan make:provider LocalServiceProvider

在 local 的時候自動掛載,正式時不要掛載

新建兩個 properties,放入想要掛入的 providers.

protected $providers = [
        \Barryvdh\Debugbar\ServiceProvider::class,
    ];

protected $aliases = [
    'Debugbar' => \Barryvdh\Debugbar\Facade::class,
];

Register

public function register()
{
    if ($this->app->isLocal() && ! empty($this->providers)) {

        foreach ($this->providers as $provider) {
            $this->app->register($provider);
        }

        if ( ! empty($this->aliases)) {
            foreach ($this->aliases as $alias => $facade) {
                $this->app->alias($alias, $facade);
            }
        }
    }
}

config/app.php

register LocalServiceProvider

/*
 * Development Service Provider
 */
\App\Providers\LocalServiceProvider::class,

對比

laravel-ide-helper

$ composer require --dev barryvdh/laravel-ide-helper

LocalServiceProvider

protected $providers = [
    \Barryvdh\Debugbar\ServiceProvider::class,
    \Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
];

Artisan

產生 _ide_helper.php

$ php artisan ide-helper:generate

讓 PHPStorm for laravel 更神奇

情境

連字串符都要直接跳轉過去!

https://github.com/Mombuyish/yish.im-posts-source/blob/master/Laravel-plugin-on-phpstorm.md

同場加映

Laravel Exceptions

讓頁面除錯更快速

原本這東西 4.2 之前是內建的,但後來 Taylor 覺得這東西是需要再掛載,所以就獨立了

https://github.com/GrahamCampbell/Laravel-Exceptions

$ composer require graham-campbell/exceptions

掛入 ExceptionHandler

改變 Handler 繼承

use Exception;
use GrahamCampbell\Exceptions\NewExceptionHandler;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends NewExceptionHandler
{
....

default

NewExceptionHandler

等等!我的錯誤 endpoint 沒出現啊!

還要裝 filp/whoops

$ composer require filp/whoops --dev

安心妥當

Made with Slides.com