LanguageMapOfLists   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 37
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkMember() 0 14 3
A contains() 0 9 3
A jsonLDSerialize() 0 9 2
1
<?php declare(strict_types = 1);
2
3
namespace JSKOS;
4
5
use InvalidArgumentException;
6
use TypeError;
7
8
/**
9
 * Language map of lists for labels and notes.
10
 */
11
class LanguageMapOfLists extends LanguageMap
12
{
13
    protected static function checkMember($value)
14
    {
15
        if ($value instanceof Listing) {
16
            return $value;
17
        } else {
18
            try {
19
                return new Listing($value);
20
            } catch (TypeError $e) {
21
                throw new InvalidArgumentException(
22
                    'JSKOS\LanguageMapOfLists may only contain JSKOS\Listing'
23
                );
24
            }
25
        }
26
    }
27
28
    public function contains($member): bool
29
    {
30
        foreach ($this->members as $list) {
31
            if ($list->contains($member)) {
32
                return true;
33
            }
34
        }
35
        return false;
36
    }
37
38
    public function jsonLDSerialize(string $context = self::DEFAULT_CONTEXT, bool $types = null)
39
    {
40
        ksort($this->members);
41
        $map = new \stdClass();
42
        foreach ($this->members as $lang => $list) {
43
            $map->$lang = $list->jsonLDSerialize('', $types);
44
        }
45
        return $map;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $map; (stdClass) is incompatible with the return type of the parent method JSKOS\Container::jsonLDSerialize of type array.

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...
46
    }
47
}
48