Passed
Push — master ( 918acc...2e1e94 )
by Gerrit
01:58
created

createArgumentFromString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 21
loc 21
ccs 10
cts 10
cp 1
rs 9.584
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\SymfonyGenerics\Arguments\ArgumentFactory;
14
15
use Addiks\SymfonyGenerics\Arguments\ArgumentFactory\ArgumentFactory;
16
use Webmozart\Assert\Assert;
17
use Addiks\SymfonyGenerics\Arguments\Argument;
18
19
final class ArgumentFactoryAggregate implements ArgumentFactory
20
{
21
22
    /**
23
     * @var array<ArgumentFactory>
24
     */
25
    private $innerArgumentFactories;
26
27 19
    public function __construct(array $innerArgumentFactories)
28
    {
29 19
        Assert::null($this->innerArgumentFactories);
30 19
        $this->innerArgumentFactories = array();
31
32 19
        foreach ($innerArgumentFactories as $innerArgumentFactory) {
33
            /** @var ArgumentFactory $innerArgumentFactory */
34
35 19
            Assert::isInstanceOf($innerArgumentFactory, ArgumentFactory::class);
36
37 19
            $this->innerArgumentFactories[] = $innerArgumentFactory;
38
        }
39 19
    }
40
41 5
    public function understandsString(string $source): bool
42
    {
43
        /** @var bool $understandsString */
44 5
        $understandsString = false;
45
46 5
        foreach ($this->innerArgumentFactories as $innerArgumentFactory) {
47
            /** @var ArgumentFactory $innerArgumentFactory */
48
49 4
            $understandsString = $innerArgumentFactory->understandsString($source);
50
51 4
            if ($understandsString) {
52 4
                break;
53
            }
54
        }
55
56 5
        return $understandsString;
57
    }
58
59 5
    public function understandsArray(array $source): bool
60
    {
61
        /** @var bool $understandsArray */
62 5
        $understandsArray = false;
63
64 5
        foreach ($this->innerArgumentFactories as $innerArgumentFactory) {
65
            /** @var ArgumentFactory $innerArgumentFactory */
66
67 4
            $understandsArray = $innerArgumentFactory->understandsArray($source);
68
69 4
            if ($understandsArray) {
70 4
                break;
71
            }
72
        }
73
74 5
        return $understandsArray;
75
    }
76
77 4 View Code Duplication
    public function createArgumentFromString(string $source): Argument
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        /** @var Argument $argument */
80 4
        $argument = null;
81
82 4
        foreach ($this->innerArgumentFactories as $innerArgumentFactory) {
83
            /** @var ArgumentFactory $innerArgumentFactory */
84
85 4
            if ($innerArgumentFactory->understandsString($source)) {
86 3
                $argument = $innerArgumentFactory->createArgumentFromString($source);
87 4
                break;
88
            }
89
        }
90
91 4
        Assert::isInstanceOf($argument, Argument::class, sprintf(
92 4
            "Could not parse '%s' into argument!",
93 4
            $source
94
        ));
95
96 3
        return $argument;
97
    }
98
99 4 View Code Duplication
    public function createArgumentFromArray(array $source): Argument
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        /** @var Argument $argument */
102 4
        $argument = null;
103
104 4
        foreach ($this->innerArgumentFactories as $innerArgumentFactory) {
105
            /** @var ArgumentFactory $innerArgumentFactory */
106
107 4
            if ($innerArgumentFactory->understandsArray($source)) {
108 3
                $argument = $innerArgumentFactory->createArgumentFromArray($source);
109 4
                break;
110
            }
111
        }
112
113 4
        Assert::isInstanceOf($argument, Argument::class, "Could not parse array into argument!");
114
115 3
        return $argument;
116
    }
117
118
}
119