Completed
Push — master ( edf0e8...eb9261 )
by Nikola
03:47
created

ClassUtils::isClassNameValid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 12
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 View Code Duplication
    public static function getNamespace($class)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public static function getShortName($class)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        if ($class instanceof ClassMetadata) {
74
            return $class->getShortName();
75
        }
76
77
        $parts = explode('\\', $class);
78
        return array_pop($parts);
79
    }
80
}
81