Completed
Push — master ( 461460...f33045 )
by Владислав
13:25
created

RuCaptchaClick   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 4
c 4
b 1
f 0
lcom 1
cbo 2
dl 0
loc 60
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 37 1
A getCode() 0 16 3
1
<?php
2
3
namespace jumper423\decaptcha\services;
4
5
/**
6
 * Class RuCaptchaGrid.
7
 */
8
class RuCaptchaClick extends RuCaptchaGrid
9
{
10
    public function init()
11
    {
12
        parent::init();
13
14
        unset(
15
            $this->paramsNames[static::ACTION_FIELD_RECAPTCHA],
16
            $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_RECAPTCHA]
17
        );
18
19
        $this->paramsNames[static::ACTION_FIELD_COORDINATE] = 'coordinatescaptcha';
20
21
        $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_INSTRUCTIONS][static::PARAM_SLUG_REQUIRE] = false;
22
        $this->actions[static::ACTION_RECOGNIZE][static::ACTION_FIELDS][static::ACTION_FIELD_COORDINATE] = [
23
            static::PARAM_SLUG_DEFAULT    => 1,
24
            static::PARAM_SLUG_TYPE       => static::PARAM_FIELD_TYPE_INTEGER,
25
            static::PARAM_SLUG_NOTWIKI    => true,
26
        ];
27
28
        $this->wiki->setText(['service', 'name'], 'RuCaptcha ClickCaptcha');
29
        $this->wiki->setText(['recognize', 'price'], [
30
            'ru' => 'Стоимость 1000 распознаний данной капчи - 70 рублей.',
31
            'en' => 'It costs $1,2 to recognize 1000 CAPTCHAs this way.',
32
        ]);
33
        $this->wiki->setText(['recognize', 'desc'], [
34
            'ru' => 'Распознание любой ClickCaptcha (в том числе и ReCaptcha 2.0). В ответ приходит массив координат, от верхнего левого угла.',
35
            'en' => 'Recognizing any ClickCaptcha (including ReCaptcha 2.0). In response comes an array of coordinates from the top left corner.',
36
        ]);
37
        $this->wiki->setText(['recognize', 'data'], [
38
            static::ACTION_FIELD_INSTRUCTIONS => 'Where\'s the cat?',
39
        ]);
40
        $this->wiki->setText(['menu', 'from_service'], [
41
            RuCaptcha::class,
42
            RuCaptchaInstruction::class,
43
            RuCaptchaGrid::class,
44
            RuCaptchaReCaptcha::class,
45
        ]);
46
    }
47
48
    /**
49
     * @return array
50
     */
51
    public function getCode()
52
    {
53
        $code = parent::getCode();
54
        $code = explode(':', $code)[1];
55
        $code = explode(';', $code);
56
        $result = [];
57
        foreach ($code as $row) {
58
            $rowCoord = explode(',', $row);
59
            foreach ($rowCoord as &$rowCoordOne) {
60
                $rowCoordOne = substr($rowCoordOne, 2);
61
            }
62
            $result[] = $rowCoord;
63
        }
64
65
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (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...
66
    }
67
}
68