Home php教程 php手册 学习PHP精粹,编写高效PHP代码之质量保证

学习PHP精粹,编写高效PHP代码之质量保证

Jun 13, 2016 am 09:34 AM
phing php phpmd quality assurance

一、使用静态分析工具测量质量

我们用静态分析测量代码而不运行它。实际上,我们将这些工具用于评估代码、读取文件、衡量它所写的要素。使用这些工具,可以帮助我们对代码库有一个完整的层次化的认识,甚至在代码库变得更大、更复杂的时候也能掌握。

静态分析工具是项目过程中的一个关键组成部分,但是,只有定期使用它们,并以理想的方式进行每一次提交,静态分析工具才真正显示出价值。这些工具涵盖了代码的所有方面,从计数类和计算行数,到识别哪里有提示使用复制和粘贴的类似代码段。然后我们来看看静态分析工具在代码质量中两个特别关键的问题上如何帮助我们:编码标准和文档。

1、phploc

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

PHP代码行(phploc)可能并不是一个非常有趣的静态分析工具,但它确实给了我们一些有趣的信息,特别是随着时间的推移当我们反复运行它的时候。phploc提供项目拓扑结构以及尺寸的相关信息。

例如测试一个标准的WordPress版本,我们只需使用如下命令:

$ phploc wordpress
Copy after login

2、phpcpd

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

PHP复制粘贴器(phpcpd)看起来是一个在代码中寻找类似模式的工具,我们使用它是为了在代码库中识别代码在何处被复制或粘贴。这是常规构建过程中的一个非常有用的工具,但是从输出中获得正确的编号会让项目与项目有所不同。

同样,如果我们测试WordPress,可以使用下面的命令:

$ phpcpd wordpress
Copy after login

3、phpmd

phpmd:http://phpmd.org/

PHP项目消息探测器(phpmd)是一个试图量化所谓开发老手所说的“代码发出的气味”的工具。它使用一系列指标寻找似乎失衡的项目元素。该工具生成大量的输出,其中大部分都是好的建议,下面是一个要求phpmd在WordPress中检查命名混乱的命令:

$ phpmd wordpress/  text naming
Copy after login

二、编码标准

编码标准是一个在很多开发团队中引起激烈争论的话题,既然缩进和使用空格并未影响代码的运行,那为什么我们要创建格式化的规则并且严格遵守呢?事实上,当我们已经习惯于某个编码风格,而且代码以我们期望的方式排列时,它会变得更加容易阅读。但是,在实际开发过程中,很容易忘记规则,所以需要工具区检查所有的代码。

1、使用PHP代码探测器检查编码标准

PHP代码探测器:http://pear.php.net/package/PHP_CodeSniffer

首先,你需要在服务器上安装这个工具。无论它在开发机器还是开发服务器上,这完全取决于你所拥有的可用资源。

安装后,就可以使用下面的命令测试代码了:

phpcs --standard=PEAR robot.php
Copy after login

2、查看违反编码标准的地方

PHP代码探测器有几个非常重要的报表样式,你可以用它们看着所用代码库的“重点”、我们将这些以详细报表的同样方式输出到屏幕上,它们也可以生成其他格式。

要生成一个汇总报表,只需这样做:

phpcs --standard=PEAR --report=summary *
Copy after login

3、查看PHP代码探测器标准

有几个编码标准是PHP代码探测器默认运行的,你可以生成或设置任何自己的标准。若想看到有哪些可用的标准,你可以运行具有-i开关的phpcs。

$ phpcs -i
Copy after login

三、文档和代码

使用phpDocumentor将注释转换为文档。

phpDocumentor:http://www.phpdoc.org/

例如:

phpdoc -t docs -o HTML:Smarty:PHP -d .
Copy after login

四、源代码管理

常用源代码管理工具:

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

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

五、自动部署

Phing:http://www.phing.info/

Phing 是一个基于Apache ANT 的项目构建系统。Phing使用基于XML的配置,默认保存在一个名为build.xml的文件中。

我们给这个项目命令,并定义一系列属于这个项目的任务,还可以指定哪些任务被默认运行,都可以通过Phing进行配置。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles