Total Complexity | 45 |
Total Lines | 168 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like BucketStream 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.
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 BucketStream, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | final class BucketStream implements StreamInterface |
||
15 | { |
||
16 | private ConverterMatcherInterface $converterMatcher; |
||
17 | private ?DataBucket $bucket; |
||
18 | private ?GeneratorStream $stream = null; |
||
19 | private bool $calculated = false; |
||
20 | private ?MatchingResult $matchedResult = null; |
||
21 | |||
22 | public function __construct(ConverterMatcherInterface $converterMatcher, DataBucket $bucket) |
||
23 | { |
||
24 | $this->bucket = $bucket; |
||
25 | $this->converterMatcher = $converterMatcher; |
||
26 | $this->prepareConverter(); |
||
27 | } |
||
28 | public function __toString(): string |
||
29 | { |
||
30 | try { |
||
31 | if ($this->isSeekable()) { |
||
32 | $this->seek(0); |
||
33 | } |
||
34 | return $this->getContents(); |
||
35 | } catch (\Exception $e) { |
||
36 | return ''; |
||
37 | } |
||
38 | } |
||
39 | public function close(): void |
||
40 | { |
||
41 | $this->detach(); |
||
42 | } |
||
43 | public function detach() |
||
44 | { |
||
45 | $this->matchedResult = null; |
||
46 | $this->stream = null; |
||
47 | $result = $this->bucket; |
||
48 | $this->bucket = null; |
||
49 | return $result; |
||
|
|||
50 | } |
||
51 | public function getSize(): ?int |
||
52 | { |
||
53 | return $this->stream === null ? null : $this->stream->getSize(); |
||
54 | } |
||
55 | public function tell(): int |
||
56 | { |
||
57 | return $this->stream === null ? 0 : $this->stream->tell(); |
||
58 | } |
||
59 | public function eof(): bool |
||
60 | { |
||
61 | return $this->stream === null ? false : $this->stream->eof(); |
||
62 | } |
||
63 | public function isSeekable(): bool |
||
64 | { |
||
65 | return false; |
||
66 | } |
||
67 | public function seek($offset, $whence = \SEEK_SET): void |
||
68 | { |
||
69 | throw new \RuntimeException('Stream is not seekable.'); |
||
70 | } |
||
71 | public function rewind(): void |
||
77 | } |
||
78 | } |
||
79 | public function isWritable(): bool |
||
80 | { |
||
81 | return false; |
||
82 | } |
||
83 | public function write($string): int |
||
84 | { |
||
85 | throw new \RuntimeException('Cannot write to a non-writable stream.'); |
||
86 | } |
||
87 | public function isReadable(): bool |
||
88 | { |
||
89 | if ($this->matchedResult === null) { |
||
90 | return false; |
||
91 | } |
||
92 | $this->initConverter(); |
||
93 | return $this->stream === null ? false : $this->stream->isReadable(); |
||
94 | } |
||
95 | public function read($length): string |
||
96 | { |
||
97 | if (!$this->isReadable()) { |
||
98 | throw new \RuntimeException('Stream should be rendered.'); |
||
99 | } |
||
100 | # read generator stream |
||
101 | return $this->stream->read($length); |
||
102 | } |
||
103 | public function getContents(): string |
||
104 | { |
||
105 | return $this->read(PHP_INT_MAX); |
||
106 | } |
||
107 | public function getMetadata($key = null) |
||
108 | { |
||
109 | $meta = [ |
||
110 | 'seekable' => $this->isSeekable(), |
||
111 | 'eof' => $this->eof(), |
||
112 | ]; |
||
113 | |||
114 | if (null === $key) { |
||
115 | return $meta; |
||
116 | } |
||
117 | |||
118 | return $meta[$key] ?? null; |
||
119 | } |
||
120 | |||
121 | public function withBucket(DataBucket $bucket): self |
||
124 | } |
||
125 | |||
126 | public function hasConverter(): bool |
||
127 | { |
||
128 | return $this->matchedResult !== null; |
||
129 | } |
||
130 | public function getConverter(): ?ConverterInterface |
||
133 | } |
||
134 | public function hasFormat(): bool |
||
135 | { |
||
136 | return $this->matchedResult !== null; |
||
137 | } |
||
138 | public function getFormat(): ?string |
||
139 | { |
||
140 | return $this->matchedResult === null ? null : $this->matchedResult->getFormat(); |
||
141 | } |
||
142 | |||
143 | public function isRenderStarted(): bool |
||
144 | { |
||
145 | return $this->stream !== null; |
||
146 | } |
||
147 | |||
148 | public function getBucket(): ?DataBucket |
||
149 | { |
||
150 | return $this->bucket; |
||
151 | } |
||
152 | public function getMatchedResult(): ?MatchingResult |
||
155 | } |
||
156 | |||
157 | private function initConverter(): bool |
||
158 | { |
||
159 | if ($this->stream !== null) { |
||
160 | return true; |
||
161 | } |
||
162 | if ($this->matchedResult === null || $this->bucket === null) { |
||
163 | return false; |
||
164 | } |
||
165 | $this->stream = new GeneratorStream($this->matchedResult->getConverter()->convert($this->bucket)); |
||
166 | return true; |
||
167 | } |
||
168 | private function prepareConverter(): void |
||
169 | { |
||
170 | if ($this->calculated) { |
||
182 | } |
||
183 | } |
||
184 |