1

I've created an extension which I uploaded on marketplace for technical review. I got the following response in technical submission:

Review Results:

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

vendor/MCB/CreditmemoCronTime.php - similar to the Magento2 - Fail
magento/module-backup/magento-module-backup-100.3.0-beta32/Model/Config/Backend/Cron.php
vendor/MCB/ContactsCronTime.php - similar to the Magento2 - Fail
magento/module-backup/magento-module-backup-100.3.0-beta32/Model/Config/Backend/Cron.php
vendor/MCB/InvoicesCronTime.php - similar to the Magento2 - Fail
magento/module-backup/magento-module-backup-100.3.0-beta32/Model/Config/Backend/Cron.php
vendor/MCB/ItemsCronTime.php - similar to the Magento2 - Fail
magento/module-backup/magento-module-backup-100.3.0-beta32/Model/Config/Backend/Cron.php

ContactsCronTime.php file

<?php

namespace Vendor\Test\Model\Config\Backend;

class ContactsCronTime extends \Magento\Framework\App\Config\Value
{
    /**
     * Cron string path variable
     */
    const CRON_STRING_PATH = 'crontab/default/jobs/test_contacts_job/schedule/cron_expr';

    /**
     * Cron model path variable
     */
    const CRON_MODEL_PATH = 'crontab/default/jobs/test_contacts_job/run/model';

    /**
     * @var \Magento\Framework\App\Config\ValueFactory
     */
    protected $_configValueFactory;

    /**
     * @var string
     */
    protected $_runModelPath = '';

    /**
     *
     * @param \Magento\Framework\Model\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
     * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
     * @param \Magento\Framework\App\Config\ValueFactory $configValueFactory
     * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
     * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
     * @param string $runModelPath
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\App\Config\ScopeConfigInterface $config,
        \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
        \Magento\Framework\App\Config\ValueFactory $configValueFactory,
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
        $runModelPath = '',
        array $data = []
    ) {
        $this->_runModelPath = $runModelPath;
        $this->_configValueFactory = $configValueFactory;
        parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
    }

    /**
     * {@inheritdoc}
     *
     * @return $this
     * @throws \Exception
     */
    public function afterSave()
    {
        $time = $this->getData('groups/synchronization/groups/test_contacts/fields/cron_time/value');

        // phpcs:disable
        if ($time == '60') {
            $time = 0;
            $cronExprArray = [
                intval($time), //Minute
                '*', //Hour
                '*', //Day of the Month
                '*', //Month of the Year
                '*', //Day of the Week
            ];
        } elseif ($time == '120') {
            $time = 2;
            $cronExprArray = [
                '0', //Minute
                '*/'.intval($time), //Hour
                '*', //Day of the Month
                '*', //Month of the Year
                '*', //Day of the Week
            ];
        } else {
            $cronExprArray = [
                '*/'.intval($time), //Minute
                '*', //Hour
                '*', //Day of the Month
                '*', //Month of the Year
                '*', //Day of the Week
            ];
        }

        $cronExprString = join(' ', $cronExprArray);

        try {
            $this->_configValueFactory->create()->load(
                self::CRON_STRING_PATH,
                'path'
            )->setValue(
                $cronExprString
            )->setPath(
                self::CRON_STRING_PATH
            )->save();
            $this->_configValueFactory->create()->load(
                self::CRON_MODEL_PATH,
                'path'
            )->setValue(
                $this->_runModelPath
            )->setPath(
                self::CRON_MODEL_PATH
            )->save();
        } catch (\Exception $e) {
            throw new \Exception(__('We can\'t save the cron expression.'));
        }
        // phpcs:enable

        return parent::afterSave();
    }
}

Although core file and mine file code is totally different.

Can anyone explain what is the meaning of this error and how can I fix this to resubmit it again for technical review?

1 Answer 1

1

You are using the duplicated code of Magento core class

magento/module-backup/magento-module-backup-100.3.0-beta32/Model/Config/Backend/Cron.php

in your following classes

vendor/MCB/CreditmemoCronTime.php
vendor/MCB/ContactsCronTime.php
vendor/MCB/InvoicesCronTime.php
vendor/MCB/ItemsCronTime.php

you need to extend your classes from Magento core class instead of duplicating its code

EDIT

please check it your code is duplicated with the core file

enter image description here

10
  • My custom file code is totally different from magento core file. Only 1 thing is similar between mine and that core file. We both are extending same class.
    – DJ Dev
    Dec 27, 2019 at 5:11
  • then you should extend with the core class instead of extending it with the other class which the core class is already extended to
    – ASQ
    Dec 27, 2019 at 5:13
  • I am not using magento-module-backup module in my extension anywhere then why should i extend it's class. And as i also checked many core files which are also extending that core class which I and magento-module-backup-100.3.0-beta32/Model/Config/Backend/Cron.php module-backup file using then why marketplace technical review showing only module-backup class as duplicate.
    – DJ Dev
    Dec 27, 2019 at 5:19
  • can you share the code of your class
    – ASQ
    Dec 27, 2019 at 5:39
  • I have updated code in my question. Please check.
    – DJ Dev
    Dec 27, 2019 at 5:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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