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.
Completed
Pull Request — master (#15)
by Cees-Jan
10:52 queued 53s
created

Cache   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 25
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 13 3
A write() 0 8 1
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Tools\CommandBus;
4
5
use ExceptionalJSON\DecodeErrorException;
6
use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;
7
8
/**
9
 * @internal
10
 */
11
final class Cache
12
{
13
    public static function read(string $cacheFile): iterable
14
    {
15
        try {
16
            $mapping = json_try_decode(file_get_contents($cacheFile));
17
            foreach ($mapping as $command => $handler) {
18
                yield $command => new Handler($handler);
19
            }
20
        } catch (DecodeErrorException $decodeErrorException) {
21
            // Do nothing, we'll return an empty array instead
22
        }
23
24
        return [];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type documented by ApiClients\Tools\CommandBus\Cache::read of type Generator.

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...
25
    }
26
27
    public static function write(string $cacheFile, array $mapping): void
28
    {
29
        file_put_contents($cacheFile, json_encode(array_map(function (Handler $handler) {
30
            return [
31
                $handler->getHandler(),
32
            ];
33
        }, $mapping)));
34
    }
35
}
36