OCLint的分析結(jié)果:
優(yōu)先級(jí)的級(jí)別是從Priority 1, Priority 2, Priority 3 依次降低的
Total Files 總文件數(shù)
Files with Violations 違規(guī)文件數(shù)
Compiler Warnings 表示項(xiàng)目中的警告??
Compiler Errors 表示編譯錯(cuò)誤
Location 表示警告的位置
報(bào)告中的描述其實(shí)非常清晰符相,一般找到代碼位置甲棍,結(jié)合代碼理解
一删窒、OCLint 安裝
使用Homebrew 安裝:在安裝前德迹,確保安裝了 homebrew知染。
$ brew tap oclint/formulae
$ brew install oclint
安裝完以后驗(yàn)證下 OCLint 是否安裝成功烈掠。輸入
$ oclint --version
二彤钟、xcpretty 的安裝
在安裝前来候,確保安裝了 Ruby gem.
$gem install xcpretty
三、 使用 oclint
1.如果項(xiàng)目使用了 Cocopod逸雹,則需要指定 -workspace xxx.workspace
2.每次編譯之前需要 clean
3.cd 進(jìn)入項(xiàng)目
4.查看項(xiàng)目基本信息
$xcodebuild -list
sikong@localhost OCLintDemo % xcodebuild -list
Command line invocation:
/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -list
Information about project "OCLintDemo":
Targets:
OCLintDemo
OCLintDemoTests
Build Configurations:
Debug
Release
If no build configuration is specified and -scheme is not passed then "Release" is used.
Schemes:
OCLintDemo
sikong@localhost OCLintDemo %
5.編譯.編譯成功后营搅,會(huì)在項(xiàng)目的文件夾下出現(xiàn) compile_commands.json 文件
$xcodebuild -scheme OCLintDemo -workspace OCLintDemo.xcworkspace clean && xcodebuild -scheme OCLintDemo -workspace OCLintDemo.xcworkspace -configuration Debug | xcpretty -r json-compilation-database -o compile_commands.json
6.生成 html 報(bào)表
$ oclint-json-compilation-database -e Pods -- -report-type html -o oclintReport.html
看到有報(bào)錯(cuò),但是報(bào)錯(cuò)信息太多了梆砸,不好定位,利用下面的腳本則可以將報(bào)錯(cuò)信息寫(xiě)入 log 文件,方便查看
oclint-json-compilation-database -e Pods -- -report-type html -o oclintReport.html 2>&1 | tee 1.log
報(bào)錯(cuò)信息:oclint: error: one compiler command contains multiple jobs:
的解決方案如下:
將Project
和 Targets
中 Building Settings
下的 COMPILER_INDEX_STORE_ENABLE
設(shè)置為NO
在podfile
中 target 'xx' do
前面添加下面的腳本:
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['COMPILER_INDEX_STORE_ENABLE'] = "NO"
end
end
end
報(bào)錯(cuò)信息:oclint: error: violations exceed threshold: P1=0[0] P2=323[10] P3=4523[20]
的解決方案如下:
看到報(bào)錯(cuò)信息是默認(rèn)的警告數(shù)量超過(guò)限制朋譬,則 lint 失敗棍苹。事實(shí)上 lint 后可以跟參數(shù),所以我們修改腳本如下:
oclint-json-compilation-database -e Pods -- -report-type html -o oclintReport.html -rc LONG_LINE=300 -max-priority-1=9999 -max-priority-2=9999 -max-priority-3=9999
四日矫、oclint規(guī)則
規(guī)則默認(rèn)值:
名稱(chēng) | 描述 | 默認(rèn)閾值 |
---|---|---|
CYCLOMATIC_COMPLEXITY | 方法的循環(huán)復(fù)雜性(圈負(fù)責(zé)度) | 10 |
LONG_CLASS | C類(lèi)或Objective-C接口赂弓,類(lèi)別,協(xié)議和實(shí)現(xiàn)的行數(shù) | 1000 |
LONG_LINE | 一行代碼的字符數(shù) | 100 |
LONG_METHOD | 方法或函數(shù)的行數(shù) | 50 |
LONG_VARIABLE_NAME | 變量名稱(chēng)的字符數(shù) | 20 |
MAXIMUM_IF_LENGTH | if語(yǔ)句的行數(shù) | 15 |
MINIMUM_CASES_IN_SWITCH | switch語(yǔ)句中的case數(shù) | 3 |
NPATH_COMPLEXITY | 方法的NPath復(fù)雜性 | 200 |
NCSS_METHOD | 一個(gè)沒(méi)有注釋的方法語(yǔ)句數(shù) | 30 |
NESTED_BLOCK_DEPTH | 塊或復(fù)合語(yǔ)句的深度 | 5 |
SHORT_VARIABLE_NAME | 變量名稱(chēng)的字符數(shù) | 3 |
TOO_MANY_FIELDS | 類(lèi)的字段數(shù) | 20 |
TOO_MANY_METHODS | 類(lèi)的方法數(shù) | 30 |
TOO_MANY_PARAMETERS | 方法的參數(shù)數(shù) | 10 |
根據(jù)實(shí)際需要修改部分檢測(cè)規(guī)則:
oclint-json-compilation-database -e Pods -e 3rd -- -rc=LONG_LINE=300 -rc=LONG_METHOD=200 -rc=TOO_MANY_METHODS=100 -rc=CYCLOMATIC_COMPLEXITY=50 -rc=NCSS_METHOD=120 -disable-rule ShortVariableName -disable-rule ObjCAssignIvarOutsideAccessors -disable-rule AssignIvarOutsideAccessors -max-priority-1=100000 -max-priority-2=100000 -max-priority-3=100000 -report-type html -o oclintReport.html
參考:https://juejin.cn/post/6844903853775650830#heading-6
http://docs.oclint.org/en/stable/howto/rcfile.html
http://docs.oclint.org/en/stable/rules/index.html
http://www.reibang.com/p/35cf99c07eaa