聚會時間公告: 因應COSCUP 2011, Kalug 8月份休會一次

九月 10, 2014

小惡魔AppleBOY
AppleBOY
is about »

tag cloud

» 在 Windows 上安裝 Sublime Text 2 搭配 PHP-CS-Fixer 套件

Sublime_Text_Logo

由於近幾年來 PHP-Fig 發佈 PSR-0 ~ PSR-4 標準,所以在撰寫 PHP 程式碼時,請依照標準,而為了符合這標準,@fabpot 寫了一個轉換工具叫 PHP-CS-Fixer (PHP Coding Standards Fixer) 最主要目的是按照 PSR-1 and PSR-2 的 Coding Style,只要透過 command 就可以將程式碼轉成標準格式。此篇要紀錄在 Windows 搭配 Sublime Text 安裝 PHP-CS-Fixer。

在 Sublime 編輯器可以使 ctrl + shift + p 後選 Install package 找到 PHPCs 點下安裝即可。打開 Preferences -> Package settings -> PHP Code Sniffer -> Settings,裡面把相關路徑補上去即可。底下是參考設定

{
    // Plugin settings

    // Turn the debug output on/off
    "show_debug": true,

    // Which file types (file extensions), do you want the plugin to
    // execute for
    "extensions_to_execute": ["php"],

    // Do we need to blacklist any sub extensions from extensions_to_execute
    // An example would be ["twig.php"]
    "extensions_to_blacklist": [],

    // Execute the sniffer on file save
    "phpcs_execute_on_save": true,

    // Show the error list after save.
    "phpcs_show_errors_on_save": true,

    // Show the errors in the gutter
    "phpcs_show_gutter_marks": true,

    // Show outline for errors
    "phpcs_outline_for_errors": true,

    // Show the errors in the status bar
    "phpcs_show_errors_in_status": true,

    // Show the errors in the quick panel so you can then goto line
    "phpcs_show_quick_panel": true,

    // The path to the php executable.
    // Needed for windows, or anyone who doesn't/can't make phars
    // executable. Avoid setting this if at all possible
    "phpcs_php_prefix_path": "C:\\xampp\\php\\php.exe",

    // Options include:
    // - Sniffer
    // - Fixer
    // - Mess Detector
    //
    // This will prepend the application with the path to php
    // Needed for windows, or anyone who doesn't/can't make phars
    // executable. Avoid setting this if at all possible
    "phpcs_commands_to_php_prefix": ["Fixer"],

    // What color to stylise the icon
    // https://www.sublimetext.com/docs/3/api_reference.html#sublime.View
    // add_regsions
    "phpcs_icon_scope_color": "comment",


    // PHP_CodeSniffer settings

    // Do you want to run the phpcs checker?
    "phpcs_sniffer_run": true,

    // Execute the sniffer on file save
    "phpcs_command_on_save": false,

    // It seems python/sublime cannot always find the phpcs application
    // If empty, then use PATH version of phpcs, else use the set value
    "phpcs_executable_path": "C:\\xampp\\php\\phpcs.bat",

    // Additional arguments you can specify into the application
    //
    // Example:
    // {
    //     "--standard": "PEAR",
    //     "-n"
    // }
    "phpcs_additional_args": {
        "--standard": "PSR2",
        "-n": ""
    },



    // PHP-CS-Fixer settings

    // Fix the issues on save
    "php_cs_fixer_on_save": true,

    // Show the quick panel
    "php_cs_fixer_show_quick_panel": true,

    // Path to where you have the php-cs-fixer installed
    "php_cs_fixer_executable_path": "C:\\xampp\\php\\php-cs-fixer.phar",

    // Additional arguments you can specify into the application
    //
    // Example:
    // {
    //     "--level": "all"
    // }
    "php_cs_fixer_additional_args": {
        "--level": "all"
    },



    // PHP Linter settings

    // Are we going to run php -l over the file?
    "phpcs_linter_run": true,

    // Execute the linter on file save
    "phpcs_linter_command_on_save": true,

    // It seems python/sublime cannot always find the php application
    // If empty, then use PATH version of php, else use the set value
    "phpcs_php_path": "",

    // What is the regex for the linter? Has to provide a named match for 'message' and 'line'
    "phpcs_linter_regex": "(?P<message>.*) on line (?P<line>\\d+)",



    // PHP Mess Detector settings

    // Execute phpmd
    "phpmd_run": false,

    // Execute the phpmd on file save
    "phpmd_command_on_save": true,

    // It seems python/sublime cannot always find the phpmd application
    // If empty, then use PATH version of phpmd, else use the set value
    "phpmd_executable_path": "",

    // Additional arguments you can specify into the application
    //
    // Example:
    // {
    //     "codesize,unusedcode"
    // }
    "phpmd_additional_args": {
        "codesize,unusedcode,naming": ""
    },


    // PHP Scheck settings

    // Execute scheck
    "scheck_run": false,

    // Execute the scheck on file save
    "scheck_command_on_save": false,

    // It seems python/sublime cannot always find the scheck application
    // If empty, then use PATH version of scheck, else use the set value
    "scheck_executable_path": "",

    // Additional arguments you can specify into the application
    //
    //Example:
    //{
    //  "-php_stdlib" : "/path/to/pfff",
    //  "-strict" : ""
    //}
    "scheck_additional_args": {
        "-strict" : ""
    }
}

這裡面需要注意的是底下參數

"phpcs_php_prefix_path": "C:\\xampp\\php\\php.exe",
"phpcs_commands_to_php_prefix": ["Fixer"],
"phpcs_executable_path": "C:\\xampp\\php\\phpcs.bat",
"php_cs_fixer_executable_path": "C:\\xampp\\php\\php-cs-fixer.phar",
"php_cs_fixer_on_save": true,

如果發現沒作用,請打開 sublime console 介面,請直接按快速鍵 ctrl + ~ 就可以看到哪邊設定錯誤。

八月 20, 2012

小惡魔AppleBOY
AppleBOY
is about »

tag cloud

» 快速修正專案 PHP Coding Standards

底下是之前的投影片教導創業團隊制定 PHP 程式碼規範,讓工程師可以遵循此規則達到團隊合作

Maintainable PHP Source Project from Bo-Yi Wu

投影片內容都是現有 PHP FIG Group 所制定的 PSR-0, PSR-1, PSR-2 規則,那如何將既有專案的 Coding Style 轉換成上述標準呢,可以透過 PHP Coding Standards Fixer tool 來快速完成,此工具是由 Symfony Framework 完成。透過此工具可以快速且無痛轉換程式碼風格,舉個簡單例子:

本來

<?php
if ($a > $b)
{
    .....
}

轉換後變成

<?php
if ($a > $b) {
    .....
}

也可以透過指定的方式來修正,例如: indentation (將 Tabs 轉換成 4 Spaces),trailing_spaces (去除單行程式碼結尾空白),php_closing_tag (忽略檔案結尾 ?> 符號) … 等,大家可以到官網看看,如果是透過 wget 方式下載,請務必轉換該執行檔權限

$ chmod 755 /usr/local/bin/php-cs-fixer

如果是用在修正其他 Framework 上面,請務必注意 PSR-0 規則會跟 Framework 命名方式相衝突 (如命名方式 by CodeIgniter)

Related View

support:

biggo.com.tw

A Django site.