Thursday, April 28, 2011

Sonar for PHP code coverage

Sonar is quality management platform, checking code quality based on certain rules and custom rules.

Sonar runs on java so requires JDK v1.5 or later.

After that download latest version of sonar from http://www.sonarsource.org/downloads/ 

Starting sonar server

[PATH_TO_SONAR]/bin/[OS]/sonar.sh console

e.g.

/usr/local/sonar/linux-x86-32/sonar.sh console/start

Now the sonar has been started at http://localhost:9000 by default, config details of sonar can be updated
at sonar.properties inside conf.

For running php code coverage

Sonar is basically used for Java projects which can be extended for php using maven plugin.
So download latest maven from http://maven.apache.org/download.html

 

Now for adding project to sonar pom.xml file has to be created in the root folder of the project.

<?xml version="1.0"?> <project > <modelVersion>1.0.0</modelVersion> <groupId>Group_name</groupId> <artifactId>name</artifactId> <name>Project</name> <version>1.0</version> <packaging>pom</packaging> <build> <directory>build</directory> <sourceDirectory>application</sourceDirectory> <testSourceDirectory>tests</testSourceDirectory> </build> <properties> <sonar.language>php</sonar.language> </properties> </project>

 

For running php code coverage it requires phpunit to be configured.
Then coverage report is created using phpunit.

e.g.

phpunit --coverage-clover build/logs/phpunit.coverage.xml --log-junit build/logs/phpunit.xml

According to the pom.xml file the reports are created inside build/logs folder.

Some of the additional plugins are required for maven to run for PHP.

 

  1. Install PHP runtime with the corresponding xdebug extensions. Version 5.2.6 or newer is required.
  2. Install PEAR. Version 1.8.0 or newer is required
  3. Install PHPUnit version 3.5.5 (strictly)
  4. Install phpcpd version 1.3.0 (strictly)
  5. Install PHPDepend PHPMD version 0.2.5
  6. Install PHP_CodeSniffer version 1.3.0

After running PHPUnit, it ready for sonar to run with maven from project root folder.

[PATH_TO_MAVEN]/bin/mvn sonar:sonar

with -e option it lists the trace errors. After Build is success the web is updated with the new build reports. 

 

Wednesday, April 27, 2011

Thursday, April 21, 2011

Twitter updates via RSS using PHP

$rssUrl = "http://twitter.com/statuses/user_timeline/USERNAME.rss"; $rss = @file_get_contents($rssUrl); if($rss){ $xml = @simplexml_load_string($rss); if($xml !== false){ foreach($xml->channel->item as $tweet){ echo $tweet->pubDate."\n"; echo substr($tweet->description,10)."\n\n"; } }else{         echo "Error: RSS not valid"; } }else{ echo "Error: Cant load RSS"; }

Just replace the username with the required one.

Tips to make php better

Write Clean Logic Statements
unclean code

if($userLoggedIn) { // Hundreds of lines of code }else{ exit(); }

clean code

if(!$userLoggedIn) { exit();  }  // Hundreds of lines of code


Use Less PHP And More HTML With Alternative Syntax
 

Use "sprintf" and Friends

uncleanly spliced code

$sql = "SELECT col1, col2, col3 FROM people WHERE first_name = '" . mysql_real_escape_string($first_name) . "' AND last_name = '" . mysql_real_escape_string($last_name) . "'AND foo = '" . ($bar = "good" ? "good" : "bad") . "' ORDER BY col1" ;

cleanly assembled string with sprintf

$sql = 'SELECT col1, col2, col3 ' . 'FROM people ' . 'WHERE first_name = "%s" ' . 'AND last_name = "%s" ' . 'AND foo = "%s" ' . 'ORDER BY col1 ';  $sql = sprintf($sql, mysql_real_escape_string($first_name), mysql_real_escape_string($last_name), ($bar = "good" ? "good" : "bad"));

Wednesday, April 6, 2011

GIT Committing to a different remote branch

From master

git branch -a

gives the list of all remote branches


Create remote branch in local and get all the updates from the corresponding branch

git branch -t branch_name(same as in server) origin/branch_name

git checkout branch branch_name

make changes

commit ur code

then 

git pull origin branch_name

git push origin branch_name