RefundService   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 6
lcom 1
cbo 3
dl 7
loc 56
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B refund() 7 48 6

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
class RefundService extends PaymentService{
4
5
	/**
6
	 * Return money to the previously charged credit card.
7
	 * @return PaymentResponse encapsulated response info
8
	 */
9
	public function refund($data = array()) {
10
		if ($this->payment->Status !== 'Captured') {
0 ignored issues
show
Documentation introduced by
The property Status does not exist on object<Payment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
11
			return null; //could be handled better? send payment response?
12
		}
13
		if (!$this->payment->isInDB()) {
14
			$this->payment->write();
15
		}
16
17
		if(empty($data['receipt'])) {
18
			return null;
19
		}
20
21
		$message = $this->createMessage('RefundRequest');
22
		$message->write();
23
		$request = $this->oGateway()->refund(array_merge(
24
			$data,
25
			array(
26
				'amount' => (float) $this->payment->MoneyAmount,
0 ignored issues
show
Documentation introduced by
The property MoneyAmount does not exist on object<Payment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
27
				'receipt' => (int) $data['receipt'],
28
			)
29
		));
30
		$this->logToFile($request->getParameters(), 'RefundRequest_post');
31
		$gatewayresponse = $this->createGatewayResponse();
32
		try {
33
			$response = $this->response = $request->send();
34
			//update payment model
35
			if ($response->isSuccessful()) {
36
				//successful payment
37
				$this->createMessage('RefundedResponse', $response);
38
				$this->payment->Status = 'Refunded';
0 ignored issues
show
Documentation introduced by
The property Status does not exist on object<Payment>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
39
				$gatewayresponse->setMessage('Payment refunded');
40
				$this->payment->extend('onRefunded', $gatewayresponse);
41 View Code Duplication
			} else {
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...
42
				//handle error
43
				$this->createMessage('RefundError', $response);
44
				$gatewayresponse->setMessage(
45
					"Error (".$response->getCode()."): ".$response->getMessage()
46
				);
47
			}
48
			$this->payment->write();
49
			$gatewayresponse->setOmnipayResponse($response);
50
		} catch (Omnipay\Common\Exception\OmnipayException $e) {
51
			$this->createMessage('GatewayErrorMessage', $e);
0 ignored issues
show
Documentation introduced by
$e is of type object<Omnipay\Common\Exception\OmnipayException>, but the function expects a array|string|object<Omni...<OmnipayException>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
			$gatewayresponse->setMessage($e->getMessage());
53
		}
54
55
		return $gatewayresponse;
56
	}
57
58
}
59