分析和解析PHP代碼的7大工具
PHP已成為時(shí)下最熱門(mén)的編程語(yǔ)言之一,然而卻有許多PHP程序員苦惱找不到合適的工具來(lái)幫助自己分析和解析PHP代碼。今天小編就為大家介紹幾個(gè)非常不錯(cuò)的工具,來(lái)幫助程序員們提高自己的工作效率,一起來(lái)看看吧!
PHP Parser
PHP-Parser是一個(gè)用PHP編寫(xiě)的PHP解析器(支持PHP 5.4以及更早的版本),這種特殊的解析器非常適合靜態(tài)代碼分析。該工具的目的就是簡(jiǎn)化靜態(tài)代碼分析和操作,它使程序員能夠以編程的方式來(lái)處理任何應(yīng)用程序的代碼。
PHPSandbox
PHPSandbox將運(yùn)行PHP作為獨(dú)立進(jìn)程的一種方式。 它為程序員提供一種外圍腳本的保護(hù),比如錯(cuò)誤、崩潰、運(yùn)行慢的腳本,或者不適合在代碼中運(yùn)行的腳本,都可作為獨(dú)立進(jìn)程來(lái)運(yùn)行。
PHP Mess Detector
PHPMD這個(gè)工具能夠探測(cè)PHP源代碼中一些潛在的問(wèn)題。例如:
- 可能存在的Bug
- 未達(dá)最佳標(biāo)準(zhǔn)的代碼
- 過(guò)于復(fù)雜的語(yǔ)法
- 從未使用過(guò)的參數(shù)、方法、屬性
PHPCPD
PHPCPD是一個(gè)在代碼中尋找類似模式的工具,使用它是為了在代碼庫(kù)中識(shí)別代碼在何處被復(fù)制或粘貼。這是常規(guī)構(gòu)建過(guò)程中一個(gè)非常有用的工具,它會(huì)幫助程序員分析代碼,以避免在代碼庫(kù)中重復(fù)調(diào)用函數(shù)。
PHPCheckstyle
PHPCheckstyle是一個(gè)幫助PHP程序員檢查代碼和報(bào)告錯(cuò)誤的工具,運(yùn)行于PHP 5.0以及更高的版本。通過(guò)SVN鉤子腳本的方式來(lái)調(diào)用PHPCheckstyle,可以強(qiáng)制代碼必須符合預(yù)先設(shè)定的編碼標(biāo)準(zhǔn)(比如PEAR編碼標(biāo)準(zhǔn)),有助于在多人合作項(xiàng)目中提高代碼整體質(zhì)量。
Ubench
Ubench是一個(gè)用于評(píng)測(cè)PHP代碼執(zhí)行時(shí)間和內(nèi)存使用效率的開(kāi)發(fā)庫(kù)。使用方法如下:
- require_once 'src/Ubench.<span id="1_nwp" style="width: auto; height: auto; float: none;"><a id="1_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?app_id=0&c=news&cf=1001&ch=0&di=128&fv=11&is_app=0&jk=f35ff87300cda873&k=php&k0=php&kdi0=0&luki=8&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=73a8cd0073f85ff3&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6257%2Ehtml&urlid=0" target="_blank" mpid="1" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">php</span></a></span>';
- $bench = new Ubench;
- $bench->start();
- // Execute some code
- $bench->end();
- // Get elapsed time and <span id="2_nwp" style="width: auto; height: auto; float: none;"><a id="2_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?app_id=0&c=news&cf=1001&ch=0&di=128&fv=11&is_app=0&jk=f35ff87300cda873&k=memory&k0=memory&kdi0=0&luki=4&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=73a8cd0073f85ff3&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6257%2Ehtml&urlid=0" target="_blank" mpid="2" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">memory</span></a></span>
- echo $bench->getTime(); // 156ms or 1.123s
- echo $bench->getTime(true); // elapsed microtime in float
- echo $bench->getTime(false, '%d%s'); // 156ms or 1s
- echo $bench->getMemoryPeak(); // 152B or 90.00Kb or 15.23Mb
- echo $bench->getMemoryPeak(true); // memory <span id="3_nwp" style="width: auto; height: auto; float: none;"><a id="3_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?app_id=0&c=news&cf=1001&ch=0&di=128&fv=11&is_app=0&jk=f35ff87300cda873&k=peak&k0=peak&kdi0=0&luki=3&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=73a8cd0073f85ff3&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6257%2Ehtml&urlid=0" target="_blank" mpid="3" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">peak</span></a></span> in bytes
- echo $bench->getMemoryPeak(false, '%.3f%s'); // 152B or 90.152Kb or 15.234Mb
- // Returns the memory usage at the end <span id="4_nwp" style="width: auto; height: auto; float: none;"><a id="4_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?app_id=0&c=news&cf=1001&ch=0&di=128&fv=11&is_app=0&jk=f35ff87300cda873&k=mark&k0=mark&kdi0=0&luki=2&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=73a8cd0073f85ff3&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6257%2Ehtml&urlid=0" target="_blank" mpid="4" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">mark</span></a></span>
- echo $bench->getMemoryUsage(); // 152B or 90.00Kb or 15.23Mb
PHP Analyzer
PHP Analyzer執(zhí)行和編譯器相同的流動(dòng)分析,確保代碼在每個(gè)潛在執(zhí)行路徑的每一行都執(zhí)行了測(cè)試。這種特殊的工具幫助開(kāi)發(fā)人員提高了自己的代碼質(zhì)量,從而確保了工作效率。