Skip to content

Commit b1ef941

Browse files
committed
Extractor: maintains exact visibility
1 parent 23462f6 commit b1ef941

8 files changed

+39
-41
lines changed

src/PhpGenerator/Extractor.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,7 @@ private function addPropertyToClass(ClassType $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(ClassType $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(ClassType $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
}
@@ -421,6 +406,19 @@ private function setupFunction($function, Node\FunctionLike $node): void
421406
}
422407

423408

409+
private function toVisibility(int $flags): ?string
410+
{
411+
if ($flags & Node\Stmt\Class_::MODIFIER_PUBLIC) {
412+
return ClassType::VisibilityPublic;
413+
} elseif ($flags & Node\Stmt\Class_::MODIFIER_PROTECTED) {
414+
return ClassType::VisibilityProtected;
415+
} elseif ($flags & Node\Stmt\Class_::MODIFIER_PRIVATE) {
416+
return ClassType::VisibilityPrivate;
417+
}
418+
return null;
419+
}
420+
421+
424422
private function toPhp($value): string
425423
{
426424
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
2323
, (string) $class);

tests/PhpGenerator/Factory.fromClassCode.phpt

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

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ 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
}

0 commit comments

Comments
 (0)