Completed
Push — master ( 6366df...fbe022 )
by Kirill
23s queued 19s
created

ModificationTimeKeyGenerator::forClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
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
 * A generator that returns a key containing information about the
16
 * time the file was last modified.
17
 *
18
 * @internal ModificationTimeKeyGenerator is an internal library class, please do not use it in your code.
19
 * @psalm-internal Spiral\Attributes
20
 */
21
final class ModificationTimeKeyGenerator implements KeyGeneratorInterface
22
{
23
    /**
24
     * {@inheritDoc}
25
     */
26
    public function forClass(\ReflectionClass $class): string
27
    {
28
        if ($class->isUserDefined()) {
29
            return (string)\filemtime(
30
                $class->getFileName()
31
            );
32
        }
33
34
        return $class->getExtension()
35
            ->getVersion()
36
        ;
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function forProperty(\ReflectionProperty $prop): string
43
    {
44
        return $this->forClass(
45
            $prop->getDeclaringClass()
46
        );
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function forConstant(\ReflectionClassConstant $const): string
53
    {
54
        return $this->forClass(
55
            $const->getDeclaringClass()
56
        );
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public function forFunction(\ReflectionFunctionAbstract $fn): string
63
    {
64
        if ($fn instanceof \ReflectionMethod) {
65
            return $this->forClass(
66
                $fn->getDeclaringClass()
67
            );
68
        }
69
70
        if ($fn->isUserDefined()) {
71
            return (string)\filemtime(
72
                $fn->getFileName()
73
            );
74
        }
75
76
        if ($extension = $fn->getExtension()) {
77
            return $extension->getVersion();
78
        }
79
80
        throw new \LogicException('Can not determine modification time of [' . $fn->getName() . ']');
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function forParameter(\ReflectionParameter $param): string
87
    {
88
        return $this->forFunction(
89
            $param->getDeclaringFunction()
90
        );
91
    }
92
}
93