Magento – Why the use of function file_get_content(), fopen() is discouraged in the magento 2

extensionsmagento-2.1magento2meqp2

I am developing a Magento 2 extension, rejected by QA MARKETPLACE team three times due to this warnings and this is basic of my extension..

1. *

    FILE:
        ...ranslation\Block\Adminhtml\System\Config\Form\Field\ClientId.php
            ---------------------------------------------------------------------- FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
            ----------------------------------------------------------------------  27 | WARNING | Use of protected class members is discouraged.
            ----------------------------------------------------------------------


            FILE: ...lation\Block\Adminhtml\System\Config\Form\Field\ClientSecret.php
            ---------------------------------------------------------------------- FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
            ----------------------------------------------------------------------  26 | WARNING | Use of protected class members is discouraged.
            ----------------------------------------------------------------------


            FILE: ...translation\Block\Adminhtml\System\Config\Form\Field\Credits.php
            ---------------------------------------------------------------------- FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
            ----------------------------------------------------------------------  27 | WARNING | Use of protected class members is discouraged.
            ----------------------------------------------------------------------


            FILE: ...rstranslation\Block\Adminhtml\System\Config\Form\Field\IsLog.php
            ---------------------------------------------------------------------- FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
            ----------------------------------------------------------------------  26 | WARNING | Use of protected class members is discouraged.
            ----------------------------------------------------------------------


            FILE: ...slation\Block\Adminhtml\System\Config\Form\Field\LoginSubmit.php
            ---------------------------------------------------------------------- FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
            ----------------------------------------------------------------------  14 | WARNING | Property name "$_template" should not be prefixed
                |         | with an underscore to indicate visibility  52 | WARNING | The method parameter $element is never used
            ----------------------------------------------------------------------


            FILE: ...lation\Block\Adminhtml\System\Config\Form\Field\LogoutSubmit.php
            ---------------------------------------------------------------------- FOUND 0 ERRORS AND 3 WARNINGS AFFECTING 2 LINES
            ----------------------------------------------------------------------  14 | WARNING | Property name "$_template" should not be prefixed
                |         | with an underscore to indicate visibility  48 | WARNING | Use of protected class members is discouraged.  48 |
        WARNING | The method parameter $element is never used
            ----------------------------------------------------------------------

            FILE: ...agento2\app\code\Marstranslation\Marstranslation\Helper\Data.php
            ---------------------------------------------------------------------- FOUND 0 ERRORS AND 9 WARNINGS AFFECTING 8 LINES
            ----------------------------------------------------------------------   85 | WARNING | [ ] Function's cyclomatic complexity (20) exceeds
                 |         |     10; consider refactoring the function  195 | WARNING | [ ] The use of function fopen() is discouraged 302 |
        WARNING | [ ] Array size calculation function count() detected
                 |         |     in loop    309 | WARNING | [ ] Array size calculation function count() detected
                 |         |     in loop  364 | WARNING | [ ] The direct use of ObjectManager is discouraged.
                 |         |     Inject necessary dependencies via constructor. FILE:
        ...ento2\app\code\Marstranslation\Marstranslation\Model\Project.php
            ---------------------------------------------------------------------- FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
            ----------------------------------------------------------------------  15 | WARNING | Use of protected class members is discouraged.
            ---------------------------------------------------------------------- FILE:
        ...app\code\Marstranslation\Marstranslation\Setup\InstallSchema.php
            ---------------------------------------------------------------------- FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
            ----------------------------------------------------------------------  16 | WARNING | There was not found any index in database schema
                |         | file.
            ----------------------------------------------------------------------

*

Best Answer

Cyclomatic complexity validate errors:

Magento marketplace rejected with above error means your code doesn't validate Cyclomatic complexity.

I can suggest solutions for some errors, not all.

FILE: ...agento2\app\code\Marstranslation\Marstranslation\Helper\Data.php

1. 85 | WARNING | [ ] Function's cyclomatic complexity (20) exceeds 10; consider refactoring the function
This error is because when code is complexity is checked, it will check how many loops in the code. More loops will increase cyclomatic complexity of your code.

So, reduce number of loops in the code. Create another function and add some code in new function to refactor. This will reduce cyclomatic complexity.

2. 195 | WARNING | [ ] The use of function fopen() is discouraged Using fopen() in the code will increase possibility to hack your code. This is discouraged to use in magento2. Because magento2 provides framework functions for this.

i.e. \Magento\Framework\Filesystem\Driver\File:fileOpen

3. 302 | WARNING | [ ] Array size calculation function count() detected in loop..... 309 | WARNING | [ ] Array size calculation function count() detected in loop

I think there should not be any functions like count, load inside the looping. To avoid this error, create new function and add you code for count() inside the new function. If you using ->count() method on collection to check collection count, then this should not use. You should use ->getSize() method on collection.

4. 364 | WARNING | [ ] The direct use of ObjectManager is discouraged. Inject necessary dependencies via constructor.

Object manager is deprecated. If you want to load method of any class then you must use dependency injection functionality of Magento2.

Related Topic