iOS项目的静态代码扫描之OClint使用

24 篇文章 0 订阅

上文介绍了如何安装OClint,这次简单介绍下如何使用OClint。

  1. 先确定要扫描的项目(本文使用了一个很久很久用来学习的demo)
  2. 通过CD命令进入到项目目录
cd /Users/XXX/Documents/MyXcodeDemo/Demo/UiDemo

/Users/XXX/Documents/MyXcodeDemo/Demo/UiDemo”是项目目录
3. 逐步输入扫描脚本

1、

# Cleanup before building
rm -f compile_commands.json
xctool -project UiDemo.xcodeproj -scheme UiDemo clean

2、

# Build Project
xctool build \
    -project UiDemo.xcodeproj -scheme UiDemo \
    -reporter json-compilation-database:compile_commands.json

3、

# Analyze Project
oclint-json-compilation-database -e Pods -- \
    -max-priority-1=100000 \
    -max-priority-2=100000 -max-priority-3=100000 \
    -disable-rule=InvertedLogic \
    -disable-rule=CollapsibleIfStatements \
    -disable-rule=UnusedMethodParameter \
    -disable-rule=LongLine \
    -disable-rule=LongVariableName \
    -disable-rule=ShortVariableName \
    -disable-rule=UselessParentheses \
    -disable-rule=IvarAssignmentOutsideAccessorsOrInit | sed 's/\(.*\.\m\{1,2\}:[0-9]*:[0-9]*:\)/\1 warning:/'

输出结果:

OCLint Report

Summary: TotalFiles=10 FilesWithViolations=1 P1=0 P2=0 P3=1 
/Users/river/Documents/MyXcodeDemo/Demo/UiDemo/UiDemo/YAViewController.m:30:1: warning: long method [size|P3] Method with 53 lines exceeds limit of 50
[OCLint (http://oclint.org) v0.10.3]

这里写图片描述
问题分析:

/Users/river/Documents/MyXcodeDemo/Demo/UiDemo/UiDemo/YAViewController.m 是违反规则的文件
30是违反规则的代码位置
long method是违反的规则名称
ethod with 53 lines exceeds limit of 50是违反的规则描述

具体查阅官方文档
http://docs.oclint.org/en/stable/rules/index.html

最后清理扫描结果文件

# Final cleanup
rm -f compile_commands.json

为了方便我们统计分析,我们可以通过html来输出结果,其脚本如下:

oclint-json-compilation-database -v \
> -e Pods \
> oclint_args -- -report-type html -o oclintReport.html \
> -disable-rule ObjCAssignIvarOutsideAccessors \
> -max-priority-1=100000 \
> -max-priority-2=100000 -max-priority-3=100000 \
>    -disable-rule=InvertedLogic \
>    -disable-rule=CollapsibleIfStatements \
>  -disable-rule=UnusedMethodParameter \
> -disable-rule=LongLine \
> -disable-rule=LongVariableName \
> -disable-rule=ShortVariableName \
> -disable-rule=UselessParentheses \
> -disable-rule=IvarAssignmentOutsideAccessorsOrInit

执行后会在项目目录输出oclintReport.html,通过oclintReport.html可以分析。
这里写图片描述
File - 违反规则的文件
Location - 违反规则的代码位置
Rule Name - 违反的规则名称
Rule Category - 违反的规则类别
Priority - 违反的规则优先级
Message - 违反的规则描述

大概功能简单介绍下,具体问题具体分析~~

脚本下载

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值