how to check code duplication in magento 2 Extension?

Magento 2 Setup folder

Step-1 for check with code extension

/dev/tests/static/testsuite/Magento/Test/Php/_files/phpcpd/blacklist
rename common.txt--

Step-2 run below command

php bin/magento dev:tests:run static

Step -3 see duplication code

dev/tests/static/report
phpcpd_report.xml

Now check phpcpd_report.xml


Here is some description for the Magento 2 command used to check code duplication.

The command to check code duplication/copy-paste is below.

php bin/magento dev:tests:run static

This command will first go to dev/tests/static folder. Here you can see declaration file phpunit.xml.dist for this test suite.

<testsuites>
    <testsuite name="Less Static Code Analysis">
        <file>testsuite/Magento/Test/Less/LiveCodeTest.php</file>
    </testsuite>
    <testsuite name="Javascript Static Code Analysis">
        <file>testsuite/Magento/Test/Js/LiveCodeTest.php</file>
    </testsuite>
    <testsuite name="PHP Coding Standard Verification">
        <file>testsuite/Magento/Test/Php/LiveCodeTest.php</file>
    </testsuite>
    <testsuite name="Code Integrity Tests">
        <directory>testsuite/Magento/Test/Integrity</directory>
    </testsuite>
    <testsuite name="Xss Unsafe Output Test">
        <file>testsuite/Magento/Test/Php/XssPhtmlTemplateTest.php</file>
    </testsuite>
</testsuites>

In this file, you will find above code which will define which file to execute for different code tests.

To narrow down you can see PHP Coding Standard Verification testsuite This will execute file testsuite/Magento/Test/Php/LiveCodeTest.php

When you open this file, you will find different functions to check for different types of code issues. The function which will be executed is testCopyPaste

public function testCopyPaste()
{
    $reportFile = self::$reportDir . '/phpcpd_report.xml';
    $copyPasteDetector = new CopyPasteDetector($reportFile);

    if (!$copyPasteDetector->canRun()) {
        $this->markTestSkipped('PHP Copy/Paste Detector is not available.');
    }

    $blackList = [];
    foreach (glob(__DIR__ . '/_files/phpcpd/blacklist/*.txt') as $list) {
        $blackList = array_merge($blackList, file($list, FILE_IGNORE_NEW_LINES));
    }

    $copyPasteDetector->setBlackList($blackList);

    $result = $copyPasteDetector->run([BP]);

    $output = "";
    if (file_exists($reportFile)) {
        $output = file_get_contents($reportFile);
    }

    $this->assertTrue(
        $result,
        "PHP Copy/Paste Detector has found error(s):" . PHP_EOL . $output
    );
}

Here, you will find a code which will used to blacklist any file/folders from this code check.

foreach (glob(__DIR__ . '/_files/phpcpd/blacklist/*.txt') as $list) {
    $blackList = array_merge($blackList, file($list, FILE_IGNORE_NEW_LINES));
}

This foreach function will check for any .txt file added in dev/tests/static/testsuite/Magento/Test/Php/_files/phpcpd/blacklist location. It will read the file and will ignore all the folders to exclude from copy paste code detection process.

After adding all blacklist files/folders to code, it will run below code.

$result = $copyPasteDetector->run([BP]);

This code will execute run function of dev/tests/static/framework/Magento/TestFramework/CodingStandard/Tool/CopyPasteDetector.php file.

public function run(array $whiteList)
{
    $blackListStr = ' ';
    foreach ($this->blacklist as $file) {
        $file = escapeshellarg(trim($file));
        if (!$file) {
            continue;
        }
        $blackListStr .= '--exclude ' . $file . ' ';
    }

    $vendorDir = require BP . '/app/etc/vendor_path.php';
    $command = 'php ' . BP . '/' . $vendorDir . '/bin/phpcpd' . ' --log-pmd ' . escapeshellarg(
            $this->reportFile
        ) . ' --names-exclude "*Test.php" --min-lines 13' . $blackListStr . ' ' . implode(' ', $whiteList);

    exec($command, $output, $exitCode);

    return !(bool)$exitCode;
}

Here, the code adds all the blacklisted folders/files in --exclude list.

After that it will run vendor/bin/phpcpd command.

Here in the command itself Magento has

excluded all Test files by code

--names-exclude "*Test.php" 

It has also skipped all the code duplicates which are less than 13 lines by code

--min-lines 13

The output for this command execution will be added to the file defined in testCopyPaste function. The filename for copy-paste detection is phpcpd_report.xml located at dev/tests/static/report location.

After successful execution of the command, the output will be added to report files.