Search code examples
phpsonarqubesonar-runner

How to get SonarQube running to analyze a PHP project?


I want to assess SonarQube as a source code inspection tool.

The project is hosted in a git repository, and I want to SonarQube to check my PHP project on each commit.


Solution

  • I got a basic instance of SonarQube via docker. (The current version of sonar cube is 6.7 -- yet I do not know if the steps remain the same. This answer takes into account 5.1.)

    Run the container:

    sudo docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 sonarqube:5.1
    

    This provides with a standard install of SonarQube accessable at

    http://localhost:9000/
    

    I can login via username and passoword admin, and install the PHP component via:

    Settings > System > Update Center

    (or: http://localhost:9000/updatecenter )

    and search for PHP and install.

    There I can add PHP and after restart of the SonarQube server, (I did it via docker stop container_id, container start container_id), the extension is loaded.

    The server will not run your tests. It will only display the results.

    You will need a machine dedicated to work as your sonar-runner, for a quick start you may use your local dev machine and your local checkout from bitbucket. Install the sonar-runner on that machine.

    Download the sonar runner via:

     $ wget http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip
    

    and extracted it to:

     ~/programs/sonar-runner-2.4
    

    In this directory, there you find a file conf/sonar-runner.properties that should contain:

    #Configure here general information about the environment, such as SonarQube DB details for example
    #No information about specific project should appear here
    
    #----- Default SonarQube server
    sonar.host.url=http://localhost:9000
    
    #----- PostgreSQL
    #sonar.jdbc.url=jdbc:postgresql://localhost/sonar
    
    #----- MySQL
    #sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
    
    #----- Oracle
    #sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE
    
    #----- Microsoft SQLServer
    #sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor
    
    #----- Global database settings
    sonar.jdbc.username=sonar
    sonar.jdbc.password=sonar
    
    #----- Default source code encoding
    sonar.sourceEncoding=UTF-8
    
    #----- Security (when 'sonar.forceAuthentication' is set to 'true')
    sonar.login=admin
    sonar.password=admin
    

    Go into your project's root directory and create a file called sonar-project.properties:

    # must be unique in a given SonarQube instance
    sonar.projectKey=yourProjectKey
    # this is the name displayed in the SonarQube UI
    sonar.projectName=yourProject
    sonar.projectVersion=1.0
    
    # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
    # Since SonarQube 4.2, this property is optional if sonar.modules is set. 
    # If not set, SonarQube starts looking for source code from the directory containing 
    # the sonar-project.properties file.
    sonar.sources=./classes/,./tests/
    
    # Encoding of the source code. Default is default system encoding
    sonar.sourceEncoding=UTF-8
    
    # Encoding of the source code. Default is default system encoding
    sonar.sourceEncoding=UTF-8
    

    I then ran:

     your/projects/dir$: ~/programs/sonar-runner-2.4/bin/sonar-runner
    

    You will then see an new entry at your SonarCube dashboard.