GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RobokassaPayment   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 3.03 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 3
loc 99
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A content() 0 25 2
A customCheck() 3 18 4
A checkResult() 0 18 3
A getSignature() 0 11 1
A getSignatureResult() 0 10 1

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 app\components\payment;
4
5
use app\modules\shop\models\OrderTransaction;
6
use yii\helpers\Json;
7
use yii\web\BadRequestHttpException;
8
9
/***
10
 *
11
 * В настройках прописать следующие данные, отправлять с помощью POST
12
 *
13
 * Result URL: /shop/payment/custom-check?type=2&action=result
14
 * Success Url /shop/payment/custom-check?type=2&action=success
15
 * Fail Url /shop/payment/custom-check?type=2&action=fail
16
 *
17
 * Где type - id Robokassa на странице /shop/backend-payment-type/index
18
 *
19
 * Class RobokassaPayment
20
 * @package app\components\payment
21
 */
22
class RobokassaPayment extends AbstractPayment
23
{
24
    public $merchantLogin;
25
    public $merchantPass1;
26
    public $merchantPass2;
27
    public $merchantUrl;
28
29
    public function content()
30
    {
31
        $invoiceDescription = urlencode(\Yii::t('app', 'Payment of order #{orderId}', ['orderId' => $this->order->id]));
32
        $inCurrency = '';
33
        $culture = 'ru';
34
        $data = [
35
            'MrchLogin' => $this->merchantLogin,
36
            'OutSum' => $this->transaction->total_sum,
37
            'InvId' => $this->transaction->id,
38
            'IncCurrLabel' => $inCurrency,
39
            'Desc' => $invoiceDescription,
40
            'Culture' => $culture,
41
            'Encoding' => 'utf-8'
42
        ];
43
        if ($this->merchantUrl == 'test.robokassa.ru') {
44
            $data['IsTest'] = 1;
45
        }
46
        $SignatureValue = $this->getSignature();
47
        $data['SignatureValue'] = $SignatureValue;
48
        $url = 'http://' . $this->merchantUrl . '/Index.aspx?' . http_build_query($data);
49
50
        $this->redirect($url);
51
52
        \Yii::$app->end();
53
    }
54
55
    /**
56
     * @return string|null
0 ignored issues
show
Documentation introduced by
Should the return type not be string|\yii\web\Response?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
57
     */
58
    public function customCheck()
59
    {
60
61 View Code Duplication
        if (null === $this->transaction = $this->loadTransaction(\Yii::$app->request->post('InvId'))) {
62
            throw new BadRequestHttpException();
63
        }
64
        if (\Yii::$app->request->get('action') == 'result') {
65
            return $this->checkResult($this->transaction->generateHash());
66
        } elseif (\Yii::$app->request->get('action') == 'success') {
67
            return $this->redirect(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirect($...s->createSuccessUrl()); (yii\web\Response) is incompatible with the return type of the parent method app\components\payment\A...actPayment::customCheck of type string|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
68
                $this->createSuccessUrl()
69
            );
70
        }
71
        return $this->redirect(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirect($this->createErrorUrl()); (yii\web\Response) is incompatible with the return type of the parent method app\components\payment\A...actPayment::customCheck of type string|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
72
            $this->createErrorUrl()
73
        );
74
75
    }
76
77
78
    public function checkResult($hash = '')
79
    {
80
        \Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
81
        $result = "bad sign\n";
82
        if ($this->getSignatureResult() == \Yii::$app->request->post('SignatureValue')) {
83
            $this->transaction->result_data = Json::encode([
84
                \Yii::$app->request->post('OutSum'),
85
                \Yii::$app->request->post('InvId'),
86
                \Yii::$app->request->post('SignatureValue')
87
            ]);
88
            $this->transaction->status = OrderTransaction::TRANSACTION_SUCCESS;
89
            if ($this->transaction->save(true, ['status', 'result_data'])) {
90
                $result = "OK" . $this->transaction->id . "\n";
91
            }
92
        }
93
        return $result;
94
95
    }
96
97
    protected function getSignature()
98
    {
99
        $sData = [
100
            $this->merchantLogin,
101
            $this->transaction->total_sum,
102
            $this->transaction->id,
103
104
        ];
105
        $sData[] = $this->merchantPass1;
106
        return md5(implode(':', $sData));
107
    }
108
109
    protected function getSignatureResult()
110
    {
111
        $sData = [
112
            \Yii::$app->request->post('OutSum'),
113
            \Yii::$app->request->post('InvId'),
114
            $this->merchantPass2
115
        ];
116
        return strtoupper(md5(implode(':', $sData)));
117
118
    }
119
120
}
121
122
?>
123