Total Complexity | 40 |
Total Lines | 257 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like FileCacheReader 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 FileCacheReader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class FileCacheReader implements Reader |
||
16 | { |
||
17 | /** |
||
18 | * @var Reader |
||
19 | */ |
||
20 | private $reader; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $dir; |
||
26 | |||
27 | /** |
||
28 | * @var bool |
||
29 | */ |
||
30 | private $debug; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $loadedAnnotations = []; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private $classNameHashes = []; |
||
41 | |||
42 | /** |
||
43 | * @var int |
||
44 | */ |
||
45 | private $umask; |
||
46 | |||
47 | /** |
||
48 | * Constructor. |
||
49 | * |
||
50 | * @param Reader $reader |
||
51 | * @param string $cacheDir |
||
52 | * @param boolean $debug |
||
53 | * |
||
54 | * @throws \InvalidArgumentException |
||
55 | */ |
||
56 | public function __construct(Reader $reader, $cacheDir, $debug = false, $umask = 0002) |
||
57 | { |
||
58 | if ( ! is_int($umask)) { |
||
59 | throw new \InvalidArgumentException(sprintf( |
||
60 | 'The parameter umask must be an integer, was: %s', |
||
61 | gettype($umask) |
||
62 | )); |
||
63 | } |
||
64 | |||
65 | $this->reader = $reader; |
||
66 | $this->umask = $umask; |
||
67 | |||
68 | if (!is_dir($cacheDir) && !@mkdir($cacheDir, 0777 & (~$this->umask), true)) { |
||
69 | throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist and could not be created.', $cacheDir)); |
||
70 | } |
||
71 | |||
72 | $this->dir = rtrim($cacheDir, '\\/'); |
||
73 | $this->debug = $debug; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * {@inheritDoc} |
||
78 | */ |
||
79 | public function getClassAnnotations(\ReflectionClass $class) |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * {@inheritDoc} |
||
112 | */ |
||
113 | public function getPropertyAnnotations(\ReflectionProperty $property) |
||
114 | { |
||
115 | $class = $property->getDeclaringClass(); |
||
116 | if ( ! isset($this->classNameHashes[$class->name])) { |
||
117 | $this->classNameHashes[$class->name] = sha1($class->name); |
||
118 | } |
||
119 | $key = $this->classNameHashes[$class->name].'$'.$property->getName(); |
||
120 | |||
121 | if (isset($this->loadedAnnotations[$key])) { |
||
122 | return $this->loadedAnnotations[$key]; |
||
123 | } |
||
124 | |||
125 | $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php'; |
||
126 | if (!is_file($path)) { |
||
127 | $annot = $this->reader->getPropertyAnnotations($property); |
||
128 | $this->saveCacheFile($path, $annot); |
||
129 | return $this->loadedAnnotations[$key] = $annot; |
||
130 | } |
||
131 | |||
132 | if ($this->debug |
||
133 | && (false !== $filename = $class->getFilename()) |
||
134 | && filemtime($path) < filemtime($filename)) { |
||
135 | @unlink($path); |
||
136 | |||
137 | $annot = $this->reader->getPropertyAnnotations($property); |
||
138 | $this->saveCacheFile($path, $annot); |
||
139 | return $this->loadedAnnotations[$key] = $annot; |
||
140 | } |
||
141 | |||
142 | return $this->loadedAnnotations[$key] = include $path; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * {@inheritDoc} |
||
147 | */ |
||
148 | public function getMethodAnnotations(\ReflectionMethod $method) |
||
149 | { |
||
150 | $class = $method->getDeclaringClass(); |
||
151 | if ( ! isset($this->classNameHashes[$class->name])) { |
||
152 | $this->classNameHashes[$class->name] = sha1($class->name); |
||
153 | } |
||
154 | $key = $this->classNameHashes[$class->name].'#'.$method->getName(); |
||
155 | |||
156 | if (isset($this->loadedAnnotations[$key])) { |
||
157 | return $this->loadedAnnotations[$key]; |
||
158 | } |
||
159 | |||
160 | $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php'; |
||
161 | if (!is_file($path)) { |
||
162 | $annot = $this->reader->getMethodAnnotations($method); |
||
163 | $this->saveCacheFile($path, $annot); |
||
164 | return $this->loadedAnnotations[$key] = $annot; |
||
165 | } |
||
166 | |||
167 | if ($this->debug |
||
168 | && (false !== $filename = $class->getFilename()) |
||
169 | && filemtime($path) < filemtime($filename)) { |
||
170 | @unlink($path); |
||
171 | |||
172 | $annot = $this->reader->getMethodAnnotations($method); |
||
173 | $this->saveCacheFile($path, $annot); |
||
174 | return $this->loadedAnnotations[$key] = $annot; |
||
175 | } |
||
176 | |||
177 | return $this->loadedAnnotations[$key] = include $path; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Saves the cache file. |
||
182 | * |
||
183 | * @param string $path |
||
184 | * @param mixed $data |
||
185 | * |
||
186 | * @return void |
||
187 | */ |
||
188 | private function saveCacheFile($path, $data) |
||
189 | { |
||
190 | if (!is_writable($this->dir)) { |
||
191 | throw new \InvalidArgumentException(sprintf('The directory "%s" is not writable. Both, the webserver and the console user need access. You can manage access rights for multiple users with "chmod +a". If your system does not support this, check out the acl package.', $this->dir)); |
||
192 | } |
||
193 | |||
194 | $tempfile = tempnam($this->dir, uniqid('', true)); |
||
195 | |||
196 | if (false === $tempfile) { |
||
197 | throw new \RuntimeException(sprintf('Unable to create tempfile in directory: %s', $this->dir)); |
||
198 | } |
||
199 | |||
200 | @chmod($tempfile, 0666 & (~$this->umask)); |
||
201 | |||
202 | $written = file_put_contents($tempfile, '<?php return unserialize('.var_export(serialize($data), true).');'); |
||
203 | |||
204 | if (false === $written) { |
||
205 | throw new \RuntimeException(sprintf('Unable to write cached file to: %s', $tempfile)); |
||
206 | } |
||
207 | |||
208 | @chmod($tempfile, 0666 & (~$this->umask)); |
||
209 | |||
210 | if (false === rename($tempfile, $path)) { |
||
211 | @unlink($tempfile); |
||
212 | throw new \RuntimeException(sprintf('Unable to rename %s to %s', $tempfile, $path)); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * {@inheritDoc} |
||
218 | */ |
||
219 | public function getClassAnnotation(\ReflectionClass $class, $annotationName) |
||
220 | { |
||
221 | $annotations = $this->getClassAnnotations($class); |
||
222 | |||
223 | foreach ($annotations as $annotation) { |
||
224 | if ($annotation instanceof $annotationName) { |
||
225 | return $annotation; |
||
226 | } |
||
227 | } |
||
228 | |||
229 | return null; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * {@inheritDoc} |
||
234 | */ |
||
235 | public function getMethodAnnotation(\ReflectionMethod $method, $annotationName) |
||
236 | { |
||
237 | $annotations = $this->getMethodAnnotations($method); |
||
238 | |||
239 | foreach ($annotations as $annotation) { |
||
240 | if ($annotation instanceof $annotationName) { |
||
241 | return $annotation; |
||
242 | } |
||
243 | } |
||
244 | |||
245 | return null; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * {@inheritDoc} |
||
250 | */ |
||
251 | public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName) |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Clears loaded annotations. |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | public function clearLoadedAnnotations() |
||
272 | } |
||
273 | } |
||
274 |
If you suppress an error, we recommend checking for the error condition explicitly: