MethodUtils::getVisibility()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 8
nop 2
crap 5
1
<?php
2
/*
3
 * This file is part of the Abstract builder package, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\AbstractBuilder\Utils;
11
12
use PhpParser\Node\Stmt\Class_;
13
use PhpParser\Node\Stmt\ClassMethod;
14
use RunOpenCode\AbstractBuilder\Ast\Metadata\MethodMetadata;
15
16
/**
17
 * Class MethodUtils
18
 *
19
 * @package RunOpenCode\AbstractBuilder\Utils
20
 */
21
final class MethodUtils
22
{
23
    private function __construct() { /* noop */ }
24
25
    /**
26
     * Get method visibility from AST definition of method
27
     *
28
     * @param int|ClassMethod $value
29
     * @param string $default
30
     *
31
     * @return string
32
     */
33 5
    public static function getVisibility($value, $default = MethodMetadata::PUBLIC)
34
    {
35 5
        if ($value instanceof ClassMethod) {
36 1
            $value = $value->flags;
37
        }
38
39 5
        if (($value & Class_::MODIFIER_PUBLIC) !== 0) {
40 1
            return MethodMetadata::PUBLIC;
41
        }
42
43 4
        if (($value & Class_::MODIFIER_PROTECTED) !== 0) {
44 1
            return MethodMetadata::PROTECTED;
45
        }
46
47 3
        if (($value & Class_::MODIFIER_PRIVATE) !== 0) {
48 1
            return MethodMetadata::PRIVATE;
49
        }
50
51 2
        return $default;
52
    }
53
}
54