Completed
Push — master ( bfac0b...36c0b2 )
by Andrii
03:30
created

QueryBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 0
cbo 2
dl 0
loc 28
ccs 14
cts 15
cp 0.9333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildUri() 0 8 2
A buildPrefix() 0 16 4
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