Skip to content

Commit

Permalink
support psalm scalar types
Browse files Browse the repository at this point in the history
  • Loading branch information
smoench committed Jul 17, 2020
1 parent e878a14 commit bac872b
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ final class TypeResolver
private $keywords = [
'string' => Types\String_::class,
'class-string' => Types\ClassString::class,
'trait-string' => Types\TraitString::class,
'int' => Types\Integer::class,
'integer' => Types\Integer::class,
'bool' => Types\Boolean::class,
Expand All @@ -81,12 +82,14 @@ final class TypeResolver
'object' => Object_::class,
'mixed' => Types\Mixed_::class,
'array' => Array_::class,
'array-key' => Types\ArrayKey::class,
'resource' => Types\Resource_::class,
'void' => Types\Void_::class,
'null' => Types\Null_::class,
'scalar' => Types\Scalar::class,
'callback' => Types\Callable_::class,
'callable' => Types\Callable_::class,
'callable-string' => Types\CallableString::class,
'false' => Types\False_::class,
'true' => Types\True_::class,
'self' => Types\Self_::class,
Expand Down
34 changes: 34 additions & 0 deletions src/Types/ArrayKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/

namespace phpDocumentor\Reflection\Types;

/**
* Value Object representing a array-key Type.
*
* A array-key Type is the supertype (but not a union) of int and string.
*
* @psalm-immutable
*/
final class ArrayKey extends AggregatedType
{
public function __construct()
{
parent::__construct([new String_(), new Integer()], '|');
}

public function __toString() : string
{
return 'array-key';
}
}
32 changes: 32 additions & 0 deletions src/Types/CallableString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/

namespace phpDocumentor\Reflection\Types;

use phpDocumentor\Reflection\Type;

/**
* Value Object representing the type 'string'.
*
* @psalm-immutable
*/
final class CallableString implements Type
{
/**
* Returns a rendered output of the Type as it would be used in a DocBlock.
*/
public function __toString() : string
{
return 'callable-string';
}
}
32 changes: 32 additions & 0 deletions src/Types/NumericString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/

namespace phpDocumentor\Reflection\Types;

use phpDocumentor\Reflection\Type;

/**
* Value Object representing the type 'string'.
*
* @psalm-immutable
*/
final class NumericString implements Type
{
/**
* Returns a rendered output of the Type as it would be used in a DocBlock.
*/
public function __toString() : string
{
return 'numeric-string';
}
}
32 changes: 32 additions & 0 deletions src/Types/TraitString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/

namespace phpDocumentor\Reflection\Types;

use phpDocumentor\Reflection\Type;

/**
* Value Object representing the type 'string'.
*
* @psalm-immutable
*/
final class TraitString implements Type
{
/**
* Returns a rendered output of the Type as it would be used in a DocBlock.
*/
public function __toString() : string
{
return 'trait-string';
}
}
3 changes: 3 additions & 0 deletions tests/unit/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ public function provideKeywords() : array
return [
['string', Types\String_::class],
['class-string', Types\ClassString::class],
['trait-string', Types\TraitString::class],
['int', Types\Integer::class],
['integer', Types\Integer::class],
['float', Types\Float_::class],
Expand All @@ -710,8 +711,10 @@ public function provideKeywords() : array
['resource', Types\Resource_::class],
['null', Types\Null_::class],
['callable', Types\Callable_::class],
['callable-string', Types\CallableString::class],
['callback', Types\Callable_::class],
['array', Array_::class],
['array-key', Types\ArrayKey::class],
['scalar', Types\Scalar::class],
['object', Object_::class],
['mixed', Types\Mixed_::class],
Expand Down
45 changes: 45 additions & 0 deletions tests/unit/Types/ArrayKeyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/

namespace phpDocumentor\Reflection\Types;

use PHPUnit\Framework\TestCase;

/**
* @coversDefaultClass \phpDocumentor\Reflection\Types\ArrayKey
*/
final class ArrayKeyTest extends TestCase
{
/**
* @covers ::__construct
* @covers ::__toString
*/
public function testArrayKeyCanBeConstructedAndStringifiedCorrectly() : void
{
$this->assertSame('array-key', (string) (new ArrayKey()));
}

/**
* @uses ::__construct
*
* @covers ::getIterator
*/
public function testArrayKeyCanBeIterated() : void
{
$types = [String_::class, Integer::class];

foreach (new ArrayKey() as $index => $type) {
$this->assertInstanceOf($types[$index], $type);
}
}
}

0 comments on commit bac872b

Please sign in to comment.