Collection   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 89
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getIterator() 0 33 6
A __toString() 0 4 1
1
<?php
2
3
namespace Acquia\Rest;
4
5
use Guzzle\Http\Message\RequestInterface;
6
7
class Collection extends \ArrayObject
8
{
9
    /**
10
     * @var \Guzzle\Http\Message\Response
11
     */
12
    protected $response;
13
14
    /**
15
     * @var string
16
     */
17
    protected $elementClass = '\Acquia\Rest\Element';
18
19
    /**
20
     * The array key containing the collection, null if it is not nested.
21
     *
22
     * Alternately set an array of keys that the may contain the collection.
23
     * This is useful when working with inconsistent APIs that store collections
24
     * of the same elements in different properties depending on the endpoint
25
     * that is consumed.
26
     *
27
     * @var string|array
28
     */
29
    protected $collectionProperty;
30
31
    /**
32
     * @param \Guzzle\Http\Message\RequestInterface $request
33
     */
34 42
    public function __construct(RequestInterface $request)
35
    {
36 42
        $this->response = $request->send();
37 42
        parent::__construct($this->response->json());
38 42
    }
39
40
    /**
41
     * Keys the array of objects by their identifier, constructs and returns and
42
     * array object.
43
     *
44
     * When the object is cast to a string, its unique identifier is returned.
45
     *
46
     * @return \ArrayObject
47
     *
48
     * @throws \OutOfBoundsException
49
     *
50
     * @see \Acquia\Rest\Element::__toString()
51
     */
52 36
    public function getIterator()
53
    {
54 36
        $array = $this->getArrayCopy();
55
56
        // Is the collection nested in the array?
57 36
        if (isset($this->collectionProperty)) {
58
59
            // Locate the collection in the response.
60 6
            $collectionFound = false;
61 6
            $property = NULL;
62 6
            foreach ((array) $this->collectionProperty as $property) {
63 6
                if (isset($array[$property])) {
64 3
                    $collectionFound = true;
65 3
                    break;
66
                }
67 6
            }
68
69 6
            if (!$collectionFound) {
70 3
                throw new \OutOfBoundsException('Collection not found in response');
71
            }
72
73 3
            $array = $array[$property];
74 3
        }
75
76
        // Build the collection.
77 33
        $collection = array();
78 33
        foreach ($array as $item) {
79 33
            $element = new $this->elementClass($item);
80 33
            $collection[(string) $element] = $element;
81 33
        }
82
83 33
        return new \ArrayObject($collection);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \ArrayObject($collection); (ArrayObject) is incompatible with the return type of the parent method ArrayObject::getIterator of type ArrayIterator.

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...
84
    }
85
86
    /**
87
     * Returns the raw response body, usually a string containing JSON.
88
     *
89
     * @return string
90
     */
91 3
    public function __toString()
92
    {
93 3
        return $this->response->getBody(true);
94
    }
95
}
96