Total Complexity | 48 |
Total Lines | 296 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 2 | Features | 0 |
Complex classes like Binary 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 Binary, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class Binary |
||
47 | { |
||
48 | use FileOps; |
||
49 | |||
50 | private $php; |
||
51 | private $extName; |
||
52 | private $extVersion; |
||
53 | private $progress = null; |
||
54 | private $input = null; |
||
55 | private $output = null; |
||
56 | private $extDll = null; |
||
57 | |||
58 | /** |
||
59 | * @param string $ext |
||
60 | */ |
||
61 | public function __construct($ext) |
||
62 | { |
||
63 | // used only if only the extension name is given |
||
64 | if (strpos('//', $ext) !== false) { |
||
65 | $this->extensionPeclExists(); |
||
66 | } |
||
67 | |||
68 | $this->extName = $ext; |
||
69 | $this->php = Engine::factory(); |
||
70 | } |
||
71 | |||
72 | public function setProgress($progress) |
||
73 | { |
||
74 | $this->progress = $progress; |
||
75 | } |
||
76 | |||
77 | public function setInput(InputInterface $input) |
||
78 | { |
||
79 | $this->input = $input; |
||
80 | } |
||
81 | |||
82 | public function setOutput(OutputInterface $output) |
||
83 | { |
||
84 | $this->output = $output; |
||
85 | } |
||
86 | |||
87 | private function extensionPeclExists() |
||
88 | { |
||
89 | $url = 'https://pecl.php.net/get/'.$this->extName; |
||
90 | $headers = get_headers($url, 1); |
||
91 | $status = $headers[0]; |
||
92 | if (strpos($status, '404')) { |
||
93 | throw new \Exception("Extension <$this->extName> cannot be found"); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param string $url |
||
99 | */ |
||
100 | private function findInLinks($url, $toFind) |
||
101 | { |
||
102 | $page = @file_get_contents($url); |
||
|
|||
103 | $opts = [ |
||
104 | 'http' => [ |
||
105 | 'header' => 'User-Agent: pickle' |
||
106 | ], |
||
107 | ]; |
||
108 | $context = stream_context_create($opts); |
||
109 | $page = @file_get_contents($url, false, $context); |
||
110 | if (!$page) { |
||
111 | return false; |
||
112 | } |
||
113 | $dom = new \DOMDocument(); |
||
114 | $dom->loadHTML($page); |
||
115 | $links = $dom->getElementsByTagName('a'); |
||
116 | if (!$links) { |
||
117 | return false; |
||
118 | } |
||
119 | |||
120 | foreach ($links as $link) { |
||
121 | if ($link->nodeValue[0] == '[') { |
||
122 | continue; |
||
123 | } |
||
124 | $value = trim($link->nodeValue); |
||
125 | if ($toFind == $value) { |
||
126 | return $value; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | return false; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @return string |
||
135 | * |
||
136 | * @throws \Exception |
||
137 | */ |
||
138 | private function fetchZipName() |
||
139 | { |
||
140 | $phpVc = $this->php->getCompiler(); |
||
141 | $phpArch = $this->php->getArchitecture(); |
||
142 | $phpZts = $this->php->getZts() ? '-ts' : '-nts'; |
||
143 | $phpVersion = $this->php->getMajorVersion().'.'.$this->php->getMinorVersion(); |
||
144 | $pkgVersion = $this->extVersion; |
||
145 | $extName = strtolower($this->extName); |
||
146 | $baseUrl = 'https://windows.php.net/downloads/pecl/releases/'; |
||
147 | |||
148 | if (false === $this->findInLinks($baseUrl.$extName, $pkgVersion)) { |
||
149 | throw new \Exception('Binary for <'.$extName.'-'.$pkgVersion.'> cannot be found'); |
||
150 | } |
||
151 | |||
152 | $fileToFind = 'php_'.$extName.'-'.$pkgVersion.'-'.$phpVersion.$phpZts.'-'.$phpVc.'-'.$phpArch.'.zip'; |
||
153 | $fileUrl = $this->findInLinks($baseUrl.$extName.'/'.$pkgVersion, $fileToFind); |
||
154 | |||
155 | if (!$fileUrl) { |
||
156 | throw new \Exception('Binary for <'.$fileToFind.'> cannot be found'); |
||
157 | } |
||
158 | $url = $baseUrl.$extName.'/'.$pkgVersion.'/'.$fileToFind; |
||
159 | |||
160 | return $url; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param string $zipFile |
||
165 | * |
||
166 | * @throws \Exception |
||
167 | */ |
||
168 | private function uncompress($zipFile) |
||
169 | { |
||
170 | $this->createTempDir($this->extName); |
||
171 | $this->cleanup(); |
||
172 | $zipClass = Archive\Factory::getUnzipperClassName(); |
||
173 | $zipArchive = $zipClass($zipFile); |
||
174 | /** @var \Pickle\Base\Interfaces\Archive\Unzipper $zipArchive */ |
||
175 | $this->output->writeln('Extracting archives...'); |
||
176 | $zipArchive->extractTo($this->tempDir); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param string $url |
||
181 | * |
||
182 | * @return string |
||
183 | * |
||
184 | * @throws \Exception |
||
185 | */ |
||
186 | private function download($url) |
||
187 | { |
||
188 | $progress = $this->progress; |
||
189 | $progress->setOverwrite(true); |
||
190 | $ctx = stream_context_create( |
||
191 | array( |
||
192 | 'http' => [ |
||
193 | 'header' => 'User-Agent: pickle' |
||
194 | ] |
||
195 | ), |
||
196 | array( |
||
197 | 'notification' => function ($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax) use ($progress) { |
||
198 | switch ($notificationCode) { |
||
199 | case STREAM_NOTIFY_RESOLVE: |
||
200 | case STREAM_NOTIFY_AUTH_REQUIRED: |
||
201 | case STREAM_NOTIFY_COMPLETED: |
||
202 | case STREAM_NOTIFY_FAILURE: |
||
203 | case STREAM_NOTIFY_AUTH_RESULT: |
||
204 | break; |
||
205 | |||
206 | case STREAM_NOTIFY_REDIRECTED: |
||
207 | break; |
||
208 | |||
209 | case STREAM_NOTIFY_CONNECT: |
||
210 | break; |
||
211 | |||
212 | case STREAM_NOTIFY_FILE_SIZE_IS: |
||
213 | $progress->start($bytesMax); |
||
214 | break; |
||
215 | |||
216 | case STREAM_NOTIFY_MIME_TYPE_IS: |
||
217 | break; |
||
218 | |||
219 | case STREAM_NOTIFY_PROGRESS: |
||
220 | $progress->setProgress($bytesTransferred); |
||
221 | break; |
||
222 | }; |
||
223 | }, |
||
224 | ) |
||
225 | ); |
||
226 | $output->writeln("downloading $url "); |
||
227 | $fileContents = file_get_contents($url, false, $ctx); |
||
228 | $progress->finish(); |
||
229 | if (!$fileContents) { |
||
230 | throw new \Exception('Cannot fetch <'.$url.'>'); |
||
231 | } |
||
232 | $tmpdir = Util\TmpDir::get(); |
||
233 | $path = $tmpdir.'/'.$this->extName.'.zip'; |
||
234 | if (!file_put_contents($path, $fileContents)) { |
||
235 | throw new \Exception('Cannot save temporary file <'.$path.'>'); |
||
236 | } |
||
237 | |||
238 | return $path; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @throws \Exception |
||
243 | */ |
||
244 | private function copyFiles() |
||
245 | { |
||
246 | $DLLs = glob($this->tempDir.'/*.dll'); |
||
247 | $this->extDll = []; |
||
248 | foreach ($DLLs as $dll) { |
||
249 | $dll = realpath($dll); |
||
250 | $basename = basename($dll); |
||
251 | $dest = $this->php->getExtensionDir().DIRECTORY_SEPARATOR.$basename; |
||
252 | if (substr($basename, 0, 4) == 'php_') { |
||
253 | $this->extDll[] = $basename; |
||
254 | $this->output->writeln("copying $dll to ".$dest."\n"); |
||
255 | $success = copy($dll, $this->php->getExtensionDir().'/'.$basename); |
||
256 | if (!$success) { |
||
257 | throw new \Exception('Cannot copy DLL <'.$dll.'> to <'.$dest.'>'); |
||
258 | } |
||
259 | } else { |
||
260 | $success = copy($dll, dirname($this->php->getPath()).'/'.$basename); |
||
261 | if (!$success) { |
||
262 | throw new \Exception('Cannot copy DLL <'.$dll.'> to <'.$dest.'>'); |
||
263 | } |
||
264 | } |
||
265 | } |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @throws \Exception |
||
270 | */ |
||
271 | private function updateIni() |
||
272 | { |
||
273 | $ini = \Pickle\Engine\Ini::factory($this->php); |
||
274 | $ini->updatePickleSection($this->extDll); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @return array |
||
279 | * |
||
280 | * @throws \Exception |
||
281 | */ |
||
282 | private function getInfoFromPecl() |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * 1. check if ext exists |
||
316 | * 2. check if given version requested |
||
317 | * 2.1 yes? check if builds available |
||
318 | * 2.2 no? get latest version+build. |
||
319 | * |
||
320 | * @throws \Exception |
||
321 | */ |
||
322 | public function install() |
||
331 | } |
||
332 | |||
333 | public function getExtDllPaths() |
||
334 | { |
||
335 | $ret = array(); |
||
336 | |||
346 |