Completed
Push — master ( 3be072...ba2d3a )
by Marco
231:32 queued 209:55
created

ClassNameInflector   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 60
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManager\Inflector;
6
7
use ProxyManager\Inflector\Util\ParameterHasher;
8
use function ltrim;
9
use function strlen;
10
use function strrpos;
11
use function substr;
12
13
/**
14
 * {@inheritDoc}
15
 */
16
final class ClassNameInflector implements ClassNameInflectorInterface
17
{
18
    protected string $proxyNamespace;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
19
    /** @var int @TODO annotation still needed for phpstan to understand this */
20
    private int $proxyMarkerLength;
21
    private string $proxyMarker;
22
    private ParameterHasher $parameterHasher;
23
24
    public function __construct(string $proxyNamespace)
25
    {
26
        $this->proxyNamespace    = $proxyNamespace;
27
        $this->proxyMarker       = '\\' . self::PROXY_MARKER . '\\';
28
        $this->proxyMarkerLength = strlen($this->proxyMarker);
29
        $this->parameterHasher   = new ParameterHasher();
30
    }
31 13
32
    /**
33 13
     * {@inheritDoc}
34 13
     */
35 13
    public function getUserClassName(string $className) : string
36 13
    {
37 13
        $className = ltrim($className, '\\');
38
        $position  = strrpos($className, $this->proxyMarker);
39
40
        if ($position === false) {
41
            return $className;
42 13
        }
43
44 13
        return substr(
45 13
            $className,
46
            $this->proxyMarkerLength + $position,
47 13
            strrpos($className, '\\') - ($position + $this->proxyMarkerLength)
48 13
        );
49
    }
50
51 2
    /**
52 2
     * {@inheritDoc}
53 2
     */
54 2
    public function getProxyClassName(string $className, array $options = []) : string
55
    {
56
        return $this->proxyNamespace
57
            . $this->proxyMarker
58
            . $this->getUserClassName($className)
59
            . '\\Generated' . $this->parameterHasher->hashParameters($options);
60
    }
61 13
62
    /**
63 13
     * {@inheritDoc}
64 13
     */
65 13
    public function isProxyClassName(string $className) : bool
66 13
    {
67
        return strrpos($className, $this->proxyMarker) !== false;
68
    }
69
}
70