Skip to content

Commit cc9c760

Browse files
author
smoench
committed
support psalm scalar types
1 parent e878a14 commit cc9c760

File tree

7 files changed

+185
-0
lines changed

7 files changed

+185
-0
lines changed

src/TypeResolver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use phpDocumentor\Reflection\Types\Intersection;
2626
use phpDocumentor\Reflection\Types\Iterable_;
2727
use phpDocumentor\Reflection\Types\Nullable;
28+
use phpDocumentor\Reflection\Types\NumericString;
2829
use phpDocumentor\Reflection\Types\Object_;
2930
use phpDocumentor\Reflection\Types\String_;
3031
use RuntimeException;
@@ -71,6 +72,7 @@ final class TypeResolver
7172
private $keywords = [
7273
'string' => Types\String_::class,
7374
'class-string' => Types\ClassString::class,
75+
'trait-string' => Types\TraitString::class,
7476
'int' => Types\Integer::class,
7577
'integer' => Types\Integer::class,
7678
'bool' => Types\Boolean::class,
@@ -81,19 +83,22 @@ final class TypeResolver
8183
'object' => Object_::class,
8284
'mixed' => Types\Mixed_::class,
8385
'array' => Array_::class,
86+
'array-key' => Types\ArrayKey::class,
8487
'resource' => Types\Resource_::class,
8588
'void' => Types\Void_::class,
8689
'null' => Types\Null_::class,
8790
'scalar' => Types\Scalar::class,
8891
'callback' => Types\Callable_::class,
8992
'callable' => Types\Callable_::class,
93+
'callable-string' => Types\CallableString::class,
9094
'false' => Types\False_::class,
9195
'true' => Types\True_::class,
9296
'self' => Types\Self_::class,
9397
'$this' => Types\This::class,
9498
'static' => Types\Static_::class,
9599
'parent' => Types\Parent_::class,
96100
'iterable' => Iterable_::class,
101+
'numeric-string' => NumericString::class,
97102
];
98103

99104
/**

src/Types/ArrayKey.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Reflection\Types;
15+
16+
/**
17+
* Value Object representing a array-key Type.
18+
*
19+
* A array-key Type is the supertype (but not a union) of int and string.
20+
*
21+
* @psalm-immutable
22+
*/
23+
final class ArrayKey extends AggregatedType
24+
{
25+
public function __construct()
26+
{
27+
parent::__construct([new String_(), new Integer()], '|');
28+
}
29+
30+
public function __toString() : string
31+
{
32+
return 'array-key';
33+
}
34+
}

src/Types/CallableString.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Reflection\Types;
15+
16+
use phpDocumentor\Reflection\Type;
17+
18+
/**
19+
* Value Object representing the type 'string'.
20+
*
21+
* @psalm-immutable
22+
*/
23+
final class CallableString implements Type
24+
{
25+
/**
26+
* Returns a rendered output of the Type as it would be used in a DocBlock.
27+
*/
28+
public function __toString() : string
29+
{
30+
return 'callable-string';
31+
}
32+
}

src/Types/NumericString.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Reflection\Types;
15+
16+
use phpDocumentor\Reflection\Type;
17+
18+
/**
19+
* Value Object representing the type 'string'.
20+
*
21+
* @psalm-immutable
22+
*/
23+
final class NumericString implements Type
24+
{
25+
/**
26+
* Returns a rendered output of the Type as it would be used in a DocBlock.
27+
*/
28+
public function __toString() : string
29+
{
30+
return 'numeric-string';
31+
}
32+
}

src/Types/TraitString.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Reflection\Types;
15+
16+
use phpDocumentor\Reflection\Type;
17+
18+
/**
19+
* Value Object representing the type 'string'.
20+
*
21+
* @psalm-immutable
22+
*/
23+
final class TraitString implements Type
24+
{
25+
/**
26+
* Returns a rendered output of the Type as it would be used in a DocBlock.
27+
*/
28+
public function __toString() : string
29+
{
30+
return 'trait-string';
31+
}
32+
}

tests/unit/TypeResolverTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use phpDocumentor\Reflection\Types\Iterable_;
2424
use phpDocumentor\Reflection\Types\Null_;
2525
use phpDocumentor\Reflection\Types\Nullable;
26+
use phpDocumentor\Reflection\Types\NumericString;
2627
use phpDocumentor\Reflection\Types\Object_;
2728
use phpDocumentor\Reflection\Types\String_;
2829
use PHPUnit\Framework\TestCase;
@@ -697,6 +698,7 @@ public function provideKeywords() : array
697698
return [
698699
['string', Types\String_::class],
699700
['class-string', Types\ClassString::class],
701+
['trait-string', Types\TraitString::class],
700702
['int', Types\Integer::class],
701703
['integer', Types\Integer::class],
702704
['float', Types\Float_::class],
@@ -710,8 +712,10 @@ public function provideKeywords() : array
710712
['resource', Types\Resource_::class],
711713
['null', Types\Null_::class],
712714
['callable', Types\Callable_::class],
715+
['callable-string', Types\CallableString::class],
713716
['callback', Types\Callable_::class],
714717
['array', Array_::class],
718+
['array-key', Types\ArrayKey::class],
715719
['scalar', Types\Scalar::class],
716720
['object', Object_::class],
717721
['mixed', Types\Mixed_::class],
@@ -721,6 +725,7 @@ public function provideKeywords() : array
721725
['self', Types\Self_::class],
722726
['parent', Types\Parent_::class],
723727
['iterable', Iterable_::class],
728+
['numeric-string', NumericString::class],
724729
];
725730
}
726731

tests/unit/Types/ArrayKeyTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Reflection\Types;
15+
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* @coversDefaultClass \phpDocumentor\Reflection\Types\ArrayKey
20+
*/
21+
final class ArrayKeyTest extends TestCase
22+
{
23+
/**
24+
* @covers ::__construct
25+
* @covers ::__toString
26+
*/
27+
public function testArrayKeyCanBeConstructedAndStringifiedCorrectly() : void
28+
{
29+
$this->assertSame('array-key', (string) (new ArrayKey()));
30+
}
31+
32+
/**
33+
* @uses ::__construct
34+
*
35+
* @covers ::getIterator
36+
*/
37+
public function testArrayKeyCanBeIterated() : void
38+
{
39+
$types = [String_::class, Integer::class];
40+
41+
foreach (new ArrayKey() as $index => $type) {
42+
$this->assertInstanceOf($types[$index], $type);
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)