Completed
Push — master ( c8064d...729f46 )
by Dmitry
02:44
created

Connection::getResponseError()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 1
crap 3.072
1
<?php
2
/**
3
 * Tools to use API as ActiveRecord for Yii2
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart\rest;
12
13
use hiqdev\hiart\ResponseInterface;
14
15
class Connection extends \hiqdev\hiart\AbstractConnection
16
{
17
    public $queryBuilderClass = QueryBuilder::class;
18
19
    /**
20
     * Method checks whether the response is an error.
21
     *
22
     * @param ResponseInterface $response
23
     * @return false|string the error text or boolean `false`, when the response is not an error
24
     */
25 2
    public function getResponseError(ResponseInterface $response)
26
    {
27 2
        $code = $response->getStatusCode();
28 2
        if ($code >= 200 && $code < 300) {
29 2
            return false;
30
        }
31
32
        return $response->getReasonPhrase();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $response->getReasonPhrase(); (hiqdev\hiart\ResponseInterface) is incompatible with the return type declared by the abstract method hiqdev\hiart\AbstractConnection::getResponseError of type false|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...
33
    }
34
}
35