Skip to content

Commit e4e32ef

Browse files
committed
Extractor: maintains exact visibility
1 parent 010e108 commit e4e32ef

9 files changed

+48
-52
lines changed

src/PhpGenerator/Extractor.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,7 @@ private function addPropertyToClass(ClassLike $class, Node\Stmt\Property $node):
311311
foreach ($node->props as $item) {
312312
$prop = $class->addProperty($item->name->toString());
313313
$prop->setStatic($node->isStatic());
314-
if ($node->isPrivate()) {
315-
$prop->setPrivate();
316-
} elseif ($node->isProtected()) {
317-
$prop->setProtected();
318-
}
319-
314+
$prop->setVisibility($this->toVisibility($node->flags));
320315
$prop->setType($node->type ? $this->toPhp($node->type) : null);
321316
if ($item->default) {
322317
$prop->setValue(new Literal($this->getReformattedContents([$item->default], 1)));
@@ -334,12 +329,7 @@ private function addMethodToClass(ClassLike $class, Node\Stmt\ClassMethod $node)
334329
$method->setAbstract($node->isAbstract());
335330
$method->setFinal($node->isFinal());
336331
$method->setStatic($node->isStatic());
337-
if ($node->isPrivate()) {
338-
$method->setPrivate();
339-
} elseif ($node->isProtected()) {
340-
$method->setProtected();
341-
}
342-
332+
$method->setVisibility($this->toVisibility($node->flags));
343333
$this->setupFunction($method, $node);
344334
}
345335

@@ -349,12 +339,7 @@ private function addConstantToClass(ClassLike $class, Node\Stmt\ClassConst $node
349339
foreach ($node->consts as $item) {
350340
$value = $this->getReformattedContents([$item->value], 1);
351341
$const = $class->addConstant($item->name->toString(), new Literal($value));
352-
if ($node->isPrivate()) {
353-
$const->setPrivate();
354-
} elseif ($node->isProtected()) {
355-
$const->setProtected();
356-
}
357-
342+
$const->setVisibility($this->toVisibility($node->flags));
358343
$const->setFinal(method_exists($node, 'isFinal') && $node->isFinal());
359344
$this->addCommentAndAttributes($const, $node);
360345
}
@@ -423,6 +408,17 @@ private function setupFunction(GlobalFunction|Method $function, Node\FunctionLik
423408
}
424409

425410

411+
private function toVisibility(int $flags): ?string
412+
{
413+
return match (true) {
414+
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PUBLIC) => ClassType::VisibilityPublic,
415+
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PROTECTED) => ClassType::VisibilityProtected,
416+
(bool) ($flags & Node\Stmt\Class_::MODIFIER_PRIVATE) => ClassType::VisibilityPrivate,
417+
default => null,
418+
};
419+
}
420+
421+
426422
private function toPhp(mixed $value): string
427423
{
428424
return $this->printer->prettyPrint([$value]);

tests/PhpGenerator/ClassType.fromCode.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Assert::match(<<<'XX'
1717
*/
1818
interface Interface1
1919
{
20-
function func1();
20+
public function func1();
2121
}
2222
XX, (string) $class);
2323

tests/PhpGenerator/Factory.fromClassCode.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ Assert::match(<<<'XX'
2020
*/
2121
interface Interface1
2222
{
23-
function func1();
23+
public function func1();
2424
}
2525
XX, (string) $class);

tests/PhpGenerator/expected/Extractor.bodies.expect

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,32 @@ use const BAR;
1010

1111
abstract class Class7
1212
{
13-
abstract public function abstractFun();
13+
abstract function abstractFun();
1414

1515

16-
public function emptyFun()
16+
function emptyFun()
1717
{
1818
}
1919

2020

21-
public function emptyFun2()
21+
function emptyFun2()
2222
{
2323
}
2424

2525

26-
public function simple()
26+
function simple()
2727
{
2828
return 1;
2929
}
3030

3131

32-
public function simple2()
32+
function simple2()
3333
{
3434
return 1;
3535
}
3636

3737

38-
public function long()
38+
function long()
3939
{
4040
if ($member instanceof Method) {
4141
$s = [1, 2, 3];
@@ -47,7 +47,7 @@ abstract class Class7
4747
}
4848

4949

50-
public function resolving($a = a\FOO, self $b = null, $c = self::FOO)
50+
function resolving($a = a\FOO, self $b = null, $c = self::FOO)
5151
{
5252
echo FOO;
5353
echo \FOO;
@@ -67,7 +67,7 @@ abstract class Class7
6767
}
6868

6969

70-
public function complex()
70+
function complex()
7171
{
7272
echo 1;
7373
// single line comment

tests/PhpGenerator/expected/Extractor.bodies.resolving.expect

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,32 @@ use Abc\a\func;
55

66
abstract class Class7
77
{
8-
abstract public function abstractFun();
8+
abstract function abstractFun();
99

1010

11-
public function emptyFun()
11+
function emptyFun()
1212
{
1313
}
1414

1515

16-
public function emptyFun2()
16+
function emptyFun2()
1717
{
1818
}
1919

2020

21-
public function simple()
21+
function simple()
2222
{
2323
return 1;
2424
}
2525

2626

27-
public function simple2()
27+
function simple2()
2828
{
2929
return 1;
3030
}
3131

3232

33-
public function long()
33+
function long()
3434
{
3535
if ($member instanceof \Abc\Method) {
3636
$s = [1, 2, 3];
@@ -42,7 +42,7 @@ abstract class Class7
4242
}
4343

4444

45-
public function resolving($a = \Abc\a\FOO, self $b = null, $c = self::FOO)
45+
function resolving($a = \Abc\a\FOO, self $b = null, $c = self::FOO)
4646
{
4747
echo FOO;
4848
echo \FOO;
@@ -62,7 +62,7 @@ abstract class Class7
6262
}
6363

6464

65-
public function complex()
65+
function complex()
6666
{
6767
echo 1;
6868
// single line comment

tests/PhpGenerator/expected/Extractor.bodies.unresolving.expect

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,32 @@ use Abc\a\func;
55

66
abstract class Class7
77
{
8-
abstract public function abstractFun();
8+
abstract function abstractFun();
99

1010

11-
public function emptyFun()
11+
function emptyFun()
1212
{
1313
}
1414

1515

16-
public function emptyFun2()
16+
function emptyFun2()
1717
{
1818
}
1919

2020

21-
public function simple()
21+
function simple()
2222
{
2323
return 1;
2424
}
2525

2626

27-
public function simple2()
27+
function simple2()
2828
{
2929
return 1;
3030
}
3131

3232

33-
public function long()
33+
function long()
3434
{
3535
if ($member instanceof \Abc\Method) {
3636
$s = [1, 2, 3];
@@ -42,7 +42,7 @@ abstract class Class7
4242
}
4343

4444

45-
public function resolving($a = \Abc\a\FOO, self $b = null, $c = self::FOO)
45+
function resolving($a = \Abc\a\FOO, self $b = null, $c = self::FOO)
4646
{
4747
echo FOO;
4848
echo \FOO;
@@ -62,7 +62,7 @@ abstract class Class7
6262
}
6363

6464

65-
public function complex()
65+
function complex()
6666
{
6767
echo 1;
6868
// single line comment

tests/PhpGenerator/expected/Extractor.classes.80.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Class9
2121
/** Commented */
2222
#[ExampleAttribute]
2323
#[WithArguments(true)]
24-
public const FOO = 123;
24+
const FOO = 123;
2525

2626
/** @var resource */
2727
#[ExampleAttribute]
@@ -42,7 +42,7 @@ class Class10
4242
public string|int $prop;
4343

4444

45-
public function test(mixed $param): string|int
45+
function test(mixed $param): string|int
4646
{
4747
}
4848
}

tests/PhpGenerator/expected/Extractor.classes.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Abc;
1414
*/
1515
interface Interface1
1616
{
17-
function func1();
17+
public function func1();
1818
}
1919

2020
interface Interface2
@@ -82,7 +82,7 @@ class Class3
8282

8383
class Class4
8484
{
85-
public const THE_CONSTANT = 9;
85+
const THE_CONSTANT = 9;
8686
}
8787

8888
class Class5

tests/PhpGenerator/expected/Extractor.enum.expect

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace Abc;
1010
#[\ExampleAttribute]
1111
enum Enum1
1212
{
13-
public const FOO = 123;
14-
public const BAR = self::Clubs;
13+
const FOO = 123;
14+
const BAR = self::Clubs;
1515

1616
/** Commented */
1717
case Clubs;
@@ -31,14 +31,14 @@ enum Enum2: string implements \Countable
3131
case GET = 'get';
3232
case POST = 'post';
3333

34-
public function count(): int
34+
function count(): int
3535
{
3636
}
3737
}
3838

3939
enum Enum3: int
4040
{
41-
public const FOO = 123;
41+
const FOO = 123;
4242

4343
case A = self::FOO;
4444
case B = 20 + 5;

0 commit comments

Comments
 (0)