HasGatewaySpecificFields   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 21.54 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 74.19%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 14
loc 65
ccs 23
cts 31
cp 0.7419
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getGatewaySpecificFields() 0 4 1
A setGatewaySpecificFields() 0 4 1
C fillGatewaySpecificFields() 14 24 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Omnipay\Spreedly\Message\Concerns;
4
5
use Omnipay\Common\Exception\InvalidRequestException;
6
use Omnipay\Spreedly\Arr;
7
8
trait HasGatewaySpecificFields
9
{
10
    protected $gatewaySpecificFieldsConfig = array(
11
        'test' => array(
12
            'required' => array(),
13
            'optional' => array(),
14
        ),
15
        'fake' => array(
16
            'required' => array(
17
                'foo',
18
            ),
19
            'optional' => array(
20
                'bar',
21
            ),
22
        ),
23
        'conekta' => array(
24
            'required' => array(),
25
            'optional' => array(
26
                'device_fingerprint',
27
                'customer',
28
                'phone',
29
            ),
30
        ),
31
    );
32
33 18
    public function getGatewaySpecificFields()
34
    {
35 18
        return $this->getParameter('gateways_specific_fields');
0 ignored issues
show
Bug introduced by
It seems like getParameter() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
36
    }
37
38 2
    public function setGatewaySpecificFields($value)
39
    {
40 2
        return $this->setParameter('gateways_specific_fields', $value);
0 ignored issues
show
Bug introduced by
It seems like setParameter() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
41
    }
42
43
    /**
44
     * @param array $data
45
     * @return array
46
     * @throws InvalidRequestException
47
     */
48 18
    protected function fillGatewaySpecificFields($data)
49
    {
50 18
        $gateway = $this->getGateway();
0 ignored issues
show
Bug introduced by
The method getGateway() does not exist on Omnipay\Spreedly\Message...asGatewaySpecificFields. Did you maybe mean getGatewaySpecificFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
51 18
        $gatewaySpecificFieldsData = $this->getGatewaySpecificFields();
52 18
        $gatewaySpecificFieldsConfig = Arr::get($this->gatewaySpecificFieldsConfig, $gateway);
53 18
        if ($gatewaySpecificFieldsData && $gatewaySpecificFieldsConfig) {
54 2 View Code Duplication
            foreach ($gatewaySpecificFieldsConfig['required'] as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55 2
                $value = Arr::get($gatewaySpecificFieldsData, $field);
56 2
                if (!is_null($value)) {
57 2
                    Arr::set($data, 'gateway_specific_fields.' . $gateway . '.' . $field, $value);
58 2
                } else {
59
                    throw new InvalidRequestException("Missing gateway specific field: $field.");
60
                }
61 2
            }
62 2 View Code Duplication
            foreach ($gatewaySpecificFieldsConfig['optional'] as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63 2
                $value = Arr::get($gatewaySpecificFieldsData, $field);
64 2
                if (!is_null($value)) {
65 2
                    Arr::set($data, 'gateway_specific_fields.' . $gateway . '.' . $field, $value);
66 2
                }
67 2
            }
68 2
        }
69
70 18
        return $data;
71
    }
72
}
73