Skip to content

Commit 1fd0a68

Browse files
author
Kirill Nesmeyanov
committed
Component basic implementation
0 parents  commit 1fd0a68

22 files changed

Lines changed: 757 additions & 0 deletions

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.{yml,yaml,neon}]
15+
indent_size = 2

.gitattributes

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
* text=auto eol=lf
2+
*.pp eol=lf linguist-language=EBNF
3+
*.pp2 eol=lf linguist-language=EBNF
4+
5+
.editorconfig export-ignore
6+
.php-cs-fixer.php export-ignore
7+
.gitattributes export-ignore
8+
.gitignore export-ignore
9+
10+
phpunit.xml export-ignore
11+
psalm.xml export-ignore
12+
rector.php export-ignore
13+
14+
/.github export-ignore
15+
/example export-ignore
16+
/tests export-ignore

.github/workflows/codestyle.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: codestyle
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
psalm:
9+
name: Code Style
10+
runs-on: ${{ matrix.os }}
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
php: [ '8.3' ]
15+
os: [ ubuntu-latest ]
16+
steps:
17+
- name: Set Git To Use LF
18+
run: |
19+
git config --global core.autocrlf false
20+
git config --global core.eol lf
21+
- name: Checkout
22+
uses: actions/checkout@v4
23+
- name: Setup PHP ${{ matrix.php }}
24+
uses: shivammathur/setup-php@v2
25+
with:
26+
php-version: ${{ matrix.php }}
27+
- name: Validate Composer
28+
run: composer validate
29+
- name: Get Composer Cache Directory
30+
id: composer-cache
31+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
32+
- name: Restore Composer Cache
33+
uses: actions/cache@v3
34+
with:
35+
path: ${{ steps.composer-cache.outputs.dir }}
36+
key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
37+
restore-keys: ${{ runner.os }}-${{ matrix.php }}-composer-
38+
- name: Install Dependencies
39+
uses: nick-invision/retry@v2
40+
with:
41+
timeout_minutes: 5
42+
max_attempts: 5
43+
command: composer update --prefer-dist --no-interaction --no-progress
44+
- name: Check Code Style
45+
run: composer phpcs:check

.github/workflows/security.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: security
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: '0 0 * * *'
8+
9+
jobs:
10+
security:
11+
name: Security
12+
runs-on: ${{ matrix.os }}
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
php: [ '8.3' ]
17+
os: [ ubuntu-latest ]
18+
steps:
19+
- name: Set Git To Use LF
20+
run: |
21+
git config --global core.autocrlf false
22+
git config --global core.eol lf
23+
- name: Checkout
24+
uses: actions/checkout@v4
25+
- name: Setup PHP ${{ matrix.php }}
26+
uses: shivammathur/setup-php@v2
27+
with:
28+
php-version: ${{ matrix.php }}
29+
- name: Validate Composer
30+
run: composer validate
31+
- name: Get Composer Cache Directory
32+
id: composer-cache
33+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
34+
- name: Restore Composer Cache
35+
uses: actions/cache@v3
36+
with:
37+
path: ${{ steps.composer-cache.outputs.dir }}
38+
key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
39+
restore-keys: ${{ runner.os }}-${{ matrix.php }}-composer-
40+
- name: Install Dependencies
41+
uses: nick-invision/retry@v2
42+
with:
43+
timeout_minutes: 5
44+
max_attempts: 5
45+
command: composer update --prefer-dist --no-interaction --no-progress
46+
- name: Composer Audit
47+
run: composer audit
48+
- name: Security Advisories
49+
run: composer require --dev roave/security-advisories:dev-latest
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: static-analysis
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
psalm:
9+
name: Psalm
10+
runs-on: ${{ matrix.os }}
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
php: [ '8.3' ]
15+
os: [ ubuntu-latest ]
16+
steps:
17+
- name: Set Git To Use LF
18+
run: |
19+
git config --global core.autocrlf false
20+
git config --global core.eol lf
21+
- name: Checkout
22+
uses: actions/checkout@v4
23+
- name: Setup PHP ${{ matrix.php }}
24+
uses: shivammathur/setup-php@v2
25+
with:
26+
php-version: ${{ matrix.php }}
27+
- name: Validate Composer
28+
run: composer validate
29+
- name: Get Composer Cache Directory
30+
id: composer-cache
31+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
32+
- name: Restore Composer Cache
33+
uses: actions/cache@v3
34+
with:
35+
path: ${{ steps.composer-cache.outputs.dir }}
36+
key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
37+
restore-keys: ${{ runner.os }}-${{ matrix.php }}-composer-
38+
- name: Install Dependencies
39+
uses: nick-invision/retry@v2
40+
with:
41+
timeout_minutes: 5
42+
max_attempts: 5
43+
command: composer update --prefer-dist --no-interaction --no-progress
44+
- name: Static Analysis
45+
run: composer linter:check

