preface

OCLint is a static code review tool for checking code quality

The deployment environment

There are many similar tutorials online, please refer to OCLint in Xcode using OCLint to implement Code Review – to improve the quality of your Code

The script

#! / bin/bash - il source ~ /. Bashrc myworkspace = OCLintProject. Xcworkspace # fill in the workspace myscheme = OCLintProject # fill in the Target If [-d./compile_commands. Json]; Then echo '----- 'to clear the last compiled data -----' rm compile_commands.json; rm $reportFile; Fi # clean -- build -- OCLint analyse echo '----- start generating compile data -----'; xcodebuild -workspace $myworkspace -scheme $myscheme clean&& xcodebuild -workspace $myworkspace -scheme $myscheme \ -configuration Debug -sdk iphoneos \ CLANG_ENABLE_MODULE_DEBUGGING=NO CODE_SIGNING_REQUIRED=NO ENABLE_BITCODE=NO COMPILER_INDEX_STORE_ENABLE=NO \ | xcpretty -r json-compilation-database -o compile_commands.json if [ -f ./compile_commands.json] then echo '----- Compile data is generated -----' else echo "----- Failed to compile data -----" return-1 fi oclint-json-compilation-database -e Pods -e QMUI -e Libraries -- \ -report-type $reportType \ -o $reportFile \ -rc LONG_LINE=300 \ -rc LONG_METHOD=200 \ -rc LONG_VARIABLE_NAME=40 \ -rc LONG_CLASS=3000 \ -max-priority-1=10000 \ -max-priority-2=30000 \ -max-priority-3=50000 \ if [-f./$reportFile] then echo '----- 'else echo '----- Analysis failed -----' fiCopy the code
  1. First you need to use xcodeBuild clean and build projects and add COMPILER_INDEX_STORE_ENABLE=NO otherwise you may get oclint: error: Error message One Compiler Command contains multiple Jobs
xcodebuild -workspace $myworkspace -scheme $myscheme clean&&xcodebuild -workspace $myworkspace -scheme $myscheme
Copy the code
  1. Run the xcpretty command to analyze log information. Xcpretty is the tool used to format the xCodeBuild output.
xcpretty -r json-compilation-database -o compile_commands.json
Copy the code
  1. Analyze the code using the oclint-json-compilation-database command
  • -e Specifies the files to be ignored for analysis. Warnings for these files will not appear in the report
  • -rc Specifies the threshold of the rule to be overridden. You can customize the item threshold, default threshold
  • -enable-rule Specifies the supported rules. By default, oclint supports all the rules. You can combine -disable-rule to filter out some rules
  • -disable-rule Specifies the rule to be ignored based on the project requirements
  • -report-type Indicates the type of the report to be analyzed. The value can be text, HTML, XML, JSON, or PMDSample; Readable HTML or PMD is preferred; So here we takepmdType, used for combinationPMD analysisGenerate PMD warnings, which can be displayed in Jenkins’ kanban in a friendly way.
  1. Analysis of the rules
The name of the describe The default threshold
CYCLOMATIC_COMPLEXITY Cyclic complexity of method (cyclic accountability) 10
LONG_CLASS Number of lines of C class or Objective-C interface, category, protocol and implementation 1000
LONG_LINE The number of characters in a line of code 100
LONG_METHOD The number of rows of a method or function 50
LONG_VARIABLE_NAME The number of characters in the variable name 20
MAXIMUM_IF_LENGTH The number of rows in the if statement 15
MINIMUM_CASES_IN_SWITCH The number of cases in the switch statement 3
NPATH_COMPLEXITY Method NPath complexity 200
NCSS_METHOD The number of uncommented method statements 30
NESTED_BLOCK_DEPTH The depth of a block or compound statement 5
SHORT_VARIABLE_NAME The number of characters in the variable name 3
TOO_MANY_FIELDS The number of fields in the class 20
TOO_MANY_METHODS The number of methods of the class 30
TOO_MANY_PARAMETERS The number of arguments to the method 10
More detailed default rules are availableHere,To view
  1. Analysis results: