在 Sublime Text 裡的格式化 PHP 代碼


由於某些原因,需要處理一堆比較舊的PHP代碼,
而且編程風格也不統一,所以需要先統一後再處理會比較方便.
不過於由 Pear 的東西有點參差不齊.所以整合到 Editor 內的過程也不太順利.
來記錄一下.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 更新已有的 Pear 套件
# 之後安裝 PHP_Beautifier
# 再列出使用方法
cd /Applications/XAMPP/xamppfiles/bin
sudo ./pear update-channels
sudo ./pear upgrade-all
sudo ./pear install --alldeps PHP_Beautifier-0.1.15
./php_beautifier --help

# 修正出現 Cannot redeclare class Config 錯誤
# 切進 PHP_Beautifier 的 Filter 目錄
cd /Applications/XAMPP/xamppfiles/lib/php/pear/PHP/Beautifier/Filter

# 打開下面兩個檔案
- Pear.filter.php
- phpBB.filter.php

# 注解掉下句這句
require_once ('PEAR/Config.php');

# 安裝 Sublime Text 2 的 PhpBeautifier 套件
cd ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/
git clone https://github.com/SublimeText/PhpBeautifier.git PhpBeautifier
cd PhpBeautifier/
vim php_beautifier.py

indent = "-t"
cmd = "/Applications/XAMPP/xamppfiles/bin/php_beautifier"

# 最後在需要美化/格式化的代碼編輯界面裡就可以格式化了
Open File > Command+Shift+P > Format PHP > Enter