Completed
Push — master ( 69d3bf...4d78d0 )
by Владислав
02:12
created

DeCaptchaBase::notTrue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
25
    const PARAM_FIELD_TYPE_STRING = 0;
26
    const PARAM_FIELD_TYPE_INTEGER = 1;
27
28
    const PARAM_SLUG_DEFAULT = 0;
29
    const PARAM_SLUG_TYPE = 1;
30
    const PARAM_SLUG_REQUIRE = 2;
31
    const PARAM_SLUG_SPEC = 3;
32
    const PARAM_SLUG_VARIABLE = 4;
33
34
    const PARAM_SPEC_KEY = 0;
35
    const PARAM_SPEC_FILE = 1;
36
37
    protected $paramsNames = [
38
        self::PARAM_FIELD_METHOD => 'method',
39
        self::PARAM_FIELD_KEY => 'key',
40
        self::PARAM_FIELD_FILE => 'file',
41
        self::PARAM_FIELD_PHRASE => 'phrase',
42
        self::PARAM_FIELD_REGSENSE => 'regsense',
43
        self::PARAM_FIELD_NUMERIC => 'numeric',
44
        self::PARAM_FIELD_MIN_LEN => 'min_len',
45
        self::PARAM_FIELD_MAX_LEN => 'max_len',
46
        self::PARAM_FIELD_LANGUAGE => 'language',
47
        self::PARAM_FIELD_SOFT_ID => 'soft_id',
48
    ];
49
50
    protected $paramsSettings = [
51
        self::PARAM_FIELD_METHOD => [
52
            self::PARAM_SLUG_DEFAULT => 'post',
53
            self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_STRING,
54
        ],
55
        self::PARAM_FIELD_KEY => [
56
            self::PARAM_SLUG_REQUIRE => true,
57
            self::PARAM_SLUG_SPEC    => self::PARAM_SPEC_KEY,
58
            self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_STRING,
59
        ],
60
        self::PARAM_FIELD_FILE => [
61
            self::PARAM_SLUG_REQUIRE => true,
62
            self::PARAM_SLUG_SPEC    => self::PARAM_SPEC_FILE,
63
            self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_STRING,
64
        ],
65
        self::PARAM_FIELD_PHRASE => [
66
            self::PARAM_SLUG_DEFAULT => 0,
67
            self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
68
        ],
69
        self::PARAM_FIELD_REGSENSE => [
70
            self::PARAM_SLUG_DEFAULT => 0,
71
            self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
72
        ],
73
        self::PARAM_FIELD_NUMERIC => [
74
            self::PARAM_SLUG_DEFAULT => 0,
75
            self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
76
        ],
77
        self::PARAM_FIELD_MIN_LEN => [
78
            self::PARAM_SLUG_DEFAULT => 0,
79
            self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
80
        ],
81
        self::PARAM_FIELD_MAX_LEN => [
82
            self::PARAM_SLUG_DEFAULT => 0,
83
            self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
84
        ],
85
        self::PARAM_FIELD_LANGUAGE => [
86
            self::PARAM_SLUG_DEFAULT => 0,
87
            self::PARAM_SLUG_TYPE    => self::PARAM_FIELD_TYPE_INTEGER,
88
        ],
89
        self::PARAM_FIELD_SOFT_ID => [
90
            self::PARAM_SLUG_VARIABLE => false,
91
            self::PARAM_SLUG_TYPE     => self::PARAM_FIELD_TYPE_INTEGER,
92
        ],
93
    ];
94
95
    protected $paramsSpec = [
96
        self::PARAM_SPEC_KEY => null,
97
        self::PARAM_SPEC_FILE => null,
98
    ];
99
100
    protected $params = [];
