Skip to content

Commit be1ed4e

Browse files
author
Kirill Nesmeyanov
committed
Rename reflection converter package to reader
1 parent ec84a00 commit be1ed4e

12 files changed

+178
-107
lines changed

README.md

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
---
66

77
<p align="center">
8-
<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>
9-
<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>
10-
<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>
11-
<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>
8+
<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>
9+
<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>
10+
<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>
11+
<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>
1212
</p>
1313
<p align="center">
14-
<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>
14+
<a href="https://github.com/php-type-language/reader/actions"><img src="https://github.com/php-type-language/reader/workflows/tests/badge.svg"></a>
1515
</p>
1616

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

2727
```sh
28-
$ composer require type-lang/reflection-converter
28+
$ composer require type-lang/reader
2929
```
3030

3131
## Quick Start
3232

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

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

4140
var_dump($node);
4241
```
@@ -59,42 +58,42 @@ TypeLang\Parser\Node\Stmt\NamedTypeNode {
5958
}
6059
```
6160

62-
### Creating From Reflection
61+
### Creating From Reflection
6362

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

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

70-
$converter = new \TypeLang\ReflectionConverter\Converter();
69+
$converter = new \TypeLang\Reader\ReflectionReader();
7170

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

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

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

9594
// Creates type node AST from a parameter's type.
9695
foreach ($method->getParameters() as $parameter) {
97-
if ($type = $converter->convertParameterType($parameter)) {
96+
if ($type = $converter->findParameterType($parameter)) {
9897
echo 'parameter ' . $parameter->name . ' has type ' . $printer->print($type) . "\n";
9998
}
10099
}

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{
2-
"name": "type-lang/reflection-converter",
2+
"name": "type-lang/reader",
33
"type": "library",
44
"description": "PHP Type Language reflection converter",
55
"keywords": ["converter", "language", "php", "parser", "reflection"],
66
"license": "MIT",
77
"support": {
8-
"source": "https://github.com/php-type-language/reflection-converter",
9-
"issues": "https://github.com/php-type-language/reflection-converter/issues"
8+
"source": "https://github.com/php-type-language/reader",
9+
"issues": "https://github.com/php-type-language/reader/issues"
1010
},
1111
"require": {
1212
"php": "^8.1",
1313
"type-lang/parser": "^1.0"
1414
},
1515
"autoload": {
1616
"psr-4": {
17-
"TypeLang\\ReflectionConverter\\": "src"
17+
"TypeLang\\Reader\\": "src"
1818
}
1919
},
2020
"require-dev": {
@@ -27,7 +27,7 @@
2727
},
2828
"autoload-dev": {
2929
"psr-4": {
30-
"TypeLang\\ReflectionConverter\\Tests\\": "tests"
30+
"TypeLang\\Reader\\Tests\\": "tests"
3131
}
3232
},
3333
"extra": {

src/ConverterInterface.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Exception/ConverterExceptionInterface.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Exception/ConverterException.php renamed to src/Exception/ReaderException.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
declare(strict_types=1);
44

5-
namespace TypeLang\ReflectionConverter\Exception;
5+
namespace TypeLang\Reader\Exception;
66

77
/**
88
* @psalm-consistent-constructor
99
*/
10-
class ConverterException extends \LogicException implements ConverterExceptionInterface
10+
class ReaderException extends \LogicException implements ReaderExceptionInterface
1111
{
1212
final public const ERROR_CODE_INTERNAL_ERROR = 0x01;
1313

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

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

2525
return new static($message, self::ERROR_CODE_INTERNAL_ERROR, $e);
2626
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Reader\Exception;
6+
7+
interface ReaderExceptionInterface extends \Throwable {}

src/Exception/UnrecognizedReflectionTypeException.php

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,66 @@
22

33
declare(strict_types=1);
44

5-
namespace TypeLang\ReflectionConverter\Exception;
5+
namespace TypeLang\Reader\Exception;
66

7-
class UnrecognizedReflectionTypeException extends ConverterException
7+
class UnrecognizedReflectionTypeException extends ReaderException
88
{
9-
final public const ERROR_CODE_UNSUPPORTED_REFLECTION_TYPE = 0x01 + parent::CODE_LAST;
9+
final public const ERROR_CODE_INVALID_TYPE = 0x01 + parent::CODE_LAST;
1010

11-
protected const CODE_LAST = self::ERROR_CODE_UNSUPPORTED_REFLECTION_TYPE;
11+
final public const ERROR_CODE_INVALID_TYPE_FROM_CONST = 0x02 + parent::CODE_LAST;
12+
13+
final public const ERROR_CODE_INVALID_TYPE_FROM_PROPERTY = 0x03 + parent::CODE_LAST;
14+
15+
final public const ERROR_CODE_INVALID_TYPE_FROM_FUNCTION = 0x04 + parent::CODE_LAST;
16+
17+
final public const ERROR_CODE_INVALID_TYPE_FROM_PARAMETER = 0x05 + parent::CODE_LAST;
18+
19+
protected const CODE_LAST = self::ERROR_CODE_INVALID_TYPE_FROM_PARAMETER;
1220

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

17-
return new static($message, self::ERROR_CODE_UNSUPPORTED_REFLECTION_TYPE);
25+
return new static($message, self::ERROR_CODE_INVALID_TYPE);
26+
}
27+
28+
public static function fromReflectionConstant(\ReflectionType $type, \ReflectionClassConstant $const): static
29+
{
30+
$message = \vsprintf('Unsupported reflection type defined in %s const: %s', [
31+
$const->getName(),
32+
$type::class,
33+
]);
34+
35+
return new static($message, self::ERROR_CODE_INVALID_TYPE_FROM_CONST);
36+
}
37+
38+
public static function fromReflectionProperty(\ReflectionType $type, \ReflectionProperty $property): static
39+
{
40+
$message = \vsprintf('Unsupported reflection type defined in $%s property: %s', [
41+
$property->getName(),
42+
$type::class,
43+
]);
44+
45+
return new static($message, self::ERROR_CODE_INVALID_TYPE_FROM_PROPERTY);
46+
}
47+
48+
public static function fromReflectionFunction(\ReflectionType $type, \ReflectionFunctionAbstract $function): static
49+
{
50+
$message = \vsprintf('Unsupported reflection type defined in %s function: %s', [
51+
$function->getName(),
52+
$type::class,
53+
]);
54+
55+
return new static($message, self::ERROR_CODE_INVALID_TYPE_FROM_FUNCTION);
56+
}
57+
58+
public static function fromReflectionParameter(\ReflectionType $type, \ReflectionParameter $parameter): static
59+
{
60+
$message = \vsprintf('Unsupported reflection type defined in $%s parameter: %s', [
61+
$parameter->getName(),
62+
$type::class,
63+
]);
64+
65+
return new static($message, self::ERROR_CODE_INVALID_TYPE_FROM_PARAMETER);
1866
}
1967
}

src/ReaderInterface.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Reader;
6+
7+
use TypeLang\Parser\Node\Stmt\TypeStatement;
8+
use TypeLang\Reader\Exception\ReaderExceptionInterface;
9+
10+
interface ReaderInterface
11+
{
12+
/**
13+
* Returns a type AST structure based on an {@see ReflectionProperty} object.
14+
*
15+
* @throws ReaderExceptionInterface In case of any reading error occurs.
16+
*/
17+
public function findPropertyType(\ReflectionProperty $property): ?TypeStatement;
18+
19+
/**
20+
* Returns a type AST structure based on an {@see ReflectionFunctionAbstract} object.
21+
*
22+
* @throws ReaderExceptionInterface In case of any reading error occurs.
23+
*/
24+
public function findFunctionType(\ReflectionFunctionAbstract $function): ?TypeStatement;
25+
26+
/**
27+
* Returns a type AST structure based on an {@see ReflectionParameter} object.
28+
*
29+
* @throws ReaderExceptionInterface In case of any reading error occurs.
30+
*/
31+
public function findParameterType(\ReflectionParameter $parameter): ?TypeStatement;
32+
33+
/**
34+
* Returns a type AST structure based on an {@see ReflectionClassConstant} object.
35+
*
36+
* @throws ReaderExceptionInterface In case of any reading error occurs.
37+
*/
38+
public function findConstantType(\ReflectionClassConstant $constant): ?TypeStatement;
39+
}

0 commit comments

Comments
 (0)