Completed
Push — master ( b1e8a1...edf0e8 )
by Nikola
03:49
created

ClassUtils   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
lcom 0
cbo 2
dl 0
loc 62
ccs 0
cts 46
cp 0
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A isClassNameValid() 0 11 3
A isBuildable() 0 12 3
B isBuilder() 0 20 5
A getNamespace() 0 11 2
1
<?php
2
3
namespace RunOpenCode\AbstractBuilder\Utils;
4
5
use RunOpenCode\AbstractBuilder\AbstractBuilder;
6
use RunOpenCode\AbstractBuilder\Ast\Metadata\ClassMetadata;
7
use RunOpenCode\AbstractBuilder\ReflectiveAbstractBuilder;
8
9
final class ClassUtils
10
{
11
    private function __construct() { /* noop */ }
12
13
    public static function isClassNameValid($fqcn)
14
    {
15
        foreach (explode('\\', ltrim($fqcn, '\\')) as $part) {
16
17
            if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $part)) {
18
                return false;
19
            }
20
        }
21
22
        return true;
23
    }
24
25
    public static function isBuildable(ClassMetadata $class)
26
    {
27
        if (!$class->hasPublicMethod('__construct')) {
28
            return false;
29
        }
30
31
        if (0 === count($class->getPublicMethod('__construct')->getParameters())) {
32
            return false;
33
        }
34
35
        return true;
36
    }
37
38
    public static function isBuilder(ClassMetadata $class)
39
    {
40
        if (null === $class->getParent()) {
41
            return false;
42
        }
43
44
        if (
45
            null !== $class->getParent()
46
            &&
47
            (
48
                ReflectiveAbstractBuilder::class === $class->getParent()->getName()
49
                ||
50
                AbstractBuilder::class === $class->getParent()->getName()
51
            )
52
        ) {
53
            return true;
54
        }
55
56
        return self::isBuilder($class->getParent());
57
    }
58
59
    public static function getNamespace($class)
60
    {
61
        if ($class instanceof ClassMetadata) {
62
            $class = $class->getName();
63
        }
64
65
        $parts = explode('\\', $class);
66
        array_pop($parts);
67
68
        return implode('\\', $parts);
69
    }
70
}
71