Learning the essence of PHP and writing efficient PHP code-quality assurance-PHP Tutorial

Source: Internet
Author: User
Tags coding standards wordpress version
Learn the essence of PHP and write efficient PHP code to ensure the quality. I. using static analysis tools to measure quality we use static analysis and measurement code without running it. In fact, we use these tools to evaluate code, read files, and measure what it writes. 1. use static analysis tools to measure quality.

We use static analysis to measure the code without running it. In fact, we use these tools to evaluate code, read files, and measure the elements it writes. Using these tools can help us have a complete hierarchical understanding of the code library, even when the code library becomes larger and more complex.

Static analysis tools are a key component of the project process. However, only when they are used regularly and submitted in an ideal way can the static analysis tool truly display the bid value. These tools cover all aspects of the code, from the counting class and the number of calculated lines, to identifying where similar code segments are prompted to be copied and pasted. Then let's take a look at how static analysis tools help us with two key issues in code quality: Coding Standards and documentation.

1. phploc

Phploc: https://github.com/sebastianbergmann/phploc

PHP code line (phploc) may not be a very interesting static analysis tool, but it does give us some interesting information, especially over time when we run it repeatedly. Phploc provides information about the project topology and dimensions.

For example, to test a standard WordPress version, run the following command:

$ phploc wordpress

2. phpcpd

Phpcpd: https://github.com/sebastianbergmann/phpcpd

PHP phpcpd looks like a tool for finding similar patterns in code. We use it to identify where the code is copied or pasted in the code library. This is a very useful tool in the general build process, but obtaining the correct number from the output will make the project different from the project.

Similarly, if we test WordPress, we can use the following command:

$ phpcpd wordpress

3. phpmd

Phpmd: http://phpmd.org/

The PHP project message detector (phpmd) is a tool that attempts to quantify the "smell of code" called by developers. It uses a series of metrics to find seemingly unbalanced project elements. This tool generates a large amount of output, most of which are good suggestions. The following is a command that requires PHP to check for naming disorder in WordPress:

$ phpmd wordpress/  text naming

II. coding standards

Coding Standards are a topic that has aroused heated debate among many development teams. since indentation and space usage do not affect code execution, why should we create formatting rules and strictly abide by them? In fact, it is easier to read when we are used to a certain encoding style and the code is arranged as we expected. However, in the actual development process, it is easy to forget the rules, so you need to check all the code in the tool area.

1. use the PHP code detector to check encoding standards

PHP code detector: http://pear.php.net/package/PHP_CodeSniffer

First, you need to install this tool on the server. Whether it is on the development machine or the development server, it depends on the available resources you have.

After installation, you can use the following command to test the code:

phpcs --standard=PEAR robot.php

2. View information that violates the encoding standard

The PHP code detector has several very important report Styles. you can use them to look at the "key" of the code library used, and output these to the screen in the same way as detailed reports, they can also generate other formats.

To generate a summary report, you only need to do this:

phpcs --standard=PEAR --report=summary *

3. View PHP code detector standards

Several coding standards are run by default by the PHP code detector. you can generate or set any of your own standards. If you want to see the available standards, you can run the phpcs with the-I switch.

$ phpcs -i

III. documents and code

Use phpDocumentor to convert comments into documents.

PhpDocumentor: http://www.phpdoc.org/

For example:

phpdoc -t docs -o HTML:Smarty:PHP -d .

IV. source code management

Common source code management tools:

Subversion: http://subversion.apache.org/

Git: the http://git-scm.com/

5. automatic deployment

PHP: http://www.phing.info/

Phing is a project construction system based on Apache ANT. Phing uses XML-based configuration and is saved in a file named build. xml by default.

Run the command for this project and define a series of tasks belonging to this project. you can also specify which tasks are run by default. you can configure them through Phing.

We use static analysis to measure the code without running it. In fact, we use these tools to evaluate code, read files, and measure what it writes...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.