Jump to content

Rule set and debugger for PHP in Visual Studio


Recommended Posts

We have planned to use Visual Studio as IDE for our PHP web application. Please suggest how to set up PHP_code sniffer (for Rule set) and Xdebug (for debugging with break points) in Visual Studio. Or do we have alternate tools. Can you please help. Thanks

Link to comment
Share on other sites

Visual Studio or VS Code?

Anything you haven't been able to figure out with some searching on your own? Myself, I'm seeing a number of results on how to set both of those IDEs up for PHP development...

Link to comment
Share on other sites

10 hours ago, requinix said:

Visual Studio or VS Code?

Anything you haven't been able to figure out with some searching on your own? Myself, I'm seeing a number of results on how to set both of those IDEs up for PHP development...

Using VS code we are able to find tools. We used PHP_CodeSniffer and xdebugg in VS Code. We are not able to find such tools in Visual Studio. 

Link to comment
Share on other sites

I would first check the VS PHP extension to see what support it has; probably not PHP_CodeSniffer, but quite possibly Xdebug.

If not then there's a good chance you're out of luck - because nobody uses Visual Studio for PHP. And, frankly, you should take that as a hint, and go for a PHP IDE (like PhpStorm) or switch to VS Code (at least for this).

Don't get me wrong, I like Visual Studio. I think it's a great platform. But software development is about using the right tool for the job.

Link to comment
Share on other sites

Visual Studio can be a powerful IDE for PHP development with the help of plugins and extensions. Below are the steps to set up PHP_CodeSniffer for coding standards and Xdebug for debugging with breakpoints in Visual Studio, along with some alternative tools:

PHP_CodeSniffer Setup: PHP_CodeSniffer helps maintain coding standards in your project. You can integrate it into Visual Studio using the following steps:

First, install PHP_CodeSniffer globally using Composer. Open your command line interface and run:

composer global require "squizlabs/php_codesniffer=*"

Next, you'll need to add PHP_CodeSniffer as an external tool in Visual Studio.

Go to Tools > External Tools.

Click on Add and fill in the following fields:

Title: PHP_CodeSniffer

Command: Enter the path to the phpcs.bat file. This can typically be found in C:\Users\YourUsername\AppData\Roaming\Composer\vendor\bin.

Arguments: --standard=PSR2 $(ItemPath)

Initial directory: $(ItemDir)

Check the option for "Use Output window".

Click OK to save.

Now, you can use PHP_CodeSniffer by right-clicking on a PHP file in Solution Explorer, and then selecting PHP_CodeSniffer from the Tools menu. It will check your code against PSR2 coding standards.

 

Best Regard

Danish hafeez | QA Assistant

ICTInnovations

Xdebug Setup: Xdebug is a PHP extension that allows for debugging PHP scripts. Here's how you can set it up in Visual Studio:

First, download and install Xdebug on your server. You can follow the instructions on the Xdebug website.

Once installed, configure Xdebug in your php.ini file. Make sure to set xdebug.remote_enable=1 and xdebug.remote_autostart=1.

In Visual Studio, you'll need to install the "PHP Debug" extension.

Go to Extensions > Manage Extensions.

Search for "PHP Debug" and install it.

Configure PHP Debug extension:

Go to Tools > Options.

Navigate to PHP Tools > Debugging.

Set the "Xdebug Port" to the port configured in your php.ini file (default is 9000).

Click OK to save the settings.

Now, you can set breakpoints in your PHP code and start debugging by pressing F5 or clicking on the Start Debugging button.

Alternate Tools: If you find the setup process for PHP_CodeSniffer and Xdebug too complicated, or if you're looking for alternatives, you can consider the following:

PHP Mess Detector (PHPMD): Similar to PHP_CodeSniffer, PHPMD focuses on detecting potential problems in your code.

PHPCSFixer: This tool automatically fixes many coding standard violations reported by PHP_CodeSniffer.

DBGp Proxy: An alternative to Xdebug for debugging PHP applications.

Visual Studio Code: If you're open to using a different IDE, Visual Studio Code has excellent support for PHP development with extensions available for PHP_CodeSniffer and Xdebug integration.

Choose the tools that best fit your requirements and preferences.

Link to comment
Share on other sites

There are a couple of PHP specific commercial addons.  The default one isn't very good unfortunately, and you should disable it.

I have had good results (as have many other people) using PHP Inteliphense.  Inteliphense can be used in basic mode, or you can pay $20 use to get upgraded features.  I would start with the free version and see if it is working for you, and then if you like it, consider paying for the extra features.  

It is very important that you follow the instructions on the page, which has you disable the default php intelisense.  Otherwise it will conflict with other php addons.

As for xdebug, there isn't one magic way for it to work, because it really depends on how and where you are running php.  

In the php4lamp docker project I have this config file:  Docker4lamp xdebug instructions

It is designed to work with this xdebug.ini file (for use with the docker containers). xdebug.ini

Of particular interest, and a setting you should look at in the xdebug.ini file is the xdebug.client_host= setting

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.