.github/workflows/tests.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: '0 0 * * *'
8+
9+
jobs:
10+
tests:
11+
name: Tests (${{matrix.php}}, ${{ matrix.os }}, ${{ matrix.stability }})
12+
runs-on: ${{ matrix.os }}
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
php: [ '8.1', '8.2', '8.3' ]
17+
os: [ ubuntu-latest, macos-latest, windows-latest ]
18+
stability: [ prefer-lowest, prefer-stable ]
19+
steps:
20+
- name: Set Git To Use LF
21+
run: |
22+
git config --global core.autocrlf false
23+
git config --global core.eol lf
24+
- name: Checkout
25+
uses: actions/checkout@v4
26+
- name: Setup PHP ${{ matrix.php }}
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: ${{ matrix.php }}
30+
tools: pecl
31+
ini-values: "memory_limit=-1"
32+
- name: Validate Composer
33+
run: composer validate
34+
- name: Get Composer Cache Directory
35+
id: composer-cache
36+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
37+
- name: Restore Composer Cache
38+
uses: actions/cache@v3
39+
with:
40+
path: ${{ steps.composer-cache.outputs.dir }}
41+
key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
42+
restore-keys: ${{ runner.os }}-${{ matrix.php }}-composer-
43+
- name: Install Dependencies
44+
uses: nick-invision/retry@v2
45+
with:
46+
timeout_minutes: 5
47+
max_attempts: 5
48+
command: composer update --${{ matrix.stability }} --ignore-platform-reqs --prefer-dist --no-interaction --no-progress
49+
- name: Execute Unit Tests
50+
run: composer test:unit
51+
- name: Execute Functional Tests
52+
run: composer test:functional

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# IDE
2+
/.idea/
3+
4+
# Composer
5+
/composer.lock
6+
/vendor/
7+
8+
# Testing
9+
test.php

.php-cs-fixer.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
$files = PhpCsFixer\Finder::create()
4+
->in([__DIR__ . '/src']);
5+
6+
return (new PhpCsFixer\Config())
7+
->setRules([
8+
'@PER-CS2.0' => true,
9+
'@PER-CS2.0:risky' => true,
10+
'strict_param' => true,
11+
'array_syntax' => [
12+
'syntax' => 'short',
13+
],
14+
])
15+
->setCacheFile(__DIR__ . '/vendor/.cache.php-cs-fixer')
16+
->setFinder($files);

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Nesmeyanov Kirill
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<a href="https://github.com/php-type-language" target="_blank">
2+
<picture>
3+
<img align="center" src="https://github.com/php-type-language/.github/blob/master/assets/dark.png?raw=true">
4+
</picture>
5+
</a>
6+
7+
---
8+
9+
<p align="center">
10+
<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>
11+
<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>
12+
<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>
13+
<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>
14+
</p>
15+
<p align="center">
16+
<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>
17+
</p>
18+
19+
Provides a set of methods for converting PHP reflection objects into the
20+
Type Language AST nodes.
21+
22+
Read [documentation pages](https://phpdoc.io) for more information.
23+
24+
## Installation
25+
26+
Type Language Reflection Converter is available as Composer repository and can
27+
be installed using the following command in a root of your project:
28+
29+
```sh
30+
$ composer require type-lang/reflection-converter
31+
```
32+
33+
## Quick Start
34+
35+
```php
36+
// Type contains ReflectionNamedType{ name: "void" }
37+
$function = new \ReflectionFunction(function(): void {});
38+
$type = $function->getReturnType();
39+
40+
$converter = new \TypeLang\ReflectionConverter\Converter();
41+
$node = $converter->convert($type);
42+
43+
var_dump($node);
44+
```
45+
46+
**Expected Output:**
47+
```
48+
TypeLang\Parser\Node\Stmt\NamedTypeNode {
49+
+offset: 0
50+
+name: TypeLang\Parser\Node\Name {
51+
+offset: 0
52+
-parts: array:1 [
53+
0 => TypeLang\Parser\Node\Identifier {
54+
+offset: 0
55+
+value: "void"
56+
}
57+
]
58+
}
59+
+arguments: null
60+
+fields: null
61+
}
62+
```
63+
64+
### Creating From Reflection
65+
66+
```php
67+
$class = new \ReflectionClass(Path\To\Example::class);
68+
69+
// Printer component provided by "type-lang/printer" package.
70+
$printer = new \TypeLang\Printer\PrettyPrinter();
71+
72+
$converter = new \TypeLang\ReflectionConverter\Converter();
73+
74+
// Dump all constants with its types.
75+
foreach ($class->getReflectionConstants() as $constant) {
76+
// Creates type node AST from a constant's type.
77+
if ($type = $converter->convertConstantType($constant)); {
78+
echo 'const ' . $constant->name . ' has type ' . $printer->print($type) . "\n";
79+
}
80+
}
81+
82+
// Dump all properties with its types.
83+
foreach ($class->getProperties() as $property) {
84+
// Creates type node AST from a property's type.
85+
if ($type = $converter->convertPropertyType($property)); {
86+
echo 'property ' . $property->name . ' has type ' . $printer->print($type) . "\n";
87+
}
88+
}
89+
90+
// Dump all methods with its types.
91+
foreach ($class->getMethods() as $method) {
92+
// Creates type node AST from any function's return type.
93+
if ($type = $converter->convertFunctionType($method)); {
94+
echo 'function ' . $method->name . ' has type ' . $printer->print($type) . "\n";
95+
}
96+
97+
// Creates type node AST from a parameter's type.
98+
foreach ($method->getParameters() as $parameter) {
99+
if ($type = $converter->convertParameterType($parameter)); {
100+
echo 'parameter ' . $parameter->name . ' has type ' . $printer->print($type) . "\n";
101+
}
102+
}
103+
}
104+
```

0 commit comments

Comments
 (0)