Completed
Push — master ( 45ae8e...ba335a )
by Andrii
03:05
created

QueryBuilder::buildPrefix()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.074

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 10
cts 12
cp 0.8333
rs 9.2
cc 4
eloc 10
nc 4
nop 1
crap 4.074
1
<?php
2
/**
3
 * GitHub API implementation for yii2-hiart.
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart-github
6
 * @package   yii2-hiart-github
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart\github;
12
13
use hiqdev\hiart\Query;
14
15
/**
16
 * Query builder for GitHub API.
17
 *
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
class QueryBuilder extends \hiqdev\hiart\rest\QueryBuilder
21
{
22 2
    public function buildUri(Query $query)
23
    {
24 2
        $from = $query->from . 's';
25
26 2
        $prefix = $this->buildPrefix($query);
27
28 2
        return ($prefix ? $prefix . '/' : '') . $from;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return ($prefix ? $prefix . '/' : '') . $from; (string) is incompatible with the return type of the parent method hiqdev\hiart\rest\QueryBuilder::buildUri of type array.

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...
29
    }
30
31 2
    public function buildPrefix(Query $query)
32
    {
33 2
        if ($query->from === 'repo') {
34 2
            if (!empty($query->where['organization'])) {
35 1
                $organization = $query->where['organization'];
36 1
                unset($query->where['organization']);
37
38 1
                return "orgs/$organization";
39 1
            } elseif (!empty($query->where['user'])) {
40 1
                $user = $query->where['user'];
41 1
                unset($query->where['user']);
42
43 1
                return "users/$user";
44
            }
45
        }
46
    }
47
}
48