Skip to content

Commit 2cfe6a8

Browse files
committed
Initial commit
0 parents  commit 2cfe6a8

File tree

8 files changed

+365
-0
lines changed

8 files changed

+365
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
vendor
3+
composer.lock
4+
build

.travis.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: php
2+
sudo: false
3+
php:
4+
- 7.1
5+
- 7.2
6+
7+
before_install:
8+
- composer self-update
9+
10+
install:
11+
- travis_retry composer install --no-interaction --prefer-source
12+
13+
script:
14+
- vendor/bin/phpstan analyze src/ test/ --level max
15+
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml
16+
17+
after_success:
18+
- travis_retry php vendor/bin/php-coveralls -v

LICENSE

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

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# TypeSignature - Type signature generator for PHP
2+
3+
[![Build Status](https://travis-ci.com/Sevavietl/TypeSignature.svg?branch=master)](https://travis-ci.com/Sevavietl/TypeSignature)
4+
[![Coverage Status](https://coveralls.io/repos/github/Sevavietl/TypeSignature/badge.svg)](https://coveralls.io/github/Sevavietl/TypeSignature)
5+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6+
[![PHPStan](https://img.shields.io/badge/PHPStan-enabled-brightgreen.svg?style=flat)](https://github.com/phpstan/phpstan)
7+
8+
## Features
9+
10+
Supports following PHP [types](http://php.net/manual/en/language.types.intro.php):
11+
12+
- [boolean](http://php.net/manual/en/language.types.boolean.php)
13+
- [integer](http://php.net/manual/en/language.types.integer.php)
14+
- [float](http://php.net/manual/en/language.types.float.php)
15+
- [string](http://php.net/manual/en/language.types.string.php)
16+
- [array](http://php.net/manual/en/language.types.array.php)
17+
18+
```php
19+
\TypeSignature\TypeSignature::array('string'); // => 'string[]'
20+
```
21+
22+
- [object](http://php.net/manual/en/language.types.object.php)
23+
- [callable](http://php.net/manual/en/language.types.callable.php)
24+
- [mixed](http://php.net/manual/en/language.pseudo-types.php#language.types.mixed)
25+
- [number](http://php.net/manual/en/language.pseudo-types.php#language.types.number)
26+
27+
```php
28+
\TypeSignature\TypeSignature::number(); // => 'integer|float|double'
29+
```
30+
31+
- [callback](http://php.net/manual/en/language.pseudo-types.php#language.types.callback)
32+
- [union types](https://ceylon-lang.org/documentation/1.3/tour/types/#union_types)
33+
34+
```php
35+
\TypeSignature\TypeSignature::union(\TypeSignature\TypeSignature::integer(), \TypeSignature\TypeSignature::string()); // => 'integer|string'
36+
```
37+
38+
- [intersection types](https://ceylon-lang.org/documentation/1.3/tour/types/#intersection_types)
39+
40+
```php
41+
\TypeSignature\TypeSignature::intersection(\ArrayAccess::class, \Countable::class); // => 'ArrayAccess&Countable'
42+
```
43+
44+
- [optional types](http://php.net/manual/en/migration71.new-features.php)
45+
46+
```php
47+
\TypeSignature\TypeSignature::optional(TypeSignature::string()); => 'string
48+
```
49+
50+
## TODO
51+
52+
- iterable
53+
- [resource](http://php.net/manual/en/language.types.resource.php)

composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "sevavietl/type-signature",
3+
"description": "Type signature generator for PHP",
4+
"type": "library",
5+
"require": {
6+
"php": ">=7.1"
7+
},
8+
"require-dev": {
9+
"phpunit/phpunit": "^7.3",
10+
"phpstan/phpstan": "^0.10.3",
11+
"php-coveralls/php-coveralls": "^2.1"
12+
},
13+
"autoload": {
14+
"psr-4": {
15+
"TypeSignature\\": "src/"
16+
}
17+
},
18+
"autoload-dev": {
19+
"psr-4": {
20+
"TypeSignature\\Test\\": "test/"
21+
}
22+
},
23+
"license": "MIT",
24+
"authors": [
25+
{
26+
"name": "sevavietl",
27+
"email": "sevavietl@gmail.com"
28+
}
29+
]
30+
}

phpunit.xml.dist

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<filter>
12+
<whitelist processUncoveredFilesFromWhitelist="true">
13+
<directory suffix=".php">./src</directory>
14+
</whitelist>
15+
</filter>
16+
<testsuites>
17+
<testsuite name="unit">
18+
<directory suffix="Test.php">test/</directory>
19+
</testsuite>
20+
</testsuites>
21+
</phpunit>

src/TypeSignature.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
namespace TypeSignature;
4+
5+
final class TypeSignature
6+
{
7+
public static function arguments(string ...$types): string
8+
{
9+
return implode(', ', $types);
10+
}
11+
12+
/**
13+
* @see http://php.net/manual/en/language.types.boolean.php
14+
* @return string
15+
*/
16+
public static function boolean(): string
17+
{
18+
return 'bool';
19+
}
20+
21+
/**
22+
* @see http://php.net/manual/en/language.types.integer.php
23+
* @return string
24+
*/
25+
public static function integer(): string
26+
{
27+
return 'integer';
28+
}
29+
30+
/**
31+
* @see http://php.net/manual/en/language.types.float.php
32+
* @return string
33+
*/
34+
public static function float(): string
35+
{
36+
return self::union('float', 'double');
37+
}
38+
39+
/**
40+
* @see http://php.net/manual/en/language.types.float.php
41+
* @return string
42+
*/
43+
public static function double(): string
44+
{
45+
return self::float();
46+
}
47+
48+
/**
49+
* @see http://php.net/manual/en/language.types.string.php
50+
* @return string
51+
*/
52+
public static function string(): string
53+
{
54+
return 'string';
55+
}
56+
57+
/**
58+
* @see http://php.net/manual/en/language.types.array.php
59+
* @param string $type
60+
* @return string
61+
*/
62+
public static function array(string $type): string
63+
{
64+
if ((bool) preg_match('/(?:\[\])$/', $type)) {
65+
return $type;
66+
}
67+
68+
return "{$type}[]";
69+
}
70+
71+
/**
72+
* @see http://php.net/manual/en/language.types.object.php
73+
* @return string
74+
*/
75+
public static function object(): string
76+
{
77+
return 'object';
78+
}
79+
80+
/**
81+
* @see http://php.net/manual/en/language.types.callable.php
82+
* @return string
83+
*/
84+
public static function callable(): string
85+
{
86+
return 'callable';
87+
}
88+
89+
/**
90+
* @see http://php.net/manual/en/language.pseudo-types.php#language.types.mixed
91+
* @return string
92+
*/
93+
public static function mixed(): string
94+
{
95+
return 'mixed';
96+
}
97+
98+
/**
99+
* @see http://php.net/manual/en/language.pseudo-types.php#language.types.number
100+
* @return string
101+
*/
102+
public static function number(): string
103+
{
104+
return self::union(self::integer(), self::float());
105+
}
106+
107+
/**
108+
* @see http://php.net/manual/en/language.pseudo-types.php#language.types.callback
109+
* @return string
110+
*/
111+
public static function callback(): string
112+
{
113+
return self::callable();
114+
}
115+
116+
public static function union(string ...$types): string
117+
{
118+
return implode('|', $types);
119+
}
120+
121+
public static function intersection(string ...$types): string
122+
{
123+
return implode('&', $types);
124+
}
125+
126+
public static function optional(string $type): string
127+
{
128+
if (0 === strpos($type, '?')) {
129+
return $type;
130+
}
131+
132+
return "?{$type}";
133+
}
134+
}

test/TypeSignatureTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace TypeSignature\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use TypeSignature\TypeSignature;
7+
8+
final class TypeSignatureTest extends TestCase
9+
{
10+
public function testArguments(): void
11+
{
12+
$this->assertSame('string, integer', TypeSignature::arguments('string', 'integer'));
13+
}
14+
15+
public function testBoolean(): void
16+
{
17+
$this->assertSame('bool', TypeSignature::boolean());
18+
}
19+
20+
public function testInteger(): void
21+
{
22+
$this->assertSame('integer', TypeSignature::integer());
23+
}
24+
25+
public function testFloat(): void
26+
{
27+
$this->assertSame('float|double', TypeSignature::float());
28+
}
29+
30+
public function testDouble(): void
31+
{
32+
$this->assertSame('float|double', TypeSignature::double());
33+
}
34+
35+
public function testString(): void
36+
{
37+
$this->assertSame('string', TypeSignature::string());
38+
}
39+
40+
public function testArray(): void
41+
{
42+
$this->assertSame('string[]', TypeSignature::array('string'));
43+
$this->assertSame('string[]', TypeSignature::array(TypeSignature::array('string')));
44+
}
45+
46+
public function testObject(): void
47+
{
48+
$this->assertSame('object', TypeSignature::object());
49+
}
50+
51+
public function testCallable(): void
52+
{
53+
$this->assertSame('callable', TypeSignature::callable());
54+
}
55+
56+
public function testMixed(): void
57+
{
58+
$this->assertSame('mixed', TypeSignature::mixed());
59+
}
60+
61+
public function testNumber(): void
62+
{
63+
$this->assertSame('integer|float|double', TypeSignature::number());
64+
}
65+
66+
public function testCallback(): void
67+
{
68+
$this->assertSame('callable', TypeSignature::callback());
69+
}
70+
71+
public function testUnion(): void
72+
{
73+
$this->assertSame('integer|string', TypeSignature::union(TypeSignature::integer(), TypeSignature::string()));
74+
}
75+
76+
public function testIntersection(): void
77+
{
78+
$this->assertSame('ArrayAccess&Countable', TypeSignature::intersection(\ArrayAccess::class, \Countable::class));
79+
}
80+
81+
public function testOptional(): void
82+
{
83+
$this->assertSame('?string', TypeSignature::optional(TypeSignature::string()));
84+
$this->assertSame('?string', TypeSignature::optional(TypeSignature::optional(TypeSignature::string())));
85+
}
86+
}

0 commit comments

Comments
 (0)