Sonar setup with local for PHP
Hi All,
Please setup sonar in local machines. first validate your code in local, whatever you are committing the code should be 100% compliance.
Please follow following instructions
Installation Steps :
To setup a sonar for PHP we need to have following on our machine.
-
JDK for Window/linux.
-
Winant for Windows or ant for linux.
-
PHP PEAR
-
Install XDebug
-
Sonar 3.6.3 http://dist.sonar.codehaus.org/sonar-3.6.3.zip
-
Sonar PHP Plugin
-
Code Sniffers for Drupal / Moodle and it's ruleset for sonar.
-
build.xml file for creating a ant task.
JDK for Window/Linux
Download and install the latest JDK from http://www.oracle.com/technetwork/java/javase/downloads/index.html on windows machine. Or can use smb://192.168.102.226/software/Java/Windows .
After installation Right click on “My Computer” >> Properties
Advanced Tab >> Click on Environment Variables Button
System Variable >> Click on “New” Button
Variable Name : JAVA_HOME
Variable Name : C:\Program Files\Java\jdk*.*.*_**
The linux user please use their appropriate methods. Ubuntu users can get latest jdk from Ubuntu Software Center.
Winant for Windows or ant for linux.
Download and intsall winant from http://winant.googlecode.com/files/winant-install-v7.exe
during the installaton it will ask for java home path which is the one shows when u install java.
After installation Put “sonar-ant-task-2.0.jar” to “C:\Program Files\WinAnt\lib” , you can downlaod it from
http://repository.codehaus.org/org/codehaus/sonar-plugins/sonar-ant-task/2.0/sonar-ant-task-2.0.jar
please not that the Environment Variable “ANT_HOME” is set Automatically.
Ubuntu user can easily do this with
apt-get install ant
PHP PEAR
Windows
XAMPP
-
put your command prompt to {XAMPP Installation}\php :>
WAMP
-
put your command prompt to {WAMP Installation}\bin\php\PHP5.3.*\ :>
-
Copy “go-pear.phar” to {WAMP Installation}\bin\php\php5.3.*\PEAR\
-
you can get it from http://pear.php.net/go-pear.phar
-
run php PEAR\go-pear.phar
Linux
-
use apt-get install php-pear
Run below command on Command promp/Terminal
pear version (make sure you are having php version 5.3.* or higher)
pear upgrade pear
pear clear-cache
pear channel-discover pear.phpunit.de -- NOT AVAILABLE
pear channel-discover pear.symfony-project.com
pear install --force --alldeps phpunit/PHPUnit -- WILL NOT WORK
Note : phpunit is not more available for installation from PEAR
pear channel-discover pear.pdepend.org
pear install --force --alldeps pdepend/PHP_Depend
-
Create a directory for PHP binaries; e.g.,
C:\bin
-
Append
;C:\bin
to yourPATH
environment variable
-
Download https://phar.phpunit.de/phpunit.phar and save the file as
C:\bin\phpunit.phar
-
Open a command line (e.g., press Windows+R » type
cmd
» ENTER)
-
Create a wrapping batch script (results in
C:\bin\phpunit.cmd
):
C:\Users\username>
cd C:\bin
C:\bin>
echo @php "%~dp0phpunit.phar" %* > phpunit.cmd
C:\bin>
exit
pear channel-discover pear.pdepend.org
pear install --force --alldeps pdepend/PHP_Depend
pear channel-discover pear.phpmd.org
pear install --force --alldeps phpmd/PHP_PMD
pear install --force --alldeps PHP_CodeSniffer
pear channel-discover components.ez.no
pear install --force --alldeps phpunit/phpcpd
download
https://phar.phpunit.de/phpcpd-2.0.2.phar
in c:\bin C:\Users\username>cd C:\bin
C:\bin>
echo @php "%~dp0phpcpd-2.0.2.phar" %* > phpcpd.cmdC:\bin>
exit
pear clear-cache
for any detail please check :
Install XDebug
Windows
-
Download appropriate dll file from http://xdebug.org/download.php
-
Go to : {WAMP Installation}\bin\php\php5.3.*\ext or {XAMPP Installation}\php\ext Place php_xdebug-2.2.1-5.*-vc9.dll
-
Go to : {WAMP Installation}\bin\php\php5.3.*\ or {XAMPP Installation}\php\
-
Edit php.ini and set include_path=".;{PHP_HOME}\PEAR"
-
zend_extension="{PHP_HOME}/ext/php_xdebug-2.2.1-5.*-vc9.dll "
PHP_HOME = {WAMP Installation}\bin\php\php5.3.*\ or {XAMPP Installation}\php\
Linux
apt-get install make
apt-get install php5-dev
pecl install xdebug
And then edit your $PHP_HOME/php.ini file to add the following line:
zend_extension="FULL_PATH_TO/xdebug.so"
Install Sonar 3.3.2 and sonar-php-plugin-1.1
-
Put “sonar-php-plugin-1.1.jar” to {web_root}\sonar-3.3.2\extensions\plugins”
-
Go to : “D:\wamp\www\sonar-3.3.2\conf”
-
Edit “sonar.properties” file.Setup the below propertiessonar.jdbc.username: rootsonar.jdbc.password:
-
Comment Line No : 46
-
UnComment Line No : 57
-
Run below command in MysqlCREATE DATABASE `sonar` ;
-
Add Following into system Variable 'Path'
-
Right click on “My Computer” >> PropertiesAdvanced Tab >> Click on Environment Variables ButtonSystem Variable >> “Path”
-
Add “C:\Program Files\Java\jdk*.*.*_*\bin\;{PHP_HOME}\” AT end of previously set path.
-
Restart the PC.
Integreting Drupal/Moodle Standards
-
Add the Drupal coding Standards in PHP CodeSniffer.
-
extract drupal.zip , moodle.zip in “{PHP_HOME}\PEAR\pear\PHP\CodeSniffer\Standards\”
or where CodeSniffer is Installed.
Check it with phpcs -i (Drupal,Moodle must be in the list)
-
Adding Extension to ruleset
{sonar_installation}\extensions\rules
create directory named php_codesniffer_rules
copy rules.xml in it.
-
Run the below command on Command prompt
Windows
{sonar_installation}\bin\windows-x86-32>StartSonar.bat
Linux
./{sonar_installation}/bin/linux-x86-64/sonar.sh console
-
In console "Started SelectChannelConnector@0.0.0.0:9000" message will be displayed .
custom standards files
-
Open browser and access http://localhost:9000
-
Login using admin/admin
-
Adding “.module” in File suffixesIn “Configuration” goto SYSTEM → General Settings.
-
PHP -> File Suffixes . add “module” in the list.
-
Go to Configuration by clicking configuration link in top menu
-
Click on Restore Profile link
-
Download any of followings.PHP_Core_Standards.xmlPHP_Drupal_Standards_php.xmlPHP_Moodle_Standards_php.xmlPHP_Magento_Projects_php.xmlPHP_Zend_Standards_php.xml
-
Click on Restore Profile Button, this will install PHP Rules in Sonar
-
Now you can see "PHP Standards" in PHP Profiles Table
-
Click on set as default against appropriate.
-
place attached build.xml to your projects source and make the necessary property chnages like sonar.projectname etc.
-
Verify the ant location withclasspath path="/usr/local/sonar-ant-task-2.0.jar" orclasspath path="c:\Program Files\WinAnt\lib\sonar-ant-task-2.0.jar"
-
change soruce location in <property name="sonar.sources" _cke_saved_name="sonar.sources" _cke_saved_name="sonar.sources" _cke_saved_name="sonar.sources" _cke_saved_name="sonar.sources" _cke_saved_name="sonar.sources" _cke_saved_name="sonar.sources" _cke_saved_name="sonar.sources" _cke_saved_name="sonar.sources" _cke_saved_name="sonar.sources" _cke_saved_name="sonar.sources" value="source_code_location"/>
-
give sonar key value key="php.project_name:project_name"
-
Open build.xml and change following two properties with your mysql database username and password
-
<property name="sonar.jdbc.username" value="root" />
-
<property name="sonar.jdbc.password" value="" />
-
<property name="sonar.projectName" value="Your project Name" />
-
-
Then generate sonar report using command ant sonar from your source directory.
-
Open http://localhost:9000 , you will see a project "My-Project"
If you find any difficulties to setup please contact Bhavin Vora OR configuration-experts team.
Comments