Passed
Push — master ( 952fd2...f07abe )
by Valentin
03:44 queued 02:05
created

ModificationInspector::getDatetime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Spiral Framework. Cycle ProxyFactory
5
 *
6
 * @license MIT
7
 * @author  Valentin V (Vvval)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\ORM\Promise\Materizalizer;
13
14
use DateTime;
15
use Exception;
16
use ReflectionClass;
17
18
final class ModificationInspector
19
{
20
    /**
21
     * @param ReflectionClass $reflection
22
     * @return DateTime
23
     *
24
     * @throws Exception
25
     */
26
    public function getLastModifiedDate(ReflectionClass $reflection): DateTime
27
    {
28
        $modifiedDate = $this->getLatestParentsModifiedDate($reflection);
29
30
        foreach ($reflection->getTraits() as $trait) {
31
            $traitModifiedDate = $this->getLatestParentsModifiedDate($trait);
32
33
            if ($traitModifiedDate > $modifiedDate) {
34
                $modifiedDate = $traitModifiedDate;
35
            }
36
        }
37
38
        foreach ($reflection->getInterfaces() as $interface) {
39
            $interfaceModifiedDate = $this->getLatestParentsModifiedDate($interface);
40
41
            if ($interfaceModifiedDate > $modifiedDate) {
42
                $modifiedDate = $interfaceModifiedDate;
43
            }
44
        }
45
46
        return $modifiedDate;
47
    }
48
49
    /**
50
     * @param ReflectionClass $reflection
51
     * @return DateTime
52
     *
53
     * @throws Exception
54
     */
55
    private function getLatestParentsModifiedDate(ReflectionClass $reflection): DateTime
56
    {
57
        $modifiedDate = new DateTime('@' . $this->getDatetime($reflection));
58
59
        $parent = $reflection->getParentClass();
60
        while ($parent !== false) {
61
            $parentsModifiedDate = new DateTime('@' . $this->getDatetime($reflection));
62
63
            if ($parentsModifiedDate > $modifiedDate) {
64
                $modifiedDate = $parentsModifiedDate;
65
            }
66
67
            $parent = $parent->getParentClass();
68
        }
69
70
        return $modifiedDate;
71
    }
72
73
    private function getDatetime(ReflectionClass $reflection)
74
    {
75
        return $reflection->getFileName() ? filemtime($reflection->getFileName()) : time();
76
    }
77
}
78