YetiForceCompany/YetiForceCRM

View on GitHub
modules/Import/readers/VCardReader.php

Summary

Maintainability
A
4 hrs
Test Coverage
F
0%

Function read has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
Open

    public function read()
    {
        $defaultCharset = App\Config::main('default_charset');

        $filePath = $this->getFilePath();
Severity: Minor
Found in modules/Import/readers/VCardReader.php - About 2 hrs 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

Method read has 39 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public function read()
    {
        $defaultCharset = App\Config::main('default_charset');

        $filePath = $this->getFilePath();
Severity: Minor
Found in modules/Import/readers/VCardReader.php - About 1 hr to fix

    Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed.
    Open

        public function read()

    Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.

    See

    The method getFirstRowData has a boolean flag argument $hasHeader, which is a certain sign of a Single Responsibility Principle violation.
    Open

        public function getFirstRowData($hasHeader = true)

    BooleanArgumentFlag

    Since: 1.4.0

    A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.

    Example

    class Foo {
        public function bar($flag = true) {
        }
    }

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

    Avoid using static access to class 'App\Config' in method 'getFirstRowData'.
    Open

            $default_charset = App\Config::main('default_charset');

    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

    Avoid using static access to class 'App\Config' in method 'read'.
    Open

            $defaultCharset = App\Config::main('default_charset');

    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

    Define a constant instead of duplicating this literal "file_encoding" 3 times.
    Open

                    $data[$label] = $this->convertCharacterEncoding($value, $this->request->get('file_encoding'), $default_charset);

    Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.

    On the other hand, constants can be referenced from many places, but only need to be updated in a single place.

    Noncompliant Code Example

    With the default threshold of 3:

    function run() {
      prepare('action1');                              // Non-Compliant - 'action1' is duplicated 3 times
      execute('action1');
      release('action1');
    }
    

    Compliant Solution

    ACTION_1 = 'action1';
    
    function run() {
      prepare(ACTION_1);
      execute(ACTION_1);
      release(ACTION_1);
    }
    

    Exceptions

    To prevent generating some false-positives, literals having less than 5 characters are excluded.

    Avoid unused parameters such as '$hasHeader'.
    Open

        public function getFirstRowData($hasHeader = true)

    UnusedFormalParameter

    Since: 0.2

    Avoid passing parameters to methods or constructors and then not using those parameters.

    Example

    class Foo
    {
        private function bar($howdy)
        {
            // $howdy is not used
        }
    }

    Source https://phpmd.org/rules/unusedcode.html#unusedformalparameter

    Call to deprecated function \Import_VCardReader_Reader::convertCharacterEncoding() defined at /code/modules/Import/readers/FileReader.php:94
    Open

                    $data[$label] = $this->convertCharacterEncoding($value, $this->request->get('file_encoding'), $default_charset);
    Severity: Minor
    Found in modules/Import/readers/VCardReader.php by phan

    Call to deprecated function \Import_VCardReader_Reader::convertCharacterEncoding() defined at /code/modules/Import/readers/FileReader.php:94
    Open

                        $mappedData[$fieldName] = $this->convertCharacterEncoding($fieldValue, $this->request->get('file_encoding'), $defaultCharset);
    Severity: Minor
    Found in modules/Import/readers/VCardReader.php by phan

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

    class Import_VCardReader_Reader extends Import_FileReader_Reader

    The class Import_VCardReader_Reader is not named in CamelCase.
    Open

    class Import_VCardReader_Reader extends Import_FileReader_Reader
    {
        protected $vCardPattern = '/BEGIN:VCARD.*?END:VCARD/si';
        protected $skipLabels = ['BEGIN', 'END', 'VERSION'];
        public static $fileContents = null;

    CamelCaseClassName

    Since: 0.2

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

    Example

    class class_name {
    }

    Source

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

        protected $skipLabels = ['BEGIN', 'END', 'VERSION'];

    Line exceeds 120 characters; contains 128 characters
    Open

                    $data[$label] = $this->convertCharacterEncoding($value, $this->request->get('file_encoding'), $default_charset);

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

                $fieldValueMappings = explode("\r\n", $row);

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

        public static $fileContents = null;

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

            if (empty(self::$fileContents)) {

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

                        $data[$valueCounter++] = $value;

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

                    if ($this->request->get('file_encoding') !== $defaultCharset) {

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

                        $allValuesEmpty = false;

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

            foreach ($fieldValueMappings as $fieldValueMapping) {

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

                    }

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

                foreach ($fieldMapping as $fieldName => $index) {

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

                        $mappedData[$fieldName] = $this->convertCharacterEncoding($fieldValue, $this->request->get('file_encoding'), $defaultCharset);

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

                }

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

                $this->addRecordToDB($mappedData);

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

            preg_match_all($this->vCardPattern, self::$fileContents, $matches);

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

            $matches = [];

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

                }

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

            $data = null;

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

            $matches = [];

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

                $value = str_replace(';', ' ', $value);

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

        }

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

         * Function creates tables for import in database.

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

        protected $vCardPattern = '/BEGIN:VCARD.*?END:VCARD/si';

    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

            $row = $matches[0][0];

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

            $data = [];

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

                [$label, $value] = explode(':', $fieldValueMapping, 2);

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

            $defaultCharset = App\Config::main('default_charset');

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

            $this->createTable();

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

                foreach ($fieldValueMappings as $fieldValueMapping) {

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

                $allValuesEmpty = true;

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

                    $mappedData[$fieldName] = $fieldValue;

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

            $filePath = $this->getFilePath();

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

                    }

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

            $filePath = $this->getFilePath();

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

        {

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

                if (!\in_array($label, $this->skipLabels)) {

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

                    $data[$label] = $this->convertCharacterEncoding($value, $this->request->get('file_encoding'), $default_charset);

    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

        {

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

            if (empty(self::$fileContents)) {

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

                $valueCounter = 0;

    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

        {

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

            $default_charset = App\Config::main('default_charset');

    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

                self::$fileContents = file_get_contents($filePath);

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

                $data = [];

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

                    if (!\in_array($label, $this->skipLabels)) {

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

            $fieldValueMappings = explode("\r\n", $row);

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

                    $value = str_replace(';', ' ', $value);

    Line exceeds 120 characters; contains 146 characters
    Open

                        $mappedData[$fieldName] = $this->convertCharacterEncoding($fieldValue, $this->request->get('file_encoding'), $defaultCharset);

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

                if ($allValuesEmpty) {

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

            }

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

            for ($i = 0; $i < $countMatches; ++$i) {

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

                    continue;

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

            return true;

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

        public function getFirstRowData($hasHeader = true)

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

                self::$fileContents = file_get_contents($filePath);

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

        public function read()

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

                    }

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

        public function hasHeader()

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

                    [$label, $value] = explode(':', $fieldValueMapping, 2);

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

                    $fieldValue = $data[$index];

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

                }

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

            return $data;

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

            $fieldMapping = $this->request->get('field_mapping');

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

            preg_match_all($this->vCardPattern, self::$fileContents, $matches);

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

            $countMatches = \count($matches[0]);

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

                $row = $matches[0][$i];

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

                $mappedData = [];

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

                    if (!empty($fieldValue)) {

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

    class Import_VCardReader_Reader extends Import_FileReader_Reader

    The variable $default_charset is not named in camelCase.
    Open

        public function getFirstRowData($hasHeader = true)
        {
            $default_charset = App\Config::main('default_charset');
    
            $filePath = $this->getFilePath();

    CamelCaseVariableName

    Since: 0.2

    It is considered best practice to use the camelCase notation to name variables.

    Example

    class ClassName {
        public function doSomething() {
            $data_module = new DataModule();
        }
    }

    Source

    The variable $default_charset is not named in camelCase.
    Open

        public function getFirstRowData($hasHeader = true)
        {
            $default_charset = App\Config::main('default_charset');
    
            $filePath = $this->getFilePath();

    CamelCaseVariableName

    Since: 0.2

    It is considered best practice to use the camelCase notation to name variables.

    Example

    class ClassName {
        public function doSomething() {
            $data_module = new DataModule();
        }
    }

    Source

    There are no issues that match your filters.

    Category
    Status