Home php教程 php手册 PHP开发必备 PHP持续集成工具介绍(上)

PHP开发必备 PHP持续集成工具介绍(上)

Jun 13, 2016 am 11:13 AM
php superior introduce exist Will tool develop Essential of at present software engineering integrated

在本文中,将介绍在目前软件工程中经常用到的持续集成概念,并且会介绍在PHP开发中,如何能用好PHP目前开源的一些持续集成管理工具,去管理好项目。

持续集成的概念

持续集成的概念是在现代软件工程中提出的,最早见于敏捷开发方法论中,大师Martin Fowler对持续集成是这样定义的:持续集成是一种软件开发实践,即团队开发成员经常集成它们的工作,通常每个成员每天至少集成一次,也就意味着每天可能会发生多次集成。每次集成都通过自动化的构建(包括编译,发布,自动化测试)来验证,从而尽快地发现集成错误。许多团队发现这个过程可以大大减少集成的问题,让团队能够更快的开发内聚的软件。

下面介绍PHP中的持续集成工具:

PHPUNIT

首先,PHPUNIT是PHP中的单元测试利器,项目地址在:http://www.phpunit.it。它

能自动运行你编写的单元测试代码,并给出是否通过的结果。安装步骤如下,可以使用PHP中的PEAR安装:

<ol class="dp-c">
<li class="alt"><span><span>sudo apt-get install php5-curl php-pear php5-dev  </span></span></li>
<li><span>sudo pear upgrade pear  </span></li>
<li class="alt"><span>sudo pear channel-discover pear.phpunit.de  </span></li>
<li><span>sudo pear channel-discover components.ez.no  </span></li>
<li class="alt"><span>sudo pear channel-discover pear.symfony-project.com  </span></li>
<li><span>sudo pear install phpunit/PHPUnit </span></li>
</ol>
Copy after login

之后,就可以在命令行下,以如下格式执行phpunit:

Phpunit 单元测试的php文件名.php

此外,还可以执行如下命令,生成单元测试的覆盖报告:

phpunit --coverage-html ../CodeCoverage

这将在指定的目录中生成单元测试的覆盖报告,比如这个例子中,将在目录CodeCoverage中生成单元测试覆盖报告,如下图:

498)this.width=498;' onmousewheel = 'javascript:return big(this)' style="cursor: hand" border="1" alt="PHP开发必备 PHP持续集成工具介绍(上)" href="/picshow/index595837.shtml" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095310781.jpg" />
▲点击看大图

如果点上图的每一个PHP文件,还可以具体看到每个文件的单元测试覆盖情况,如下图:

498)this.width=498;' onmousewheel = 'javascript:return big(this)' style="cursor: hand" border="1" alt="PHP开发必备 PHP持续集成工具介绍(上)" href="/picshow/index595838.shtml" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095310397.jpg" />
▲点击看大图

从上图中,可以看到每个PHP文件中,调用的方法的单元测试覆盖情况,还可以看到具体哪些代码行是已经单元测试覆盖过,哪些没有覆盖过(绿色表示已经覆盖,橙色表示还没覆盖),如下图:

498)this.width=498;' onmousewheel = 'javascript:return big(this)' style="cursor: hand" border="1" alt="PHP开发必备 PHP持续集成工具介绍(上)" href="/picshow/index595839.shtml" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095310154.jpg" />
▲点击看大图

PHP CodeSniffer

PHP CodeSniffer是一个PHP的代码风格检测器,它根据预先设定好的PHP编码风格和规则,去检查应用中的代码风格情况,内置了ZEND,PEAR的编码风格规则,当然开发者也可以进行自定义。项目的地址在:http://pear.php.net/package/PHP_CodeSniffer/redirected,安装方法如下:

<ol class="dp-c">
<li class="alt"><span><span>sudo pear install PHP_CodeSniffer  </span></span></li>
<li><span>phpcs --standard=Zend c:phpcode </span></li>
</ol>
Copy after login

其中phpcs –standard后指定了使用Zend的建议代码风格标准进行检查,最后一个参数是要检查的PHP文件所在的目录。运行后,检查的一个效果图如下图所示:

498)this.width=498;' onmousewheel = 'javascript:return big(this)' style="cursor: hand" border="1" alt="PHP CodeSniffer" href="/picshow/index595841.shtml" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095310674.jpg" />
▲点击查看大图

PHP Depend

