
搜索PHP源代碼以獲取功能和方法調用,可變分配,類以及更多直接從PHP。
composer require permafrost-dev/php-code-search要搜索文件,請使用search方法。它的唯一參數可以是包含有效文件名的字符串,也可以PermafrostPhpCodeSearchSupportFile的實例。
要搜索字符串,請使用searchCode方法。
搜索方法返回一個具有results屬性的PermafrostPhpCodeSearchResultsFileSearchResults的實例。
每個result都是PermafrostPhpCodeSearchResultsSearchResult的實例,具有以下屬性:
node - 找到的特定項目node->name(): stringlocation - 文件中的文件中的位置location->startLine(): intlocation->endLine(): intsnippet - 文件中的代碼線的摘要snippet->toString(): stringfile() (方法) - 提供對已搜索的文件的訪問權限要在字符串或文件中搜索代碼,請使用Searcher類:
use Permafrost PhpCodeSearch Searcher ;
$ searcher = new Searcher ();要搜索文件,請使用search方法和searchCode方法搜索代碼字符串。
$ searcher
-> functions ([ ' strtolower ' , ' strtoupper ' ])
-> search ( ' ./file1.php ' );
$ searcher
-> variables ([ ' /^one[A-Z]$/ ' ])
-> searchCode ( ' <?php $oneA = "1a"; ' );在使用任何可用方法搜索時,可以通過用slashes / ,IE /testd+/名稱來使用正則表達式。
要通過名稱搜索變量,請使用variables方法。
$ results = $ searcher
-> variables ([ ' twoA ' , ' /^one.$/ ' ])
-> searchCode ( ' <?php ' .
' $oneA = "1a"; ' .
' $oneB = "1b"; ' .
' $twoA = "2a"; ' .
' $twoB = "2b"; ' .
'' );
foreach ( $ results -> results as $ result ) {
echo " Found ' { $ result -> node -> name ()} ' on line { $ result -> location -> startLine }" . PHP_EOL ;
}要搜索函數調用或定義,請使用functions方法。
// search for references AND definitions for 'strtolower' and/or 'myfunc'
$ searcher
-> functions ([ ' strtolower ' , ' myfunc ' ])
-> search ( ' file1.php ' );要按名稱搜索方法調用,請使用methods方法。
方法調用節點具有一個可以循環介紹的args屬性,以檢索方法調用的參數。
$ results = $ searcher
-> methods ([ ' /test(One|Two)/ ' ])
-> searchCode ( ' <?php ' .
' $obj->testOne("hello world 1"); ' .
' $obj->testTwo("hello world", 2); ' .
''
);
foreach ( $ results -> results as $ result ) {
echo " Found ' { $ result -> node -> name ()} ' on line { $ result -> location -> startLine }" . PHP_EOL ;
foreach ( $ result -> node -> args as $ arg ) {
echo " argument: ' { $ arg -> value } ' " . PHP_EOL ;
}
}要搜索靜態方法或屬性調用,請使用static方法。
有效的搜索術語要么是諸如Cache類的類名稱,要么是類名稱和諸如Cache::remember 。
$ searcher
-> static ([ ' Ray ' , ' Cache::has ' , ' Request::$myProperty ' ])
-> search ( ' ./app/Http/Controllers/MyController.php ' );要搜索類定義或new關鍵字創建的類,請使用classes方法。
$ searcher
-> classes ([ ' MyController ' ])
-> search ( ' ./app/Http/Controllers/MyController.php ' );要通過可變名稱搜索變量分配,請使用assignments方法。注意:應省略$ 。
$ searcher
-> assignments ([ ' myVar ' ])
-> search ( ' ./app/Http/Controllers/MyController.php ' );要返回沒有關聯的代碼段的搜索結果,請使用withoutSnippets方法:
$ searcher
-> withoutSnippets ()
-> functions ([ ' strtolower ' ])
-> search ( ' file1.php ' );./vendor/bin/phpunit有關最近發生了變化的更多信息,請參見ChangElog。
請有關詳細信息,請參閱貢獻。
請查看我們有關如何報告安全漏洞的安全政策。
麻省理工學院許可證(麻省理工學院)。請參閱許可證文件以獲取更多信息。