Completed
Push — master ( 7888fa...8e3a44 )
by Ivannis Suárez
04:39
created

ClassMetadata.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cubiche\Core\Metadata;
12
13
use Cubiche\Core\Collections\ArrayCollection\ArrayHashMap;
14
use Cubiche\Core\Collections\ArrayCollection\ArrayHashMapInterface;
15
16
/**
17
 * ClassMetadata class.
18
 *
19
 * @author Ivannis Suárez Jerez <[email protected]>
20
 */
21
class ClassMetadata implements \Serializable, ClassMetadataInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $className;
27
28
    /**
29
     * @var \ReflectionClass
30
     */
31
    protected $reflection;
32
33
    /**
34
     * @var ArrayHashMapInterface
35
     */
36
    protected $methodsMetadata;
37
38
    /**
39
     * @var ArrayHashMapInterface
40
     */
41
    protected $propertiesMetadata;
42
43
    /**
44
     * ClassMetadata constructor.
45
     *
46
     * @param string $className
47
     */
48
    public function __construct($className)
49
    {
50
        $this->className = $className;
51
52
        $this->reflection = new \ReflectionClass($className);
53
        $this->methodsMetadata = new ArrayHashMap();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Cubiche\Core\Collec...llection\ArrayHashMap() of type object<Cubiche\Core\Coll...ollection\ArrayHashMap> is incompatible with the declared type object<Cubiche\Core\Coll...\ArrayHashMapInterface> of property $methodsMetadata.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
        $this->propertiesMetadata = new ArrayHashMap();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Cubiche\Core\Collec...llection\ArrayHashMap() of type object<Cubiche\Core\Coll...ollection\ArrayHashMap> is incompatible with the declared type object<Cubiche\Core\Coll...\ArrayHashMapInterface> of property $propertiesMetadata.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function className()
61
    {
62
        return $this->className;
63
    }
64
65
    /**
66
     * @return \ReflectionClass
67
     */
68
    public function reflection()
69
    {
70
        return $this->reflection;
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function methodsMetadata()
77
    {
78
        return $this->methodsMetadata->toArray();
79
    }
80
81
    /**
82
     * @param MethodMetadata $metadata
83
     */
84
    public function addMethodMetadata(MethodMetadata $metadata)
85
    {
86
        $this->methodsMetadata->set($metadata->methodName(), $metadata);
87
    }
88
89
    /**
90
     * @param string $methodName
91
     *
92
     * @return MethodMetadata|null
93
     */
94
    public function methodMetadata($methodName)
95
    {
96
        return $this->methodsMetadata->get($methodName);
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    public function propertiesMetadata()
103
    {
104
        return $this->propertiesMetadata->toArray();
105
    }
106
107
    /**
108
     * @param PropertyMetadata $metadata
109
     */
110
    public function addPropertyMetadata(PropertyMetadata $metadata)
111
    {
112
        $this->propertiesMetadata->set($metadata->propertyName(), $metadata);
113
    }
114
115
    /**
116
     * @param string $propertyName
117
     *
118
     * @return PropertyMetadata|null
119
     */
120
    public function propertyMetadata($propertyName)
121
    {
122
        return $this->propertiesMetadata->get($propertyName);
123
    }
124
125
    /**
126
     * @param ClassMetadataInterface $object
127
     *
128
     * @return ClassMetadataInterface
129
     */
130
    public function merge(ClassMetadataInterface $object)
131
    {
132
        $this->className = $object->className();
133
        $this->reflection = $object->reflection();
134
135
        foreach ($object->methodsMetadata() as $methodName => $metadata) {
136
            $this->addMethodMetadata($metadata);
137
        }
138
139
        foreach ($object->propertiesMetadata() as $propertyName => $metadata) {
140
            $this->addPropertyMetadata($metadata);
141
        }
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function serialize()
148
    {
149
        return serialize(array(
150
            $this->className,
151
            $this->methodsMetadata->toArray(),
152
            $this->propertiesMetadata->toArray(),
153
        ));
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function unserialize($str)
160
    {
161
        list(
162
            $this->className,
163
            $methodsMetadata,
164
            $propertiesMetadata) = unserialize($str);
165
166
        $this->reflection = new \ReflectionClass($this->className);
167
        $this->methodsMetadata = new ArrayHashMap($methodsMetadata);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Cubiche\Core\Collec...shMap($methodsMetadata) of type object<Cubiche\Core\Coll...ollection\ArrayHashMap> is incompatible with the declared type object<Cubiche\Core\Coll...\ArrayHashMapInterface> of property $methodsMetadata.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
168
        $this->propertiesMetadata = new ArrayHashMap($propertiesMetadata);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Cubiche\Core\Collec...ap($propertiesMetadata) of type object<Cubiche\Core\Coll...ollection\ArrayHashMap> is incompatible with the declared type object<Cubiche\Core\Coll...\ArrayHashMapInterface> of property $propertiesMetadata.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
169
    }
170
}
171