YetiForceCompany/YetiForceCRM

View on GitHub
modules/Settings/PDF/actions/ExportTemplate.php

Summary

Maintainability
A
2 hrs
Test Coverage
F
0%

Method process has 40 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public function process(App\Request $request)
    {
        $recordId = $request->getInteger('id');
        $pdfModel = Vtiger_PDF_Model::getInstanceById($recordId);

Severity: Minor
Found in modules/Settings/PDF/actions/ExportTemplate.php - About 1 hr to fix

    Function process has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
    Open

        public function process(App\Request $request)
        {
            $recordId = $request->getInteger('id');
            $pdfModel = Vtiger_PDF_Model::getInstanceById($recordId);
    
    
    Severity: Minor
    Found in modules/Settings/PDF/actions/ExportTemplate.php - About 55 mins to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Missing class import via use statement (line '25', column '14').
    Open

            $xml = new DOMDocument('1.0', 'utf-8');

    MissingImport

    Since: 2.7.0

    Importing all external classes in a file through use statements makes them clearly visible.

    Example

    function make() {
        return new \stdClass();
    }

    Source http://phpmd.org/rules/cleancode.html#MissingImport

    The method process uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
    Open

                    } else {
                        $value = '';
                    }

    ElseExpression

    Since: 1.4.0

    An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.

    Example

    class Foo
    {
        public function bar($flag)
        {
            if ($flag) {
                // one branch
            } else {
                // another branch
            }
        }
    }

    Source https://phpmd.org/rules/cleancode.html#elseexpression

    Avoid using static access to class 'Vtiger_PDF_Model' in method 'process'.
    Open

            $pdfModel = Vtiger_PDF_Model::getInstanceById($recordId);

    StaticAccess

    Since: 1.4.0

    Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

    Example

    class Foo
    {
        public function bar()
        {
            Bar::baz();
        }
    }

    Source https://phpmd.org/rules/cleancode.html#staticaccess

    The method process uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
    Open

                } else {
                    $value = $pdfModel->get($field);
                    $xmlColumn = $xml->createElement($field, html_entity_decode($value, ENT_COMPAT, 'UTF-8'));
                }

    ElseExpression

    Since: 1.4.0

    An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.

    Example

    class Foo
    {
        public function bar($flag)
        {
            if ($flag) {
                // one branch
            } else {
                // another branch
            }
        }
    }

    Source https://phpmd.org/rules/cleancode.html#elseexpression

    Each class must be in a namespace of at least one level (a top-level vendor name)
    Open

    class Settings_PDF_ExportTemplate_Action extends Settings_Vtiger_Index_Action

    The class Settings_PDF_ExportTemplate_Action is not named in CamelCase.
    Open

    class Settings_PDF_ExportTemplate_Action extends Settings_Vtiger_Index_Action
    {
        /** {@inheritdoc} */
        public function process(App\Request $request)
        {

    CamelCaseClassName

    Since: 0.2

    It is considered best practice to use the CamelCase notation to name classes.

    Example

    class class_name {
    }

    Source

    Avoid variables with short names like $im. Configured minimum length is 3.
    Open

                        $im = file_get_contents($watermarkPath);

    ShortVariable

    Since: 0.2

    Detects when a field, local, or parameter has a very short name.

    Example

    class Something {
        private $q = 15; // VIOLATION - Field
        public static function main( array $as ) { // VIOLATION - Formal
            $r = 20 + $this->q; // VIOLATION - Local
            for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
                $r += $this->q;
            }
        }
    }

    Source https://phpmd.org/rules/naming.html#shortvariable

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $pdfModel = Vtiger_PDF_Model::getInstanceById($recordId);

    Spaces must be used to indent lines; tabs are not allowed
    Open

            header('content-type: application/xml; charset=utf-8');

    Spaces must be used to indent lines; tabs are not allowed
    Open

            header('cache-control: private');

    Spaces must be used to indent lines; tabs are not allowed
    Open

        /** {@inheritdoc} */

    Spaces must be used to indent lines; tabs are not allowed
    Open

        public function process(App\Request $request)

    Spaces must be used to indent lines; tabs are not allowed
    Open

            header('content-disposition: attachment; filename="' . $recordId . '_pdftemplate.xml"');

    Spaces must be used to indent lines; tabs are not allowed
    Open

            foreach (Settings_PDF_Module_Model::$allFields as $field) {

    Spaces must be used to indent lines; tabs are not allowed
    Open

        {

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $xmlField = $xml->createElement('field');

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $request->validateReadAccess();

    Spaces must be used to indent lines; tabs are not allowed
    Open

        {

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $xmlFields->appendChild($xmlField);

    Spaces must be used to indent lines; tabs are not allowed
    Open

        }

    Spaces must be used to indent lines; tabs are not allowed
    Open

            header('pragma: public');

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $xmlFields = $xml->createElement('fields');

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $cDataColumns = ['header_content', 'body_content', 'footer_content', 'conditions'];

    Spaces must be used to indent lines; tabs are not allowed
    Open

                } elseif ('watermark_image' === $field) {

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    } else {

    Spaces must be used to indent lines; tabs are not allowed
    Open

                } else {

    Spaces must be used to indent lines; tabs are not allowed
    Open

                $xmlField->appendChild($xmlColumn);

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $recordId = $request->getInteger('id');

    Spaces must be used to indent lines; tabs are not allowed
    Open

                        $watermarkPath = $pdfModel->get($field);

    Spaces must be used to indent lines; tabs are not allowed
    Open

                }

    Spaces must be used to indent lines; tabs are not allowed
    Open

            }

    Spaces must be used to indent lines; tabs are not allowed
    Open

            echo $xml->saveXML();

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $xml->preserveWhiteSpace = false;

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $xmlTemplate = $xml->createElement('pdf_template');

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    if (file_exists($pdfModel->get($field))) {

    Spaces must be used to indent lines; tabs are not allowed
    Open

                        $imData = base64_encode($im);

    Spaces must be used to indent lines; tabs are not allowed
    Open

        }

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    $name = $xmlField->appendChild($xml->createElement($field));

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    $xmlColumn = $xml->createElement($field, $value);

    Spaces must be used to indent lines; tabs are not allowed
    Open

                if (\in_array($field, $cDataColumns)) {

    Spaces must be used to indent lines; tabs are not allowed
    Open

                        $xmlColumn = $xml->createElement('imageblob', $imData);

    Spaces must be used to indent lines; tabs are not allowed
    Open

                        $xmlField->appendChild($xmlColumn);

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    }

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    $value = $pdfModel->get($field);

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $xmlTemplate->appendChild($xmlFields);

    Spaces must be used to indent lines; tabs are not allowed
    Open

        public function validateRequest(App\Request $request)

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    $name->appendChild($xml->createCDATASection(html_entity_decode($pdfModel->getRaw($field))));

    Spaces must be used to indent lines; tabs are not allowed
    Open

                        $im = file_get_contents($watermarkPath);

    Spaces must be used to indent lines; tabs are not allowed
    Open

                        $value = $watermarkPath;

    Spaces must be used to indent lines; tabs are not allowed
    Open

                        $value = '';

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $xml = new DOMDocument('1.0', 'utf-8');

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $xml->appendChild($xmlTemplate);

    Spaces must be used to indent lines; tabs are not allowed
    Open

            header('content-description: PHP Generated Data');

    Spaces must be used to indent lines; tabs are not allowed
    Open

        /** {@inheritdoc} */

    Spaces must be used to indent lines; tabs are not allowed
    Open

            $xml->formatOutput = true;

    Spaces must be used to indent lines; tabs are not allowed
    Open

                    $xmlColumn = $xml->createElement($field, html_entity_decode($value, ENT_COMPAT, 'UTF-8'));

    Class name "Settings_PDF_ExportTemplate_Action" is not in camel caps format
    Open

    class Settings_PDF_ExportTemplate_Action extends Settings_Vtiger_Index_Action

    There are no issues that match your filters.

    Category
    Status