These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
|
|||
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 | return 'orgs/' . $query->where['organization']; |
|
36 | 1 | } elseif (!empty($query->where['user'])) { |
|
37 | 1 | return 'users/' . $query->where['user']; |
|
38 | } |
||
39 | } |
||
40 | } |
||
41 | } |
||
42 |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.