美文网首页
OCLint使用

OCLint使用

作者: 司空123 | 来源:发表于2021-04-21 15:49 被阅读0次
    image.png

    OCLint的分析结果:
    优先级的级别是从Priority 1, Priority 2, Priority 3 依次降低的
    Total Files 总文件数
    Files with Violations 违规文件数
    Compiler Warnings 表示项目中的警告⚠️
    Compiler Errors 表示编译错误
    Location 表示警告的位置
    报告中的描述其实非常清晰,一般找到代码位置,结合代码理解

    一、OCLint 安装
    使用Homebrew 安装:在安装前,确保安装了 homebrew。

    $ brew tap oclint/formulae
    $ brew install oclint

    安装完以后验证下 OCLint 是否安装成功。输入
    $ oclint --version

    二、xcpretty 的安装
    在安装前,确保安装了 Ruby gem.
    $gem install xcpretty

    三、 使用 oclint
    1.如果项目使用了 Cocopod,则需要指定 -workspace xxx.workspace
    2.每次编译之前需要 clean
    3.cd 进入项目
    4.查看项目基本信息
    $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.编译.编译成功后,会在项目的文件夹下出现 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 报表
    $ oclint-json-compilation-database -e Pods -- -report-type html -o oclintReport.html

    看到有报错,但是报错信息太多了,不好定位,利用下面的脚本则可以将报错信息写入 log 文件,方便查看
    oclint-json-compilation-database -e Pods -- -report-type html -o oclintReport.html 2>&1 | tee 1.log

    报错信息:oclint: error: one compiler command contains multiple jobs:的解决方案如下:

    ProjectTargetsBuilding Settings下的 COMPILER_INDEX_STORE_ENABLE 设置为NO
    podfiletarget '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
    
    

    报错信息:oclint: error: violations exceed threshold: P1=0[0] P2=323[10] P3=4523[20]的解决方案如下:
    看到报错信息是默认的警告数量超过限制,则 lint 失败。事实上 lint 后可以跟参数,所以我们修改脚本如下:
    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规则


    image

    规则默认值:

    名称 描述 默认阈值
    CYCLOMATIC_COMPLEXITY 方法的循环复杂性(圈负责度) 10
    LONG_CLASS C类或Objective-C接口,类别,协议和实现的行数 1000
    LONG_LINE 一行代码的字符数 100
    LONG_METHOD 方法或函数的行数 50
    LONG_VARIABLE_NAME 变量名称的字符数 20
    MAXIMUM_IF_LENGTH if语句的行数 15
    MINIMUM_CASES_IN_SWITCH switch语句中的case数 3
    NPATH_COMPLEXITY 方法的NPath复杂性 200
    NCSS_METHOD 一个没有注释的方法语句数 30
    NESTED_BLOCK_DEPTH 块或复合语句的深度 5
    SHORT_VARIABLE_NAME 变量名称的字符数 3
    TOO_MANY_FIELDS 类的字段数 20
    TOO_MANY_METHODS 类的方法数 30
    TOO_MANY_PARAMETERS 方法的参数数 10

    根据实际需要修改部分检测规则:
    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
    https://www.jianshu.com/p/35cf99c07eaa

    相关文章

      网友评论

          本文标题:OCLint使用

          本文链接:https://www.haomeiwen.com/subject/sihzlltx.html