joshbrw/type-enforcement
Composer 安装命令:
composer require joshbrw/type-enforcement
包简介
Enforce types and throw InvalidArgumentExceptions upon invalid types.
README 文档
README
If you've ever come across a time where you're writing loosely-coupled code but want to check a parameters type at the start of a method, then this library is for you.
How you're probably doing it
class UserRegistrar { public function register(array $usersDetails, $author) { if (!$author instanceof SomeAuthor) { throw new \InvalidArgumentException('The author must be an instance of SomeAuthor'); } } }
Let's refactor!
use Joshbrw\TypeEnforcement\Type; class UserRegistrar { public function register(array $usersDetails, $author) { /* Throws \InvalidArgumentException on invalid input */ Type::enforce($author, SomeAuthor::class); } }
Custom Exception Messages
By default the package will provide a useful exception message, stating which type is expected and which type has been provided, i.e:
Expected [Tests\NonExistentClass], [array '["array"]'] provided.
This exception message can be switched out by providing a custom message as the third parameter, i.e.:
Type::enforce($variable, Type::class, 'The variable must be a Type!');
Not a fan of static method calls?
This package also includes a helper method that accepts the same parameters:
enforce_type($variable, Type::class, 'The variable must be a Type!');
统计信息
- 总下载量: 51
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-01