15

I have created a module in Magento 2 and now I am trying to submit it on Magento Marketplace. My extension has passed from business review and Technical review but I am facing issues with QA review.

I recieved a mail from Magento marketplace stating that I had code duplication in my extension. Below is the sample of the mail.

Code quality issues: CPD: This extension contains duplicated code.

When I went to my product on Marketplace account, and checked the technical report, I found below.

Code Duplicates Detected

This extension contains code that is directly copied from the Magento codebase. This is in direct violation of Sections 3.1 and 9.1b of the Magento Developer Agreement.

File: vendor/module/vendor-module-1.0.0.0/Block/Adminhtml/Module/Edit/Tab/Stores.php
Line: 58
File: magento/module-checkout-agreements/magento-module-checkout-agreements-100.0.6.0/Block/Adminhtml/Agreement/Edit/Form.php
Line: 122

File: magento/module-cms/magento-module-cms-100.0.7.0/Block/Adminhtml/Block/Edit/Form.php
Line: 100
File: vendor/module/vendor-module-1.0.0.0/Block/Adminhtml/Module/Renderer/Files.php
Line: 49

File: magento/framework/magento-framework-100.0.16.0/Data/Form/Element/Image.php
Line: 86
File: vendor/module/vendor-module-1.0.0.0/Model/ResourceModel/AbstractCollection.php
Line: 2
File: magento/module-cms/magento-module-cms-100.0.7.0/Model/ResourceModel/AbstractCollection.php
Line: 6

Is there any way I can check for code duplication on my setup to avoid this issue for my other extensions?

6

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

  • 1
    Hello nikhil, can you please explain in detail – Sagar Dobariya Jul 19 '17 at 6:58
6

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.