GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ListResource::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace prgTW\BaseCRM\Resource;
4
5
use Doctrine\Common\Inflector\Inflector;
6
use prgTW\BaseCRM\Resource\Partial\CreateResource;
7
use prgTW\BaseCRM\Transport\Transport;
8
9
abstract class ListResource extends Resource implements \ArrayAccess, \IteratorAggregate, \Countable
10
{
11
	use CreateResource;
12
13
	/** @var ResourceCollection */
14
	protected $collection;
15
16
	/** @var array */
17
	private $query;
18
19
	/** {@inheritdoc} */
20
	public function __construct(Transport $transport, $uri)
21
	{
22
		parent::__construct($transport, $uri);
23
		$this->reset();
24
	}
25
26
	protected function reset()
27
	{
28
		$this->collection = null;
29
		$this->query      = [];
30
	}
31
32
	/**
33
	 * @param string $id
34
	 *
35
	 * @return InstanceResource
36
	 */
37
	public function get($id)
38
	{
39
		$instanceClassName = Inflector::singularize(static::class);
40
		$uri               = sprintf('%s/%s', $this->uri, $id);
41
		/** @var InstanceResource $resource */
42
		$resource = new $instanceClassName($this->transport, $uri);
43
44
		return $resource;
45
	}
46
47
	/**
48
	 * @param string $id
49
	 *
50
	 * @return bool
51
	 */
52
	public function delete($id)
53
	{
54
		$uri    = $this->getFullUri(sprintf('/%d', $id));
55
		$result = $this->transport->delete($uri);
56
57
		return in_array($result, [null, true], true);
58
	}
59
60
	/**
61
	 * @param array $query
62
	 *
63
	 * @return ResourceCollection
64
	 */
65
	public function all($query = [])
66
	{
67
		if (null !== $this->collection && $this->query === $query)
68
		{
69
			return $this->collection;
70
		}
71
72
		$uri     = $this->getFullUri();
73
		$options = [] === $query ? [] : ['query' => $query];
74
		$data    = $this->transport->get($uri, null, $options);
75
76
		$this->query      = $options;
77
		$this->collection = $this->postAll($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $this->transport->get($uri, null, $options) on line 74 can also be of type boolean; however, prgTW\BaseCRM\Resource\ListResource::postAll() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
78
79
		return $this->collection;
80
	}
81
82
	/** {@inheritdoc} */
83
	public function count()
84
	{
85
		$collection = $this->all();
86
87
		return count($collection);
88
	}
89
90
	/**
91
	 * @return ResourceCollection
92
	 */
93
	public function getIterator()
94
	{
95
		$collection = $this->all();
96
97
		return $collection;
98
	}
99
100
	/**
101
	 * @param array $data
102
	 *
103
	 * @return ResourceCollection
104
	 */
105
	protected function postAll(array $data)
106
	{
107
		$childResourceName = $this->getChildResourceName();
108
109
		foreach ($data as $key => $resourceData)
110
		{
111
			$data[$key] = $this->getObjectFromArray($resourceData[$childResourceName]);
112
		}
113
114
		return new ResourceCollection($data, $childResourceName);
0 ignored issues
show
Unused Code introduced by
The call to ResourceCollection::__construct() has too many arguments starting with $childResourceName.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
115
	}
116
117
	/** {@inheritdoc} */
118
	public function offsetExists($offset)
119
	{
120
		$this->all($this->query);
121
122
		return $this->collection->offsetExists($offset);
123
	}
124
125
	/** {@inheritdoc} */
126
	public function offsetGet($offset)
127
	{
128
		$this->all($this->query);
129
130
		return $this->collection->offsetGet($offset);
131
	}
132
133
	/** {@inheritdoc} */
134
	public function offsetSet($offset, $value)
135
	{
136
		$this->all($this->query);
137
138
		$this->collection->offsetSet($offset, $value);
139
	}
140
141
	/** {@inheritdoc} */
142
	public function offsetUnset($offset)
143
	{
144
		$this->all($this->query);
145
146
		return $this->collection->offsetUnset($offset);
147
	}
148
}
149