Skip to content

Commit 5adb01c

Browse files
committedJul 21, 2018
optimize code
1 parent 1984bbf commit 5adb01c

File tree

4 files changed

+33
-38
lines changed

4 files changed

+33
-38
lines changed
 

‎demo/_inc.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ static function severity2string($severity)
7878
if (! isset($map)) {
7979
$map = get_defined_constants(true);
8080
$map = $map['Core'];
81-
foreach ($map as $k => $v) {
82-
if (0 !== strpos($k, 'E_')) {
83-
unset($map[$k]);
81+
foreach (array_keys($map) as $v) {
82+
if (0 !== strpos($v, 'E_')) {
83+
unset($map[$v]);
8484
}
8585
}
8686
$map = array_flip($map);

‎demo/basic.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
CURLOPT_URL => 'http://baidu.com'
1212
),
1313
'args' => [
14-
'This is user arg for ' . $v,
14+
'This is user arg',
1515
&$responseCode
1616
]
1717
),

‎src/HttpClone.php

+20-24
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ function start()
117117
'file' => $this->url2file($v),
118118
'isDownload' => false
119119
)
120-
),
121-
array(
120+
), array(
122121
$this,
123122
'onProcess'
124123
));
@@ -170,15 +169,15 @@ function onProcess($r, $args)
170169
$doParse = ! $args['isDownload'];
171170
// Download file may need to be parsed.
172171
if (! $doParse && ! isset($args['type']) &&
173-
false !== strpos($r['info']['content_type'], 'text/html')) {
172+
false !== strpos($r['info']['content_type'], 'text/html')) {
174173
$r['body'] = file_get_contents($args['file']);
175174
$doParse = true;
176175
}
177176
if ($doParse) {
178177
$r['body'] = trim($r['body']);
179178
while (substr($r['body'], 0, 3) ===
180-
chr(239) . chr(187) . chr(191)) {
181-
$r['body'] = substr($r['body'], 3);
179+
chr(239) . chr(187) . chr(191)) {
180+
$r['body'] = substr($r['body'], 3);
182181
}
183182
$r['body'] = $this->htmlEncode($r['body']);
184183
$urlCurrent = $r['info']['url'];
@@ -293,7 +292,7 @@ function onProcess($r, $args)
293292
$v = pq($v);
294293
$href = $v->attr('href');
295294
if (empty($href) ||
296-
strtolower(substr(ltrim($href), 0, 11)) == 'javascript:') {
295+
strtolower(substr(ltrim($href), 0, 11)) == 'javascript:') {
297296
continue;
298297
}
299298
$url = $this->uri2url($href, $urlCurrent);
@@ -304,8 +303,7 @@ function onProcess($r, $args)
304303
} else {
305304
$urlParse[$url] = array();
306305
}
307-
$v->attr('href',
308-
$this->url2src($url, $urlCurrent, true));
306+
$v->attr('href', $this->url2src($url, $urlCurrent, true));
309307
} else {
310308
$v->attr('href',
311309
$this->url2src($url, $urlCurrent, false));
@@ -361,11 +359,10 @@ function onProcess($r, $args)
361359
'isDownload' => $isDownload
362360
)
363361
);
364-
$this->getCurl()->add($item,
365-
array(
366-
$this,
367-
'onProcess'
368-
));
362+
$this->getCurl()->add($item, array(
363+
$this,
364+
'onProcess'
365+
));
369366
}
370367
}
371368
}
@@ -394,6 +391,7 @@ protected function parseLinks(&$content, $urlCurrent)
394391
'/\s*url\((\'|")?(.+?)\\1?\)/i'
395392
);
396393
foreach ($patterns as $pattern) {
394+
$matches = [];
397395
preg_match_all($pattern, $content, $matches);
398396
if (! empty($matches[2])) {
399397
if (! is_array($matches[2])) {
@@ -408,10 +406,9 @@ protected function parseLinks(&$content, $urlCurrent)
408406
}
409407
$ext = pathinfo(parse_url($v, PHP_URL_PATH),
410408
PATHINFO_EXTENSION);
411-
if (in_array($ext,
412-
array(
413-
'css'
414-
))) {
409+
if (in_array($ext, array(
410+
'css'
411+
))) {
415412
$vUrl = array(
416413
'type' => 'css'
417414
);
@@ -502,7 +499,7 @@ public function url2uri($url, $urlCurrent)
502499
$arr2 = explode('/', rtrim($path2, '/'));
503500
}
504501
$path = '../';
505-
foreach ($arr2 as $v) {
502+
for ($v = 0; $v < count($arr2); $v ++) {
506503
$path .= '../';
507504
}
508505
$path .= $path1;
@@ -528,7 +525,7 @@ protected function url2file($url)
528525
}
529526
if (file_exists($file)) {
530527
if (! isset($this->expire) ||
531-
time() - filemtime($file) < $this->expire) {
528+
time() - filemtime($file) < $this->expire) {
532529
$file = null;
533530
}
534531
}
@@ -594,10 +591,9 @@ protected function getPath($url)
594591
"lpt9"
595592
);
596593
}
597-
$invalidNameReplace = array_map(
598-
function ($v) {
599-
return '_' . $v;
600-
}, $invalidName);
594+
$invalidNameReplace = array_map(function ($v) {
595+
return '_' . $v;
596+
}, $invalidName);
601597
$path = str_replace($invalid, '-', $path);
602598
$path = str_replace($invalidName, $invalidNameReplace, $path);
603599
return $path;
@@ -693,7 +689,7 @@ protected function fixPath($path)
693689
} else {
694690
if (in_array($pathArr['extension'], $this->htmlExt)) {
695691
$path = rtrim($pathArr['dirname'], ' /') . '/' .
696-
$pathArr['filename'] . '.html';
692+
$pathArr['filename'] . '.html';
697693
}
698694
}
699695
return $path . $query . $fragment;

