CollectionMap   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
c 3
b 0
f 0
lcom 1
cbo 2
dl 0
loc 50
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 8 2
A add() 0 14 2
A getWithCallback() 0 14 3
A addWithTypeCheck() 0 8 3
1
<?php
2
3
namespace DCP\Mapper\Collections\Maps;
4
5
use DCP\Mapper\Collection;
6
use DCP\Mapper\Exception\InvalidArgumentException;
7
use DCP\Mapper\Exception\OutOfBoundsException;
8
9
abstract class CollectionMap extends Map implements CollectionMapInterface
10
{
11
    public function get($key, $default = true, $callback = null)
12
    {
13
        if ($default) {
14
            return $this->getWithCallback($key, $callback);
15
        }
16
17
        return $this->getWithCallback($key, null);
18
    }
19
20
    public function add($key, $value, callable $defaultCollectionCallback = null)
21
    {
22
        $collection = self::get($key, false);
23
24
        if (!$collection) {
25
            /** @var Collection $collection */
26
            $collection = call_user_func($defaultCollectionCallback);
27
            $this->set($key, $collection);
28
        }
29
30
        $collection->add($value);
31
32
        return $this;
33
    }
34
35
    protected function getWithCallback($key, callable $defaultCallback = null)
36
    {
37
        $collection = null;
38
39
        try {
40
            $collection = parent::get($key);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (get() instead of getWithCallback()). Are you sure this is correct? If so, you might want to change this to $this->get().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
41
        } catch (OutOfBoundsException $exception) {
42
            if ($defaultCallback) {
43
                $collection = call_user_func($defaultCallback);
44
            }
45
        }
46
47
        return $collection;
48
    }
49
50
    protected function addWithTypeCheck($key, $value, $type, callable $callback = null)
51
    {
52
        if (!(is_object($value) && is_subclass_of($value, $type))) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $type can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
53
            throw new InvalidArgumentException(sprintf('$value must be an instance of %s', $type));
54
        }
55
56
        return self::add($key, $value, $callback);
57
    }
58
}
59