PHP Depend(http://pdepend.org/)是一个PHP中静态代码分析的工具。它可以用来检查你的PHP项目中的代码规模和复杂程度。安装方法如下:

<ol class="dp-c">
<li class="alt"><span><span>sudo pear channel-discover pear.pdepend.org  </span></span></li>
<li><span>sudo pear install pdepend/PHP_Depend-beta </span></li>
</ol>
Copy after login

使用的一个例子如下:

pdepend --jdepend-xml=../jdepend.xml --jdepend-chart=../dependencies.svg --overview-pyramid=../overview-pyramid.svg

上面会生成一个XML文件,两个SVG的图形文件。其中XML文件说明了项目中各PHP文件的互相依赖调用(引用)情况及每个方法、函数,类的代码行,一个例子如下:

<ol class="dp-c">
<li class="alt"><span><span><?xml  version=&rdquo;1.0&rdquo; encoding=&rdquo;UTF-8&rdquo;?>  </span></span></li>
<li><span><metrics>  </metrics></span></li>
<li class="alt"><span><package><span class="keyword">global</span><span>” noc=”17” nof=”0” noi=”0” nom=”237”>  </span></package></span></li>
<li><span><span class="keyword">class</span><span> name=”data_container”nom=”55”>  </span></span></li>
<li class="alt"><span><file><span class="keyword">case</span><span>-study/libs/data/container.php”/>  </span></file></span></li>
<li>
<span></span><span class="keyword">class</span><span>>  </span>
</li>
<li class="alt"><span><span class="keyword">class</span><span> name=”data_download” nom=”10”>  </span></span></li>
<li><span><file><span class="keyword">case</span><span>-study/libs/data/download.php”/>  </span></file></span></li>
<li class="alt">
<span></span><span class="keyword">class</span><span>> </span>
</li>
</ol>
Copy after login

其中的noc含义是类的代码的行书,nof为函数的代码行数,noi为接口代码行数,nom为方法method代码行数。

接下来我们来看下产生的一张SVG图,该图如下所示:

498)this.width=498;' onmousewheel = 'javascript:return big(this)' style="cursor: hand" border="1" alt="PHP CodeSniffer" href="/picshow/index595842.shtml" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095310442.jpg" />

这张图的反映了项目中包的依赖性度量情况。其基本理论来源于Robert C. Martin的论文(http://www.objectmentor.com/resources/articles/oodmetrc.pdf)中所介绍的关于软件中模块依赖性和稳定性的度量。其中,提到了如下概念:

Afferent Couplings (Ca)

依赖于被分析package的其他package的数量,用于衡量pacakge的职责。

简单来说,即有多少其他的包或模块调用了被分析的包或模块。

Efferent Couplings (Ce)

被分析package的类所依赖的其他package的数量,用于衡量package的独立性。

即它调用了多少其他包。

Abstractness (A)

被分析package中的抽象类和接口与所在package所有类数量的比例,取值范围为0-1。如果该值为0,证明包中没任何抽象类,有的只是具体实现逻辑方法的类,如果该值为1,则包中只有抽象类或接口了

Instability (I)

I=Ce/(Ce+Ca),用于衡量package的不稳定性,取值范围为0到1。I=0表示最稳定,I=1表示最不稳定。

Distance (D)

用于衡量package在稳定性和抽象性之间的平衡。可以看到,最理想的情况是A+I越趋近1的,这样能获得抽象性和稳定性的最佳平衡。

此外,介绍php depend中生成的另外一张图的含义,图片如下:

498)this.width=498;' onmousewheel = 'javascript:return big(this)' style="cursor: hand" border="1" alt="PHP CodeSniffer" href="/picshow/index595844.shtml" src="http://www.bkjia.com/uploadfile/2013/0904/20130904095311587.jpg" />

在这张图中,最上面的ANDC表示继承其他类的平均数目,即项目中有多少类是继承其他的类;AHH是继承其他类的层次数。CALLS是方法被调用的总的次数,FANOUT是类所引用的类型数目。而CYCLO是圈复杂度(关于圈复杂度的解析,请参考(http://en.wikipedia.org/wiki/Cyclomatic_complexity)。LOC是代码行数,NOM是项目中的方法的数目,NOC为项目中类的数目,NOP为项目中包的数目。

根据上图中标明的除法规则,可以计算出相关的数据。更多的请参考php depend手册(http://pdepend.org/documentation/handbook/reports/overview-pyramid.html)的详细解析。


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 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)

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

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.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

How to optimize website performance: Experiences and lessons learned from using the Minify library How to optimize website performance: Experiences and lessons learned from using the Minify library Apr 17, 2025 pm 11:18 PM

In the process of developing a website, improving page loading has always been one of my top priorities. Once, I tried using the Miniify library to compress and merge CSS and JavaScript files in order to improve the performance of the website. However, I encountered many problems and challenges during use, which eventually made me realize that Miniify may no longer be the best choice. Below I will share my experience and how to install and use Minify through Composer.

Accelerate PHP code inspection: Experience and practice using overtrue/phplint library Accelerate PHP code inspection: Experience and practice using overtrue/phplint library Apr 17, 2025 pm 11:06 PM

During the development process, we often need to perform syntax checks on PHP code to ensure the correctness and maintainability of the code. However, when the project is large, the single-threaded syntax checking process can become very slow. Recently, I encountered this problem in my project. After trying multiple methods, I finally found the library overtrue/phplint, which greatly improves the speed of code inspection through parallel processing.

Solve CSS prefix problem using Composer: Practice of padaliyajay/php-autoprefixer library Solve CSS prefix problem using Composer: Practice of padaliyajay/php-autoprefixer library Apr 17, 2025 pm 11:27 PM

I'm having a tricky problem when developing a front-end project: I need to manually add a browser prefix to the CSS properties to ensure compatibility. This is not only time consuming, but also error-prone. After some exploration, I discovered the padaliyajay/php-autoprefixer library, which easily solved my troubles with Composer.

See all articles