Parser
Name resolution
The parser knows nothing about namespaces and use statements:
Collection<User> is parsed as written. TypeLang\Parser\TypeResolver
resolves the names afterwards.
use TypeLang\Parser\TypeParser;use TypeLang\Parser\TypeResolver;
$type = new TypeParser()->parse('Name|Error\ParsingException');
$type = new TypeResolver() // use TypeLang\Type\Name; ->withTypeImport('TypeLang\Type\Name') // use TypeLang\Parser\Exception as Error; ->withTypeImportAs('TypeLang\Parser\Exception', 'Error') ->resolve($type);
// TypeLang\Type\Name|TypeLang\Parser\Exception\ParsingExceptionThe first segment of a name is matched against the imports, without regard
to case, as in PHP. The resolver is immutable: every with*() returns a new
one.
Imports
Section titled “Imports”The same imports as a constructor argument, an alias is a key:
$resolver = new TypeResolver([ // use TypeLang\Type\Name; 'TypeLang\Type\Name', // use TypeLang\Parser\Exception as Error; 'Error' => 'TypeLang\Parser\Exception',]);Or read from the file of a class or a function:
$resolver = new TypeResolver() ->withTypeImportsFromClass(new ReflectionClass(App\Example::class)) ->withTypeImportsFromFunction(new ReflectionFunction('App\example'));Namespace
Section titled “Namespace”withNamespace() registers the namespace of the file. A type string also
holds names that are not classes (list, positive-int, an alias of your
own), so a name is resolved only when such a class, interface or enum
exists:
$resolver = new TypeResolver()->withNamespace('App');
// list<Address> list<App\Address>, when App\Address exists// list<uuid> list<uuid>// namespace\Address App\Address, alwaysThe second argument replaces the check, such as
static fn(string $name): bool => true to resolve every name as PHP does.
An empty string is the global namespace.
self, static and parent
Section titled “self, static and parent”withClasses() registers the class the type is written in (self) and the
class it is used for (static, the same class by default). parent is the
parent of self.
$resolver = new TypeResolver()->withClasses(App\User::class);
// list<self> list<App\User>// self::STATUS_* App\User::STATUS_*Custom rules
Section titled “Custom rules”Any other rule (a class map, a container, a PSR-4 lookup) is a callback of
TypeMapVisitor, the visitor
TypeResolver is built on.