YetiForceCompany/YetiForceCRM

View on GitHub
app/RecordCollectors/BrReceitaWsCnpj.php

Summary

Maintainability
A
2 hrs
Test Coverage
F
0%

Method getDataFromApi has 27 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    private function getDataFromApi(string $cnpj): void
    {
        try {
            $this->setApiKey();
            if ($this->apiKey) {
Severity: Minor
Found in app/RecordCollectors/BrReceitaWsCnpj.php - About 1 hr to fix

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

        private function getDataFromApi(string $cnpj): void
        {
            try {
                $this->setApiKey();
                if ($this->apiKey) {
    Severity: Minor
    Found in app/RecordCollectors/BrReceitaWsCnpj.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

    Avoid using static access to class '\App\Json' in method 'getDataFromApi'.
    Open

                $data = $this->parseData(\App\Json::decode($response->getBody()->getContents()));

    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\Log' in method 'getDataFromApi'.
    Open

                \App\Log::warning($e->getMessage(), 'RecordCollectors');

    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 getDataFromApi uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
    Open

                } else {
                    $this->response['error'] = $e->getMessage();
                }

    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 '\App\Language' in method 'getDataFromApi'.
    Open

                    $this->response['error'] = \App\Language::translate('LBL_BR_RECITA_WS_CNPJ_ERROR', 'Other.RecordCollector');

    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 assigning values to variables in if clauses and the like (line '207', column '8').
    Open

        private function setApiKey(): void
        {
            if (($params = $this->getParams()) && !empty($params['api_key'])) {
                $this->apiKey = $params['api_key'];
            }

    IfStatementAssignment

    Since: 2.7.0

    Assignments in if clauses and the like are considered a code smell. Assignments in PHP return the right operand as their result. In many cases, this is an expected behavior, but can lead to many difficult to spot bugs, especially when the right operand could result in zero, null or an empty string and the like.

    Example

    class Foo
    {
        public function bar($flag)
        {
            if ($foo = 'bar') { // possible typo
                // ...
            }
            if ($baz = 0) { // always false
                // ...
            }
        }
    }

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

    Avoid using static access to class '\App\Utils' in method 'parseData'.
    Open

            return \App\Utils::flattenKeys($data, 'ucfirst');

    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 getDataFromApi uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
    Open

                } else {
                    $this->data = $data;
                }

    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

    syntax error, unexpected 'string' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
    Open

        protected string $addOnName = 'YetiForceRcBrReceitaWsCnpj';
    Severity: Critical
    Found in app/RecordCollectors/BrReceitaWsCnpj.php by phan

    Avoid excessively long variable names like $formFieldsToRecordMap. Keep variable name length under 20.
    Open

        public $formFieldsToRecordMap = [
            'Accounts' => [
                'nome' => 'accountname',
                'fantasia' => 'accountname',
                'email' => 'email1',

    LongVariable

    Since: 0.2

    Detects when a field, formal or local variable is declared with a long name.

    Example

    class Something {
        protected $reallyLongIntName = -3; // VIOLATION - Field
        public static function main( array $interestingArgumentsList[] ) { // VIOLATION - Formal
            $otherReallyLongName = -5; // VIOLATION - Local
            for ($interestingIntIndex = 0; // VIOLATION - For
                 $interestingIntIndex < 10;
                 $interestingIntIndex++ ) {
            }
        }
    }

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

    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

        /** {@inheritdoc} */

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

        public $docUrl = 'https://developers.receitaws.com.br/#/operations/queryCNPJFree';

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

                'fantasia' => 'accountname',

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

                'bairro' => 'addresslevel4a',

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

                'nome' => 'company',

    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

                'capital_social' => 'annual_revenue',

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

                'numero' => 'buildingnumbera',

    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 $displayType = 'FillFields';

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

            ],

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

                'cnpj' => 'registration_number_1',

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

                'logradouro' => 'addresslevel8a',

    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 $settingsFields = [

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

        ];

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

                'cep' => 'addresslevel7a',

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

                'nome' => 'subject',

    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

            'api_key' => ['required' => 0, 'purifyType' => 'Text', 'label' => 'LBL_API_KEY_OPTIONAL'],

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

                'telefone' => 'phone',

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

                'atividade_principal0Code' => 'siccode',

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

                'cep' => 'addresslevel7a',

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

        public $label = 'LBL_BR_RECITA_WS_CNPJ';

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

                'logradouro' => 'addresslevel8a',

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

                'municipio' => 'addresslevel5a',

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

                'telefone' => 'phone',

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

            'Accounts' => [

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

                'nome' => 'accountname',

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

                'numero' => 'buildingnumbera',

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

            'Partners' => [

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

        public $icon = 'yfi-receita-cnpj-br';

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

            'Leads' => [

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

                'bairro' => 'addresslevel4a',

    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

                'typeofdata' => 'V~O',

    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

            $this->loadData();

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

            return $this->response;

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

         * @param string $cnpj

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

            try {

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

                    $this->response['error'] = \App\Language::translate('LBL_BR_RECITA_WS_CNPJ_ERROR', 'Other.RecordCollector');

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

        }

    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

        /** {@inheritdoc} */

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

                'email' => 'email1',

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

                'cnpj' => 'registration_number_1',

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

                'email' => 'email',

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

                'bairro' => 'addresslevel4a',

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

            ],

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

                'bairro' => 'addresslevel4a',

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

                'logradouro' => 'addresslevel8a',

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

            'Vendors' => [

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

                $this->setApiKey();

    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

                $this->apiKey = $params['api_key'];

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

                'label' => 'LBL_BR_RECITA_WS_CNPJ_NUMBER',

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

            $cnpj = str_replace([' ', '/', '.', '-'], '', $this->request->getByType('cnpj', 'Text'));

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

                if (isset($data['status']) && 'ERROR' === $data['status']) {

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

                'numero' => 'buildingnumbera',

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

                'email' => 'email',

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

            'Competition' => [

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

            'Accounts' => [

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

            'Leads' => [

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

        private $url = 'https://receitaws.com.br/v1/cnpj/';

    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

        /** {@inheritdoc} */

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

        public $formFieldsToRecordMap = [

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

                'cep' => 'addresslevel7a',

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

                'fantasia' => 'subject',

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

                'cnpj' => 'registration_number_1',

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

        /** @var string Api key */

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

        public function search(): array

    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 ($this->apiKey) {

    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

        public $allowedModules = ['Accounts', 'Leads', 'Vendors', 'Partners', 'Competition'];

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

        public $description = 'LBL_BR_RECITA_WS_CNPJ_DESC';

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

                'fantasia' => 'company',

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

                'municipio' => 'addresslevel5a',

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

                'municipio' => 'addresslevel5a',

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

            'cnpj' => [

    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

        /** {@inheritdoc} */

    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 fetching company data by CNPJ number.

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

         * @return void

    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

            ],

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

                'labelModule' => 'Other.RecordCollector',

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

                'cnpj' => 'registration_number_1',

    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->response['error'] = $data['message'];

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

            } catch (\GuzzleHttp\Exception\GuzzleException $e) {

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

                \App\Log::warning($e->getMessage(), 'RecordCollectors');

    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 (($params = $this->getParams()) && !empty($params['api_key'])) {

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

                'email' => 'email',

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

                'nome' => 'vendorname',

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

                'numero' => 'buildingnumbera',

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

                'cep' => 'addresslevel7a',

    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

        protected string $addOnName = 'YetiForceRcBrReceitaWsCnpj';

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

            $this->getDataFromApi($cnpj);

    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 = $this->parseData(\App\Json::decode($response->getBody()->getContents()));

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

                'telefone' => 'phone',

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

                'logradouro' => 'addresslevel8a',

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

            }

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

                'municipio' => 'addresslevel5a',

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

        private function parseData(array $data): array

    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 setup Api Key.

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

                        'headers' => [

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

                            'Authorization' => 'Bearer ' . $this->apiKey

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

                if (429 === $e->getCode()) {

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

         * @param array $data

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

         *

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

                'cep' => 'addresslevel7a',

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

                'municipio' => 'addresslevel5a',

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

            }

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

                    unset($this->data['fields']);

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

        protected $modulesFieldsMap = [

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

                'cnpj' => 'registration_number_1',

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

            ],

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

        /** @var string CNJP sever address */

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

                return [];

    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->data = $data;

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

                } elseif ($e->getCode() > 400) {

    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

        }

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

            'Vendors' => [

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

                'nome' => 'subject',

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

                $response = \App\RequestHttp::getClient()->get($this->url . $cnpj, $options ?? []);

    Line exceeds 120 characters; contains 124 characters
    Open

                    $this->response['error'] = \App\Language::translate('LBL_BR_RECITA_WS_CNPJ_ERROR', 'Other.RecordCollector');

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

         * @return void

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

        private function setApiKey(): void

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

            }

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

                'fantasia' => 'subject',

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

        protected $fields = [

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

                'logradouro' => 'addresslevel8a',

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

                'numero' => 'buildingnumbera',

    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

        private $apiKey;

    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 (!$this->isActive() || empty($cnpj)) {

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

            return \App\Utils::flattenKeys($data, 'ucfirst');

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

                    $options = [

    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

                    $this->response['error'] = $e->getResponse()->getReasonPhrase();

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

                'email' => 'email',

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

                'bairro' => 'addresslevel4a',

    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->response['error'] = $e->getMessage();

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

         * @return array

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

                'fantasia' => 'vendorname',

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

                'cnpj' => 'registration_number_1',

    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

        private function getDataFromApi(string $cnpj): void

    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 parsing data to fields from API.

    There are no issues that match your filters.

    Category
    Status