Skip to content

Commit

Permalink
Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Mar 1, 2024
1 parent 3d2cd4c commit a03a7df
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 197 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Typhoon

The ultimate type system and reflection for PHP.

## Components

- [Type](docs/Type.md)
- [Reflection](docs/Reflection.md)
- [TypeStringifier](docs/TypeStringifier.md)
4 changes: 2 additions & 2 deletions docs/Reflection.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Typhoon Reflection is an alternative to [native PHP Reflection](https://www.php.
- static,
- fast (due to lazy loading and caching),
- [99% compatible with native reflection](#compatibility-with-native-reflection),
- supports most of the Psalm/PHPStan types,
- supports most of the Psalm and PHPStan phpDoc types,
- can resolve templates,
- does not create circular object references (can be safely used with [zend.enable_gc=0](https://www.php.net/manual/en/info.configuration.php#ini.zend.enable-gc)).
- can be safely used with [zend.enable_gc=0](https://www.php.net/manual/en/info.configuration.php#ini.zend.enable-gc).

## Installation

Expand Down
17 changes: 9 additions & 8 deletions docs/Type.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Typhoon Type

Typhoon Type is an abstraction over the PHP static analysis types. It is the main building block of the whole project.
Typhoon type system is compatible with the popular PHP static analyzers [Psalm](https://psalm.dev/) and [PHPStan](https://phpstan.org/).
Typhoon Type is an abstraction for the PHP static type system, inspired by two popular analyzers [Psalm](https://psalm.dev/) and [PHPStan](https://phpstan.org/).

This library will never have any dependencies. Once full and stable, it might be proposed as a [PSR](https://www.php-fig.org/psr/) or [PER](https://www.php-fig.org/per/).
Once full and stable, Typhoon Type might be proposed as a [PSR](https://www.php-fig.org/psr/) or [PER](https://www.php-fig.org/per/).

## Installation

Expand Down Expand Up @@ -42,16 +41,18 @@ $type = types::arrayShape([
], sealed: false);
```

Note that all classes that implement `Type` (except `types::` itself) are `@internal` and should not be instantiated directly.
Note that all classes that implement `Type` (except `types::` itself) are `@internal` and should not be used outside the library.

## Analyzing types

Types should be analyzed via [TypeVisitor](../src/Type/TypeVisitor.php) or [DefaultTypeVisitor](../src/Type/DefaultTypeVisitor.php). `instanceof`, `==` and `===` operators should not be used for this purpose,
firstly because type classes are internal, secondly because types might be implicitly decorated.
Typhoon types should be analyzed only via [TypeVisitors](../src/Type/TypeVisitor.php): `$type->accept(new MyVisitor())`. Comparison operators and `instanceof`
should never be used with Typhoon types for two reasons:
1. type classes are internal and not subject to backward compatibility,
2. equal types might have different internal structure (e.g., one is decorated).

## Checking type relations
### Comparing types

Typhoon team is currently working on a type comparator component. For now use [DefaultTypeVisitor](../src/Type/DefaultTypeVisitor.php) to check basic type relations:
Typhoon team is currently working on a type comparator. Until it is released, you can use [DefaultTypeVisitor](../src/Type/DefaultTypeVisitor.php) for simple checks:

```php
use Typhoon\Type\Type;
Expand Down
117 changes: 4 additions & 113 deletions src/Reflection/README.md
Original file line number Diff line number Diff line change
@@ -1,115 +1,6 @@
# Typhoon Reflection
# [Typhoon Reflection](https://github.com/typhoon-php/typhoon/blob/0.3.x/docs/Reflection.md)

This library is an alternative to [native PHP Reflection](https://www.php.net/manual/en/book.reflection.php). It is:
- static,
- lazy (does not load inherited classes until you reflect properties or methods),
- [PSR-16](https://www.php-fig.org/psr/psr-16/) cacheable,
- [99% compatible with native reflection](docs/compatibility.md),
- supports most of the Psalm/PHPStan types,
- can resolve templates,
- does not create circular object references (can be safely used with [zend.enable_gc=0](https://www.php.net/manual/en/info.configuration.php#ini.zend.enable-gc)).
This repository is a readonly Typhoon monorepo subsplit.
Please, open pull requests and issues in the [main repository](https://github.com/typhoon-php/typhoon).

## Installation

```
composer require typhoon/reflection jetbrains/phpstorm-stubs
```

Installing `jetbrains/phpstorm-stubs` is highly recommended. Without stubs core PHP classes are reflected via
[NativeReflector](src/NativeReflector/NativeReflector.php) that does not support phpDoc types.

## Basic Usage

```php
namespace My\Awesome\App;

use Typhoon\Reflection\TyphoonReflector;
use Typhoon\Type\types;

/**
* @template T
*/
final readonly class Article
{
/**
* @param non-empty-list<non-empty-string> $tags
* @param T $data
*/
public function __construct(
private array $tags,
public mixed $data,
) {}
}

$reflector = TyphoonReflector::build();
$articleReflection = $reflector->reflectClass(Article::class);

$tagsReflection = $articleReflection->getProperty('tags');

var_dump($tagsReflection->getTyphoonType()); // object representation of non-empty-list<non-empty-string> type

$dataReflection = $articleReflection->getProperty('data');

var_dump($dataReflection->getTyphoonType()); // object representation of T template type
```

## Caching

By default, Typhoon Reflection uses in-memory LRU cache which should be enough for the majority of use cases.

However, if you need persistent cache, you can use any [PSR-16](https://www.php-fig.org/psr/psr-16/) implementation. We highly recommend [Typhoon OPcache](https://github.com/typhoon-php/opcache).
It stores values as php files that could be opcached. It is much faster than an average file cache implementation that uses `serialize`.

```php
use Typhoon\Reflection\TyphoonReflector;
use Typhoon\OPcache\TyphoonOPcache;

$reflector = TyphoonReflector::build(
cache: new TyphoonOPcache('path/to/cache/dir'),
);
```

To detect file changes during development, decorate your cache with [FreshCache](src/Cache/FreshCache.php).

```php
use Typhoon\Reflection\TyphoonReflector;
use Typhoon\Reflection\Cache\FreshCache;
use Typhoon\OPcache\TyphoonOPcache;

$reflector = TyphoonReflector::build(
cache: new FreshCache(new TyphoonOPcache('path/to/cache/dir')),
);
```

## Class locators

By default, reflector uses:
- [ComposerClassLocator](src/ClassLocator/ComposerClassLocator.php) if composer autoloading is used,
- [PhpStormStubsClassLocator](src/ClassLocator/PhpStormStubsClassLocator.php) if `jetbrains/phpstorm-stubs` is installed,
- [NativeReflectionFileLocator](src/ClassLocator/NativeReflectionFileLocator.php) (tries to detect class file via native reflection),
- [NativeReflectionLocator](src/ClassLocator/NativeReflectionLocator.php) (returns native reflection).

You can implement your own locators and pass them to the `build` method:

```php
use Typhoon\Reflection\ClassLocator;
use Typhoon\Reflection\ClassLocator\ClassLocators;
use Typhoon\Reflection\TyphoonReflector;

final class MyClassLocator implements ClassLocator
{
// ...
}

$reflector = TyphoonReflector::build(
classLocator: new ClassLocators([
new MyClassLocator(),
TyphoonReflector::defaultClassLocator(),
]),
);
```

## TODO

- [ ] enums, cases
- [ ] functions
Read [documentation](https://github.com/typhoon-php/typhoon/blob/0.3.x/docs/Reflection.md).
46 changes: 4 additions & 42 deletions src/Type/README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,6 @@
# Typhoon Type
# [Typhoon Type](https://github.com/typhoon-php/typhoon/blob/0.3.x/docs/Type.md)

Collection of value objects that represent the extended PHP type system.
All the types are inspired by popular PHP static analysis tools: [Psalm](https://psalm.dev/) and [PHPStan](https://phpstan.org/).
This repository is a readonly Typhoon monorepo subsplit.
Please, open pull requests and issues in the [main repository](https://github.com/typhoon-php/typhoon).

This library will never have any dependencies. Once full and stable, it might be proposed as a [PSR](https://www.php-fig.org/psr/) or [PER](https://www.php-fig.org/per/).

Please, note that this is a low-level API for static analysers and reflectors. It's not designed for convenient general usage in a project.
For that purpose we plan to release a special package.

## Installation

```
composer require typhoon/type
```

## Usage

```php
use Typhoon\Type\types;

/**
* Equivalent to type array{
* a: non-empty-list,
* b?: int|float,
* c: Traversable<numeric-string, false>,
* d: callable(PDO::*, TSend:Generator=, scalar...): void,
* ...
* }
*/
$type = types::arrayShape([
'a' => types::nonEmptyString,
'b' => types::arrayElement(types::union(types::int, types::float), optional: true),
'c' => types::object(Traversable::class, types::numericString, types::false),
'd' => types::callable(
parameters: [
types::classConstant(types::object(PDO::class), '*'),
types::param(types::template('TSend', types::atClass(Generator::class)), hasDefault: true),
types::param(types::scalar, variadic: true),
],
return: types::void,
),
], sealed: false);
```
Read [documentation](https://github.com/typhoon-php/typhoon/blob/0.3.x/docs/Type.md).
36 changes: 4 additions & 32 deletions src/TypeStringifier/README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
# Typhoon Type Stringifier
# [Typhoon Type Stringifier](https://github.com/typhoon-php/typhoon/blob/0.3.x/docs/TypeStringifier.md)

## Installation
This repository is a readonly Typhoon monorepo subsplit.
Please, open pull requests and issues in the [main repository](https://github.com/typhoon-php/typhoon).

```
composer require typhoon/type-stringifier
```

## Usage

```php
use Typhoon\Type\types;
use function Typhoon\TypeStringifier\stringify;

echo stringify(
types::arrayShape([
'a' => types::nonEmptyString,
'b' => types::arrayElement(types::union(types::int, types::float), optional: true),
'c' => types::object(Traversable::class, types::numericString, types::false),
'd' => types::callable(
parameters: [
types::classConstant(types::object(PDO::class), '*'),
types::param(types::template('TSend', types::atClass(Generator::class)), hasDefault: true),
types::param(types::scalar, variadic: true),
],
return: types::void,
),
], sealed: false),
);
```

```
array{a: non-empty-string, b?: int|float, c: Traversable<numeric-string, false>, d: callable(PDO::*, TSend:Generator=, bool|int|float|string...): void, ...}
```
Read [documentation](https://github.com/typhoon-php/typhoon/blob/0.3.x/docs/TypeStringifier.md).

0 comments on commit a03a7df

Please sign in to comment.