Completed
Push — develop ( 34db25...1b1cc6 )
by Paul
02:36
created

MethodNodeParser::setter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PhpUnitGen\Parser\NodeParser;
4
5
use PhpParser\Node;
6
use PhpUnitGen\Annotation\GetAnnotation;
7
use PhpUnitGen\Annotation\SetAnnotation;
8
use PhpUnitGen\Model\FunctionModel;
9
use PhpUnitGen\Model\ModelInterface\InterfaceModelInterface;
10
use PhpUnitGen\Model\PropertyInterface\NodeInterface;
11
use PhpUnitGen\Parser\NodeParserUtil\MethodVisibilityHelper;
12
use Respect\Validation\Validator;
13
14
/**
15
 * Class MethodNodeParser.
16
 *
17
 * @author     Paul Thébaud <[email protected]>.
18
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
19
 * @license    https://opensource.org/licenses/MIT The MIT license.
20
 * @link       https://github.com/paul-thebaud/phpunit-generator
21
 * @since      Class available since Release 2.0.0.
22
 */
23
class MethodNodeParser extends AbstractFunctionNodeParser
24
{
25
    /**
26
     * Parse a node to update the parent node model.
27
     *
28
     * @param mixed         $node   The node to parse.
29
     * @param NodeInterface $parent The parent node.
30
     */
31
    public function invoke($node, NodeInterface $parent): void
32
    {
33
        if (! $node instanceof Node\Stmt\ClassMethod || ! $parent instanceof InterfaceModelInterface) {
34
            throw new Exception('MethodNodeParser is made to parse a method node');
0 ignored issues
show
Bug introduced by
The type PhpUnitGen\Parser\NodeParser\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
35
        }
36
37
        $function = new FunctionModel();
38
        $function->setParentNode($parent);
39
        $function->setName($node->name);
40
        $function->setIsFinal($node->isFinal());
41
        $function->setIsStatic($node->isStatic());
42
        $function->setIsAbstract($node->isAbstract());
43
        $function->setVisibility(MethodVisibilityHelper::getMethodVisibility($node));
44
45
        $this->parseFunction($node, $function);
46
47
        $parent->addFunction($function);
48
49
        if ($this->config->hasAuto()) {
50
            if ($this->getter($function)) {
51
                return;
52
            }
53
            if ($this->setter($function)) {
54
                return;
55
            }
56
        }
57
        if (($documentation = $node->getDocComment()) !== null) {
58
            $this->documentationNodeParser->invoke($documentation, $function);
59
        }
60
    }
61
62
    private function getter(FunctionModel $function): bool
63
    {
64
        // Check if function name matches
65
        preg_match('/^get(.+)$/', $function->getName(), $matches);
66
67
        if (Validator::arrayType()->length(2, 2)->validate($matches)) {
68
            // Check if property exists
69
            $property = lcfirst($matches[1]);
70
            if ($function->getParentNode()->hasAttribute($property, $function->isStatic())) {
0 ignored issues
show
Bug introduced by
The method hasAttribute() does not exist on PhpUnitGen\Model\PropertyInterface\NodeInterface. It seems like you code against a sub-type of PhpUnitGen\Model\PropertyInterface\NodeInterface such as PhpUnitGen\Model\ModelIn...ace\TraitModelInterface or PhpUnitGen\Model\TraitModel or PhpUnitGen\Model\ModelIn...ace\TraitModelInterface or PhpUnitGen\Model\TraitModel. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            if ($function->getParentNode()->/** @scrutinizer ignore-call */ hasAttribute($property, $function->isStatic())) {
Loading history...
71
                $annotation = new GetAnnotation();
72
                $annotation->setName('@PhpUnitGen\\getter');
73
                $function->addAnnotation($annotation);
74
                $annotation->setParentNode($function);
75
                $annotation->compile();
76
77
                return true;
78
            }
79
        }
80
81
        return false;
82
    }
83
84
    private function setter(FunctionModel $function): bool
85
    {
86
        // Check if function name matches
87
        preg_match('/^set(.+)$/', $function->getName(), $matches);
88
89
        if (Validator::arrayType()->length(2, 2)->validate($matches)) {
90
            // Check if property exists
91
            $property = lcfirst($matches[1]);
92
            if ($function->getParentNode()->hasAttribute($property, $function->isStatic())) {
93
                $annotation = new SetAnnotation();
94
                $annotation->setName('@PhpUnitGen\\setter');
95
                $annotation->setParentNode($function);
96
                $function->addAnnotation($annotation);
97
                $annotation->compile();
98
99
                return true;
100
            }
101
        }
102
103
        return false;
104
    }
105
}
106