BankAccountApiCollectionModel::createCollection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Fousky\Component\iDoklad\Model\Banks;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Fousky\Component\iDoklad\Model\iDokladAbstractModel;
7
use Fousky\Component\iDoklad\Model\iDokladModelInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
/**
11
 * @method null|ArrayCollection|BankAccountApiModel[] getBankAccounts()
12
 *
13
 * @author Lukáš Brzák <[email protected]>
14
 */
15
class BankAccountApiCollectionModel extends iDokladAbstractModel
16
{
17
    /** @var null|ArrayCollection|BankAccountApiModel[] $BankAccounts */
18
    protected $BankAccounts;
19
20
    /**
21
     * @param ResponseInterface $response
22
     *
23
     * @return iDokladModelInterface
24
     */
25
    public static function createFromResponse(ResponseInterface $response): iDokladModelInterface
26
    {
27
        /** @var \stdClass[] $items */
28
        $items = \GuzzleHttp\json_decode($response->getBody()->getContents());
29
30
        $model = new static();
31
        $model->BankAccounts = static::createCollection($items);
32
33
        return $model;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $model; (Fousky\Component\iDoklad...countApiCollectionModel) is incompatible with the return type declared by the interface Fousky\Component\iDoklad...ace::createFromResponse of type self.

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...
34
    }
35
36
    /**
37
     * @param array $items
38
     *
39
     * @return BankAccountApiCollectionModel
40
     */
41
    public static function createFromArray(array $items): self
42
    {
43
        $model = new static();
44
        $model->BankAccounts = static::createCollection($items);
45
46
        return $model;
47
    }
48
49
    /**
50
     * @param array|\stdClass[] $items
51
     *
52
     * @return ArrayCollection
53
     */
54
    protected static function createCollection(array $items): ArrayCollection
55
    {
56
        $collection = new ArrayCollection();
57
58
        foreach ($items as $item) {
59
            $collection->add(BankAccountApiModel::createFromStd($item));
60
        }
61
62
        return $collection;
63
    }
64
}
65