Xcode (swift) Unused Code 찾기 및 CPD (중복코드 찾기) - kirseia/study GitHub Wiki

1. Unused 코드 찾기

# Type a script or drag a script file from your workspace to insert its path.

cd "$SRCROOT/"
./../unused/unused.rb xcode

이런식으로 추가하면 됨

  • API 통신을 위해서 만들어놓은 model이 사용되지 않은 데이터가 있거나 할 때 (현재 사용하지 않지만 미래를 위해 있는 것들)
  • unused warning 이 나와서 가끔 한번씩 돌려서 쓰는게 좋을 듯

2. CPD (Copy&Paste Detector 중복코드 찾기)

  • PMD를 통해서 할 수 있음

  • 자바를 사용하므로 자바가 설치 안되어있으면 자바를 추가 설치해야 함 (brew cask install adoptopenjdk)

  • https://pmd.github.io/latest/pmd_userdocs_cpd.html 에서 받아서 필요한 lib 만 남겨서 사용하면 용량 줄일 수 있음

    • 또는 brew install pmd
  • 커맨드 라인 실행은

./BDS/PMD/bin/run.sh cpd --files VlogEditor/ --minimum-tokens 100 --language swift --encoding UTF-8  --failOnViolation true
  • Xcode에서 사용하려면 참고 <- 를 확인
# Type a script or drag a script file from your workspace to insert its path.
cd "$SRCROOT/"

./../BDS/PMD/bin/run.sh cpd --files ${EXECUTABLE_NAME} --minimum-tokens 150 --language swift --encoding UTF-8 --format net.sourceforge.pmd.cpd.XMLRenderer > cpd-output.xml --failOnViolation true

# Running script
php ./../BDS/PMD/cpd_script.php -cpd-xml cpd-output.xml

그리고 cpd_script.php 파일이 필요한데 보기 좋게 살짝 수정해봤음

<?php
foreach (simplexml_load_file('cpd-output.xml')->duplication as $duplication) {
    $files = $duplication->xpath('file');
    foreach ($files as $file) {
        echo $file['path'].':'.$file['line'].':1: warning: '.$duplication['lines'].' lines Copy&Pasted from: '
            .implode(', ', array_map(function ($otherFile) { return implode("/", array_slice(explode("/", $otherFile['path']), -2, 2, false)).':'.$otherFile['line']; },
            array_filter($files, function ($f) use (&$file) { return $f != $file; }))).PHP_EOL;
    }
}
?>
  • 사용해보니 중복 코드 찾기는 확실히 좋았음.
  • 빌드 하면 바로 확인 가능하니 바로 class로 빼거나 method extract를 하거나 등의 조치를 취할 수 있음.