Eric Hogue on May 9th, 2011

In a previous post, I talked about Continuous Integration. If your Continuous Integration server runs on every commits, it will help you keep your code quality high. It will also make integration a non issue.

However, when I make a mistake, produce sub-optimal code or if I write code that does not respect our coding standards I want to know before I commit. I would rather fix my errors before they break the build. I can run all the tools that the server runs. But running them on my machine and analyzing the results every time I save will be painful. Fortunately, there is an easy solution.

PHPsrc

PHPsrc is a plugin that allow you to run PHP_CodeSniffer, PHPUnit, PHP Depend and PHP Copy/Paste Detector directly in Eclipse. The site also says that more tools should come. As you work, you will see any transgression you make. That will save you from breaking the build, but it also makes it easier to fix the problem. After all, you just wrote the faulty lines of code.

You install it like any other Eclipse plugin. Go to the Help menu and click on “Install new software…” In the Install dialog, click on “Add…”, choose a name for the repository and enter “http://www.phpsrc.org/eclipse/pti/” in the location. After you click on OK, Eclipse will detect “PHP Tool integration. Select it and click on Next twice. Accept the condition and click on Finish. After the installation is completed, you will have to restart Eclipse.

After the installation, in the preferences you will have a new “PHP Tools” menu. Make sure that every tools have an PHP executable and a PEAR library selected. For PHP CodeSniffer, you will also need to pick a standard.

PHPsrc Configuration

PHPsrc Configuration

Your Eclipse should now have new buttons in the toolbar and a new “PHP Tools” sub menu in the PHP Explorer.

PHPsrc Toolbar

PHPsrc Toolbar

PHPsrc Menu

PHPsrc Menu

PHPsrc will now check your code every time you save. Errors and warnings will be added to the Problems tab and the Type will be the tool that found the problem. The lines that causes problems will also be underlined like a syntax error. The copy paste detector will send the results to the console.

Here’s what Eclipse looks like with PHPsrc:

PHPsrc Results

PHPsrc Results

If you develop PHP code in eclipse, you should definitely give PHPsrc a try. If you use a continuous integration server, being warned about potential issues before you commit to your source control repository can save you from becoming the one who broke the build.

Tags: , ,

12 Comments on PHP Tool Integration (PHPsrc)

  1. greg says:

    **DONT** install it on your working Eclipse!!!! it’s enabled by default and will flood your code with errors/warnings, and it CANNOT be disabled or uninstalled.
    Only try it on a test install of Eclipse or you will enter a nightmare with no exit :(

  2. Eric Hogue says:

    I agree that it can add a lot of errors on existing projects. But it can be uninstall, and you can remove the errors.

    Removing a plugin from Eclipse is not intuitive. Click on ‘Help/Install New Software…’. In the dialog, click on the ‘What is already installed’ link. From there you can uninstall any plugin.

    If you only want to remove the errors, you can click on the little arrow at the top right of the Problems tab. In the context menu, click on ‘Configure contents…’. In the dialog, you can create a new configuration and deselect any type you want to hide.

    But those errors are there for something. The better solution is probably to start fixing them.

  3. greg says:

    It’s a very nice idea, but it definitly shouldn’t be activated by default. I wasn’t able to remove or de-activate it in any way other than deleting the files inside the plugin folder :(
    Changing the configuration of the Problems tabs only mask the errors in the Problems tabs, the source panel is still cluttered and unreadable.
    Finally these are NOT errors, I don’t want to “fix” 30.000 of them in the project I work on everyday.
    My point is: for real work it will be a very valuable tool when it won’t be activated by default, it should only be activated in the project’s settings.
    I just wanted to warn other readers so they don’t end up with a broken Eclipse, like I am now.

  4. Eric Hogue says:

    Hey Greg,

    First thank you for your comments. You are right, activating it on an existing project can lead to way too many errors. And we need a way to activate it by project. But it can be an invaluable tool on new projects.

    I had the same issue when I installed it at work. But almost all the problems where due to PHP Code Sniffer. All I did is unselect all the standards from the settings. I can now select a standard on my new projects.

    Another thing I need to do is to write a new phpcs standard to match our coding standards. This will remove a lot of errors.

    You are right to warn others, it can make it very hard to work on projects that where not developed with a tool like this one.

    There should be an easy way to deactivate it, and Eclipse should make removing plugins more intuitive.

  5. daniel says:

    Hi Eric,
    I installed a php tool as per you said when i restart my eclipse there is no php folder in prefrences and also php tool is also not there. Could you help me to solve this problem

  6. Kent Hawley says:

    Eric,
    Thank you for the Blog on PTI.

    I’m familiar with eclipse for Java development.
    However, I am much less familiar with PHP development.
    I am now developing web sites using PHP via the PDT plugin to eclipse. This is going well.

    As my next step, I need PHPUnit capability and have the PTI eclipse plugin installed.
    I can see and execute PHP Tools -> PHPUnit -> .. . However, I see no results, e.g. in the Console.

    I suspect that I’m doing something wrong in PHP vs eclipse land.

    You said, “Make sure that every tools have an PHP executable and a PEAR library selected.”

    The first is set to “5.2/18″, the second is “”.

    I’m not sure what means?

    Meanwhile, I see a PEAR folder under
    C:\Zend\ZendServer\bin. Also, a go-pear.bat.
    Note: Zend\Apache2\htdocs is where I place my eclipse PHP projects.

    The PEAR folder has only one file, a PHAR file.

    Questions:
    1) Is the setting correct for my eclipse install?
    2) Do I need to execute the go-pear.bat? i.e. expand the PHAR?
    3) Should I be placing the PEAR folder on PHP build or include paths?

    Note: I ALSO clicked on link (next to PEAR) and added
    a new path to C:\Zend\ZendServer\bin. This didn’t seem to change anything.

    Any insights would be appreciated.

    KH

    • Eric Hogue says:

      Hi Kent,

      The PEAR library is probably set to ” because you don’t have any configured in Eclipse. You can set it in “Preferences/PHP Tools/Library/PEAR”.

      I have no idea what go-pear.bat is, sorry.

      PEAR path should be in your include_path.

      Other than that, it’s hard to say why it doesn’t work. Personally I don’t use Eclipse anymore. And I now use Guard to run my test automatically every time I save a file (http://erichogue.ca/2012/09/ph.....ith-guard/).

      Sorry I can’t help more.

  7. laurence fass says:

    Please can you confirm whether installing phpsrc on Eclipse Indigo breaks it (as of today in 2013) in the way mentioned in these comments? If so I think there is a need to maintain this page with updated info at the top if there is a possibility of causing a hindrance.

    Is Eclipse Indigo use the particular tools or is it utilising functionality from elsewhere?

    Can you also give some insight into exactly which tools from this suite have been integrated into Eclipse indigo? Thanks.

  8. laurence fass says:

    Hello again I have uninstalled phpcs and left everything else running… i cant get any output from phpdepend, md, etc… Not even any errors? Any suggestions… thanks.

    • Eric Hogue says:

      Hi Laurence,

      I am not using Eclipse anymore, so I can’t answer your questions.

      There are no recent activities on the project home page, so it’s possible that it won’t work with recent Eclipse versions.

  9. Wow, this post is nice, my younger sister is analyzing these things, thus I am going to
    inform her.

Leave a Reply