Completed
Push — master ( 67dda6...fa8d3b )
by Владислав
02:02
created

DeCaptchaBase::getParams()   C

Complexity

Conditions 14
Paths 41

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 5.0864
c 0
b 0
f 0
cc 14
eloc 20
nc 41
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace jumper423\decaptcha\core;
4
5
use Exception;
6
7
/**
8
 * Распознавание капчи.
9
 *
10
 * Class DeCaptchaBase
11
 */
12
class DeCaptchaBase extends DeCaptchaAbstract implements DeCaptchaInterface
13
{
14
    const PARAM_FIELD_METHOD = 0;
15
    const PARAM_FIELD_KEY = 1;
16
    const PARAM_FIELD_FILE = 2;
17
    const PARAM_FIELD_PHRASE = 3;
18
    const PARAM_FIELD_REGSENSE = 4;
19
    const PARAM_FIELD_NUMERIC = 5;
20
    const PARAM_FIELD_MIN_LEN = 6;
21
    const PARAM_FIELD_MAX_LEN = 7;
22
    const PARAM_FIELD_LANGUAGE = 8;
23
    const PARAM_FIELD_SOFT_ID = 9;
24
    const PARAM_FIELD_CAPTCHA_ID = 10;
25
    const PARAM_FIELD_ACTION = 11;
26
27
    const ACTION_RECOGNIZE = 0;
28
    const ACTION_UNIVERSAL = 1;
29
    const ACTION_UNIVERSAL_WITH_CAPTCHA = 2;
30
31
    protected $paramsNames = [
32
        self::PARAM_FIELD_METHOD => 'method',
33
        self::PARAM_FIELD_KEY => 'key',
34
        self::PARAM_FIELD_FILE => 'file',
35
        self::PARAM_FIELD_PHRASE => 'phrase',
36
        self::PARAM_FIELD_REGSENSE => 'regsense',
37
        self::PARAM_FIELD_NUMERIC => 'numeric',
38
        self::PARAM_FIELD_MIN_LEN => 'min_len',
39
        self::PARAM_FIELD_MAX_LEN => 'max_len',
40
        self::PARAM_FIELD_LANGUAGE => 'language',
41
        self::PARAM_FIELD_SOFT_ID => 'soft_id',
42
        self::PARAM_FIELD_CAPTCHA_ID => 'id',
43
        self::PARAM_FIELD_ACTION => 'action',
44
    ];
45
46
    protected $paramsSettings = [
47
        self::ACTION_RECOGNIZE => [
48
            self::PARAM_FIELD_METHOD => [
49
                self::PARAM_SLUG_DEFAULT => 'post',
50
                self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_STRING,
51
            ],
52
            self::PARAM_FIELD_KEY => [
53
                self::PARAM_SLUG_REQUIRE => true,
54
                self::PARAM_SLUG_SPEC => self::PARAM_SPEC_KEY,
55
                self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_STRING,
56
            ],
57
            self::PARAM_FIELD_FILE => [
58
                self::PARAM_SLUG_REQUIRE => true,
59
                self::PARAM_SLUG_SPEC => self::PARAM_SPEC_FILE,
60
                self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_STRING,
61
            ],
62
            self::PARAM_FIELD_PHRASE => [
63
                self::PARAM_SLUG_DEFAULT => 0,
64
                self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_INTEGER,
65
            ],
66
            self::PARAM_FIELD_REGSENSE => [
67
                self::PARAM_SLUG_DEFAULT => 0,
68
                self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_INTEGER,
69
            ],
70
            self::PARAM_FIELD_NUMERIC => [
71
                self::PARAM_SLUG_DEFAULT => 0,
72
                self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_INTEGER,
73
            ],
74
            self::PARAM_FIELD_MIN_LEN => [
75
                self::PARAM_SLUG_DEFAULT => 0,
76
                self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_INTEGER,
77
            ],
78
            self::PARAM_FIELD_MAX_LEN => [
79
                self::PARAM_SLUG_DEFAULT => 0,
80
                self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_INTEGER,
81
            ],
82
            self::PARAM_FIELD_LANGUAGE => [
83
                self::PARAM_SLUG_DEFAULT => 0,
84
                self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_INTEGER,
85
            ],
86
            self::PARAM_FIELD_SOFT_ID => [
87
                self::PARAM_SLUG_VARIABLE => false,
88
                self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_INTEGER,
89
            ],
90
        ],
91
        self::ACTION_UNIVERSAL => [
92
            self::PARAM_FIELD_KEY => [
93
                self::PARAM_SLUG_REQUIRE => true,
94
                self::PARAM_SLUG_SPEC => self::PARAM_SPEC_KEY,
95
                self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_STRING,
96
            ],
97
            self::PARAM_FIELD_ACTION => [
98
                self::PARAM_SLUG_REQUIRE => true,
99
                self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_STRING,
100
            ],
101
        ],
102
        self::ACTION_UNIVERSAL_WITH_CAPTCHA => [
103
            self::PARAM_FIELD_KEY => [
104
                self::PARAM_SLUG_REQUIRE => true,
105
                self::PARAM_SLUG_SPEC => self::PARAM_SPEC_KEY,
106
                self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_STRING,
107
            ],
108
            self::PARAM_FIELD_CAPTCHA_ID => [
109
                self::PARAM_SLUG_REQUIRE => true,
110
                self::PARAM_SLUG_SPEC => self::PARAM_SPEC_CAPTCHA,
111
                self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_INTEGER,
112
            ],
113
            self::PARAM_FIELD_ACTION => [
114
                self::PARAM_SLUG_REQUIRE => true,
115
                self::PARAM_SLUG_TYPE => self::PARAM_FIELD_TYPE_STRING,
116
            ],
117
        ],
118
    ];
119
120
    public function recognize($filePath)
121
    {
122
        try {
123
            $this->setParamSpec(static::PARAM_SPEC_FILE, $this->getFilePath($filePath));
124
            $result = $this->getCurlResponse($this->getInUrl(), $this->getParams(static::ACTION_RECOGNIZE));
125
            $this->setError($result);
0 ignored issues
show
Bug introduced by
The method setError() does not exist on jumper423\decaptcha\core\DeCaptchaBase. Did you maybe mean setErrorLang()?

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...
126
            list(, $this->captchaId) = explode('|', $result);
127
            $waitTime = 0;
128
            sleep($this->requestTimeout);
0 ignored issues
show
Bug introduced by
The property requestTimeout does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
129 View Code Duplication
            while (true) {
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...
130
                $result = $this->getResponse('get');
131
                $this->setError($result);
0 ignored issues
show
Bug introduced by
The method setError() does not exist on jumper423\decaptcha\core\DeCaptchaBase. Did you maybe mean setErrorLang()?

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...
132
                if ($result == 'CAPCHA_NOT_READY') {
133
                    $waitTime += $this->requestTimeout;
134
                    if ($waitTime > $this->maxTimeout) {
0 ignored issues
show
Bug introduced by
The property maxTimeout does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
135
                        break;
136
                    }
137
                    sleep($this->requestTimeout);
138
                } else {
139
                    $ex = explode('|', $result);
140
                    if (trim($ex[0]) == 'OK') {
141
                        $this->result = trim($ex[1]);
0 ignored issues
show
Bug introduced by
The property result does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
142
143
                        return true;
144
                    }
145
                }
146
            }
147
            throw new Exception('Лимит времени превышен');
148
        } catch (Exception $e) {
149
            $this->error = $e->getMessage();
0 ignored issues
show
Bug introduced by
The property error does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
150
151
            return false;
152
        }
153
    }
154
155
    public function getCode()
156
    {
157
    }
158
159
    public function getError()
160
    {
161
    }
162
163
    /**
164
     * Запуск распознавания капчи.
165
     *
166
     * @deprecated
167
     *
168
     * @param string $filePath Путь до файла или ссылка на него
169
     *
170
     * @return bool
171
     */
172
    public function run($filePath)
173
    {
174
        return $this->recognize($filePath);
175
    }
176
177
    /**
178
     * Не верно распознана.
179
     */
180
    public function notTrue()
181
    {
182
        $this->getResponse('reportbad');
183
    }
184
185
    protected function decodeResponse($data, $type, $format = self::RESPONSE_TYPE_STRING)
186
    {
187
        $result = [
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
188
            'type' => null,
189
            'data' => null,
190
        ];
191
        switch ($this->responseType) {
192
            case self::RESPONSE_TYPE_STRING:
193
                if ($type) {
194
                    $array = explode('|', $this->responseType);
0 ignored issues
show
Unused Code introduced by
$array is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
195
                }
196
197
                break;
198
        }
199
    }
200
}
201