Skip to content

Commit ace4950

Browse files
authored
Add support for properties-of and its variations (#267)
* Add support for `properties-of` and its variations * Fix CS
1 parent eab6d97 commit ace4950

File tree

10 files changed

+256
-0
lines changed

10 files changed

+256
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
/**
17+
* Value Object representing the `private-properties-of` type.
18+
*
19+
* @psalm-immutable
20+
*/
21+
final class PrivatePropertiesOf extends PropertiesOf
22+
{
23+
public function __toString(): string
24+
{
25+
return 'private-properties-of<' . $this->type . '>';
26+
}
27+
}

src/PseudoTypes/PropertiesOf.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\Array_;
19+
use phpDocumentor\Reflection\Types\Mixed_;
20+
use phpDocumentor\Reflection\Types\String_;
21+
22+
/**
23+
* Value Object representing the `properties-of` type.
24+
*
25+
* @psalm-immutable
26+
*/
27+
class PropertiesOf extends Array_ implements PseudoType
28+
{
29+
/** @var Type */
30+
protected $type;
31+
32+
public function __construct(Type $type)
33+
{
34+
parent::__construct(new Mixed_(), new String_());
35+
36+
$this->type = $type;
37+
}
38+
39+
public function getType(): Type
40+
{
41+
return $this->type;
42+
}
43+
44+
public function underlyingType(): Type
45+
{
46+
return new Array_(new Mixed_(), new String_());
47+
}
48+
49+
public function __toString(): string
50+
{
51+
return 'properties-of<' . $this->type . '>';
52+
}
53+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
/**
17+
* Value Object representing the `protected-properties-of` type.
18+
*
19+
* @psalm-immutable
20+
*/
21+
final class ProtectedPropertiesOf extends PropertiesOf
22+
{
23+
public function __toString(): string
24+
{
25+
return 'protected-properties-of<' . $this->type . '>';
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
/**
17+
* Value Object representing the `public-properties-of` type.
18+
*
19+
* @psalm-immutable
20+
*/
21+
final class PublicPropertiesOf extends PropertiesOf
22+
{
23+
public function __toString(): string
24+
{
25+
return 'public-properties-of<' . $this->type . '>';
26+
}
27+
}

src/TypeResolver.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
use phpDocumentor\Reflection\PseudoTypes\ObjectShapeItem;
5151
use phpDocumentor\Reflection\PseudoTypes\OffsetAccess;
5252
use phpDocumentor\Reflection\PseudoTypes\PositiveInteger;
53+
use phpDocumentor\Reflection\PseudoTypes\PrivatePropertiesOf;
54+
use phpDocumentor\Reflection\PseudoTypes\PropertiesOf;
55+
use phpDocumentor\Reflection\PseudoTypes\ProtectedPropertiesOf;
56+
use phpDocumentor\Reflection\PseudoTypes\PublicPropertiesOf;
5357
use phpDocumentor\Reflection\PseudoTypes\Scalar;
5458
use phpDocumentor\Reflection\PseudoTypes\StringValue;
5559
use phpDocumentor\Reflection\PseudoTypes\TraitString;
@@ -440,6 +444,18 @@ private function createFromGeneric(GenericTypeNode $type, Context $context): Typ
440444
case 'value-of':
441445
return new ValueOf($this->createType($type->genericTypes[0], $context));
442446

447+
case 'properties-of':
448+
return new PropertiesOf($this->createType($type->genericTypes[0], $context));
449+
450+
case 'public-properties-of':
451+
return new PublicPropertiesOf($this->createType($type->genericTypes[0], $context));
452+
453+
case 'protected-properties-of':
454+
return new ProtectedPropertiesOf($this->createType($type->genericTypes[0], $context));
455+
456+
case 'private-properties-of':
457+
return new PrivatePropertiesOf($this->createType($type->genericTypes[0], $context));
458+
443459
case 'int-mask':
444460
return new IntMask(...$this->createTypesByTypeNodes($type->genericTypes, $context));
445461

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Reflection\PseudoTypes;
6+
7+
use phpDocumentor\Reflection\Types\Self_;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class PrivatePropertiesOfTest extends TestCase
11+
{
12+
public function testToString(): void
13+
{
14+
$type = new PrivatePropertiesOf(new Self_());
15+
16+
$this->assertSame('private-properties-of<self>', (string) $type);
17+
}
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Reflection\PseudoTypes;
6+
7+
use phpDocumentor\Reflection\Types\Array_;
8+
use phpDocumentor\Reflection\Types\Mixed_;
9+
use phpDocumentor\Reflection\Types\Self_;
10+
use phpDocumentor\Reflection\Types\String_;
11+
use PHPUnit\Framework\TestCase;
12+
13+
final class PropertiesOfTest extends TestCase
14+
{
15+
public function testCreate(): void
16+
{
17+
$childType = new Self_();
18+
$type = new PropertiesOf($childType);
19+
20+
$this->assertSame($childType, $type->getType());
21+
$this->assertEquals(new Array_(new Mixed_(), new String_()), $type->underlyingType());
22+
$this->assertEquals(new String_(), $type->getKeyType());
23+
$this->assertEquals(new Mixed_(), $type->getValueType());
24+
}
25+
26+
public function testToString(): void
27+
{
28+
$type = new PropertiesOf(new Self_());
29+
30+
$this->assertSame('properties-of<self>', (string) $type);
31+
}
32+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Reflection\PseudoTypes;
6+
7+
use phpDocumentor\Reflection\Types\Self_;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class ProtectedPropertiesOfTest extends TestCase
11+
{
12+
public function testToString(): void
13+
{
14+
$type = new ProtectedPropertiesOf(new Self_());
15+
16+
$this->assertSame('protected-properties-of<self>', (string) $type);
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Reflection\PseudoTypes;
6+
7+
use phpDocumentor\Reflection\Types\Self_;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class PublicPropertiesOfTest extends TestCase
11+
{
12+
public function testToString(): void
13+
{
14+
$type = new PublicPropertiesOf(new Self_());
15+
16+
$this->assertSame('public-properties-of<self>', (string) $type);
17+
}
18+
}

tests/unit/TypeResolverTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
use phpDocumentor\Reflection\PseudoTypes\ObjectShapeItem;
5151
use phpDocumentor\Reflection\PseudoTypes\OffsetAccess;
5252
use phpDocumentor\Reflection\PseudoTypes\PositiveInteger;
53+
use phpDocumentor\Reflection\PseudoTypes\PrivatePropertiesOf;
54+
use phpDocumentor\Reflection\PseudoTypes\PropertiesOf;
55+
use phpDocumentor\Reflection\PseudoTypes\ProtectedPropertiesOf;
56+
use phpDocumentor\Reflection\PseudoTypes\PublicPropertiesOf;
5357
use phpDocumentor\Reflection\PseudoTypes\Scalar;
5458
use phpDocumentor\Reflection\PseudoTypes\StringValue;
5559
use phpDocumentor\Reflection\PseudoTypes\TraitString;
@@ -1122,6 +1126,22 @@ public function genericsProvider(): array
11221126
'non-empty-list<mixed>',
11231127
new NonEmptyList(new Mixed_()),
11241128
],
1129+
[
1130+
'properties-of<self>',
1131+
new PropertiesOf(new Self_()),
1132+
],
1133+
[
1134+
'public-properties-of<self>',
1135+
new PublicPropertiesOf(new Self_()),
1136+
],
1137+
[
1138+
'protected-properties-of<self>',
1139+
new ProtectedPropertiesOf(new Self_()),
1140+
],
1141+
[
1142+
'private-properties-of<self>',
1143+
new PrivatePropertiesOf(new Self_()),
1144+
],
11251145
];
11261146
}
11271147

0 commit comments

Comments
 (0)