TraitParser   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 112
Duplicated Lines 8.04 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 9
loc 112
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getToken() 0 4 1
A getConstants() 0 4 1
B getDefinitionFromTokens() 9 62 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * \AppserverIo\Doppelgaenger\Parser\TraitParser
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Bernhard Wick <[email protected]>
15
 * @copyright 2015 TechDivision GmbH - <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/doppelgaenger
18
 * @link      http://www.appserver.io/
19
 */
20
21
namespace AppserverIo\Doppelgaenger\Parser;
22
23
use AppserverIo\Doppelgaenger\Entities\Definitions\TraitDefinition;
24
use AppserverIo\Doppelgaenger\Exceptions\GeneratorException;
25
use AppserverIo\Psr\MetaobjectProtocol\Dbc\Annotations\Invariant;
26
27
/**
28
 * Parser which is used to parse trait definitions.
29
 * Does inherit from the class parser, as both have a lot in common
30
 *
31
 * @author    Bernhard Wick <[email protected]>
32
 * @copyright 2015 TechDivision GmbH - <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/appserver-io/doppelgaenger
35
 * @link      http://www.appserver.io/
36
 */
37
class TraitParser extends AbstractStructureParser
38
{
39
40
    /**
41
     * We want to be able to parse properties
42
     */
43
    use PropertyParserTrait;
44
45
    /**
46
     * Token representing the structure this parser is used for
47
     *
48
     * @var integer TOKEN
49
     */
50
    const TOKEN = T_TRAIT;
51
52
    /**
53
     * Will return the token representing the structure the parser is used for e.g. T_CLASS
54
     *
55
     * @return integer
56
     */
57
    public function getToken()
58
    {
59
        return self::TOKEN;
60
    }
61
62
    /**
63
     * Will return the constants within the main token array.
64
     * Traits cannot have constants sadly...
65
     *
66
     * @return array
67
     */
68
    public function getConstants()
69
    {
70
        return array();
71
    }
72
73
    /**
74
     * Returns a TraitDefinition from a token array.
75
     *
76
     * This method will use a set of other methods to parse a token array and retrieve any
77
     * possible information from it. This information will be entered into a ClassDefinition object.
78
     *
79
     * @param array   $tokens       The token array containing structure tokens
80
     * @param boolean $getRecursive Do we have to get the ancestral conditions as well? Makes no sense currently
81
     *
82
     * @return \AppserverIo\Doppelgaenger\Interfaces\StructureDefinitionInterface
83
     *
84
     * @throws \AppserverIo\Doppelgaenger\Exceptions\GeneratorException
85
     */
86
    protected function getDefinitionFromTokens($tokens, $getRecursive = false)
87
    {
88
        // First of all we need a new TraitDefinition to fill
89 View Code Duplication
        if (is_null($this->currentDefinition)) {
90
            $this->currentDefinition = new TraitDefinition();
91
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
92
        } elseif (!$this->currentDefinition instanceof TraitDefinition) {
93
            throw new GeneratorException(sprintf(
94
                'The structure definition %s does not seem to be a trait definition.',
95
                $this->currentDefinition->getQualifiedName()
96
            ));
97
        }
98
99
        // Save the path of the original definition for later use
100
        $this->currentDefinition->setPath($this->file);
101
102
        // File based namespaces do not make much sense, so hand it over here.
103
        $this->currentDefinition->setNamespace($this->getNamespace());
104
        $this->currentDefinition->setName($this->getName($tokens));
105
        $this->currentDefinition->setUsedStructures($this->getUsedStructures());
106
107
        // For our next step we would like to get the doc comment (if any)
108
        $this->currentDefinition->setDocBlock($this->getDocBlock($tokens, $this->getToken()));
109
110
        // Get start and end line
111
        $this->currentDefinition->setStartLine($this->getStartLine($tokens));
112
        $this->currentDefinition->setEndLine($this->getEndLine($tokens));
113
114
        // So we got our docBlock, now we can parse the invariant annotations from it
115
        $annotationParser = new AnnotationParser($this->file, $this->config, $this->tokens, $this->currentDefinition);
116
        $this->currentDefinition->setInvariantConditions(
117
            $annotationParser->getConditions(
0 ignored issues
show
Security Bug introduced by
It seems like $annotationParser->getCo...\Invariant::ANNOTATION) targeting AppserverIo\Doppelgaenge...Parser::getConditions() can also be of type false; however, AppserverIo\Doppelgaenge...etInvariantConditions() does only seem to accept object<AppserverIo\Doppe...es\Lists\AssertionList>, did you maybe forget to handle an error condition?
Loading history...
118
                $this->currentDefinition->getDocBlock(),
119
                Invariant::ANNOTATION
120
            )
121
        );
122
123
        // Only thing still missing are the methods, so ramp up our FunctionParser
124
        $functionParser = new FunctionParser(
125
            $this->file,
126
            $this->config,
127
            $this->structureDefinitionHierarchy,
128
            $this->structureMap,
129
            $this->currentDefinition,
130
            $this->tokens
131
        );
132
133
        $this->currentDefinition->setFunctionDefinitions(
134
            $functionParser->getDefinitionListFromTokens(
0 ignored issues
show
Security Bug introduced by
It seems like $functionParser->getDefi...$tokens, $getRecursive) targeting AppserverIo\Doppelgaenge...initionListFromTokens() can also be of type false; however, AppserverIo\Doppelgaenge...etFunctionDefinitions() does only seem to accept object<AppserverIo\Doppe...FunctionDefinitionList>, did you maybe forget to handle an error condition?
Loading history...
135
                $tokens,
136
                $getRecursive
137
            )
138
        );
139
140
        // Lets get the attributes the class might have
141
        $this->currentDefinition->setAttributeDefinitions($this->getAttributes($tokens));
142
143
        // Before exiting we will add the entry to the current structure definition hierarchy
144
        $this->structureDefinitionHierarchy->insert($this->currentDefinition);
145
146
        return $this->currentDefinition;
147
    }
148
}
149