Completed
Push — master ( e42f8d...334244 )
by Владислав
02:27
created

RuCaptchaGrid::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 47
rs 9.0303
c 2
b 1
f 0
cc 1
eloc 31
nc 1
nop 0
1
<?php
2
3
namespace jumper423\decaptcha\services;
4
5
/**
6
 * Class RuCaptchaGrid.
7
 */
8
class RuCaptchaGrid extends RuCaptchaInstruction
9
{
10
    public function init()
11
    {
12
        parent::init();
13
14
        unset(
15
            $this->paramsNames[static::ACTION_FIELD_PHRASE],
16
            $this->paramsNames[static::ACTION_FIELD_PINGBACK],
17
            $this->paramsNames[static::ACTION_FIELD_REGSENSE],
18
            $this->paramsNames[static::ACTION_FIELD_NUMERIC],
19
            $this->paramsNames[static::ACTION_FIELD_CALC],
20
            $this->paramsNames[static::ACTION_FIELD_MIN_LEN],
21
            $this->paramsNames[static::ACTION_FIELD_MAX_LEN],
22
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][self::ACTION_FIELD_PHRASE],
23
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][self::ACTION_FIELD_PINGBACK],
24
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][self::ACTION_FIELD_REGSENSE],
25
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][self::ACTION_FIELD_NUMERIC],
26
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][self::ACTION_FIELD_CALC],
27
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][self::ACTION_FIELD_MIN_LEN],
28
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][self::ACTION_FIELD_MAX_LEN]
29
        );
30
31
        $this->paramsNames[static::ACTION_FIELD_RECAPTCHA] = 'recaptcha';
32
33
        $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_RECAPTCHA] = [
34
            static::PARAM_SLUG_DEFAULT    => 1,
35
            static::PARAM_SLUG_TYPE       => static::PARAM_FIELD_TYPE_INTEGER,
36
            static::PARAM_SLUG_NOTWIKI    => true,
37
        ];
38
39
        $this->wiki->setText(['service', 'name'], [
40
            'ru' => 'RuCaptcha Сетка (ReCaptcha v2)',
41
            'en' => 'RuCaptcha Grid (ReCaptcha v2)',
42
        ]);
43
        $this->wiki->setText(['recognize', 'price'], [
44
            'ru' => 'Стоимость 1000 распознаний данной капчи - 70 рублей.',
45
        ]);
46
        $this->wiki->setText(['recognize', 'desc'], [
47
            'ru' => 'Для решения рекапчи, где нужно выбирать определённые квадраты. В ответ придут номера картинок, на которые надо нажать.
48
            
49
Обратите внимание, что рекапчи бывают не только 3 на 3 квадрата, но попадаются и 4 на 4 квадрата. Что бы понять какую именно картинку Вы шлёте, мы смотрим размер в px картинки. Если она 300x300px, то мы накладываем на эту картинку сетку 3х3. Если размер другой - накладываем сетку 4х4. Поэтому не надо склеивать изображение с чем-либо.
50
51
Обратите внимание, что необходимо засылать саму картинку рекапчи, а не делать её скриншот.',
52
        ]);
53
        $this->wiki->setText(['recognize','data'], [
54
            static::ACTION_FIELD_INSTRUCTIONS => 'Where\'s the cat?',
55
        ]);
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function getCode()
62
    {
63
        $code = parent::getCode();
64
        $code = explode(':', $code)[1];
65
66
        return explode('/', $code);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return explode('/', $code); (array) is incompatible with the return type declared by the interface jumper423\decaptcha\core...ptchaInterface::getCode of type string.

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...
67
    }
68
}
69