101
102
    protected function getParams(){
103
        $params = [];
104
        foreach ($this->paramsSettings as $field => $settings) {
105
            $value = null;
106
            if (array_key_exists($field, $this->params) && (!array_key_exists(self::PARAM_SLUG_VARIABLE, $settings) ^ (array_key_exists(self::PARAM_SLUG_VARIABLE, $settings) && $settings[self::PARAM_SLUG_VARIABLE] === false))) {
107
                $value = $this->params[$field];
108
            }
109
            if(array_key_exists(self::PARAM_SLUG_DEFAULT, $settings)) {
110
                $value = $settings[self::PARAM_SLUG_DEFAULT];
111
            }
112
            if(array_key_exists(self::PARAM_SLUG_SPEC, $settings) && array_key_exists($settings[self::PARAM_SLUG_SPEC], $this->paramsSpec)) {
113
                $value = $this->paramsSpec[$settings[self::PARAM_SLUG_SPEC]];
114
            }
115
            if(array_key_exists(self::PARAM_SLUG_REQUIRE, $settings) && $settings[self::PARAM_SLUG_REQUIRE] === true && is_null($value)) {
116
                throw new Exception('Нет данных');
117
            }
118
            if(array_key_exists($field, $this->paramsNames)) {
119
                switch ($settings[self::PARAM_SLUG_TYPE]) {
120
                    case self::PARAM_FIELD_TYPE_INTEGER:
121
                        $params[$this->paramsNames[$field]] = (int)$value;
122
                        break;
123
                    case self::PARAM_FIELD_TYPE_STRING:
124
                        $params[$this->paramsNames[$field]] = (string)$value;
125
                        break;
126
                }
127
            }
128
        }
129
    }
130
131
    public function recognize($filePath)
132
    {
133
        $this->result = null;
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...
134
        $this->error = null;
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...
135
        try {
136
            $filePath = $this->getFilePath($filePath);
137
            $result = $this->getCurlResponse($this->getPostData($filePath));
138
            $this->setError($result);
0 ignored issues
show
Bug introduced by
The method setError() does not seem to exist on object<jumper423\decaptcha\core\DeCaptchaBase>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
139
            list(, $this->captchaId) = explode('|', $result);
140
            $waitTime = 0;
141
            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...
142 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...
143
                $result = $this->getResponse('get');
144
                $this->setError($result);
0 ignored issues
show
Bug introduced by
The method setError() does not seem to exist on object<jumper423\decaptcha\core\DeCaptchaBase>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
145
                if ($result == 'CAPCHA_NOT_READY') {
146
                    $waitTime += $this->requestTimeout;
147
                    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...
148
                        break;
149
                    }
150
                    sleep($this->requestTimeout);
151
                } else {
152
                    $ex = explode('|', $result);
153
                    if (trim($ex[0]) == 'OK') {
154
                        $this->result = trim($ex[1]);
155
156
                        return true;
157
                    }
158
                }
159
            }
160
            throw new Exception('Лимит времени превышен');
161
        } catch (Exception $e) {
162
            $this->error = $e->getMessage();
163
164
            return false;
165
        }
166
    }
167
168
    public function getCode()
169
    {
170
    }
171
172
    public function getError()
173
    {
174
    }
175
176
    /**
177
     * Запуск распознавания капчи.
178
     *
179
     * @deprecated
180
     *
181
     * @param string $filePath Путь до файла или ссылка на него
182
     *
183
     * @return bool
184
     */
185
    public function run($filePath)
186
    {
187
        return $this->recognize($filePath);
188
    }
189
190
    /**
191
     * Не верно распознана.
192
     */
193
    public function notTrue()
194
    {
195
        $this->getResponse('reportbad');
196
    }
197
198
    /**
199
     * @param string $filePath
200
     */
201
    protected function getPostData($filePath)
202
    {
203
        return [
204
            'method'   => 'post',
205
            'key'      => $this->apiKey,
206
            'file'     => (version_compare(PHP_VERSION, '5.5.0') >= 0) ? new \CURLFile($filePath) : '@'.$filePath,
207
            'phrase'   => $this->isPhrase,
0 ignored issues
show
Bug introduced by
The property isPhrase 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...
208
            'regsense' => $this->isRegSense,
0 ignored issues
show
Bug introduced by
The property isRegSense 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...
209
            'numeric'  => $this->isNumeric,
0 ignored issues
show
Bug introduced by
The property isNumeric 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...
210
            'min_len'  => $this->minLen,
0 ignored issues
show
Bug introduced by
The property minLen 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...
211
            'max_len'  => $this->maxLen,
0 ignored issues
show
Bug introduced by
The property maxLen 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...
212
            'language' => $this->language,
0 ignored issues
show
Bug introduced by
The property language 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...
213
            'soft_id'  => 882,
214
        ];
215
    }
216
217
    protected function decodeResponse($data, $type, $format = self::RESPONSE_TYPE_STRING)
218
    {
219
        $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...
220
            'type' => null,
221
            'data' => null,
222
        ];
223
        switch ($this->responseType) {
224
            case self::RESPONSE_TYPE_STRING:
225
                if ($type) {
226
                    $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...
227
                }
228
229
                break;
230
        }
231
    }
232
}
233