Skip to content

Commit 5d16666

Browse files
committed
Add support for async promoted properties
1 parent b0dff81 commit 5d16666

File tree

5 files changed

+66
-6
lines changed

5 files changed

+66
-6
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"homepage": "http://www.phpdoc.org",
66
"license": "MIT",
77
"autoload": {
8+
"files": [
9+
"src/php-parser/Modifiers.php"
10+
],
811
"psr-4": {
912
"phpDocumentor\\": "src/phpDocumentor"
1013
}

src/php-parser/Modifiers.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpParser;
6+
7+
use Composer\InstalledVersions;
8+
9+
use function strpos;
10+
11+
if (strpos(InstalledVersions::getVersion('nikic/php-parser'), '4') === 0) {
12+
/**
13+
* Modifiers used (as a bit mask) by various flags subnodes, for example on classes, functions,
14+
* properties and constants.
15+
*/
16+
final class Modifiers
17+
{
18+
public const PUBLIC = 1;
19+
public const PROTECTED = 2;
20+
public const PRIVATE = 4;
21+
public const STATIC = 8;
22+
public const ABSTRACT = 16;
23+
public const FINAL = 32;
24+
public const READONLY = 64;
25+
public const PUBLIC_SET = 128;
26+
public const PROTECTED_SET = 256;
27+
public const PRIVATE_SET = 512;
28+
29+
public const VISIBILITY_SET_MASK = self::PUBLIC_SET | self::PROTECTED_SET | self::PRIVATE_SET;
30+
}
31+
}

src/phpDocumentor/Reflection/Php/Factory/ConstructorPromotion.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
use phpDocumentor\Reflection\DocBlockFactoryInterface;
99
use phpDocumentor\Reflection\Fqsen;
1010
use phpDocumentor\Reflection\Location;
11+
use phpDocumentor\Reflection\Php\AsyncVisibility;
1112
use phpDocumentor\Reflection\Php\Class_ as ClassElement;
1213
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
1314
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
1415
use phpDocumentor\Reflection\Php\Property;
1516
use phpDocumentor\Reflection\Php\StrategyContainer;
1617
use phpDocumentor\Reflection\Php\Visibility;
18+
use PhpParser\Modifiers;
1719
use PhpParser\Node\Expr\Variable;
1820
use PhpParser\Node\Param;
19-
use PhpParser\Node\Stmt\Class_;
2021
use PhpParser\Node\Stmt\ClassMethod;
2122
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
2223
use Webmozart\Assert\Assert;
@@ -91,11 +92,36 @@ private function promoteParameterToProperty(ContextStack $context, StrategyConta
9192

9293
private function buildPropertyVisibilty(int $flags): Visibility
9394
{
94-
if ((bool) ($flags & Class_::MODIFIER_PRIVATE) === true) {
95+
if ((bool) ($flags & Modifiers::VISIBILITY_SET_MASK) !== false) {
96+
return new AsyncVisibility(
97+
$this->buildReadVisibility($flags),
98+
$this->buildWriteVisibility($flags),
99+
);
100+
}
101+
102+
return $this->buildReadVisibility($flags);
103+
}
104+
105+
private function buildReadVisibility(int $flags): Visibility
106+
{
107+
if ((bool) ($flags & Modifiers::PRIVATE) === true) {
108+
return new Visibility(Visibility::PRIVATE_);
109+
}
110+
111+
if ((bool) ($flags & Modifiers::PROTECTED) === true) {
112+
return new Visibility(Visibility::PROTECTED_);
113+
}
114+
115+
return new Visibility(Visibility::PUBLIC_);
116+
}
117+
118+
private function buildWriteVisibility(int $flags): Visibility
119+
{
120+
if ((bool) ($flags & Modifiers::PRIVATE_SET) === true) {
95121
return new Visibility(Visibility::PRIVATE_);
96122
}
97123

98-
if ((bool) ($flags & Class_::MODIFIER_PROTECTED) === true) {
124+
if ((bool) ($flags & Modifiers::PROTECTED_SET) === true) {
99125
return new Visibility(Visibility::PROTECTED_);
100126
}
101127

@@ -104,6 +130,6 @@ private function buildPropertyVisibilty(int $flags): Visibility
104130

105131
private function readOnly(int $flags): bool
106132
{
107-
return (bool) ($flags & Class_::MODIFIER_READONLY) === true;
133+
return (bool) ($flags & Modifiers::READONLY) === true;
108134
}
109135
}

src/phpDocumentor/Reflection/Types/NamespaceNodeToContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private function classAlikeUses(Namespace_ $namespace): array
5858
static fn (Node $node): bool => (
5959
$node instanceof Use_
6060
|| $node instanceof GroupUse
61-
) && in_array($node->type, [Use_::TYPE_UNKNOWN, Use_::TYPE_NORMAL], true)
61+
) && in_array($node->type, [Use_::TYPE_UNKNOWN, Use_::TYPE_NORMAL], true),
6262
);
6363
}
6464
}

tests/unit/phpDocumentor/Reflection/Middleware/ChainFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testItThrowsAnExceptionIfAnythingOtherThanAMiddlewareIsPassed():
5353

5454
ChainFactory::createExecutionChain(
5555
[$middleware],
56-
static fn (): stdClass => new stdClass()
56+
static fn (): stdClass => new stdClass(),
5757
);
5858
}
5959

0 commit comments

Comments
 (0)