Passed
Push — master ( ed53b1...556216 )
by Thorsten
03:33
created

AttributeMap   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 41.67%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 3
dl 0
loc 25
ccs 5
cts 12
cp 0.4167
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A byClassNames() 0 12 1
1
<?php
2
/**
3
 * This file is part of the daikon-cqrs/entity project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Daikon\Entity\Entity;
12
13
use Daikon\DataStructure\TypedMapTrait;
14
15
final class AttributeMap implements \IteratorAggregate, \Countable
16
{
17
    use TypedMapTrait;
18
19
    public function __construct(array $attributes = [])
20
    {
21 10
        $this->init(array_reduce($attributes, function (array $carry, AttributeInterface $attribute) {
22 10
            $carry[$attribute->getName()] = $attribute; // enforce consistent attribute keys
23 10
            return $carry;
24 10
        }, []), AttributeInterface::class);
25 10
    }
26
27
    public function byClassNames(array $classNames = []): self
28
    {
29
        $clonedMap = clone $this;
30
        (function (string ...$classNames) use ($clonedMap): void {
31
            $clonedMap->compositeMap = $clonedMap->compositeMap->filter(
32
                function (string $name, AttributeInterface $attribute) use ($classNames): bool {
33
                    return in_array(get_class($attribute), $classNames);
34
                }
35
            );
36
        })(...$classNames);
37
        return $clonedMap;
38
    }
39
}
40