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.

Cache::getMultiple()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 10
nc 3
nop 2
crap 3
1
<?php
2
3
namespace HighWire\DrupalPSR16;
4
5
class Cache implements \Psr\SimpleCache\CacheInterface {
6
7
  /**
8
    * @var \Drupal\Core\Cache\CacheBackendInterface
9
    */
10
  protected $drupal_cache;
11
12
  /**
13
    * @param \Drupal\Core\Cache\CacheBackendInterface $drupal_cache
14
    *   The drupal cache backend to convert to PSR-16
15
    * 
16
    * @example
17
    *   $drupalcache = \Drupal::cache('mybin');
18
    *   $psr16cache = new \HighWire\DrupalPSR16\Cache($drupalcache);
19
    */
20 1
  public function __construct(\Drupal\Core\Cache\CacheBackendInterface $drupal_cache) {
21 1
    $this->drupal_cache = $drupal_cache;
22 1
  }
23
24
  /**
25
    * {@inheritdoc}
26
    */
27 1
  public function get($key, $default = null) {
28 1
    $cache = $this->drupal_cache->get($key);
29 1
    if (empty($cache)) {
30 1
      return $default;
31
    }
32
    else {
33 1
      return $cache->data;
34
    }
35
  }
36
37
  /**
38
    * {@inheritdoc}
39
    */
40 1
  public function set($key, $value, $ttl = NULL) {
41 1
    if ($ttl === NULL) {
42 1
      $this->drupal_cache->set($key, $value);
43
    }
44
    else {
45
      $this->drupal_cache->set($key, $value, time() + $ttl);
46
    }
47 1
    return true;
48
  }
49
50
  /**
51
    * {@inheritdoc}
52
    */
53
  public function delete($key) {
54
    $this->drupal_cache->delete($key);
55
    return true;
56
  }
57
58
  /**
59
    * {@inheritdoc}
60
    */
61 1
  public function clear() {
62 1
    $this->drupal_cache->deleteAll();
63 1
    return true;
64
  }
65
66
  /**
67
    * {@inheritdoc}
68
    */
69 1
  public function getMultiple($keys, $default = null) {
70 1
    $result = array();
71 1
    $k = $keys;
72 1
    $caches = $this->drupal_cache->getMultiple($k);
73 1
    foreach ($keys as $key) {
74 1
      if (!empty($caches[$key])) {
75 1
        $result[$key] = $caches[$key]->data;
76
      }
77
      else {
78 1
        $result[$key] = $default;
79
      }
80
    }
81 1
    return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (array) is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::getMultiple of type Psr\SimpleCache\iterable.

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...
82
  }
83
84
  /**
85
    * {@inheritdoc}
86
    */
87 1
  public function setMultiple($values, $ttl = null) {
88 1
    $items = array();
89 1
    foreach ($values as $key => $value) {
90 1
      if ($ttl === NULL) {
91 1
        $items[$key] = array('data' => $value);
92
      }
93
      else {
94 1
        $items[$key] = array('data' => $value, 'expire' => time() + $ttl);
95
      }
96
    }
97 1
    $this->drupal_cache->setMultiple($items);
98 1
    return true;
99
  }
100
101
  /**
102
    * {@inheritdoc}
103
    */
104
  public function deleteMultiple($keys) {
105
    $this->drupal_cache->deleteMultiple($keys);
106
    return true;
107
  }
108
109
  /**
110
    * {@inheritdoc}
111
    */
112
  public function has($key) {
113
    $cache = $this->drupal_cache->get($key);
114
    return !empty($cache);
115
  }
116
117
}