ClassMetadataHelper   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 72.97%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 32
c 2
b 0
f 0
dl 0
loc 95
ccs 27
cts 37
cp 0.7297
rs 10
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A generateClassnameShort() 0 14 3
A generateNamespace() 0 19 4
A generateClassname() 0 7 2
A shouldAddGeneralNamespace() 0 23 6
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 *  This file is part of the Micro framework package.
7
 *
8
 *  (c) Stanislau Komar <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Micro\Library\DTO\Helper;
15
16
use Micro\Library\DTO\Object\AbstractDto;
17
18
class ClassMetadataHelper implements ClassMetadataHelperInterface
19
{
20
    /**
21
     * @param string $namespaceGeneral
22
     * @param string $classSuffix
23
     */
24 6
    public function __construct(
25
        private string $namespaceGeneral,
26
        private string $classSuffix
27
    ) {
28 6
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33 3
    public function generateNamespace(string $className): string
34
    {
35 3
        $exploded = explode('\\', $className);
36 3
        if (1 === \count($exploded)) {
37 3
            return $this->namespaceGeneral;
38
        }
39
40 1
        $namespaceExploded = [];
41 1
        $shouldAddGeneralNamespace = $this->shouldAddGeneralNamespace($className);
42
43 1
        if ($shouldAddGeneralNamespace && $this->namespaceGeneral) {
44 1
            $namespaceExploded[] = $this->namespaceGeneral;
45
        }
46
47 1
        $namespaceExploded = array_merge($namespaceExploded, $exploded);
48
49 1
        array_pop($namespaceExploded);
50
51 1
        return implode('\\', $namespaceExploded);
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57 1
    public function generateClassname(string $className): string
58
    {
59 1
        if (!$this->shouldAddGeneralNamespace($className)) {
60
            return $className;
61
        }
62
63 1
        return $this->generateNamespace($className).'\\'.$this->generateClassnameShort($className);
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69 3
    public function generateClassnameShort(string $className): string
70
    {
71 3
        $shouldAddSuffix = $this->shouldAddGeneralNamespace($className);
72 3
        $exploded = explode('\\', $className);
73
74 3
        if (!$shouldAddSuffix) {
75
            return array_pop($exploded);
76
        }
77
78 3
        if (1 === \count($exploded)) {
79 3
            return $className.ucfirst($this->classSuffix);
80
        }
81
82 1
        return array_pop($exploded).ucfirst($this->classSuffix);
83
    }
84
85
    /**
86
     * @param string $classname
87
     *
88
     * @return bool
89
     */
90 3
    protected function shouldAddGeneralNamespace(string $classname): bool
91
    {
92 3
        if (self::PROPERTY_TYPE_ABSTRACT === mb_strtolower($classname)) {
93
            return false;
94
        }
95
96 3
        if (!class_exists($classname)) {
97 3
            return true;
98
        }
99
100
        if (str_starts_with($classname, $this->namespaceGeneral)) {
101
            return false;
102
        }
103
104
        if (is_a($classname, AbstractDto::class, true)) {
105
            return false;
106
        }
107
108
        if (is_a($classname, \DateTimeInterface::class, true)) {
109
            return false;
110
        }
111
112
        return true;
113
    }
114
}
115