Tân Nguyễn
Tân Nguyễn I'am developer

Cài đặt PHP CS cho Sublime Text

Cài đặt PHP CS cho Sublime Text

Nếu bạn đang code PHP và thường xuyên mắc phải một số lỗi về coding convertion hoặc bạn muốn kiểm tra code mình đã chuẩn chưa thì hôm nay mình xin giới thiệu tới các bạn 1 plugins của sublimetext

Sau đây là một số package cần cài đặt trên máy của bạn. Và máy cá nhân mình đang dùng là Ubuntu 16.04

PHP Pear

1
apt-get install php-pear

PHP Code Sniffer (phpcs) Pear Package

1
pear install PHP_CodeSniffer

Sau khi cài xong thì các bạn kiểm tra thử nó đã OK chưa nhé.

1
which phpcs

/usr/bin/phpcs // Trả về như thế này là OK

PHP Depends Pear Package

1
2
pear channel-discover pear.pdepend.org
pear install pdepend/PHP_Depend

PHP Mess Detector (phpmd) Pear Package

1
2
pear channel-discover pear.phpmd.org
pear install phpmd/PHP_PMD

Tiếp tục kiểm tra xem thử phpmd đã được cài thành công chưa nhé

1
which phpmd

/usr/bin/phpmd // Trả về ntn là OK

Install PHP CS-Fixer

1
2
curl http://get.sensiolabs.org/php-cs-fixer.phar -o /usr/local/bin/php-cs-fixer
chmod a+x /usr/local/bin/php-cs-fixer

Install ocaml language interpreter

1
apt-get install ocaml

Install pfff

1
2
3
4
5
6
cd /opt/
git clone --depth=1 https://github.com/facebook/pfff.git
./configure
make depend
make
make opt

Install sublime-phpcs from Sublime Package Control.

Các bạn vào đây để config phpcs cho Sublime :

Preferences > Package Settings > PHP Code Sniffer > Settings – User:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
{
    // Plugin settings

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

    // 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": "",

    // 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": [],

    // What color to stylise the icon
    // https://www.sublimetext.com/docs/3/api_reference.html#sublime.View
    // add_regions
    "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": true,

    // 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": "",

    // 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": false,

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

    // Path to where you have the php-cs-fixer installed
    "php_cs_fixer_executable_path": "",

    // Additional arguments you can specify into the application
    "php_cs_fixer_additional_args": {

    },



    // phpcbf settings

    // Fix the issues on save
    "phpcbf_on_save": false,

    // Show the quick panel
    "phpcbf_show_quick_panel": false,

    // Path to where you have the phpcbf installed
    "phpcbf_executable_path": "",

    // Additional arguments you can specify into the application
    //
    // Example:
    // {
    //     "--level": "all"
    // }
    "phpcbf_additional_args": {
        "--standard": "PSR2",
        "-n": ""
    },



    // 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" : ""
    }
}

Save file này lại thì bây giờ có thể dùng PHP Code Sniffer trong Sublime rồi

Ngoài ra các bạn có thể cài thêm một số plugin như phpfmt

Nếu bạn cài phpfmt thì bạn có thể config thêm như dưới nhé.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
	"RemoveUseLeadingSlash": true,
	"autocomplete": true,
	"autoimport": true,
	"enable_auto_align": true,
	"format_on_save": true,
	"ignore": "Parent",
	"indent_with_space": 4,
	"passes":
	[
		"ReindentSwitchBlocks",
		"OrderAndRemoveUseClauses",
		"StripSpaceWithinControlStructures",
		"ShortArray",
		"SpaceBetweenMethods",
		"AutoSemicolon"
	],
	"psr2": true,
	"smart_linebreak_after_curly": true,
	"version": 4,
	"visibility_order": true,
	"yoda": true
}

comments powered by Disqus