Complex classes like PropertyMetadata often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PropertyMetadata, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class PropertyMetadata extends BasePropertyMetadata implements MergeableInterface |
||
12 | { |
||
13 | 88 | public function __construct($class, $name) |
|
14 | { |
||
15 | try { |
||
16 | 88 | parent::__construct($class, $name); |
|
17 | 32 | } catch (\ReflectionException $e) { |
|
18 | /* Ignore missing property definition as they might just be overridden or virtual and therefore only exist |
||
19 | in the parent class. They will be accessible after merging. */ |
||
20 | } |
||
21 | 88 | } |
|
22 | |||
23 | /** |
||
24 | * @var string|null |
||
25 | */ |
||
26 | private $type; |
||
27 | |||
28 | /** |
||
29 | * @var bool |
||
30 | */ |
||
31 | private $excluded; |
||
32 | |||
33 | /** |
||
34 | * @var Puttable|null |
||
35 | */ |
||
36 | private $puttable; |
||
37 | |||
38 | /** |
||
39 | * @var Postable|null |
||
40 | */ |
||
41 | private $postable; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | private $includable; |
||
47 | |||
48 | /** |
||
49 | * @var string[]|null |
||
50 | */ |
||
51 | private $includablePaths; |
||
52 | |||
53 | /** |
||
54 | * @var bool |
||
55 | */ |
||
56 | private $subResource; |
||
57 | |||
58 | /** |
||
59 | * @var Method[]|null |
||
60 | */ |
||
61 | private $methods; |
||
62 | |||
63 | /** |
||
64 | * @var bool |
||
65 | */ |
||
66 | private $association; |
||
67 | |||
68 | /** |
||
69 | * @var bool |
||
70 | */ |
||
71 | private $collection; |
||
72 | |||
73 | /** |
||
74 | * @var bool |
||
75 | */ |
||
76 | private $virtual; |
||
77 | |||
78 | /** |
||
79 | * @var string|null |
||
80 | */ |
||
81 | private $subResourcePath; |
||
82 | |||
83 | 12 | public function isPuttable(): bool |
|
84 | { |
||
85 | 12 | return null !== $this->puttable; |
|
86 | } |
||
87 | |||
88 | 84 | public function setPuttable(?Puttable $puttable) |
|
89 | { |
||
90 | 84 | $this->puttable = $puttable; |
|
91 | 84 | } |
|
92 | |||
93 | 12 | public function getPuttable(): ?Puttable |
|
94 | { |
||
95 | 12 | return $this->puttable; |
|
96 | } |
||
97 | |||
98 | 14 | public function isPostable(): bool |
|
99 | { |
||
100 | 14 | return null !== $this->postable; |
|
101 | } |
||
102 | |||
103 | 84 | public function setPostable(?Postable $postable) |
|
104 | { |
||
105 | 84 | $this->postable = $postable; |
|
106 | 84 | } |
|
107 | |||
108 | 14 | public function getPostable(): ?Postable |
|
109 | { |
||
110 | 14 | return $this->postable; |
|
111 | } |
||
112 | |||
113 | 64 | public function isIncludable(): bool |
|
114 | { |
||
115 | 64 | return $this->getBool($this->includable, false); |
|
116 | } |
||
117 | |||
118 | public function isVirtual(): bool |
||
119 | { |
||
120 | return $this->getBool($this->virtual, false); |
||
121 | } |
||
122 | |||
123 | 30 | public function setVirtual(bool $virtual) |
|
124 | { |
||
125 | 30 | $this->virtual = $virtual; |
|
126 | 30 | } |
|
127 | |||
128 | 76 | public function setIncludable(bool $includable) |
|
132 | |||
133 | 12 | public function isSubResource(): bool |
|
134 | { |
||
135 | 12 | return $this->getBool($this->subResource, false); |
|
136 | } |
||
137 | |||
138 | 72 | public function setSubResource(bool $subResource) |
|
139 | { |
||
140 | 72 | $this->subResource = $subResource; |
|
141 | 72 | } |
|
142 | |||
143 | 10 | public function getSubResourcePath(): ?string |
|
147 | |||
148 | public function setSubResourcePath(string $subResourcePath) |
||
149 | { |
||
150 | $this->subResourcePath = $subResourcePath; |
||
151 | } |
||
152 | |||
153 | 62 | public function isExcluded(): bool |
|
154 | { |
||
155 | 62 | return $this->getBool($this->excluded, false); |
|
156 | } |
||
157 | |||
158 | 66 | public function setExcluded(bool $excluded) |
|
159 | { |
||
160 | 66 | $this->excluded = $excluded; |
|
161 | 66 | } |
|
162 | |||
163 | /** |
||
164 | * @return null|string[] |
||
165 | */ |
||
166 | 48 | public function getIncludablePaths(): ?array |
|
170 | |||
171 | /** |
||
172 | * @param null|string[] $includablePaths |
||
173 | */ |
||
174 | 76 | public function setIncludablePaths(?array $includablePaths) |
|
178 | |||
179 | 62 | public function isAssociation(): bool |
|
183 | |||
184 | 72 | public function setAssociation(bool $association) |
|
188 | |||
189 | 32 | public function isCollection(): bool |
|
193 | |||
194 | 72 | public function setCollection(bool $collection) |
|
198 | |||
199 | 64 | public function getType(): ?string |
|
203 | |||
204 | 88 | public function setType(?string $type) |
|
208 | |||
209 | /** |
||
210 | * @param Method[] $methods |
||
211 | */ |
||
212 | 72 | public function setMethods(array $methods) |
|
216 | |||
217 | 12 | public function merge(MergeableInterface $other) |
|
239 | |||
240 | 66 | protected function getBool(?bool $value, bool $default) |
|
248 | |||
249 | 12 | private function mergeField($thisValue, $otherValue) |
|
257 | |||
258 | 38 | public function getMethod(string $methodName): ?Method |
|
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | */ |
||
276 | 4 | public function serialize() |
|
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | */ |
||
301 | 4 | public function unserialize($str) |
|
327 | } |
||
328 |