Skip to content

Eclipse and PHP Settings

Ere Maijala edited this page Sep 7, 2015 · 6 revisions

Basic Configuration for VuFind 2

To configure Eclipse and create a new workspace and project:

  • Install Eclipse for PHP Developers from http://eclipse.org/
  • Create a new workspace somewhere (such as Documents/vufind_workspace)
  • Create a new project (File -> New -> Project)
  • Select PHP -> PHP Project
  • Give the project a name, such as VuFind
  • Select "Create project at existing location" and select the local source directory
  • Check "Enable JavaScript support for this project"
  • When the project is done, edit project properties (Project -> Properties)
    • Resource:
      • Text file encoding: UTF-8
      • New text file line delimiter: Unix
    • Builders:
      • Uncheck JavaScript Validator
  • Edit also Eclipse preferences
    • In General -> Editors -> Text Editors, check "Insert spaces for tabs"
    • In General -> Keys, change Find Next to Cmd/Ctrl+G and Find Previous to Cmd/Ctrl+Shift+G
    • General -> Workspace:
      • Check "Refresh using native hooks or polling" to avoid constant messages about the files being out of sync
      • Text file encoding: UTF-8
      • New text file line delimiter: Unix
    • In PHP -> Code Style -> Formatter, copy the default profile to a new one and set
      • Tab policy: Spaces
      • Indentation size: 4
      • Default indentation for wrapped lines: 1
      • Default indentation for array initializers: 1
      • Uncheck Indent - Statements within 'switch' body
    • In PHP -> Editor -> Save Actions:
      • Check "Remove trailing whitespace"
      • Select "All lines"
    • In JavaScript -> Code Style -> Formatter, copy the default profile to a new one and set
      • Tab policy: Spaces only
      • Indentation size: 4
      • Tab size: 4

Note: If the autocompletion (content assist) doesn't work, shut down Eclipse, remove all .db files from your workspace/.metadata/.plugins/org.eclipse.dltk.core.index.sql.h2, start Eclipse and rebuild the project.

CodeSniffer

CodeSniffer can be used to report any problems in the source, including code style checking.

  • Install PHP_CodeSniffer:

          pear install PHP_CodeSniffer
    
  • Add CodeSniffer to Eclipse (full instructions):

    • Go to Help -> Eclipse Marketplace
    • Search for PTI ("PTI - PHP Tool Integration")
    • Install PTI with all options selected
    • Restart Eclipse
    • Go to Preferences -> PHP Tools -> PHP CodeSniffer
      • Add PHP Executable and point it to wherever your PHP installation is
      • Add a PEAR library from your PHP installation (e.g. /Applications/mampstack-5.4.23-0/php/lib/php) and select it as the active PEAR library. You could also use the built-in library, but it may not be up to date and could lead to wrong results (e.g. doesn't understand traits).
      • Set Standard Tab Width to 0
      • Set Ignore Directories and Files to .git,/config/,/local/cache/,/vendor/,*.phtml
      • Close settings and let Eclipse build the project
      • If JavaScript Validator outputs errors, go to Project -> Properties -> Builders and uncheck JavaScript validation
      • Any problems with build can be seen on the Problems tab
      • Check Build Automatically in Project menu so that files are checked automatically after being saved

Debugging Server Side PHP Scripts

Eclipse can function as a debugger for PHP's xdebug extension enabling easy debugging of PHP code. Here are instructions for basic setup.

  • Install xdebug extension to PHP if it's not already available (e.g. pecl install xdebug).

  • Make sure xdebug extension is enabled and configured. Here are example settings in php.ini (from MAMP Stack on OS X):

      zend_extension="/Applications/mampstack-5.4.23-0/php/lib/php/extensions/xdebug.so"
      xdebug.remote_enable=true
      xdebug.remote_host=localhost
      xdebug.remote_port=9000
      xdebug.remote_handler=dbgp
      ;xdebug.remote_autostart=1
      ;xdebug.profiler_enable=1
      ;xdebug.profiler_output_dir=/tmp
    
  • It is recommended to leave xdebug.remote_autostart commented out as above (otherwise the debugger is always active and slows down page). In this case you need a browser extension to easily enable/disable debugging. Popular ones are "The easiest Xdebug" for Firefox and "Xdebug Helper" for Chrome. See http://xdebug.org/docs/remote for more information on how to activate the debugger.

  • Configure Eclipse for xdebug (Preferences -> PHP -> Debug, see e.g. http://wiki.eclipse.org/Debugging_using_XDebug#Installation)

  • Uncheck "Break at First Line". Otherwise you'll end up in the debugger any time you load a PHP page from the local server in the browser

  • Remember to restart Apache after changing PHP settings

  • You can verify that the Xdebug extension works by entering 'php -v' on command line (it should say something like 'with Xdebug v2.2.3...')

Now, to actually debug something, set a breakpoint where you wish to start. This can be easily done by doubleclicking on the vertical blue stripe to the left of the editor window. Then just load the page in the browser, and the script execution should stop at the breakpoint. Switch to Debug perspective in Eclipse (Window -> Open Perspective) to see Variables, breakpoints etc.

A couple of handy keyboard commands for debugging:

  • F5 = Step Into - Follow execution into the function being called on the current line
  • F6 = Step Over - Execute current line without stepping into any functions being called on the line
  • F7 = Step Return - Execute the current function until it returns
  • F8 = Continue execution