Created
May 15, 2013 11:37
-
-
Save picasso250/5583382 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PHP lint | |
* 用法:放在www目录下,用浏览器访问即可 | |
* 作者:王霄池 | |
* 有 bug,请去打击王霄池 | |
*/ | |
ini_set('display_errors', 1); | |
error_reporting(E_ALL); | |
configure('root', 'E:\web.fy.fangdd.com\Branches\~team_diaosi_trade'); | |
configure('extentions', array('php')); | |
configure('exclude_dirs', array('Common', 'Helper', 'Public', 'Library', 'Config', 'Language')); | |
add_regex_rule('/\$[\w]+\[([a-z][^\[\]\(\)]*)\]/', '数组下标使用未定义常量 $1'); | |
lint(); | |
// =========================== | |
function configure($key, $value) | |
{ | |
$GLOBALS['_php_lint_config'][$key] = $value; | |
} | |
function add_regex_rule($regex, $msg = 'error') { | |
$GLOBALS['_php_lint_regex_rules'][] = array($regex, $msg); | |
} | |
function lint() | |
{ | |
$file_list = array(); | |
get_file_list($GLOBALS['_php_lint_config']['root'], $file_list); | |
$GLOBALS['lint_errors'] = array(); | |
foreach ($file_list as $fpath) { | |
$GLOBALS['lint_errors'][] = lint_file($fpath); | |
} | |
} | |
function get_file_list($dir, &$file_list) | |
{ | |
$d = opendir($dir); | |
while (false !== ($file = readdir($d))) { | |
if ($file != '.' && $file != '..') { | |
$fpath = $dir.'/'.$file; | |
if (is_dir($fpath) && strpos($file, '.') !== 0 && !in_array($file, $GLOBALS['_php_lint_config']['exclude_dirs'])) { | |
get_file_list($fpath, $file_list); | |
} elseif (is_ext($file)) { | |
$file_list[] = $fpath; | |
} | |
} | |
} | |
} | |
function lint_file($fpath) | |
{ | |
$errors = lint_php(file_get_contents($fpath)); | |
return array( | |
'file' => $fpath, | |
'errors' => $errors, | |
); | |
} | |
function lint_php($code) | |
{ | |
$ret = array(); | |
foreach (explode("\n", $code) as $n => $line) { | |
$line_no = $n + 1; | |
foreach ($GLOBALS['_php_lint_regex_rules'] as $value) { | |
if (preg_match($value[0], $line, $matches)) { | |
$msg = preg_replace_callback('/\$\d+/u', function ($p) use($matches) { | |
preg_match('/\d+/', $p[0], $m); | |
return $matches[(int) $m[0]]; | |
}, $value[1]); | |
$ret[] = array( | |
'line' => $line_no, | |
'content' => $line, | |
'message' => $msg, | |
); | |
} | |
} | |
} | |
return $ret; | |
} | |
function is_ext($fname) | |
{ | |
return in_array( | |
end(explode('.', $fname)), | |
$GLOBALS['_php_lint_config']['extentions'] | |
); | |
} | |
function code_format($code) | |
{ | |
$code = preg_replace('/ /', ' ', $code); | |
$code = preg_replace('/\b([A-Z_]+|array|true|if|empty|isset)\b/u', '<span class="kw">$1</span>', $code); | |
$code = preg_replace('/(`[a-z_]+`)/', '<span class="field">$1</span>', $code); | |
$code = preg_replace("/('.*?')/u", '<span class="str">$1</span>', $code); | |
$code = preg_replace('/(\$[a-z>-]+)/', '<span class="var">$1</span>', $code); | |
$code = nl2br($code); | |
return $code; | |
} | |
?> | |
<html> | |
<head> | |
<title>Fangyun PHP Lint 吹毛求疵的检查者</title> | |
<style type="text/css"> | |
.fpath { | |
font-family: consolas; | |
} | |
.n { | |
display: inline-block; | |
width: 2em; | |
text-align: right; | |
font-family: consolas; | |
color: #797979; | |
border-right-style: solid; | |
border-right-width: 1px; | |
border-right-color: #B9B9B9; | |
} | |
.msg { | |
padding-top: 10px; | |
padding-left: 3em; | |
color: #CC4242; | |
} | |
ul { | |
list-style: none; | |
} | |
li { | |
padding-bottom: 10px; | |
} | |
.code { | |
font-family: consolas; | |
/*font-size: small;*/ | |
} | |
.code .kw { | |
color: #4880B3; | |
} | |
.code .field { | |
color: #8F702B; | |
font-weight: bold; | |
} | |
.code .str { | |
color: #188321; | |
} | |
.code .var { | |
color: #A445C9; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Fangyun PHP Lint 吹毛求疵的检查者</h1> | |
<?php foreach ($lint_errors as $key => $value): ?> | |
<?php if ($value['errors']): ?> | |
<h3 class="fpath"><?php echo $value['file'] ?></h3> | |
<ul> | |
<?php foreach ($value['errors'] as $k => $v): ?> | |
<li> | |
<span class="n"><?php echo $v['line'] ?></span> | |
<span class="code"><?php echo code_format(trim($v['content'])) ?></span> | |
<div class="msg"><?php echo $v['message'] ?></div> | |
</li> | |
<?php endforeach ?> | |
</ul> | |
<?php endif ?> | |
<?php endforeach ?> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment