From 8fa29cc50a942c417d56a2b6a45906ba8e5af5fb Mon Sep 17 00:00:00 2001 From: Ziqiu Zhu Date: Thu, 14 Dec 2017 15:10:21 +0800 Subject: [PATCH] improved code --- src/util/download.php | 2 +- src/util/request.php | 8 +------- src/wikipedia.php | 4 ++-- src/zhihu.php | 6 +++--- 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/util/download.php b/src/util/download.php index a83e8a6..e94d042 100755 --- a/src/util/download.php +++ b/src/util/download.php @@ -17,7 +17,7 @@ function initDownloadDir($clear) { function saveAndReturnFile($url, $name = null) { global $download_dir; - if (!$name) $name = uniqid(); + if (is_null($name)) $name = uniqid(); $path = $download_dir.'/'.$name; file_put_contents($path, file_get_contents($url)); diff --git a/src/util/request.php b/src/util/request.php index 425f101..c0a4d11 100644 --- a/src/util/request.php +++ b/src/util/request.php @@ -21,13 +21,7 @@ function request($url = null, $options = null) { foreach ($options as $k => $v) $defaults[$k] = $v; - array_filter($defaults, - function($a) { - if ($a == '' || $a == null) - return false; - else - return true; - }); + array_filter($defaults, function($a) { return !empty($a); }); $ch = curl_init(); curl_setopt_array($ch, $defaults); diff --git a/src/wikipedia.php b/src/wikipedia.php index dbb4ac2..8582720 100644 --- a/src/wikipedia.php +++ b/src/wikipedia.php @@ -9,7 +9,7 @@ $wf = new Workflow; -if (strpos($query, ' ') !== FALSE) { +if (strpos($query, ' ') !== false) { $parts = explode(' ', $query); $code = array_shift($parts); $query = implode(' ', $parts); @@ -31,7 +31,7 @@ ->quicklookurl("$url"); } - if (count($json[1]) == 0) { + if (count($json[1]) === 0) { $wf->result() ->title('No Suggestions') ->subtitle('No search suggestions found. Search Wikipedia.'.$code.' for '.$query) diff --git a/src/zhihu.php b/src/zhihu.php index 045127a..1dfe108 100644 --- a/src/zhihu.php +++ b/src/zhihu.php @@ -11,14 +11,14 @@ $wf = new Workflow; $download_dir = getenv('alfred_workflow_cache').'/zhihu'; -initDownloadDir(TRUE); +initDownloadDir(true); $response = request('https://www.zhihu.com/autocomplete?token='.urlencode($query)); $json = json_decode(mb_convert_encoding($response, 'UTF-8', 'HTML-ENTITIES'))[0]; foreach ($json as $sugg) { if (is_array($sugg)) { - $matched = TRUE; + $matched = true; $type = $sugg[0]; switch ($type) { case 'topic': @@ -46,7 +46,7 @@ $icon = ICON; break; default: - $matched = FALSE; + $matched = false; break; }