Skip to content

Commit

Permalink
Rename reflection converter package to reader
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Apr 1, 2024
1 parent ec84a00 commit be1ed4e
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 107 deletions.
33 changes: 16 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
---

<p align="center">
<a href="https://packagist.org/packages/type-lang/reflection-converter"><img src="https://poser.pugx.org/type-lang/reflection-converter/require/php?style=for-the-badge" alt="PHP 8.1+"></a>
<a href="https://packagist.org/packages/type-lang/reflection-converter"><img src="https://poser.pugx.org/type-lang/reflection-converter/version?style=for-the-badge" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/type-lang/reflection-converter"><img src="https://poser.pugx.org/type-lang/reflection-converter/v/unstable?style=for-the-badge" alt="Latest Unstable Version"></a>
<a href="https://raw.githubusercontent.com/php-type-language/reflection-converter/blob/master/LICENSE"><img src="https://poser.pugx.org/type-lang/reflection-converter/license?style=for-the-badge" alt="License MIT"></a>
<a href="https://packagist.org/packages/type-lang/reader"><img src="https://poser.pugx.org/type-lang/reader/require/php?style=for-the-badge" alt="PHP 8.1+"></a>
<a href="https://packagist.org/packages/type-lang/reader"><img src="https://poser.pugx.org/type-lang/reader/version?style=for-the-badge" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/type-lang/reader"><img src="https://poser.pugx.org/type-lang/reader/v/unstable?style=for-the-badge" alt="Latest Unstable Version"></a>
<a href="https://raw.githubusercontent.com/php-type-language/reader/blob/master/LICENSE"><img src="https://poser.pugx.org/type-lang/reader/license?style=for-the-badge" alt="License MIT"></a>
</p>
<p align="center">
<a href="https://github.com/php-type-language/reflection-converter/actions"><img src="https://github.com/php-type-language/reflection-converter/workflows/tests/badge.svg"></a>
<a href="https://github.com/php-type-language/reader/actions"><img src="https://github.com/php-type-language/reader/workflows/tests/badge.svg"></a>
</p>

Provides a set of methods for converting PHP reflection objects into the
Expand All @@ -25,18 +25,17 @@ Type Language Reflection Converter is available as Composer repository and can
be installed using the following command in a root of your project:

```sh
$ composer require type-lang/reflection-converter
$ composer require type-lang/reader
```

## Quick Start

```php
// Type contains ReflectionNamedType{ name: "void" }
$function = new \ReflectionFunction(function(): void {});
$type = $function->getReturnType();
$reader = new \TypeLang\Reader\ReflectionReader();

$converter = new \TypeLang\ReflectionConverter\Converter();
$node = $converter->convert($type);
$node = $reader->findFunctionType(
function: new \ReflectionFunction(function(): void {}),
);

var_dump($node);
```
Expand All @@ -59,42 +58,42 @@ TypeLang\Parser\Node\Stmt\NamedTypeNode {
}
```

### Creating From Reflection
### Creating From Reflection

```php
$class = new \ReflectionClass(Path\To\Example::class);

// Printer component provided by "type-lang/printer" package.
$printer = new \TypeLang\Printer\PrettyPrinter();

$converter = new \TypeLang\ReflectionConverter\Converter();
$converter = new \TypeLang\Reader\ReflectionReader();

