iOS- Xcode配置OCLint

一、OCLint介绍,为什么要使用OCLint

OCLint 是基于 Clang 的静态分析工具,支持对 C、C++ 和 Objective-C 代码进行静态分析,它基于 Clang 输出的抽象语法树对代码进行静态分析,支持与现有的 CI 集成,部署之后基本不需要维护,简单方便,实现code review自动化,检查代码中的缺陷:

    • 可能出现的bug,空的if/else/try/catch/finally的参数
    • 没有使用的变量或者参数
    • 复杂的代码逻辑,多个if/else的判断
    • 不需要的代码
    • 过长的方法或者参数
    • 错误的分配方式
    • ....

二、环境配置

1、通过Homebrew安装OCLint:

brew tap oclint/formulae
brew install Clint

如果已安装过OCLint可执行升级的指令:

brew update
brew upgrade oclint

2、用gem安装xcpretty

sudo gem install xcpretty

3、运行oclint确认OClint是否安装成功

oclint

如果展示以下内容即为安装成功:

TF013550:~ name$ oclint

oclint: Not enough positional command line arguments specified!

Must specify at least 1 positional argument: See: oclint -help


扫描二维码关注公众号,回复: 2646885 查看本文章

三、XCode配置Using OCLint in Xcode

1、首先在targets点添加按钮,在Cross-platform选项中,选“Aggregate”,然后命名为"OCLint":


2、选中OCLint Target,进入Build Phases选项,点左上角的加号,选择“New Run Script Phase”


添加脚本


在脚本输入框内输入脚本代码:

 chmod -R 777 $SRCROOT/oclint
 $SRCROOT/oclint/oclint.sh

3、在主工程目录下新建oclint文件夹,新建oclint.sh脚本文件,编辑内容如下:

cd到工程目录下,执行命令:

touch oclint.sh
source ~/.bash_profile
#获取项目路径
PROJECT_DIR=$(cd `dirname $0`;cd ..;pwd)
cd ${PROJECT_DIR}

buildPath="${PROJECT_DIR}/oclint/build"
compilecommandsJsonFolderPath="${PROJECT_DIR}/oclint"
compilecommandsJsonFilePath="${PROJECT_DIR}/oclint/compile_commands.json"

rm -rf "$compilecommandsJsonFolderPath/build"

xcodebuild SYMROOT=$buildPath | xcpretty -r json-compilation-database -o $compilecommandsJsonFilePath

cd $compilecommandsJsonFolderPath

oclint-json-compilation-database -- -report-type xcode \
-rc CYCLOMATIC_COMPLEXITY=10 \
-rc LONG_CLASS=1000 \
-rc LONG_METHOD=50 \
-rc LONG_LINE=140 \
-rc LONG_VARIABLE_NAME=30 \
-rc SHORT_VARIABLE_NAME=1 \
-rc MAXIMUM_IF_LENGTH=5 \
-rc MINIMUM_CASES_IN_SWITCH=2 \
-rc NCSS_METHOD=30 \
-rc NESTED_BLOCK_DEPTH=5 \
-rc TOO_MANY_METHOD=30 \
-rc TOO_MANY_PARAMETERS=5 \
-max-priority-1 0 \
-max-priority-2 5 \
-max-priority-3 10
4、xcode工程中选中scheme选择OCLint,command+B编译

Error:编译工程,一般会出现错误找不到complie_commands.json文件

Error: compile_commands.json not found at /Users/yuge/Desktop/LJTransfar/LJAPPS/TFParty/Trunk/oclint.

Command /bin/sh failed with exit code 98

解决办法:

1>、执行命令,使用xcpretty生成的文件名是:compilation_db.json

xcodebuild |xcpretty -r json-compilation-database

Copying YGInfo.h

Touching YGModle.framework

Build Succeeded

出现Build Success后表示运行完成,生成目录在build/reports中


2>、

把xcpretty生成的文件compilation_db.json复制到当前目录即oclint文件夹下下,重命名为compile_commands.json。


3>、command+B重新编译,编译完成之后xcode则出现各种警告,证明成功了



四、OCLint规则官网,参考博文翻译

1、多余的Else语句 Unnecessary else statement

当一个IF语句有返回语句,那么else 语句就不是必要的。剩下的语句可以不再代码块中运行。

 if (response.error == nil) {
        return YES;
    }
    else {
        if (error != NULL) {
            *error = [[NSError alloc] initWithDomain:USER_DOMAIN
                                                code:response.error.code
                                            userInfo:response.error.userInfo];
        }
    }
2、Useless parentheses 无用的括号

五、禁止OCLint的检查

使用语法

未完待续...

猜你喜欢

转载自blog.csdn.net/yuge486/article/details/80081473