hhvm/type-assert
最新稳定版本:v4.2.2
Composer 安装命令:
composer require hhvm/type-assert
包简介
Convert untyped data to typed data
关键字:
README 文档
README
Hack library for converting untyped data to typed data.
Warning for TypeAssert\matches_type_structure()
TypeStructure<T>, type_structure(), and ReflectionTypeAlias::getTypeStructures() are experimental features of HHVM, and not supported by Facebook or the HHVM team. This means that matches_type_structure() may need to be removed in a future release without warning.
We strongly recommend moving to TypeAssert\matches<T>() and TypeCoerce\match<T>() instead.
Installation
composer require hhvm/type-assert Usage
TypeAssert provides functions that take a mixed input, and will either return it unmodified (but with type data) or throw an exception; for example:
<?hh // strict use namespace Facebook\TypeAssert; function needs_string(string $bar): void { } function main(): void { needs_string(TypeAssert\string('foo')); // type-safe and works fine needs_string(TypeAssert\string(123)); // type-safe, but throws }
These include:
string(mixed): stringint(mixed): intfloat(mixed): floatbool(mixed): boolresource(mixed): resourcenum(mixed): numarraykey(mixed): arraykeynot_null<T>(?T): Tinstance_of<T>(classname<T>, mixed): Tclassname_of<T>(classname<T>, mixed): classname<T>matches<T>(mixed): Tmatches_type_structure<T>(TypeStructure<T>, mixed): T
Coercion
TypeAssert also contains the Facebook\TypeCoerce namespace, which includes a similar set of functions:
string(mixed): stringint(mixed): intfloat(mixed): floatbool(mixed): boolresource(mixed): resourcenum(mixed): numarraykey(mixed): arraykeymatch<T>(mixed): Tmatch_type_structure<T>(TypeStructure<T>, mixed): T
These will do 'safe' transformations, such as int-ish strings to int, ints to strings, arrays to vecs, arrays to dicts, and so on.
TypeSpec
You can also assert/coerce complex types (except for shapes and tuples) without a type_structure:
<?hh use namespace Facebook\TypeSpec; $spec = TypeSpec\dict( TypeSpec\string(), TypeSpec\int(), ); $x = $spec->assertType(dict['foo' => 123]); // passes: $x is a dict<string, int> $x = $spec->assertType(dict['foo' => '123']); // fails $x = $spec->assertType(dict[123 => 456]); // fails $x = $spec->assertType(dict[123 => 456]); // fails $x = $spec->coerceType(dict[123 => '456']); // passes: $x is dict['123' => 456];
Shapes and tuples are not supported, as they can not be expressed generically.
matches_type_structure<T>(TypeStructure<T>, mixed): T
Asserts that a variable matches the given type structure; these can be arbitrary nested shapes. This is particular useful for dealing with JSON responses.
<?hh // strict use namespace Facebook\TypeAssert; class Foo { const type TAPIResponse = shape( 'id' => int, 'user' => string, 'data' => shape( /* ... */ ), ); public static function getAPIResponse(): self::TAPIResponse { $json_string = file_get_contents('https://api.example.com'); $array = json_decode($json_string, /* associative = */ true); return TypeAssert\matches_type_structure( type_structure(self::class, 'TAPIResponse'), $array, ); } }
You can use type_structure() to get a TypeStructure<T> for a type constant, or ReflectionTypeAlias::getTypeStructure() for top-level type aliases.
not_null<T>(?T): T
Throws if it's null, and refines the type otherwise - for example:
<?hh // strict use namespace \Facebook\TypeAssert; function needs_string(string $foo): void {} function needs_int(int $bar): void {} function main(?string $foo, ?int bar): void { needs_string(TypeAssert\not_null($foo)); // ?string => string needs_int(TypeAssert\not_null($bar)); // ?int => int }
is_instance_of<T>(classname<T>, mixed): T
Asserts that the input is an object of the given type; for example:
<?hh use namespace Facebook\TypeAssert; class Foo {} function needs_foo(Foo $foo): void {} function main(mixed $foo): void { needs_foo(TypeAssert::is_instance_of(Foo::class, $foo)); } main(new Foo());
is_classname_of<T>(classname<T>, mixed): classname<T>
Asserts that the input is the name of a child of the specified class, or implements the specified interface.
<?hh // strict use namespace Facebook\TypeAssert; class Foo { public static function doStuff(): void {} } class Bar extends Foo { <<__Override>> public static function doStuff(): void { // specialize here } } function needs_foo_class(classname<Foo> $foo): void { $foo::doStuff(); } function main(mixed $class): void { needs_foo_class(TypeAssert::is_classname_of(Foo::class, $class)); } main(Bar::class);
Credit
This library is a reimplementation of ideas from:
- @admdikramr
- @ahupp
- @dlreeves
- @periodic1236
- @schrockn
Security Issues
We use GitHub issues to track public bugs. Please ensure your description is clear and has sufficient instructions to be able to reproduce the issue.
Facebook has a bounty program for the safe disclosure of security bugs. In those cases, please go through the process outlined on that page and do not file a public issue.
License
Type-Assert is MIT-licensed.
hhvm/type-assert 适用场景与选型建议
hhvm/type-assert 是一款 基于 Hack 开发的 Composer 扩展包,目前已累计 1.12M 次下载、GitHub Stars 达 25, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「hack」 「TypeAssert」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hhvm/type-assert 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hhvm/type-assert 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hhvm/type-assert 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Convert untyped data to typed data
Hack Logging Package
Dependency Injection Container For HHVM/Hack
Middleware Dispatcher For Hack
Allows installation of Laravel where the PHP Mcrypt extension is not available. Provides encryption using OpenSSL, or by disabling encryption entierly.
Typechecker definitions for PSR-11
统计信息
- 总下载量: 1.12M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 30
- 依赖项目数: 25
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04