W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
運(yùn)行Typechecker很簡(jiǎn)單。對(duì)于大多數(shù)用戶和大多數(shù)用途,它是一個(gè)單行命令,稱為hh_client。
hh_client對(duì)于所有意圖和目的,是類型檢查器。假設(shè)您已正確安裝所有內(nèi)容,并且已將.hhconfig文件添加到代碼庫(kù)的根目錄,然后啟動(dòng)typechecking:
%(root of codebase) hh_client
這是一些Hack代碼的例子,它是如何被檢查的。
<?hh
namespace Hack\UserDocumentation\TypeChecker\Running\Examples\Simple;
function a(int $x): int {
return x + 1;
}
function main(): void {
// simple-check.php:13:14,16: Invalid argument (Typing[4110])
// simple-check.php:5:12,14: This is an int
// simple-check.php:13:14,16: It is incompatible with a string
a("5");
// No errors!
a(5);
}
main();
Output
Catchable fatal error: Argument 1 passed to Hack\UserDocumentation\TypeChecker\Running\Examples\Simple\a() must be an instance of int, string given in /data/users/joelm/user-documentation/guides/hack/25-typechecker/04-running-examples/simple-check.php.type-errors on line 7
我們來檢查調(diào)用的錯(cuò)誤消息a("5"):
simple-check.php:13:14,16: Invalid argument (Typing[4110])
simple-check.php:5:12,14: This is an int
simple-check.php:13:14,16: It is incompatible with a string
該Invalid argument行告訴您發(fā)現(xiàn)實(shí)際錯(cuò)誤的位置。在我們的情況下,調(diào)用a("5")是什么導(dǎo)致錯(cuò)誤。
注意:這Typing[4110]是服務(wù)器的狀態(tài)碼。這個(gè)代碼可以用于這樣的東西HH_FIXME
這一This is an int行讓你知道typechecker預(yù)期你要做什么?;旧险f你的電話a()應(yīng)該提供一個(gè)int。
該It is incompatible with a string行讓你知道你做了什么導(dǎo)致typechecker錯(cuò)誤。在這種情況下,你通過了string以a()代替int。
運(yùn)行時(shí)hh_client,它會(huì)檢查Typechecker服務(wù)器hh_server是否正在運(yùn)行。如果沒有,服務(wù)器將被轉(zhuǎn)移。否則,將使用服務(wù)器的當(dāng)前實(shí)例。
服務(wù)器是后臺(tái)基礎(chǔ)設(shè)施,用于對(duì)代碼進(jìn)行所有類型分析。是連續(xù)的 這就是為什么hh_client報(bào)告錯(cuò)誤接近瞬間。
typechecker只檢查Hack文件; 那就是開頭的文件<?hh。所以如果你的代碼庫(kù)是全部的<?php,那么typechecker將不會(huì)對(duì)你有太多的用處,只會(huì)報(bào)告No errors!。
Typechecker假設(shè)項(xiàng)目中的所有可用代碼都可以在項(xiàng)目的任何地方使用。沒有檢查您的require和include語(yǔ)句是否準(zhǔn)確和正確。如果不是,那么你將會(huì)在運(yùn)行時(shí)被咬死。
當(dāng)然,如果你的代碼是一個(gè)文件,這一點(diǎn)是愚蠢的。但一旦你開始擁有多個(gè)文件自動(dòng)加載的項(xiàng)目變得非常有用。如果您不自動(dòng)加載,則Typechecker仍然會(huì)正確鍵入錯(cuò)誤,但無(wú)法確定您忘記了包含一些必需代碼的文件才能實(shí)際運(yùn)行。
<?hh
namespace Hack\UserDocumentation\TypeChecker\Running\Examples\Autoloading;
class A {
private B $b;
public function __construct(B $b) {
$this->b = $b;
}
public function foo(): int {
return $this->b->getSomeInt();
}
}
function callFoo(): void {
$a = new A(new B());
var_dump($a->foo());
}
function myAutoloader(string $class): void {
// Remove all the namespace stuff and just get the 'B'
include __DIR__ . '/' . substr($class, -1) . '.inc.php';
}
spl_autoload_register(
'Hack\UserDocumentation\TypeChecker\Running\Examples\Autoloading\myAutoloader'
);
callFoo();
/*
<?hh
namespace Hack\UserDocumentation\TypeChecker\Intro\Examples\Autoloading;
class B {
public function getSomeInt(): int {
return 5;
}
}
*/
Output
int(5)
假設(shè)我們忘了在這里要求訪問類的代碼B
。沒有自動(dòng)加載代碼,如果你運(yùn)行hh_client,
你會(huì)得到No errors!
。太好了吧?嗯,不是真的 類型檢查器做了正確的事情,但是你會(huì)在運(yùn)行時(shí)死機(jī)。
Fatal error: Class undefined: Hack\UserDocumentation\TypeChecker\Intro\Examples\Autoloading\B
通過自動(dòng)加載包含的文件B,您可能會(huì)意外忘記requires,但您的代碼仍將正確運(yùn)行。
INT(5)
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: