flashios09 / php-union-types
一个用于联合类型的PHP类
Requires
- php: ^7.1
Requires (Dev)
- cakephp/cakephp-codesniffer: 4.*
- filp/whoops: ^2.5
- kahlan/kahlan: ^4.6
- symfony/var-dumper: ^4.4
This package is auto-updated.
Last update: 2024-09-04 21:26:39 UTC
README
需求
- PHP 7.1+
- Composer
安装
composer require flashios09/php-union-types
用法
UnionTypes::assert
UnionTypes::assert(mixed $value, string[] $types, array $options = []): void
如果给定值不在传递的联合类型中,则抛出 TypeError。
查看完整的有效类型列表 valid types $types
字符串数组,例如 ['int', 'string', Posts::class, ...]
。
示例
-
UnionTypes::assert(1.2, ['int', 'float']);
✓ 通过
-
UnionTypes::assert('1.2', ['int', 'float']);
✖ 将抛出 TypeError "'1.2' 必须是联合类型
int|float
,给定string
" -
UnionTypes::assert('1.2', ['int', 'float', 'string']);
✓ 通过
-
检查
instance of
,添加于1.0.2
use \DateTime; class Time extends DateTime { public static function today() { return new Time(); } } $today = Time::today();
// `instanceOf` check is enabled by default UnionTypes::assert($today, [\DateTime::class, 'string']);
✓ 通过
// disabled using the `instanceOf` option UnionTypes::assert($today, [\DateTime::class, 'string', ['instanceOf' => false]]);
✖ 将抛出 TypeError "object(Time)" 必须是联合类型
\DateTime|string
,给定Time
"
UnionTypes::is
UnionTypes::is(mixed $value, string[] $types, array $options = []): bool
检查值类型是否为传递的类型之一。
与 UnionTypes::assert($value, $types)
的工作方式相同,但它将返回一个 bool(true
/false
) 而不是抛出 TypeError。
查看完整的有效类型列表 valid types $types
字符串数组,例如 ['int', 'string', Posts::class, ...]
。
示例
-
UnionTypes::is(1.2, ['int', 'float']);
相当于
is_int(1.2) || is_float(1.2)
✓ 返回
true
-
UnionTypes::is('1.2', ['int', 'float']);
相当于
is_int('1.2') || is_float('1.2')
✖ 返回
false
-
UnionTypes::is('1.2', ['int', 'float', 'string']);
相当于
is_int('1.2') || is_float('1.2') || is_string('1.2')
✓ 返回
true
-
检查
instance of
,添加于1.0.2
use \DateTime; class Time extends DateTime { public static function today() { return new Time(); } } $today = Time::today();
// `instanceOf` check is enabled by default UnionTypes::assert($today, [\DateTime::class, 'string']);
✓ 返回
true
// disabled using the `instanceOf` option UnionTypes::assert($today, [\DateTime::class, 'string', ['instanceOf' => false]]);
✖ 返回
false
UnionTypes::assertFuncArg
UnionTypes::assertFuncArg(string $argName, string[] $types, array $options = []): void
如果参数的值不在联合类型中,则抛出 TypeError。
查看完整的有效类型列表 valid types $types
字符串数组,例如 int
,string
,Posts::class
...
示例
-
function add($a, $b) { UnionTypes::assertFuncArg('a', ['int', 'float']); return $a + $b; } // invocation add(1.2, 1);
✓ 通过
-
class Math { public static function add($a, $b) { UnionTypes::assertFuncArg('a', ['int', 'float']); return $a + $b; } } // invocation Math::add('1.2', 1);
✖ 将抛出 TypeError "参数
a
传递给Math::add(int|float $a, ...)
必须是联合类型int|float
,给定string
" -
$closure = function ($a, $b) { UnionTypes::assertFuncArg('a', ['int', 'float', 'string']); return $a + $b; }; // invocation $closure('1.2', 1);
✓ 通过
-
检查
instance of
,添加于1.0.2
use \DateTime; class Time extends DateTime { public static function today() { return new Time(); } } $today = Time::today();
$closure = function ($today) { // `instanceOf` check is enabled by default UnionTypes::assertFuncArg($today, [\DateTime::class, 'string']); // some logic here return $today; }; // invocation $closure($today);
✓ 应该通过
$closure = function ($today) { // disabled using the `instanceOf` option UnionTypes::assertFuncArg($today, [\DateTime::class, 'string', ['instanceOf' => false]]); // some logic here return $today; }; // invocation $closure($today);
✖ 将抛出 TypeError "object(Time)" 必须是联合类型
\DateTime|string
,给定Time
"
有效类型
'string'
'int'
(不是'integer'
或'double'
)'float'
(不是'double'
或'decimal'
)'bool'
(不是'boolean'
)'null'
(不是'NULL'
)'array'
- 任何有效的 类名字符串,例如
Table::class
或'Cake\ORM\Table'
资源
配置
-
默认情况下,每个抛出的 Error 或 Exception 都会在消息末尾包含一个 called in,格式为
called in #{stackTraceIndex} {file}:{line}
,例如called in #0 /path/to/app/src/Controller/PostsController.php:126
。要有一个友好的编辑器(VSCode, Atom, SublimeText, ...) 文件路径,相对于工作空间,您需要在某处定义一个常量
UnionTypes.PATH_TO_APP
,例如// config/bootstrap.php define('UnionTypes.PATH_TO_APP', '/path/to/app/'); // using `$_SERVER`(isn't available in **php cli**) $PATH_TO_APP = isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR : ''; define('UnionTypes.PATH_TO_APP', $PATH_TO_APP); // using `dirname(__FILE__)`, maybe you need to remove/add some parts, depending on the `__FILE__` location. define('UnionTypes.PATH_TO_APP', dirname(__FILE__) . DIRECTORY_SEPARATOR; // using the `PWD` key of the `getEnv()` array define('UnionTypes.PATH_TO_APP', getEnv()['PWD'] . DIRECTORY_SEPARATOR;
现在将从
/path/to/app/
前缀 中移除 called in,例如called in #0 src/Controller/PostsController.php:126
。
贡献
安装
git clone git@github.com:flashios09/php-union-types.git
cd php-union-types
composer install
(将安装开发依赖项,如whoops
,kahlan
,var-dumper
,cakephp-codesniffer
)
使用 livereload 启动本地 PHP 服务器
yarn install
(node 依赖项)yarn serve
(然后添加您的代码到php-union-types/index.php
并在浏览器窗口中打开https://:3080
)
使用 livereload 启动本地 kahlan 测试
yarn install
(node 依赖项)yarn test
(它将监视spec
文件夹中的任何更改并重新启动测试)
代码检查
composer cs-check
composer cs-fix
运行测试
composer test
许可
本项目受 MIT 许可证 许可。