Which extension should I use to beautify PHP Code with Sublime Text 3?
PHP Extension
If you want to use an auto formatter, I recommend to use Sublime PHP CS Fixer.
https://packagecontrol.io/packages/PHP%20CS%20Fixer
If you only want to check lint errors without auto formatting, we have following options, I believe.
- SublimeLinter-php
- SublimeLinter-phpcs
- SublimeLinter-phpmd
I hope this helps.
Thanks @gh640 for your reply. I tired to install CS Fixer but failed. I would like to beautify PHP Code. Thanks.
@afoysal Thank you for your response. If you have trouble to install php cs fixer, it might be good to ask for help with an issue on the repositories.
Thanks @gh640. Could you please guide me to install the extension ? I am using Linux Mint 18.3 . Thanks.
As you may already know, the steps are as following:
- Install
php-cs-fixer
command to you system with Composer or manually. If you want to use Composer and haven’t installed it, you need to install Composer before this step. - Once you’ve installed
php-cs-fixer
successfully, install Sublime PHP CS Fixer (the package). - Then, I believe you need to tell the package the path to
php-cs-fixer
. This can be done with the configuration file of the package. - Make a config file for
php-cs-fixer
. - Test its command on your Sublime Text.
The general steps are as above. Which step do you got a trouble in? The more concrete your explanation (of your issue) is, the more useful help you can get here, I believe
Thanks @gh640 for your reply. I installed PHP CS Fixer using the guideline of this page. But what to do next ? Thanks
You seem to have done the step 1!
What you need to do next is:
Once you’ve installed php-cs-fixer successfully, install Sublime PHP CS Fixer (the package).
Do you know how to install a package to your Sublime Text? If you don’t, you need to install Package Control to your Sublime Text first. You can see an instruction for that at: https://packagecontrol.io/installation
Then, after you’ve installed Package Control, you can install Sublime PHP CS Fixer. The following pages would help.
- https://packagecontrol.io/docs/usage
- https://www.granneman.com/webdev/editors/sublime-text/packages/how-to-install-and-use-package-control/
The next step
Make a config file for php-cs-fixer.
is explained in the page you saw.
If you’ve done, please give it a try to one of Sublime PHP CS Fixer’s commands though the command palette of your Sublime Text.
Thanks @gh640 for your reply. May be I installed the extension successfully. Now I have to know how to make a config file for php-cs-fixer ? Thanks.
How to configure the settings so that Sublime PHP CS Fixer can read a config file is explained in the page you see:
https://packagecontrol.io/packages/PHP%20CS%20Fixer
And there’re some examples for the config file content in the the following section:
For example, put the following code into a file named .phpcsfixer
<?php
return PhpCsFixer\Config::create()
->setIndent("\t")
->setLineEnding("\r\n")
;
and put the following setting into your Sublime PHP CS Fixer setting file,
{
"config": "${folder}/.phpcsfixer"
}
then you can see it works well.
These are explained in the pages we referenced in this thread and try reading them carefully again. Then, if there’s a part you don’t understand, please let us know exactly part you don’t understand
Thanks again @gh640 for your support. I tried again. I am sending some screenshots. I think that will be helpful for you. Thanks
Location
.phpcsfixer
config
settings
Thank you for sharing the screenshots. Yet, I don’t understand what you want me to do with the images
I believe you completed it. What is your current issue? What do you want me to do?
If you don’t know where to put the config file for Sublime PHP CS Fixer, the answer is “it depends on you”. For example, if you want to use a common config file for all of your projects, you need to put the file in your home (i.e. /home/foysal/.phpcsfixer
) and set your config like below:
{
"config": "/home/foysal/.phpcsfixer"
}
And if you want to set the project-local setting for my-project-front
, you need to put the .phpcsfixer
file just under the my-project-front
and set your config like below:
{
"config": "${folder}/.phpcsfixer"
}
Here, ${folder}
is replaced to your project root directory automatically.
Also,
It’s also possible to specify multiple config paths. In that case, the first readable file is used:
as stated in the page.
I hope this helps