Skip to content

Commit a5be03f

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

File tree

7 files changed

+183
-0
lines changed

7 files changed

+183
-0
lines changed

src/TypeResolver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ final class TypeResolver
7171
private $keywords = [
7272
'string' => Types\String_::class,
7373
'class-string' => Types\ClassString::class,
74+
'trait-string' => Types\TraitString::class,
7475
'int' => Types\Integer::class,
7576
'integer' => Types\Integer::class,
7677
'bool' => Types\Boolean::class,
@@ -81,12 +82,14 @@ final class TypeResolver
8182
'object' => Object_::class,
8283
'mixed' => Types\Mixed_::class,
8384
'array' => Array_::class,
85+
'array-key' => Types\ArrayKey::class,
8486
'resource' => Types\Resource_::class,
8587
'void' => Types\Void_::class,
8688
'null' => Types\Null_::class,
8789
'scalar' => Types\Scalar::class,
8890
'callback' => Types\Callable_::class,
8991
'callable' => Types\Callable_::class,
92+
'callable-string' => Types\CallableString::class,
9093
'false' => Types\False_::class,
9194
'true' => Types\True_::class,
9295
'self' => Types\Self_::class,

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ public function provideKeywords() : array
697697
return [
698698
['string', Types\String_::class],
699699
['class-string', Types\ClassString::class],
700+
['trait-string', Types\TraitString::class],
700701
['int', Types\Integer::class],
701702
['integer', Types\Integer::class],
702703
['float', Types\Float_::class],
@@ -710,8 +711,10 @@ public function provideKeywords() : array
710711
['resource', Types\Resource_::class],
711712
['null', Types\Null_::class],
712713
['callable', Types\Callable_::class],
714+
['callable-string', Types\CallableString::class],
713715
['callback', Types\Callable_::class],
714716
['array', Array_::class],
717+
['array-key', Types\ArrayKey::class],
715718
['scalar', Types\Scalar::class],
716719
['object', Object_::class],
717720
['mixed', Types\Mixed_::class],

tests/unit/Types/ArrayKeyTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 testCompoundCanBeConstructedAndStringifiedCorrectly() : void
28+
{
29+
$this->assertSame('array-key', (string) (new ArrayKey()));
30+
}
31+
32+
/**
33+
* @uses \phpDocumentor\Reflection\Types\Compound::__construct
34+
* @uses \phpDocumentor\Reflection\Types\Integer
35+
* @uses \phpDocumentor\Reflection\Types\Boolean
36+
*
37+
* @covers ::getIterator
38+
*/
39+
public function testCompoundCanBeIterated() : void
40+
{
41+
$types = [new Integer(), new Boolean()];
42+
43+
foreach (new Compound($types) as $index => $type) {
44+
$this->assertSame($types[$index], $type);
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)