Bad code leads to bugs, hard understanding, hard maintenance. We should really learn to write good code, and with so much open source tools for code analysis we could improve it one step higher. Sonar is a tool for code quality analysis. It helps improving software quality using static analysis tools. It targets Java code, but there are plugins for Flex, C, PHP, .Net and other languages. The Flex plug-in specifically uses FlexPMD, FlexMetrics, FlexCPD and FlexMojos. In this post we will show you the features that FlexPMD gives us within Sonar. We think that this tool is not really known by Flex developers, so we thought as necessary to show it.
Have you used eclipse for Java developing? It is great when warnings like unused variables and not called methods appear. Well FlexPMD does this for us along with a lot more things. It even can be used inside of Eclipse to show you live reports. There are defined rules that FlexPMD follows to catch issues in the code and bad practices. Great thing is that you can also define your own rules using this flex app
Step 1. So first go and download Sonar. After that download the flex plugin for it. It is a jar file. Now extract the contents of the sonar archive in a desired location, and place the sonar-flex-plugin-x.x.x.jar in extensions\plugins directory of sonar. That’s pretty much what you need to start sonar. By default it uses an embeded Apache Derby database, that you can change to whatever you want in sonar.properties file in conf folder. So now start Sonar. I start it from bin\windows-x86-32\StartSonar.bat as I am a windows user, if you are on other operating system start it from bin\YOUR_SYSTEM\sonar.sh. Have in mind that it takes a while to start so have patience. To assure that it is started open a browser and navigate to http://localhost:9000/
Step 2. Create a flex project. We will create a small test project with one class written really ugly and bad and doesn’t actually do a thing it is here to show you what errors Sonar will find. MyTestComponent.as
public class MyTestComponent extends UIComponent
{
private var r:Number;
private var variableNotUsed:Object;
override protected function updateDisplayList(
unscaledWidth:Number,
unscaledHeight:Number):void
{
r = Math.random()* 85;
var child1:UIComponent = new UIComponent();
addChild(child1);
}
public function
myFunctionWithVeryLongNameAndLotsOfParametersThatDoesNothing(
p1:String, p2:Number, p3:Object,
p4:Boolean, p5:int, p6:*,
p7:Array):void
{
return;
}
}
Step 3. Run analysis built with Maven. For this step you should have Maven 2 on your computer (or Maven 3). Assuming that you have Maven on your computer, as the documentation on the sonar site suggests, add
<profile>
<id>flex</id>
<pluginrepositories>
<pluginrepository>
<id>flexpmd.opensource.adobe</id>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<name>FlexPMD repository on opensource.adobe.com</name>
<url>http://opensource.adobe.com/svn/
opensource/flexpmd/maven-repository/release/</url>
</pluginrepository>
</pluginrepositories>
</profile>
this to the settings.xml file of maven located either in $M2_HOME/conf/settings.xml or${user.home}/.m2/settings.xml
And now in the flex project folder add a new file named pom.xml
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemalocation=
"http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelversion>4.0.0</modelversion>
<name>Test Sonar Project</name>
<groupid>com.tgeorgiev</groupid>
<artifactid>sonar.test</artifactid>
<version>0.1</version>
<packaging>pom</packaging>
<build>
<sourcedirectory>src</sourcedirectory>
</build>
<properties>
<sonar.language>flex</sonar.language>
<sonar.dynamicanalysis>false</sonar.dynamicanalysis>
</properties>
</project>
Now open a console, navigate to the location of your flex project and type “mvn sonar:sonar -Pflex”.
Step 4. After some time of downloading the needed jars for the build and running the analysis task we can check what happened when we open http://localhost:9000/ There should be displayed our project with all the violations that were detected. For our test project we have
Blocker 12 Critical 0 Major 9 Minor 0 Info 1
And when we select to see the Blocker issues for example we see:

Of course, we agree with this result, because it is obvious how bad written this code was.
Do you use Sonar for static analysis of the code, or maybe some other tool? If so, feel free to share
[Original article from: http://tgeorgiev.blogspot.com/2011/03/improve-your-code-using-sonar-with-flex.html]