Completed
Pull Request — develop (#127)
by Chuck
04:03
created

ProjectFactoryStrategies   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findMatching() 0 15 4
A __construct() 0 8 2
A addStrategy() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @copyright 2010-2018 Mike van Riel<[email protected]>
11
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
12
 * @link      http://phpdoc.org
13
 */
14
15
namespace phpDocumentor\Reflection\Php;
16
17
use OutOfBoundsException;
18
19
final class ProjectFactoryStrategies implements StrategyContainer
20
{
21
    /**
22
     * @var ProjectFactoryStrategy[]
23
     */
24
    private $strategies;
25
26
    /**
27
     * Initializes the factory with a number of strategies.
28
     *
29
     * @param ProjectFactoryStrategy[] $strategies
30
     */
31 1
    public function __construct(array $strategies)
32
    {
33 1
        foreach ($strategies as $strategy) {
34 1
            $this->addStrategy($strategy);
35
        }
36
37 1
        $this->strategies = $strategies;
38 1
    }
39
40
    /**
41
     * Find the ProjectFactoryStrategy that matches $object.
42
     *
43
     *
44
     * @param mixed $object
45
     * @throws OutOfBoundsException when no matching strategy was found.
46
     */
47 2
    public function findMatching($object): ProjectFactoryStrategy
48
    {
49 2
        foreach ($this->strategies as $strategy) {
50 1
            if ($strategy->matches($object)) {
51 1
                return $strategy;
52
            }
53
        }
54
55 1
        throw new OutOfBoundsException(
56 1
            sprintf(
57 1
                'No matching factory found for %s',
58 1
                is_object($object) ? get_class($object) : print_r($object, true)
59
            )
60
        );
61
    }
62
63
    /**
64
     * Add a strategy to this container.
65
     */
66 1
    public function addStrategy(ProjectFactoryStrategy $strategy): void
67
    {
68 1
        $this->strategies[] = $strategy;
69 1
    }
70
}
71