poseidon/type

缺失的 PHP 静态分析类型合约

这个包的规范仓库似乎已不存在,因此该包已被冻结。

0.2.0 2022-10-28 11:55 UTC

This package is auto-updated.

Last update: 2023-03-28 12:52:21 UTC


README

PHP 静态分析类型系统(Psalm, PHPStan)类型的价值对象集合。

所有 Type 的实现都应被视为密封的。在用户空间中不应实现 Type 接口。如果需要一个复杂复合类型的别名,请实现 Alias 或创建构建器/函数。

此库永远不会有任何依赖项。一旦完整且稳定,可能会被提议作为 PSR。

安装

composer require poseidon/type

命名

表示原生 PHP 类型的值对象不能以它们的名称命名,因为像 IntStrind 等单词已被 保留。可能会引入后缀来解决此问题。由于 Type 后缀过于冗长,因此我们选择了 Tint -> IntTfloat -> FloatT。尽管像 non-empty-list 这样的类型可以安全地命名为 NonEmptyList,但我们目前决定为所有类型遵循 T-约定。

支持的类型

标量类型

  • bool
  • int
  • positive-int
  • float
  • non-empty-string
  • string
  • literal-string
  • array-key
  • numeric-string
  • class-string
  • class-string<T>
  • interface-string
  • trait-string
  • enum-string
  • callable-string
  • numeric
  • scalar

字面量

  • null
  • true
  • false
  • -10
  • -0.34
  • 'foo'

数组类型

  • list<TValue>
  • non-empty-list<TValue>
  • array<TKey, TValue>
  • non-empty-array<TKey, TValue>
  • array{foo: int, bar?: string}array{int, string}
  • callable-array

对象类型

  • object
  • Foo\MyClass<T>
  • Generator<TKey, TValue, TSend, TReturn>
  • static

可调用类型

  • callable(Foo, Bar=): Baz
  • \Closure(Foo, Bar=): Baz

常量

  • Foo\Bar::MY_SCALAR_CONST
  • key-of<Foo\Bar::ARRAY_CONST>
  • value-of<Foo\Bar::ARRAY_CONST>
  • key-of<T>T[TKey] (示例)

其他类型

  • ?T
  • never
  • void
  • mixed
  • iterable<TKey, TValue>
  • resource
  • 联合类型
  • 交集类型
  • 模板类型