Completed
Pull Request — develop (#92)
by Jaap
02:48
created

AbstractFactory::create()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 17
ccs 12
cts 12
cp 1
rs 9.4285
cc 3
eloc 10
nc 2
nop 3
crap 3
1
<?php
2
3
namespace phpDocumentor\Reflection\Php\Factory;
4
5
use phpDocumentor\Reflection\Element;
6
use phpDocumentor\Reflection\Middleware\ChainFactory;
7
use phpDocumentor\Reflection\Middleware\Middleware;
8
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
9
use phpDocumentor\Reflection\Php\StrategyContainer;
10
use phpDocumentor\Reflection\Types\Context;
11
use PhpParser\Comment\Doc;
12
use PhpParser\Node;
13
14
abstract class AbstractFactory implements ProjectFactoryStrategy
15
{
16
    /**
17
     * @var callable
18
     */
19
    private $middlewareChain;
20
21
    public function __construct($middleware = [])
22
    {
23
        $lastCallable = function(CreateCommand $command) {
24
            return $this->doCreate($command->getObject(), $command->getStrategies(), $command->getContext());
25
        };
26
27
        $this->middlewareChain = ChainFactory::createExecutionChain($middleware, $lastCallable);
28
    }
29
30
31
    abstract public function matches($object);
32
33 42
    final public function create($object, StrategyContainer $strategies, Context $context = null)
34
    {
35 42
        if (!$this->matches($object)) {
36 8
            throw new \InvalidArgumentException(
37 8
                sprintf(
38 8
                    '%s cannot handle objects with the type %s',
39 8
                    __CLASS__,
40 8
                    is_object($object) ? get_class($object) : gettype($object)
41 8
                )
42 8
            );
43
        }
44
45 34
        $command = new CreateCommand($object, $strategies, $context);
46 34
        $middleware = $this->middlewareChain;
47
48 34
        return $middleware($command);
49
    }
50
51
    abstract protected function doCreate($object, StrategyContainer $strategies, Context $context = null);
52
53
    /**
54
     * @param Node|PropertyIterator|ClassConstantIterator|Doc $stmt
55
     * @param StrategyContainer $strategies
56
     * @param Context $context
57
     * @return Element
58
     */
59 11
    protected function createMember($stmt, StrategyContainer $strategies, Context $context = null)
60
    {
61 11
        $strategy = $strategies->findMatching($stmt);
62 11
        return $strategy->create($stmt, $strategies, $context);
63
    }
64
65
    /**
66
     * @param StrategyContainer $strategies
67
     * @param Doc $docBlock
68
     * @param Context $context
69
     * @return null|\phpDocumentor\Reflection\DocBlock
70
     */
71 28
    protected function createDocBlock(StrategyContainer $strategies, Doc $docBlock = null, Context $context = null)
72
    {
73 28
        if ($docBlock === null) {
74 22
            return null;
75
        }
76
77 7
        return $this->createMember($docBlock, $strategies, $context);
78
    }
79
}
80