Skip to content

Commit 5abbe96

Browse files
committed
Add *Of type
1 parent 92acc26 commit 5abbe96

File tree

9 files changed

+465
-0
lines changed

9 files changed

+465
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
declare(strict_types=1);
12+
13+
namespace phpDocumentor\Reflection\PseudoTypes;
14+
15+
use ArrayIterator;
16+
use function implode;
17+
use IteratorAggregate;
18+
19+
use function array_key_exists;
20+
use phpDocumentor\Reflection\Type;
21+
use phpDocumentor\Reflection\PseudoType;
22+
23+
/**
24+
* Base class for aggregated types like IntMask and IntMaskOf
25+
*
26+
* A Aggregated Type is not so much a special keyword or object reference but is a series of Types that are separated
27+
* using separator.
28+
*
29+
* @psalm-immutable
30+
* @template-implements IteratorAggregate<int, PseudoType>
31+
*/
32+
abstract class AggregatedPseudoType implements PseudoType, IteratorAggregate
33+
{
34+
/**
35+
* @psalm-allow-private-mutation
36+
* @var array<int, Type>
37+
*/
38+
private $types = [];
39+
40+
/** @var string */
41+
private $token;
42+
43+
/**
44+
* @param array<Type> $types
45+
*/
46+
public function __construct(array $types, string $token)
47+
{
48+
foreach ($types as $type) {
49+
$this->add($type);
50+
}
51+
52+
$this->token = $token;
53+
}
54+
55+
/**
56+
* Returns the type at the given index.
57+
*/
58+
public function get(int $index): ?Type
59+
{
60+
if (!$this->has($index)) {
61+
return null;
62+
}
63+
64+
return $this->types[$index];
65+
}
66+
67+
/**
68+
* Tests if this compound type has a type with the given index.
69+
*/
70+
public function has(int $index): bool
71+
{
72+
return array_key_exists($index, $this->types);
73+
}
74+
75+
/**
76+
* Tests if this compound type contains the given type.
77+
*/
78+
public function contains(Type $type): bool
79+
{
80+
foreach ($this->types as $typePart) {
81+
// if the type is duplicate; do not add it
82+
if ((string) $typePart === (string) $type) {
83+
return true;
84+
}
85+
}
86+
87+
return false;
88+
}
89+
90+
/**
91+
* Returns a rendered output of the Type as it would be used in a DocBlock.
92+
*/
93+
public function __toString(): string
94+
{
95+
return implode($this->token, $this->types);
96+
}
97+
98+
/**
99+
* @return ArrayIterator<int, Type>
100+
*/
101+
public function getIterator(): ArrayIterator
102+
{
103+
return new ArrayIterator($this->types);
104+
}
105+
106+
/**
107+
* @psalm-suppress ImpureMethodCall
108+
*/
109+
private function add(Type $type): void
110+
{
111+
if ($type instanceof static) {
112+
foreach ($type->getIterator() as $subType) {
113+
$this->add($subType);
114+
}
115+
116+
return;
117+
}
118+
119+
// if the type is duplicate; do not add it
120+
if ($this->contains($type)) {
121+
return;
122+
}
123+
124+
$this->types[] = $type;
125+
}
126+
}

src/PseudoTypes/IntMask.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Type;
17+
use phpDocumentor\Reflection\Types\Integer;
18+
use phpDocumentor\Reflection\PseudoTypes\AggregatedPseudoType;
19+
20+
/** @psalm-immutable */
21+
final class IntMask extends AggregatedPseudoType
22+
{
23+
public function __construct(array $types)
24+
{
25+
parent::__construct($types, ', ');
26+
}
27+
28+
public function underlyingType(): Type
29+
{
30+
return new Integer();
31+
}
32+
33+
/**
34+
* Returns a rendered output of the Type as it would be used in a DocBlock.
35+
*/
36+
public function __toString(): string
37+
{
38+
return 'int-mask<' . parent::__toString() . '>';
39+
}
40+
}

