WordPress’ PHP coding standards has this to say about the use of spaces:
Put spaces on both sides of the opening and closing parenthesis of
if
,elseif
,foreach
,for
, andswitch
blocks.
You can configure Sublime Text to do some of this for you when it auto closes brackets.
To set this up, open Sublime Text > Preferences > Key Bindings - Default
and search for // Auto-pair brackets
to find this block of code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Auto-pair brackets { "keys": ["("], "command": "insert_snippet", "args": {"contents": "($0)"}, "context": [ { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true }, { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }, { "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|;|\\}|$)", "match_all": true } ] }, { "keys": ["("], "command": "insert_snippet", "args": {"contents": "(${0:$SELECTION})"}, "context": [ { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true }, { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true } ] }, |
Copy the first two keys
sections (the bit shown above) to your clipboard and open up Sublime Text > Preferences > Key Bindings - User
. Unless you have previously set up custom key bindings, this file will be empty. Paste in the contents you copied and add a space after the (
and before
the )
in the contents
values for both key blocks as so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[ { "keys": ["("], "command": "insert_snippet", "args": {"contents": "( $0 )"}, "context": [ { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true }, { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }, { "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|;|\\}|$)", "match_all": true } ] }, { "keys": ["("], "command": "insert_snippet", "args": {"contents": "( ${0:$SELECTION} )"}, "context": [ { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true }, { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true } ] }, ] |
This will automatically insert a space after the opening (
and before the )
for you.
If you would like this to only apply to PHP files, you can add this also add this line to each block:
1 |
{ "key": "selector", "operator": "equal", "operand": "source.php" } |
Happy coding!