Passed
Push — master ( a995e6...3e1dc9 )
by Asmir
01:25 queued 49s
created

ClassMetadata::__unserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Metadata;
6
7
/**
8
 * Base class for class metadata.
9
 *
10
 * This class is intended to be extended to add your own application specific
11
 * properties, and flags.
12
 *
13
 * @author Johannes M. Schmitt <[email protected]>
14
 */
15
class ClassMetadata implements \Serializable
16
{
17
    /**
18
     * @var string
19
     */
20
    public $name;
21
22
    /**
23
     * @var MethodMetadata[]
24
     */
25
    public $methodMetadata = [];
26
27
    /**
28
     * @var PropertyMetadata[]
29
     */
30
    public $propertyMetadata = [];
31
32
    /**
33
     * @var string[]
34
     */
35
    public $fileResources = [];
36
37
    /**
38
     * @var int
39
     */
40
    public $createdAt;
41
42 20
    public function __construct(string $name)
43
    {
44 20
        $this->name = $name;
45 20
        $this->createdAt = time();
46 20
    }
47
48
    public function addMethodMetadata(MethodMetadata $metadata): void
49
    {
50
        $this->methodMetadata[$metadata->name] = $metadata;
51
    }
52
53
    public function addPropertyMetadata(PropertyMetadata $metadata): void
54
    {
55
        $this->propertyMetadata[$metadata->name] = $metadata;
56
    }
57
58 1
    public function isFresh(?int $timestamp = null): bool
59
    {
60 1
        if (null === $timestamp) {
61 1
            $timestamp = $this->createdAt;
62
        }
63
64 1
        foreach ($this->fileResources as $filepath) {
65 1
            if (!file_exists($filepath)) {
66
                return false;
67
            }
68
69 1
            if ($timestamp < filemtime($filepath)) {
70 1
                return false;
71
            }
72
        }
73
74 1
        return true;
75
    }
76
77
    /**
78
     * @return string
79
     *
80
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
81
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessReturnAnnotation
82
     */
83 5
    public function serialize()
84
    {
85 5
        return serialize($this->__serialize());
86 5
    }
87 5
88 5
    /**
89 5
     * @param string $str
90 5
     *
91
     * @return void
92
     *
93
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
94
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
95
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessReturnAnnotation
96
     */
97
    public function unserialize($str)
98
    {
99
        $this->__unserialize((array) unserialize((string) $str));
100
    }
101
102
    /**
103 5
     * @return array<mixed>
104
     */
105
    public function __serialize(): array
106 5
    {
107 5
        return [
108 5
            $this->name,
109 5
            $this->methodMetadata,
110 5
            $this->propertyMetadata,
111 5
            $this->fileResources,
112 5
            $this->createdAt,
113
        ];
114
    }
115
116
    /**
117
     * @param array<mixed> $data
118
     */
119
    public function __unserialize(array $data): void
120
    {
121
        [
122
            $this->name,
123
            $this->methodMetadata,
124
            $this->propertyMetadata,
125
            $this->fileResources,
126
            $this->createdAt,
127
        ] = $data;
128
    }
129
}
130