‎src/Toolkit.php

+9-10
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function __construct(Curl $curl = null)
5050
function onFail($error, $args)
5151
{
5252
$msg = "Curl error ($error[errorCode])$error[errorMsg], url=" .
53-
$error['info']['url'];
53+
$error['info']['url'];
5454
if ($this->_curl->onInfo == array(
5555
$this,
5656
'onInfo'
@@ -149,8 +149,7 @@ function onInfo($info, $curl = null, $isLast = null)
149149
$v[0] = mb_strlen($v[1]);
150150
}
151151
$caption .= sprintf('%-' . ($v[0] + $lenPad) . 's', $v[1]);
152-
$content .= sprintf('%-' . ($v[0] + $lenPad) . 's',
153-
$all[$k]);
152+
$content .= sprintf('%-' . ($v[0] + $lenPad) . 's', $all[$k]);
154153
} else {
155154
$format = '%-' . ($v[0] + strlen($v[1]) + 1 + $lenPad) . 's';
156155
$content .= sprintf($format, $v[1] . ':' . $all[$k]);
@@ -280,11 +279,10 @@ function between($str, $start, $end = null, $greed = true)
280279
function isUrl($url)
281280
{
282281
$url = ltrim($url);
283-
return in_array(substr($url, 0, 7),
284-
array(
285-
'http://',
286-
'https:/'
287-
));
282+
return in_array(substr($url, 0, 7), array(
283+
'http://',
284+
'https:/'
285+
));
288286
}
289287

290288
/**
@@ -341,6 +339,7 @@ function buildUrl(array $parse)
341339
if ('' !== $parse['query']) {
342340
$parse['path'] .= '?';
343341
// sort
342+
$query = [];
344343
parse_str($parse['query'], $query);
345344
asort($query);
346345
$parse['query'] = http_build_query($query);
@@ -350,8 +349,8 @@ function buildUrl(array $parse)
350349
}
351350
$parse['path'] = preg_replace('/\/+/', '/', $parse['path']);
352351
return $parse['scheme'] . $parse['user'] . $parse['pass'] .
353-
$parse['host'] . $parse['port'] . $parse['path'] . $parse['query'] .
354-
$parse['fragment'];
352+
$parse['host'] . $parse['port'] . $parse['path'] . $parse['query'] .
353+
$parse['fragment'];
355354
}
356355

357356
/**

0 commit comments

Comments
 (0)
Please sign in to comment.