Issues (96)

lib/Google/Collection.php (2 issues)

Labels
Severity
1
<?php
2
3
if (!class_exists('Google_Client')) {
4
  require_once __DIR__ . '/autoload.php';
5
}
6
7
/**
8
 * Extension to the regular Google_Model that automatically
9
 * exposes the items array for iteration, so you can just
10
 * iterate over the object rather than a reference inside.
11
 */
12
class Google_Collection extends Google_Model implements Iterator, Countable
13
{
14
  protected $collection_key = 'items';
15
16
  public function rewind()
17
  {
18
    if (isset($this->{$this->collection_key})
19
        && is_array($this->{$this->collection_key})) {
20
      reset($this->{$this->collection_key});
21
    }
22
  }
23
24
  public function current()
25
  {
26
    $this->coerceType($this->key());
27
    if (is_array($this->{$this->collection_key})) {
28
      return current($this->{$this->collection_key});
29
    }
30
  }
31
32
  public function key()
33
  {
34
    if (isset($this->{$this->collection_key})
35
        && is_array($this->{$this->collection_key})) {
36
      return key($this->{$this->collection_key});
37
    }
38
  }
39
40
  public function next()
41
  {
42
    return next($this->{$this->collection_key});
43
  }
44
45
  public function valid()
46
  {
47
    $key = $this->key();
48
    return $key !== null && $key !== false;
49
  }
50
51
  public function count()
52
  {
53
    if (!isset($this->{$this->collection_key})) {
54
      return 0;
55
    }
56
    return count($this->{$this->collection_key});
57
  }
58
59
  public function offsetExists($offset)
60
  {
61
    if (!is_numeric($offset)) {
62
      return parent::offsetExists($offset);
63
    }
64
    return isset($this->{$this->collection_key}[$offset]);
65
  }
66
67
  public function offsetGet($offset)
68
  {
69
    if (!is_numeric($offset)) {
70
      return parent::offsetGet($offset);
71
    }
72
    $this->coerceType($offset);
73
    return $this->{$this->collection_key}[$offset];
74
  }
75
76
  public function offsetSet($offset, $value)
77
  {
78
    if (!is_numeric($offset)) {
79
      return parent::offsetSet($offset, $value);
0 ignored issues
show
Are you sure the usage of parent::offsetSet($offset, $value) targeting Google_Model::offsetSet() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
80
    }
81
    $this->{$this->collection_key}[$offset] = $value;
82
  }
83
84
  public function offsetUnset($offset)
85
  {
86
    if (!is_numeric($offset)) {
87
      return parent::offsetUnset($offset);
0 ignored issues
show
Are you sure the usage of parent::offsetUnset($offset) targeting Google_Model::offsetUnset() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
88
    }
89
    unset($this->{$this->collection_key}[$offset]);
90
  }
91
92
  private function coerceType($offset)
93
  {
94
    $keyType = $this->keyType($this->collection_key);
95
    if ($keyType && !is_object($this->{$this->collection_key}[$offset])) {
96
      $this->{$this->collection_key}[$offset] =
97
          new $keyType($this->{$this->collection_key}[$offset]);
98
    }
99
  }
100
}
101