Passed
Pull Request — master (#405)
by Kirill
08:21 queued 04:03
created

ConcatKeyGenerator::forFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Attributes\Internal\Key;
13
14
/**
15
 * An implementation of a key generator that combines multiple generators
16
 * and returns a composite identifier.
17
 *
18
 * This implementation can be used to combine a unique key generator
19
 * for the reflection object ({@see NameKeyGenerator}) and a generator that
20
 * returns the key of the last file modification ({@see ModificationTimeKeyGenerator}).
21
 *
22
 * @internal ConcatKeyGenerator is an internal library class, please do not use it in your code.
23
 * @psalm-internal Spiral\Attributes
24
 */
25
final class ConcatKeyGenerator implements KeyGeneratorInterface
26
{
27
    /**
28
     * @var string
29
     */
30
    private const JOIN_DELIMITER = '_';
31
32
    /**
33
     * @var array<KeyGeneratorInterface>
34
     */
35
    private $generators;
36
37
    /**
38
     * @var string
39
     */
40
    private $join;
41
42
    /**
43
     * @param array<KeyGeneratorInterface> $generators
44
     */
45
    public function __construct(array $generators, string $join = self::JOIN_DELIMITER)
46
    {
47
        $this->generators = $generators;
48
        $this->join = $join;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function forClass(\ReflectionClass $class): string
55
    {
56
        return $this->joinBy(static function (KeyGeneratorInterface $generator) use ($class): string {
57
            return $generator->forClass($class);
58
        });
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    public function forProperty(\ReflectionProperty $prop): string
65
    {
66
        return $this->joinBy(static function (KeyGeneratorInterface $generator) use ($prop): string {
67
            return $generator->forProperty($prop);
68
        });
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function forConstant(\ReflectionClassConstant $const): string
75
    {
76
        return $this->joinBy(static function (KeyGeneratorInterface $generator) use ($const): string {
77
            return $generator->forConstant($const);
78
        });
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    public function forFunction(\ReflectionFunctionAbstract $fn): string
85
    {
86
        return $this->joinBy(static function (KeyGeneratorInterface $generator) use ($fn): string {
87
            return $generator->forFunction($fn);
88
        });
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94
    public function forParameter(\ReflectionParameter $param): string
95
    {
96
        return $this->joinBy(static function (KeyGeneratorInterface $generator) use ($param): string {
97
            return $generator->forParameter($param);
98
        });
99
    }
100
101
    /**
102
     * @param \Closure(KeyGeneratorInterface): string $each
103
     * @return string
104
     */
105
    private function joinBy(\Closure $each): string
106
    {
107
        $result = [];
108
109
        foreach ($this->generators as $generator) {
110
            $result[] = $each($generator);
111
        }
112
113
        return \implode($this->join, $result);
114
    }
115
}
116