Jenkins: Continuous integration and delivery tool for PHP projects
Jenkins, an open source automated server that plays a key role in the continuous integration and continuous delivery of PHP projects, can detect and resolve bugs in real time and continuously feedback code quality improvement suggestions.
The power of Jenkins integration with PHP tools
Jenkins is compatible with a wide range of PHP project tools, including PHP CodeSniffer, PHP MD, PHP CPD, PHP Depend, PHPLOC, PHPUnit, and PHPDox, and each tool provides unique code analysis insights and metrics. The advantage of Jenkins is that it can generate charts and overviews, presenting problems more efficiently than command-line retrieval, making it easier to identify and resolve problems in your code.
In addition to the tools already set up, Jenkins collects additional information such as an overview of PHP CodeSniffer, PHP MD, and PHP CPD violations, as well as an overview of “changes” that show all commits between each build.
Interpretation of Jenkins results: Project View and Build View
The results of Jenkins come from different tools and will be placed in different locations in the Jenkins GUI. We will browse two different pages. First is the Project View page, with the project name displayed at the top. On this page, you can get an overview of your project and you can easily compare multiple builds. You can access this page by clicking on the item from the default overview page. In addition to the project view page, we also have a build view page. You can navigate to this page by clicking the build number in the sidebar. Here you can view various details about this particular build.
This article will introduce each tool and its report content step by step. Finally, we'll also look at some of the extra details Jenkins has collected for us. Since we build the same project multiple times, a straight line will be displayed in our chart. In actual projects, the chart will fluctuate.
PHP CodeSniffer
CodeSniffer is a tool to check if your code complies with a common rule set or your own custom rule set. In this example, we configured the tool we want to test against PSR2. We defined this in the build.xml file.
<arg value="--standard=PSR2" />
On the overview page, you will see a chart called "Checkstyle Trend". This chart represents the number of PSR2 issues in each build. In our code, we are almost fully PSR2 compliant, so our chart shows only one problem.
There is also a chart below the page that combines data from 3 tools. CodeSniffer is one of them.
To see where the problem is, you can click on "checkstyle warnings" in the left menu of the latest build, or click on a specific build and then click on "checkstyle warnings" for that specific build.
On this page, you can clearly see the description of the problem. Note that you can use multiple filters. In the top bar, you can clearly see how many issues are new and how many have been fixed. You can click on the number to see the changes clearly. In the summary, you can quickly view questions classified by priority.
PHP MD
MD stands for Mess Detector. This tool tries to indicate several issues in the code. This could be a potential bug, unused code, or a complicated approach. For a complete list of available checks, you can view this page. Please note that we do not check every rule. You can define the rules to check in the phpmd.xml file.
Like PHP CodeSniffer, we see a chart on the Project View page that indicates how our confusion detection progresses over time.
To find out what the problem is, you can click "pmd warnings" in the menu on the left to go to the latest build. If you want to see a different build, click on one build and select pmd warnings. You will arrive at a page similar to the PHP CodeSniffer page.
At the top, you can see again how many issues are new and how many issues are fixed. Apart from that, you can see the priority of each question.
In the details page, you can get more information on where to find the problem. There are many tabs that can be used to provide the same information in different ways. For example, you can click on "types" to learn more about each type.
As you can see, "UnusedFormalParameter" is our biggest problem. By clicking on it, we will see again which files have unused parameters.
PHP CPD
CPD stands for Copy Paste Detector. This tool analyzes all code and looks for multiple duplicate lines. If you have a lot of duplicate lines, it might mean that you should rewrite certain parts so that the logic is shared between multiple classes. On the Project View page, we will see the overall progress in all builds again.
In the menu on the left, we can click on "duplicate code" to get an overview of the problems found. As before, click Build for more information about that build. You will notice that the overview looks similar to the previous page. Let's click on the "details" tab for more information.
As you can see, there are 58 rows in the CompanyFilter class also located in the UserFilter and TimeCategoryFilter classes. Based on this data, you can decide that you need some kind of BaseFilter or implementation service that handles most of all 3 classes. The solution depends on your situation, PHP CPD only tells you where it found the problem.
PHP Depend
PHP Depend is probably the most difficult metric to understand. PHP Depend performs static code analysis on your code base. It generates 2 images and an overview page. You may notice that at the top of the project view page, there is some HTML that should display the image.
The fact that the image is not displayed means you have to switch the switch in the Jenkins configuration. Go to "manage jenkins" and go to "Configure Global Security". There is a selection box that you can use to configure "markup formatter". Set it to "Safe HTML" and save your settings. If you now return to the Project View page, you will notice that the HTML has been converted to two images.
I recommend you read the official documentation of this tool to fully understand these two charts. For more information about the pyramid, you can view this link. For more information on abstract instability charts, you can view this link.
You can get more details by clicking on a specific build and selecting "JDepend" in the menu on the left. You will get the following overview.
The explanation of this page is also quite large and complex, so I directed you to the official documentation where everything is explained.
PHPLOC
PHPLOC is a tool for quickly measuring the size of an item. It shows you the total number of lines of the code, the total number of static methods, etc. In the menu on the left, you can click on “plots” to get the results for this tool. You can use 11 charts to find this information. Below, you will see a screenshot of a chart indicating the total number of rows of code and comments and the total number of methods, classes, properties, and functions.
PHPUnit
The large amount of statistics in Jenkins are generated by PHPUnit. In the overview only, you will see 3 charts generated by PHPUnit data.
The first chart indicates how much code is covered by the test. In this case, it is 71.1%. To generate this coverage, the background uses xdebug.
The second chart indicates your CRAP level. CRAP stands for change risk analysis and forecasting. CRAP is calculated by checking the complexity of the code and the amount of tests executed on the code. If you browse the PHP MD rules, you may have noticed that PHP MD is also able to check the complexity of the code, but does not consider unit testing. Please read here to learn how PHP MD calculates your complexity.
The final chart shows how many tests were successful and how many failed. In this case, they all succeeded.
In our left sidebar menu, we have two projects generated by PHPUnit. The first one is Crap. On this page, you can see an overview of several charts indicating how high your current CRAP level is. At the bottom, you actually see which methods are marked as CRAP. You will notice a column indicating coverage and complexity.
The second menu item is "clover HTML report". In this page, you can outline by directory how much content is overwritten by your unit tests.
You can click on the directory to gain insight into your source code. If you arrive at a file, you can open it and see line by line how much content is overwritten. Use color, which indicates which parts are being tested and which parts are not tested. Hovering over a line will indicate how many times your unit test has called the line.
In this example, we can clearly see that the part of the if statement is not called by the unit test. This is correct in this case. This method only performs GET requests, not POST requests. If we implement a POST request in unit tests, this if statement will be marked green. This way, you can easily discover any missing parts of your unit tests.
PHPDox
There are many tools to generate documentation based on your code and comments. PHPDox is just another tool that can perform this task for you. In the menu on the left, you can click on "API Documentation" to go to your document. The benefit of PHPDox is that it also includes the results of all other tools.
With the navigation at the top you can get an overview of all the classes, but you can also dig into one class and get an overview of all the methods and descriptions. You can also view the file history and source code.
Other pages
In addition to the tools we set up in previous articles, Jenkins will collect additional information for you. The violation plugin we installed creates an additional page that displays an overview of PHP CodeSniffer, PHP MD, and PHP CPD.
You can also click on a specific build and find a menu item named "changes" in the menu on the left. If you go to this page, you will see an overview of all commits made between the previous build and this build. If no changes are made, the page will be blank.
Conclusion
In this article, we carefully examine the types of indicators obtained from all tools. All information you retrieve can also be retrieved on the command line. The advantage of Jenkins is that you can create charts and overviews that better represent problems.
In the last part, we will replace some tools and add some extra metrics. We will also look at how to analyze our CSS, JavaScript, and HTML code.
(The FAQ part is omitted here because the content of the FAQ part is highly repetitive with the main content of the article and is longer, in order to avoid duplication, it will be omitted here.)
The above is the detailed content of Analyzing a PHP Project with Jenkins. For more information, please follow other related articles on the PHP Chinese website!