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

Statements::execute()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 14
cp 0
rs 9.2
cc 4
eloc 11
nc 2
nop 2
crap 20
1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2010-2015 Mike van Riel<[email protected]>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Reflection\Php\Factory\Middleware;
14
15
16
use phpDocumentor\Reflection\Fqsen;
17
use phpDocumentor\Reflection\Middleware\Middleware;
18
use phpDocumentor\Reflection\Php\Factory\ClassConstantIterator;
19
use phpDocumentor\Reflection\Php\Factory\PropertyIterator;
20
use phpDocumentor\Reflection\Php\StrategyContainer;
21
use phpDocumentor\Reflection\Types\Context;
22
use PhpParser\Node\Stmt\ClassConst;
23
use PhpParser\Node\Stmt\ClassMethod;
24
use PhpParser\Node\Stmt\TraitUse;
25
use PhpParser\Node\Stmt\Property as PropertyNode;
26
27
final class Statements implements Middleware
28
{
29
    private $callbacks;
30
31
    public function __construct()
32
    {
33
        $this->callbacks[TraitUse::class] = function($classElement, $stmt) {
34
            foreach ($stmt->traits as $use) {
35
                $classElement->addUsedTrait(new Fqsen('\\'. $use->toString()));
36
            }
37
        };
38
39
        $this->callbacks[ClassConst::class] = function($classElement, $stmt, $strategies, $context) {
40
            $constants = new ClassConstantIterator($stmt);
41
            foreach ($constants as $const) {
42
                $element = $this->createMember($const, $strategies, $context);
43
                $classElement->addConstant($element);
44
            }
45
        };
46
47
        $this->callbacks[ClassMethod::class] = function ($classElement, $stmt, $strategies, $context) {
48
            $method = $this->createMember($stmt, $strategies, $context);
49
            $classElement->addMethod($method);
50
        };
51
52
        $this->callbacks[PropertyNode::class] = function ($classElement, $stmt, $strategies, $context) {
53
            $properties = new PropertyIterator($stmt);
54
            foreach ($properties as $property) {
55
                $element = $this->createMember($property, $strategies, $context);
56
                $classElement->addProperty($element);
57
            }
58
        };
59
    }
60
61
62
    /**
63
     * Executes this middle ware class.
64
     *
65
     * @param $command
66
     * @param callable $next
67
     *
68
     * @return object
69
     */
70
    public function execute($command, callable $next)
71
    {
72
        $element = $next($command);
73
        $object = $command->getObject();
74
        $strategies = $command->getStrategies();
75
        $context = $command->getContext();
76
77
        if (isset($object->stmts)) {
78
            foreach ($object->stmts as $stmt) {
79
                if (isset($this->callbacks[get_class($stmt)])) {
80
                    $callBack = $this->callbacks[get_class($stmt)];
81
                    $callBack($element, $stmt, $strategies, $context);
82
                }
83
            }
84
        }
85
86
        return $element;
87
    }
88
89
    /**
90
     * @param Node|PropertyIterator|ClassConstantIterator|Doc $stmt
91
     * @param StrategyContainer $strategies
92
     * @param Context $context
93
     * @return Element
94
     */
95
    protected function createMember($stmt, StrategyContainer $strategies, Context $context = null)
96
    {
97
        $strategy = $strategies->findMatching($stmt);
98
        return $strategy->create($stmt, $strategies, $context);
99
    }
100
}
101