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

ClassUtils   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 72
Duplicated Lines 27.78 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
c 2
b 0
f 0
lcom 0
cbo 2
dl 20
loc 72
ccs 0
cts 54
cp 0
rs 10

6 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() 11 11 2
A getShortName() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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