Skip to content

Commit 2503785

Browse files
committed
Add true and false as PseudoTypes
In this change, I have made more allowance for identifying types as PseudoTypes and to ensure they are identified as both their underlying and pseudo-type. Using instanceof, you can identify whether it is the PseudoType, a pseudotype, but also as the underlying type.
1 parent 28517b9 commit 2503785

File tree

7 files changed

+152
-3
lines changed

7 files changed

+152
-3
lines changed

src/PseudoType.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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;
15+
16+
interface PseudoType extends Type
17+
{
18+
public function underlyingType(): Type;
19+
}

src/PseudoTypes/False_.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Reflection\PseudoTypes;
6+
7+
use phpDocumentor\Reflection\PseudoType;
8+
use phpDocumentor\Reflection\Type;
9+
use phpDocumentor\Reflection\Types\Boolean;
10+
11+
final class False_ extends Boolean implements PseudoType
12+
{
13+
public function underlyingType(): Type
14+
{
15+
return new Boolean();
16+
}
17+
18+
public function __toString(): string
19+
{
20+
return 'false';
21+
}
22+
}

src/PseudoTypes/True_.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Reflection\PseudoTypes;
6+
7+
use phpDocumentor\Reflection\PseudoType;
8+
use phpDocumentor\Reflection\Type;
9+
use phpDocumentor\Reflection\Types\Boolean;
10+
11+
final class True_ extends Boolean implements PseudoType
12+
{
13+
public function underlyingType(): Type
14+
{
15+
return new Boolean();
16+
}
17+
18+
public function __toString(): string
19+
{
20+
return 'true';
21+
}
22+
}

src/TypeResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ final class TypeResolver
8282
'scalar' => Types\Scalar::class,
8383
'callback' => Types\Callable_::class,
8484
'callable' => Types\Callable_::class,
85-
'false' => Types\Boolean::class,
86-
'true' => Types\Boolean::class,
85+
'false' => PseudoTypes\False_::class,
86+
'true' => PseudoTypes\False_::class,
8787
'self' => Types\Self_::class,
8888
'$this' => Types\This::class,
8989
'static' => Types\Static_::class,

src/Types/Boolean.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Value Object representing a Boolean type.
2020
*/
21-
final class Boolean implements Type
21+
class Boolean implements Type
2222
{
2323
/**
2424
* Returns a rendered output of the Type as it would be used in a DocBlock.

tests/unit/PseudoTypes/FalseTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\PseudoTypes;
15+
16+
use phpDocumentor\Reflection\Types\Boolean;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* @coversDefaultClass \phpDocumentor\Reflection\Types\False_
21+
*/
22+
class FalseTest extends TestCase
23+
{
24+
/**
25+
* @covers ::underlyingType
26+
*/
27+
public function testExposesUnderlyingType() : void
28+
{
29+
$false = new False_();
30+
31+
$this->assertInstanceOf(Boolean::class, $false->underlyingType());
32+
}
33+
34+
/**
35+
* @covers ::__toString
36+
*/
37+
public function testFalseStringifyCorrectly() : void
38+
{
39+
$false = new False_();
40+
41+
$this->assertSame('false', (string) $false);
42+
}
43+
}

tests/unit/PseudoTypes/TrueTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\PseudoTypes;
15+
16+
use phpDocumentor\Reflection\Types\Boolean;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* @coversDefaultClass \phpDocumentor\Reflection\Types\True_
21+
*/
22+
class TrueTest extends TestCase
23+
{
24+
/**
25+
* @covers ::underlyingType
26+
*/
27+
public function testExposesUnderlyingType() : void
28+
{
29+
$true = new True_();
30+
31+
$this->assertInstanceOf(Boolean::class, $true->underlyingType());
32+
}
33+
34+
/**
35+
* @covers ::__toString
36+
*/
37+
public function testTrueStringifyCorrectly() : void
38+
{
39+
$true = new True_();
40+
41+
$this->assertSame('true', (string) $true);
42+
}
43+
}

0 commit comments

Comments
 (0)