ClassUtils::isClassNameValid()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 3
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 RunOpenCode\AbstractBuilder\AbstractBuilder;
13
use RunOpenCode\AbstractBuilder\Ast\Metadata\ClassMetadata;
14
use RunOpenCode\AbstractBuilder\Exception\InvalidArgumentException;
15
use RunOpenCode\AbstractBuilder\ReflectiveAbstractBuilder;
16
17
/**
18
 * Class ClassUtils
19
 *
20
 * @package RunOpenCode\AbstractBuilder\Utils
21
 */
22
final class ClassUtils
23
{
24
    private function __construct() { /* noop */ }
25
26
    /**
27
     * Check if class name is valid.
28
     *
29
     * @param string $fqcn
30
     *
31
     * @return bool
32
     */
33 7
    public static function isClassNameValid($fqcn)
34
    {
35 7
        foreach (explode('\\', ltrim($fqcn, '\\')) as $part) {
36
37 7
            if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $part)) {
38 7
                return false;
39
            }
40
        }
41
42 7
        return true;
43
    }
44
45
    /**
46
     * Check if class can have its builder pattern class implemented.
47
     *
48
     * @param ClassMetadata $class
49
     *
50
     * @return bool
51
     */
52 1
    public static function isBuildable(ClassMetadata $class)
53
    {
54 1
        if (!$class->hasPublicMethod('__construct')) {
55 1
            return false;
56
        }
57
58 1
        if (0 === count($class->getPublicMethod('__construct')->getParameters())) {
59 1
            return false;
60
        }
61
62 1
        return true;
63
    }
64
65
    /**
66
     * Check if class is implementing required builder class.
67
     *
68
     * @param ClassMetadata $class
69
     *
70
     * @return bool
71
     */
72 1
    public static function isBuilder(ClassMetadata $class)
73
    {
74 1
        if (null === $class->getParent()) {
75 1
            return false;
76
        }
77
78 1
        $instanceOfAbstractBuilder = function (ClassMetadata $class) use (&$instanceOfAbstractBuilder) {
79
80 1
            if (null === $class->getParent()) {
81 1
                return false;
82
            }
83
84
            if (
85 1
                ReflectiveAbstractBuilder::class === $class->getParent()->getName()
86
                ||
87 1
                AbstractBuilder::class === $class->getParent()->getName()
88
            ) {
89 1
                return true;
90
            }
91
92 1
            return $instanceOfAbstractBuilder($class->getParent());
93 1
        };
94
95 1
        return $instanceOfAbstractBuilder($class);
96
    }
97
98
    /**
99
     * Get namespace of class.
100
     *
101
     * @param $class
102
     *
103
     * @return string|null
104
     *
105
     * @throws \RunOpenCode\AbstractBuilder\Exception\InvalidArgumentException
106
     */
107 2
    public static function getNamespace($class)
108
    {
109 2
        if ($class instanceof ClassMetadata) {
110 1
            $class = $class->getName();
111
        }
112
113 2
        $class = trim($class, '\\');
114
115 2
        if ('' === trim($class)) {
116 1
            throw new InvalidArgumentException(sprintf('Invalid class name "%s".', $class));
117
        }
118
119 1
        $parts = explode('\\', $class);
120
121 1
        if (1 === count($parts)) {
122 1
            return null;
123
        }
124
125 1
        array_pop($parts);
126
127 1
        return implode('\\', $parts);
128
    }
129
130
    /**
131
     * Get short name of class.
132
     *
133
     * @param $class
134
     *
135
     * @return string
136
     */
137 2
    public static function getShortName($class)
138
    {
139 2
        if ($class instanceof ClassMetadata) {
140 1
            return $class->getShortName();
141
        }
142
143 2
        $class = trim($class, '\\');
144
145 2
        if ('' === trim($class)) {
146 1
            throw new InvalidArgumentException(sprintf('Invalid class name "%s".', $class));
147
        }
148
149 1
        $parts = explode('\\', $class);
150
151 1
        return array_pop($parts);
152
    }
153
}
154