src/PseudoTypes/IntMaskOf.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Type;
17+
use phpDocumentor\Reflection\Types\Integer;
18+
use phpDocumentor\Reflection\PseudoTypes\AggregatedPseudoType;
19+
20+
/** @psalm-immutable */
21+
final class IntMaskOf extends AggregatedPseudoType
22+
{
23+
public function __construct(array $types)
24+
{
25+
parent::__construct($types, '| ');
26+
}
27+
28+
public function underlyingType(): Type
29+
{
30+
return new Integer();
31+
}
32+
33+
/**
34+
* Returns a rendered output of the Type as it would be used in a DocBlock.
35+
*/
36+
public function __toString(): string
37+
{
38+
return 'int-mask-of<' . parent::__toString() . '>';
39+
}
40+
}

src/PseudoTypes/KeyOf.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
use phpDocumentor\Reflection\Types\Mixed_;
18+
use phpDocumentor\Reflection\PseudoType;
19+
20+
/** @psalm-immutable */
21+
final class KeyOf implements PseudoType
22+
{
23+
/** @var Type */
24+
protected $keyType;
25+
26+
public function __construct(Type $keyType)
27+
{
28+
$this->keyType = $keyType;
29+
}
30+
31+
public function getKeyType(): Type
32+
{
33+
return $this->keyType;
34+
}
35+
36+
public function underlyingType(): Type
37+
{
38+
return new Mixed_();
39+
}
40+
41+
/**
42+
* Returns a rendered output of the Type as it would be used in a DocBlock.
43+
*/
44+
public function __toString(): string
45+
{
46+
return 'key-of<' . $this->keyType . '>';
47+
}
48+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Fqsen;
17+
18+
/** @psalm-immutable */
19+
final class PrivatePropertiesOf extends PropertiesOf
20+
{
21+
/**
22+
* Initializes this representation of a class string with the given Fqsen.
23+
*/
24+
public function __construct(?Fqsen $fqsen = null)
25+
{
26+
parent::__construct($fqsen);
27+
}
28+
29+
/**
30+
* Returns a rendered output of the Type as it would be used in a DocBlock.
31+
*/
32+
public function __toString(): string
33+
{
34+
return 'private-properties-of<' . (string) $this->fqsen . '>';
35+
}
36+
}

src/PseudoTypes/PropertiesOf.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
use phpDocumentor\Reflection\Fqsen;
18+
use phpDocumentor\Reflection\PseudoType;
19+
use phpDocumentor\Reflection\Types\Array_;
20+
21+
/** @psalm-immutable */
22+
class PropertiesOf implements PseudoType
23+
{
24+
/** @var Fqsen|null */
25+
private $fqsen;
26+
27+
/**
28+
* Initializes this representation of a class string with the given Fqsen.
29+
*/
30+
public function __construct(?Fqsen $fqsen = null)
31+
{
32+
$this->fqsen = $fqsen;
33+
}
34+
35+
/**
36+
* Returns the FQSEN associated with this object.
37+
*/
38+
public function getFqsen(): ?Fqsen
39+
{
40+
return $this->fqsen;
41+
}
42+
43+
public function underlyingType(): Type
44+
{
45+
return new Array_();
46+
}
47+
48+
/**
49+
* Returns a rendered output of the Type as it would be used in a DocBlock.
50+
*/
51+
public function __toString(): string
52+
{
53+
return 'properties-of<' . (string) $this->fqsen . '>';
54+
}
55+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Fqsen;
17+
18+
/** @psalm-immutable */
19+
final class ProtectedPropertiesOf extends PropertiesOf
20+
{
21+
/**
22+
* Initializes this representation of a class string with the given Fqsen.
23+
*/
24+
public function __construct(?Fqsen $fqsen = null)
25+
{
26+
parent::__construct($fqsen);
27+
}
28+
29+
/**
30+
* Returns a rendered output of the Type as it would be used in a DocBlock.
31+
*/
32+
public function __toString(): string
33+
{
34+
return 'protected-properties-of<' . (string) $this->fqsen . '>';
35+
}
36+
}

0 commit comments

Comments
 (0)