genesos / php-simple-lint
v0.1.2
2018-02-23 06:23 UTC
Requires
- php: ~7.0
- nikic/php-parser: ^3.1
Requires (Dev)
- phpunit/dbunit: ^3.0
- phpunit/phpunit: ^6.0
This package is not auto-updated.
Last update: 2024-09-29 06:41:10 UTC
README
php-simple-lint,可以轻松扩展 phpcs。
用法
php lint.php rule/rule.sample.json phpcs
php lint.php rule/rule.sample.json vendor/bin/phpcs
匹配过程
namespace N; use U; class ABC extends A implements B, C { const THE_CONST=1; public $property=1; public function someMethod($a, $b){ $var = 2; } }
序列化为
['type' => 'use', 'clause' => 'namespace N use U'], ['type' => 'class', 'clause' => 'namespace N class ABC extends A implements B implements C'], ['type' => 'const', 'clause' => 'namespace N class ABC extends A implements B implements C { public THE_CONST'], ['type' => 'property', 'clause' => 'namespace N class ABC extends A implements B implements C { public $property'], ['type' => 'param', 'clause' => 'namespace N class ABC extends A implements B implements C { public function someMethod ( int $a'], ['type' => 'param', 'clause' => 'namespace N class ABC extends A implements B implements C { public function someMethod ( bool $b'], ['type' => 'function', 'clause' => 'namespace N class ABC extends A implements B implements C { public function someMethod ( )'], ['type' => 'var', 'clause' => 'namespace N function someMethod ( ) { $var'],
与以下匹配
[
{
"type": "class",
"must": "class [\\w]+Model",
"if": "extends\\s+PlatformBaseModel",
"reason": "Extends PlatformBaseModel must postfix Model"
},
{
"type": "var",
"must": "\\$[a-z0-9_]+",
"reason": "var must lowercase with underbar"
},
{
"type": "param",
"must": "\\$[a-z0-9_]+",
"reason": "param must lowercase with underbar"
},
]
匹配结果
"<error line='{$line_pos}' column='{$column_pos}' source='RIDI.LINT' severity='5' fixable='1'>{$reason}</error>"
rule.json
- 类型(必需)
use,class,const,property,param,function,var
- 必须(或
必须不)(必需)- 正则表达式
- 如果(可选)(字符串或字符串数组)
- 正则表达式
- 如果不(可选)(字符串或字符串数组)
- 正则表达式
规则案例研究
[
{
"type": "use",
"must": "use .+External.+",
"if": [
"namespace Calsule"
],
"if not" : [
"namespace SafeZoneA",
"namespace SafeZoneB"
]
"reason": "`Capsuled namspace` must not use `External` namespace"
},
{
"type": "class",
"must": "class [\\w]+Model",
"if": "extends\\s+BaseModel",
"reason": "`Extends BaseModel` must postfix `Model`"
},
{
"type": "var",
"must": "\\$[a-z0-9_]+",
"reason": "var must lowercase with underbar"
},
{
"type": "param",
"must": "\\$[a-z0-9_]+",
"reason": "param must lowercase with underbar"
},
{
"type": "property",
"must": "\\$[a-z0-9_]+",
"reason": "property must lowercase with underbar"
},
{
"type": "const",
"must": "[A-Z0-9_]+",
"reason": "const must uppercase"
},
{
"type": "class",
"must": "class [A-Z][a-zA-Z0-9]+",
"reason": "class must camel cap"
},
{
"type": "class",
"must not": "class .+Object",
"reason": "class must not ends with `Object`"
},
{
"type": "function",
"must": "function [a-z][a-zA-Z0-9]+",
"reason": "function must camel"
}
]