Skip to content

Commit 696c4b1

Browse files
authored
Merge pull request #225 from mspirkov/add-type-node-to-mixed
Add support for conditional types, offset access types and `key-of`, `value-of`, `int-mask`, `int-mask-of`
2 parents 09f6c69 + 4abe9a0 commit 696c4b1

18 files changed

+924
-2
lines changed

src/PseudoTypes/Conditional.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/*
3+
* This file is part of phpDocumentor.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @link http://phpdoc.org
9+
*
10+
*/
11+
12+
declare(strict_types=1);
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+
use function sprintf;
21+
22+
/**
23+
* Value Object representing the conditional type.
24+
*
25+
* @psalm-immutable
26+
*/
27+
final class Conditional extends Mixed_ implements PseudoType
28+
{
29+
/** @var bool */
30+
private $negated;
31+
/** @var Type */
32+
private $subjectType;
33+
/** @var Type */
34+
private $targetType;
35+
/** @var Type */
36+
private $if;
37+
/** @var Type */
38+
private $else;
39+
40+
public function __construct(bool $negated, Type $subjectType, Type $targetType, Type $if, Type $else)
41+
{
42+
$this->negated = $negated;
43+
$this->subjectType = $subjectType;
44+
$this->targetType = $targetType;
45+
$this->if = $if;
46+
$this->else = $else;
47+
}
48+
49+
public function isNegated(): bool
50+
{
51+
return $this->negated;
52+
}
53+
54+
public function getSubjectType(): Type
55+
{
56+
return $this->subjectType;
57+
}
58+
59+
public function getTargetType(): Type
60+
{
61+
return $this->targetType;
62+
}
63+
64+
public function getIf(): Type
65+
{
66+
return $this->if;
67+
}
68+
69+
public function getElse(): Type
70+
{
71+
return $this->else;
72+
}
73+
74+
public function underlyingType(): Type
75+
{
76+
return new Mixed_();
77+
}
78+
79+
public function __toString(): string
80+
{
81+
return sprintf(
82+
'(%s %s %s ? %s : %s)',
83+
(string) $this->subjectType,
84+
$this->negated ? 'is not' : 'is',
85+
(string) $this->targetType,
86+
(string) $this->if,
87+
(string) $this->else
88+
);
89+
}
90+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/*
3+
* This file is part of phpDocumentor.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @link http://phpdoc.org
9+
*
10+
*/
11+
12+
declare(strict_types=1);
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+
use function sprintf;
21+
22+
/**
23+
* Value Object representing the conditional type for parameter.
24+
*
25+
* @psalm-immutable
26+
*/
27+
final class ConditionalForParameter extends Mixed_ implements PseudoType
28+
{
29+
/** @var bool */
30+
private $negated;
31+
/** @var string */
32+
private $parameterName;
33+
/** @var Type */
34+
private $targetType;
35+
/** @var Type */
36+
private $if;
37+
/** @var Type */
38+
private $else;
39+
40+
public function __construct(bool $negated, string $parameterName, Type $targetType, Type $if, Type $else)
41+
{
42+
$this->negated = $negated;
43+
$this->parameterName = $parameterName;
44+
$this->targetType = $targetType;
45+
$this->if = $if;
46+
$this->else = $else;
47+
}
48+
49+
public function isNegated(): bool
50+
{
51+
return $this->negated;
52+
}
53+
54+
public function getParameterName(): string
55+
{
56+
return $this->parameterName;
57+
}
58+
59+
public function getTargetType(): Type
60+
{
61+
return $this->targetType;
62+
}
63+
64+
public function getIf(): Type
65+
{
66+
return $this->if;
67+
}
68+
69+
public function getElse(): Type
70+
{
71+
return $this->else;
72+
}
73+
74+
public function underlyingType(): Type
75+
{
76+
return new Mixed_();
77+
}
78+
79+
public function __toString(): string
80+
{
81+
return sprintf(
82+
'(%s %s %s ? %s : %s)',
83+
'$' . $this->parameterName,
84+
$this->negated ? 'is not' : 'is',
85+
(string) $this->targetType,
86+
(string) $this->if,
87+
(string) $this->else
88+
);
89+
}
90+
}

src/PseudoTypes/IntMask.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/*
3+
* This file is part of phpDocumentor.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @link http://phpdoc.org
9+
*
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace phpDocumentor\Reflection\PseudoTypes;
15+
16+
use phpDocumentor\Reflection\PseudoType;
17+
use phpDocumentor\Reflection\Type;
18+
use phpDocumentor\Reflection\Types\Integer;
19+
20+
use function implode;
21+
22+
/**
23+
* Value Object representing the `int-mask` type.
24+
*
25+
* @psalm-immutable
26+
*/
27+
final class IntMask extends Integer implements PseudoType
28+
{
29+
/** @var Type[] */
30+
private $types;
31+
32+
public function __construct(Type ...$types)
33+
{
34+
$this->types = $types;
35+
}
36+
37+
/**
38+
* @return Type[]
39+
*/
40+
public function getTypes(): array
41+
{
42+
return $this->types;
43+
}
44+
45+
public function underlyingType(): Type
46+
{
47+
return new Integer();
48+
}
49+
50+
public function __toString(): string
51+
{
52+
return 'int-mask<' . implode(', ', $this->types) . '>';
53+
}
54+
}

src/PseudoTypes/IntMaskOf.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/*
3+
* This file is part of phpDocumentor.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @link http://phpdoc.org
9+
*
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace phpDocumentor\Reflection\PseudoTypes;
15+
16+
use phpDocumentor\Reflection\PseudoType;
17+
use phpDocumentor\Reflection\Type;
18+
use phpDocumentor\Reflection\Types\Integer;
19+
20+
/**
21+
* Value Object representing the `int-mask-of` type.
22+
*
23+
* @psalm-immutable
24+
*/
25+
final class IntMaskOf extends Integer implements PseudoType
26+
{
27+
/** @var Type */
28+
private $type;
29+
30+
public function __construct(Type $type)
31+
{
32+
$this->type = $type;
33+
}
34+
35+
public function getType(): Type
36+
{
37+
return $this->type;
38+
}
39+
40+
public function underlyingType(): Type
41+
{
42+
return new Integer();
43+
}
44+
45+
public function __toString(): string
46+
{
47+
return 'int-mask-of<' . $this->type . '>';
48+
}
49+
}

src/PseudoTypes/KeyOf.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/*
3+
* This file is part of phpDocumentor.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @link http://phpdoc.org
9+
*
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace phpDocumentor\Reflection\PseudoTypes;
15+
16+
use phpDocumentor\Reflection\PseudoType;
17+
use phpDocumentor\Reflection\Type;
18+
use phpDocumentor\Reflection\Types\ArrayKey;
19+
20+
/**
21+
* Value Object representing the `key-of` type.
22+
*
23+
* @psalm-immutable
24+
*/
25+
final class KeyOf extends ArrayKey implements PseudoType
26+
{
27+
/** @var Type */
28+
private $type;
29+
30+
public function __construct(Type $type)
31+
{
32+
$this->type = $type;
33+
}
34+
35+
public function getType(): Type
36+
{
37+
return $this->type;
38+
}
39+
40+
public function underlyingType(): Type
41+
{
42+
return new ArrayKey();
43+
}
44+
45+
public function __toString(): string
46+
{
47+
return 'key-of<' . $this->type . '>';
48+
}
49+
}

0 commit comments

Comments
 (0)