iOS代码查重工具

代码查重现状

很多IDE里面都有自己的代码查重工具,比如WebStorm的代码查重工具做的非常好用。但是Xcode里面至今没有自己的代码查重工具。这里调研了一些常见的代码查重工具,最后选择PMD里面的CPDjscpd,并将其集成到Xcode中,重复代码会以warning的形式出现在Xcode里面,方便查看。

一些IDE调研结果:

IDE Build-in Tool Extension
Xcode No No
AppCode No No
Android Studio Yes (Find and Replace Code Duplications…)
WebStorm Yes
VSCode No Copy/Paste Detector extension, based on jscpd
Visual Studio Yes

一些查重工具调研结果:

Tool Swift Support Objc Support configuration file Xocde Integration Note
JenkinsCI-iOS Yes Yes A Jenkins job setup for your XCode project. The CPD in PMD is the core of JenkinsCI-iOS.
CPD in PMD Yes Yes No Yes 对于其他语言,pmd都包含代码静态分析工具,但是对于Swift,只有一个代码查重工具CPD(Copy Paste Detector)。因此,如果想对Swift代码进行静态分析,可以选择SwiftLint。
jscpd Yes Yes Yes (yaml) Yes 支持swift、objective-c以及其它一些语言。

一些不容易写到表格中的信息

PMD

PMD

Supported IDEs

  • Maven PMD plugin
  • Gradle: The PMD Plugin
  • Eclipse plugin
  • NetBeans plugin
  • JBuilder plugin
  • JDeveloper plugin
  • IntelliJ IDEA plugin

没有Xcode的插件,但是可以通过在Build Phase里面加入Run Script来使用。

CPD has GUI

On Mac, display the GUI by running:

1
./run.sh cpdgui

The screen shot is like:

screenshot_cpd

将CPD集成到XCode里面

Integrating Copy-Paste-Detector for Swift in Xcode: This is a newer post on April 2017 and gives a detailed step by step way for integrating CPD into Xcode:

  • Add run script into Build Phase.
  • Xcode warnings will appear at the place of duplicated code.

Refer to 将代码查重工具CPD集成到Xcode for details.

Интегрируем Copy-Paste-Detection в Xcode, и не только: Integrate CPD into XCode, but this post is a little older on 2012.

NiCad4 Clone Detector

NiCad4 Clone Detector

NiCad handles a range of languages, including C, Java, Python, and C#, and provides a range of normalizations, filters and abstractions.

Visual Studio

Visual Studio Enterprise有自己的code clone analysis,see Finding Duplicate Code by using Code Clone Detection。copy一段code,然后改了某个变量名称,仍然可以被检测出重复。

The following modifications can be made, and the clone will still be recognized. In each case, there is a tolerance of a specific number of such modifications:

* Rename identifiers.
* Insert and delete statements.
* Rearrange statements.

Simian - Similarity Analyser

Simian - Similarity Analyser