Skip to content

Commit e19f70a

Browse files
committed
[#26] Display method visibility in compare report
1 parent f71d4cc commit e19f70a

File tree

2 files changed

+63
-18
lines changed

2 files changed

+63
-18
lines changed

src/PHPSemVerChecker/Operation/ClassMethodOperation.php

+6-18
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,21 @@ abstract class ClassMethodOperation extends Operation {
1717

1818
public function getCode()
1919
{
20-
$visiblityMapping = $this->getVisibilityMapping();
21-
return $this->code[$this->context][$visiblityMapping[$this->visibility]];
20+
return $this->code[$this->context][Visibility::get($this->visibility)];
2221
}
2322

2423
public function getLevel()
2524
{
26-
$visiblityMapping = $this->getVisibilityMapping();
27-
return $this->level[$this->context][$visiblityMapping[$this->visibility]];
25+
return $this->level[$this->context][Visibility::get($this->visibility)];
2826
}
2927

30-
protected function getVisibilityMapping()
28+
public function getReason()
3129
{
32-
return [
33-
Class_::MODIFIER_PUBLIC => 0,
34-
Class_::MODIFIER_PROTECTED => 1,
35-
Class_::MODIFIER_PRIVATE => 2,
36-
];
30+
return '[' . Visibility::toString($this->visibility) . '] ' . $this->reason;
3731
}
3832

39-
protected function getVisibility(ClassMethod $classMethod)
33+
protected function getVisibility($context)
4034
{
41-
if ($classMethod->isPublic()) {
42-
return Class_::MODIFIER_PUBLIC;
43-
} elseif ($classMethod->isProtected()) {
44-
return Class_::MODIFIER_PROTECTED;
45-
} else {
46-
return Class_::MODIFIER_PRIVATE;
47-
}
35+
return Visibility::getForContext($context);
4836
}
4937
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace PHPSemVerChecker\Operation;
4+
5+
use PhpParser\Node\Stmt;
6+
use PhpParser\Node\Stmt\Class_;
7+
8+
class Visibility
9+
{
10+
/**
11+
* @return array
12+
*/
13+
public static function getMapping()
14+
{
15+
return [
16+
Class_::MODIFIER_PUBLIC => 0,
17+
Class_::MODIFIER_PROTECTED => 1,
18+
Class_::MODIFIER_PRIVATE => 2,
19+
];
20+
}
21+
22+
/**
23+
* @param $visibility
24+
* @return int
25+
*/
26+
public static function get($visibility)
27+
{
28+
$mapping = self::getMapping();
29+
return $mapping[$visibility];
30+
}
31+
32+
/**
33+
* @param \PhpParser\Node\Stmt $context
34+
* @return int
35+
*/
36+
public static function getForContext(Stmt $context)
37+
{
38+
if ($context->isPublic()) {
39+
return Class_::MODIFIER_PUBLIC;
40+
} elseif ($context->isProtected()) {
41+
return Class_::MODIFIER_PROTECTED;
42+
} else {
43+
return Class_::MODIFIER_PRIVATE;
44+
}
45+
}
46+
47+
public static function toString($visibility)
48+
{
49+
if ($visibility === Class_::MODIFIER_PUBLIC) {
50+
return 'public';
51+
} elseif ($visibility === Class_::MODIFIER_PROTECTED) {
52+
return 'protected';
53+
} else {
54+
return 'private';
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)