代码统计、分析工具

分享两个工具json

关于第三方工具的安装,推荐使用Homebrew,唐巧大神也在博客中推荐过。使用brew search xxxx来查看是否有对应的工具可使用Homebrew安装。xcode

CLOC

CLOC是专门用于代码统计的命令行工具,支持几乎全部语言。bash

使用Homebrew安装:brew install cloc,使用也很是简单,命令行进入项目目录,执行cloc ./便可,这样会统计目录下的全部代码。若是不但愿统计某些文件夹,能够设置须要忽略的目录,好比忽略Pods文件夹:cloc ./ --exclude-dir=Podsapp

统计结果:tool-1.png工具

oclit

oclit做为一个静态的代码分析工具,功能很是强大,并且出自国人之手。ui

为了一劳永逸,首先安装xctoolbrew install xctool,这是Facebook提供的代替苹果xcodebuild的工具。spa

安装oclit:brew install Caskroom/cask/oclint,为了可以在Xcode上直接显示代码分析的结果,还须要完成如下几步.net

  1. 添加Aggregate,![image](/images/Tool/tool-2.png)![image](/images/Tool/tool-3.png)
  2. 为刚才建立的Aggregate添加运行脚本,注意:不要在选中Aggregate的状态下点击Editor菜单,否则Add Run Script Build Phase按钮是灰色的 ![image](/images/Tool/tool-4.png)
  3. 添加脚本代码,脚本代码和官方文档有些不一样。
#import path
export PATH=${PATH}:/usr/local/bin
#import what we have in bash_profile
source ~/.bash_profile
#check for oclint
hash oclint &> /dev/null
if [ $? -eq 1 ]; then
echo >&2 "oclint not found, analyzing stopped"
exit 1
fi

hash xctool &> /dev/null
if [ $? -eq 1 ]; then
echo >&2 "xctool not found, analyzing stopped"
exit 1
fi

cd ${SRCROOT}
xctool -workspace MyProject.xcworkspace -scheme MyProject clean
xctool -workspace MyProject.xcworkspace -scheme MyProject -reporter json-compilation-database:compile_commands.json build
oclint-json-compilation-database \
-e Pods \
-- -rc=LONG_LINE=100 \
-rc=NCSS_METHOD=60 \
-rc=MINIMUM_CASES_IN_SWITCH=1 \
| sed 's/\(.*\.\m\{1,2\}:[0-9]*:[0-9]*:\)/\1 warning:/'

注意:上面代码中的MyProject须要替换成你的工程名,若是你的工程不是用workspace来管理的,那么其中这两行代码命令行

xctool -workspace MyProject.xcworkspace -scheme MyProject clean
xctool -workspace MyProject.xcworkspace -scheme MyProject -reporter json-compilation-database:compile_commands.json build

须要替换为code

xctool -project MyProject.xcodeproj -scheme MyProject clean
xctool -project MyProject.xcodeproj -scheme MyProject -reporter json-compilation-database:compile_commands.json build

4.编译刚才建立的Aggregate,须要耐性等待结果,完成以后就能看到oclint给出的结果

错误排查

若build成功了可是没有给出任何警告,极可能是出现了错误

  1. 检查![image](/images/Tool/tool-5.png)
    是否有警告,以前就遇到team没选择对,致使没有分析结果,或者像我同样选择None。
  2. 检查是否存在~目录下.bash_profile文件,由于此文件是隐藏文件,首先要让Finder能显示隐藏文件,命令行执行defaults write com.apple.finder AppleShowAllFiles TRUEkillall Finder,在Finder中Command+Shift+G前往文件夹,填写~查看,若无此文件,命令行进入次目录建立一个便可:cd ~touch .bash_profile。恢复隐藏文件的隐藏defaults write com.apple.finder AppleShowAllFiles FALSEkillall Finder

分析结果

oclit的代码分析结果包括三种警告等级从高到底依次是:P1,P2,P3。

P3

  • 变量名长度:Long variable name P3 Variable name with 28 characters is longer than the threshold of 20 变量名超过20个字符,固然20的阀值是能够设置,以后会介绍。
  • 反向逻辑:Inverted logic,好比
if (![response isKindOfClass:[NSError class]]) {
    //
}
else{
    //
}
  • 方法行数:Long method P3 Method with 179 lines exceeds limit of 100,方法下的代码超过100行,不包括注释。
  • 参数未使用:Unused method parameter P3 The parameter 'section' is unused.,不过这个警告在IOS开发中比较广泛,能够忽略
  • 嵌套深度Deep nested block P3 Block depth of 8 exceeds limit of 5,好比各类block的嵌套或者if/else的嵌套

固然还有不少其余的状况

P2

  • 圈复杂度(CCN):High cyclomatic complexity P2 Cyclomatic Complexity Number 31 exceeds limit of 10,意味着代码的复杂程度
  • NPath 复杂度:/High npath complexity P2 NPath Complexity Number 384 exceeds limit of 200,具体不是很清楚

目前遇到的P2就是这些,P1的尚未遇到过。

回到刚才的脚本代码中

-- -rc=LONG_LINE=300 \
-rc=NCSS_METHOD=60 \

这是oclit给出的参数设置LONG_LINE表示每行代码的最长字符数,NCSS_METHOD表示有效代码行的最大行数。0.8dev版本的其余参数包括

CYCLOMATIC_COMPLEXITY
Cyclomatic complexity of a method, default value is 10

LONG_CLASS
Number of lines for a C++ class or Objective-C interface, category, protocol, and implementation, default value is 1000

LONG_LINE
Number of characters for one line of code, default value is 100

LONG_METHOD
Number of lines for a method or function, default value is 50

MINIMUM_CASES_IN_SWITCH
Count of case statements in a switch statement, default value is 3

NPATH_COMPLEXITY
NPath complexity of a method, default value is 200

NCSS_METHOD
Number of non-commenting source statements of a method, default value is 30

NESTED_BLOCK_DEPTH
Depth of a block or compound statement, default value is 5

惋惜的是,并无Unused method parameter相关参数。

总之,oclint的分析仍是具备比较高的参考性,对于提升代码的可读性、可维护性仍是有必定帮助的。

相关文章
相关标签/搜索