Completed
Push — master ( f9a814...eec64c )
by Christophe
7s
created

MagicCallPatch::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Prophecy.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *     Marcello Duarte <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Prophecy\Doubler\ClassPatch;
13
14
use Prophecy\Doubler\Generator\Node\ClassNode;
15
use Prophecy\Doubler\Generator\Node\MethodNode;
16
use Prophecy\PhpDocumentor\ClassAndInterfaceTagRetriever;
17
use Prophecy\PhpDocumentor\MethodTagRetrieverInterface;
18
19
/**
20
 * Discover Magical API using "@method" PHPDoc format.
21
 *
22
 * @author Thomas Tourlourat <[email protected]>
23
 * @author Kévin Dunglas <[email protected]>
24
 * @author Théo FIDRY <[email protected]>
25
 */
26
class MagicCallPatch implements ClassPatchInterface
27
{
28
    private $tagRetriever;
29
30
    public function __construct(MethodTagRetrieverInterface $tagRetriever = null)
31
    {
32
        $this->tagRetriever = null === $tagRetriever ? new ClassAndInterfaceTagRetriever() : $tagRetriever;
33
    }
34
35
    /**
36
     * Support any class
37
     *
38
     * @param ClassNode $node
39
     *
40
     * @return boolean
41
     */
42
    public function supports(ClassNode $node)
43
    {
44
        return true;
45
    }
46
47
    /**
48
     * Discover Magical API
49
     *
50
     * @param ClassNode $node
51
     */
52
    public function apply(ClassNode $node)
53
    {
54
        $parentClass = $node->getParentClass();
55
        $reflectionClass = new \ReflectionClass($parentClass);
56
57
        $tagList = $this->tagRetriever->getTagList($reflectionClass);
58
59
        foreach($tagList as $tag) {
60
            $methodName = $tag->getMethodName();
61
62
            if (empty($methodName)) {
63
                continue;
64
            }
65
66
            if (!$reflectionClass->hasMethod($methodName)) {
67
                $methodNode = new MethodNode($methodName);
68
                $methodNode->setStatic($tag->isStatic());
69
70
                $node->addMethod($methodNode);
71
            }
72
        }
73
    }
74
75
    /**
76
     * Returns patch priority, which determines when patch will be applied.
77
     *
78
     * @return integer Priority number (higher - earlier)
79
     */
80
    public function getPriority()
81
    {
82
        return 50;
83
    }
84
}
85
86