Skip to content

Commit 92acc26

Browse files
committed
Add conditional type
1 parent eee054a commit 92acc26

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\PseudoType;
17+
use phpDocumentor\Reflection\Type;
18+
use phpDocumentor\Reflection\Types\Mixed_;
19+
20+
/**
21+
* Represents a conditional. This is an abstract class for Conditional and ConditionalParameter.
22+
*
23+
* @psalm-immutable
24+
*/
25+
abstract class AbstractConditional implements PseudoType
26+
{
27+
/** @var Type */
28+
private $target;
29+
30+
/** @var Type */
31+
private $if;
32+
33+
/** @var Type */
34+
private $else;
35+
36+
/** @var bool */
37+
private $negated;
38+
39+
public function __construct(Type $target, Type $if, Type $else, bool $negated)
40+
{
41+
$this->target = $target;
42+
$this->if = $if;
43+
$this->else = $else;
44+
$this->negated = $negated;
45+
}
46+
47+
public function getTarget(): Type
48+
{
49+
return $this->target;
50+
}
51+
52+
public function getIf(): Type
53+
{
54+
return $this->if;
55+
}
56+
57+
public function getElse(): Type
58+
{
59+
return $this->else;
60+
}
61+
62+
public function getNegated(): bool
63+
{
64+
return $this->negated;
65+
}
66+
67+
public function underlyingType(): Type
68+
{
69+
return new Mixed_();
70+
}
71+
}

src/PseudoTypes/Conditional.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\Type;
17+
18+
use function sprintf;
19+
20+
/** @psalm-immutable */
21+
final class Conditional extends AbstractConditional
22+
{
23+
/** @var Type */
24+
private $subject;
25+
26+
public function __construct(Type $subject, Type $target, Type $if, Type $else, bool $negated)
27+
{
28+
parent::__construct($target, $if, $else, $negated);
29+
$this->subject = $subject;
30+
}
31+
32+
public function getSubject(): Type
33+
{
34+
return $this->subject;
35+
}
36+
37+
/**
38+
* Returns a rendered output of the Type as it would be used in a DocBlock.
39+
*/
40+
public function __toString(): string
41+
{
42+
return sprintf(
43+
'(%s %s %s ? %s : %s)',
44+
$this->subject,
45+
$this->negated ? 'is not' : 'is',
46+
$this->target,
47+
$this->if,
48+
$this->else
49+
);
50+
}
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\Type;
17+
18+
use function sprintf;
19+
20+
/** @psalm-immutable */
21+
final class ConditionalParameter extends AbstractConditional
22+
{
23+
/** @var string */
24+
private $parameter;
25+
26+
public function __construct(string $parameter, Type $target, Type $if, Type $else, bool $negated)
27+
{
28+
parent::__construct($target, $if, $else, $negated);
29+
$this->parameter = $parameter;
30+
}
31+
32+
public function getParameterName(): string
33+
{
34+
return $this->parameter;
35+
}
36+
37+
/**
38+
* Returns a rendered output of the Type as it would be used in a DocBlock.
39+
*/
40+
public function __toString(): string
41+
{
42+
return sprintf(
43+
'(%s %s %s ? %s : %s)',
44+
$this->parameter,
45+
$this->negated ? 'is not' : 'is',
46+
$this->target,
47+
$this->if,
48+
$this->else
49+
);
50+
}
51+
}

0 commit comments

Comments
 (0)