// Dump all constants with its types.
foreach ($class->getReflectionConstants() as $constant) {
// Creates type node AST from a constant's type.
if ($type = $converter->convertConstantType($constant)) {
if ($type = $converter->findConstantType($constant)) {
echo 'const ' . $constant->name . ' has type ' . $printer->print($type) . "\n";
}
}

// Dump all properties with its types.
foreach ($class->getProperties() as $property) {
// Creates type node AST from a property's type.
if ($type = $converter->convertPropertyType($property)) {
if ($type = $converter->findPropertyType($property)) {
echo 'property ' . $property->name . ' has type ' . $printer->print($type) . "\n";
}
}

// Dump all methods with its types.
foreach ($class->getMethods() as $method) {
// Creates type node AST from any function's return type.
if ($type = $converter->convertFunctionType($method)) {
if ($type = $converter->findFunctionType($method)) {
echo 'function ' . $method->name . ' has type ' . $printer->print($type) . "\n";
}

// Creates type node AST from a parameter's type.
foreach ($method->getParameters() as $parameter) {
if ($type = $converter->convertParameterType($parameter)) {
if ($type = $converter->findParameterType($parameter)) {
echo 'parameter ' . $parameter->name . ' has type ' . $printer->print($type) . "\n";
}
}
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"name": "type-lang/reflection-converter",
"name": "type-lang/reader",
"type": "library",
"description": "PHP Type Language reflection converter",
"keywords": ["converter", "language", "php", "parser", "reflection"],
"license": "MIT",
"support": {
"source": "https://github.com/php-type-language/reflection-converter",
"issues": "https://github.com/php-type-language/reflection-converter/issues"
"source": "https://github.com/php-type-language/reader",
"issues": "https://github.com/php-type-language/reader/issues"
},
"require": {
"php": "^8.1",
"type-lang/parser": "^1.0"
},
"autoload": {
"psr-4": {
"TypeLang\\ReflectionConverter\\": "src"
"TypeLang\\Reader\\": "src"
}
},
"require-dev": {
Expand All @@ -27,7 +27,7 @@
},
"autoload-dev": {
"psr-4": {
"TypeLang\\ReflectionConverter\\Tests\\": "tests"
"TypeLang\\Reader\\Tests\\": "tests"
}
},
"extra": {
Expand Down
16 changes: 0 additions & 16 deletions src/ConverterInterface.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/Exception/ConverterExceptionInterface.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace TypeLang\ReflectionConverter\Exception;
namespace TypeLang\Reader\Exception;

/**
* @psalm-consistent-constructor
*/
class ConverterException extends \LogicException implements ConverterExceptionInterface
class ReaderException extends \LogicException implements ReaderExceptionInterface
{
final public const ERROR_CODE_INTERNAL_ERROR = 0x01;

Expand All @@ -20,7 +20,7 @@ public function __construct(string $message, int $code = 0, ?\Throwable $previou

public static function fromInternalError(\Throwable $e): static
{
$message = 'An internal error occurred while converting reflection type';
$message = 'An internal error occurred while reading reflection type';

return new static($message, self::ERROR_CODE_INTERNAL_ERROR, $e);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Exception/ReaderExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace TypeLang\Reader\Exception;

interface ReaderExceptionInterface extends \Throwable {}
58 changes: 53 additions & 5 deletions src/Exception/UnrecognizedReflectionTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,66 @@

declare(strict_types=1);

namespace TypeLang\ReflectionConverter\Exception;
namespace TypeLang\Reader\Exception;

class UnrecognizedReflectionTypeException extends ConverterException
class UnrecognizedReflectionTypeException extends ReaderException
{
final public const ERROR_CODE_UNSUPPORTED_REFLECTION_TYPE = 0x01 + parent::CODE_LAST;
final public const ERROR_CODE_INVALID_TYPE = 0x01 + parent::CODE_LAST;

protected const CODE_LAST = self::ERROR_CODE_UNSUPPORTED_REFLECTION_TYPE;
final public const ERROR_CODE_INVALID_TYPE_FROM_CONST = 0x02 + parent::CODE_LAST;

final public const ERROR_CODE_INVALID_TYPE_FROM_PROPERTY = 0x03 + parent::CODE_LAST;

final public const ERROR_CODE_INVALID_TYPE_FROM_FUNCTION = 0x04 + parent::CODE_LAST;

final public const ERROR_CODE_INVALID_TYPE_FROM_PARAMETER = 0x05 + parent::CODE_LAST;

protected const CODE_LAST = self::ERROR_CODE_INVALID_TYPE_FROM_PARAMETER;

public static function fromReflectionType(\ReflectionType $type): static
{
$message = \sprintf('Unsupported reflection type: %s', $type::class);

return new static($message, self::ERROR_CODE_UNSUPPORTED_REFLECTION_TYPE);
return new static($message, self::ERROR_CODE_INVALID_TYPE);
}

public static function fromReflectionConstant(\ReflectionType $type, \ReflectionClassConstant $const): static
{
$message = \vsprintf('Unsupported reflection type defined in %s const: %s', [
$const->getName(),
$type::class,
]);

return new static($message, self::ERROR_CODE_INVALID_TYPE_FROM_CONST);
}

public static function fromReflectionProperty(\ReflectionType $type, \ReflectionProperty $property): static
{
$message = \vsprintf('Unsupported reflection type defined in $%s property: %s', [
$property->getName(),
$type::class,
]);

return new static($message, self::ERROR_CODE_INVALID_TYPE_FROM_PROPERTY);
}

public static function fromReflectionFunction(\ReflectionType $type, \ReflectionFunctionAbstract $function): static
{
$message = \vsprintf('Unsupported reflection type defined in %s function: %s', [
$function->getName(),
$type::class,
]);

return new static($message, self::ERROR_CODE_INVALID_TYPE_FROM_FUNCTION);
}

public static function fromReflectionParameter(\ReflectionType $type, \ReflectionParameter $parameter): static
{
$message = \vsprintf('Unsupported reflection type defined in $%s parameter: %s', [
$parameter->getName(),
$type::class,
]);

return new static($message, self::ERROR_CODE_INVALID_TYPE_FROM_PARAMETER);
}
}
39 changes: 39 additions & 0 deletions src/ReaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace TypeLang\Reader;

use TypeLang\Parser\Node\Stmt\TypeStatement;
use TypeLang\Reader\Exception\ReaderExceptionInterface;

interface ReaderInterface
{
/**
* Returns a type AST structure based on an {@see ReflectionProperty} object.
*
* @throws ReaderExceptionInterface In case of any reading error occurs.
*/
public function findPropertyType(\ReflectionProperty $property): ?TypeStatement;

/**
* Returns a type AST structure based on an {@see ReflectionFunctionAbstract} object.
*
* @throws ReaderExceptionInterface In case of any reading error occurs.
*/
public function findFunctionType(\ReflectionFunctionAbstract $function): ?TypeStatement;

/**
* Returns a type AST structure based on an {@see ReflectionParameter} object.
*
* @throws ReaderExceptionInterface In case of any reading error occurs.
*/
public function findParameterType(\ReflectionParameter $parameter): ?TypeStatement;

/**
* Returns a type AST structure based on an {@see ReflectionClassConstant} object.
*
* @throws ReaderExceptionInterface In case of any reading error occurs.
*/
public function findConstantType(\ReflectionClassConstant $constant): ?TypeStatement;
}
Loading

0 comments on commit be1ed4e

Please sign in to comment.