InterfaceParser::getToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * \AppserverIo\Doppelgaenger\Parser\InterfaceParser
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\InterfaceDefinition;
24
use AppserverIo\Doppelgaenger\Entities\Definitions\FileDefinition;
25
use AppserverIo\Doppelgaenger\Entities\Lists\StructureDefinitionList;
26
use AppserverIo\Doppelgaenger\Exceptions\GeneratorException;
27
use AppserverIo\Psr\MetaobjectProtocol\Dbc\Annotations\Invariant;
28
29
/**
30
 * The InterfaceParser class which is used to get an \AppserverIo\Doppelgaenger\Entities\Definitions\InterfaceDefinition
31
 * instance (or several) from a fail containing those definition(s)
32
 *
33
 * @author    Bernhard Wick <[email protected]>
34
 * @copyright 2015 TechDivision GmbH - <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/appserver-io/doppelgaenger
37
 * @link      http://www.appserver.io/
38
 */
39
class InterfaceParser extends AbstractStructureParser
40
{
41
42
    /**
43
     * Token representing the structure this parser is used for
44
     *
45
     * @var integer TOKEN
46
     */
47
    const TOKEN = T_INTERFACE;
48
49
    /**
50
     * Will return the token representing the structure the parser is used for e.g. T_CLASS
51
     *
52
     * @return integer
53
     */
54
    public function getToken()
55
    {
56
        return self::TOKEN;
57
    }
58
59
    /**
60
     * Will get all parent interfaces (if any).
61
     * Might return false on error
62
     *
63
     * @param array $tokens The token array
64
     *
65
     * @return array
66
     */
67
    public function getParents($tokens)
68
    {
69
        // Check the tokens
70
        $interfaceString = '';
71
        for ($i = 0; $i < count($tokens); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
72
            // If we got the interface name
73
            if ($tokens[$i][0] === T_EXTENDS) {
74
                for ($j = $i + 1; $j < count($tokens); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
75
                    if ($tokens[$j] === '{') {
76
                        // We got everything
77
                        break;
78
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
79
                    } elseif ($tokens[$j][0] === T_STRING) {
80
                        $interfaceString .= $tokens[$j][1];
81
                    }
82
                }
83
            }
84
        }
85
86
        // Normally we will have one or several interface names separated by commas
87
        $parents = explode(',', $interfaceString);
88
        foreach ($parents as $key => $parent) {
89
            $parents[$key] = trim($parent);
90
91
            // We do not want empty stuff
92
            if (empty($parents[$key])) {
93
                unset($parents[$key]);
94
            }
95
        }
96
97
        return $parents;
98
    }
99
100
    /**
101
     * Returns a ClassDefinition from a token array.
102
     *
103
     * This method will use a set of other methods to parse a token array and retrieve any
104
     * possible information from it. This information will be entered into a ClassDefinition object.
105
     *
106
     * @param array   $tokens       The token array
107
     * @param boolean $getRecursive Do we have to load the inherited contracts as well?
108
     *
109
     * @return \AppserverIo\Doppelgaenger\Entities\Definitions\InterfaceDefinition
110
     *
111
     * @throws \AppserverIo\Doppelgaenger\Exceptions\GeneratorException
112
     */
113
    protected function getDefinitionFromTokens($tokens, $getRecursive = true)
0 ignored issues
show
Unused Code introduced by
The parameter $getRecursive is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
114
    {
115
        // First of all we need a new InterfaceDefinition to fill
116 View Code Duplication
        if (is_null($this->currentDefinition)) {
117
            $this->currentDefinition = new InterfaceDefinition();
118
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
119
        } elseif (!$this->currentDefinition instanceof InterfaceDefinition) {
120
            throw new GeneratorException(sprintf(
121
                'The structure definition %s does not seem to be a interface definition.',
122
                $this->currentDefinition->getQualifiedName()
123
            ));
124
        }
125
126
        // Save the path of the original definition for later use
127
        $this->currentDefinition->setPath($this->file);
128
129
        // Get the interfaces own namespace and the namespace which are included via use
130
        $this->currentDefinition->setNamespace($this->getNamespace());
131
        $this->currentDefinition->setUsedStructures($this->getUsedStructures());
132
133
        // For our next step we would like to get the doc comment (if any)
134
        $this->currentDefinition->setDocBlock($this->getDocBlock($tokens, self::TOKEN));
135
136
        // Get start and end line
137
        $this->currentDefinition->setStartLine($this->getStartLine($tokens));
138
        $this->currentDefinition->setEndLine($this->getEndLine($tokens));
139
140
        // Get the interface identity
141
        $this->currentDefinition->setName($this->getName($tokens));
142
143
        // So we got our docBlock, now we can parse the invariant annotations from it
144
        $annotationParser = new AnnotationParser($this->file, $this->config, $this->tokens);
145
        $this->currentDefinition->setInvariantConditions($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...
146
            $this->currentDefinition->getDocBlock(),
147
            Invariant::ANNOTATION
148
        ));
149
150
        // Lets check if there is any inheritance, or if we implement any interfaces
151
        $parentNames = $this->getParents($tokens);
152
        if (count($this->currentDefinition->getUsedStructures()) === 0) {
153
            foreach ($parentNames as $parentName) {
154
                if (strpos($parentName, '\\') !== false) {
155
                    $this->currentDefinition->getExtends()[] = $parentName;
156
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
157
                } else {
158
                    $this->currentDefinition->getExtends()[] = '\\' . $this->currentDefinition->getNamespace() . '\\' . $parentName;
159
                }
160
            }
161
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
162
        } else {
163
            foreach ($this->currentDefinition->getUsedStructures() as $alias) {
164
                foreach ($parentNames as $parentName) {
165
                    if (strpos($alias, $parentName) !== false) {
166
                        $this->currentDefinition->setExtends('\\' . $alias);
167
                    }
168
                }
169
            }
170
        }
171
172
        // Clean possible double-\
173
        $this->currentDefinition->setExtends(str_replace('\\\\', '\\', $this->currentDefinition->getExtends()));
174
175
        $this->currentDefinition->setConstants($this->getConstants($tokens));
0 ignored issues
show
Unused Code introduced by
The call to InterfaceParser::getConstants() has too many arguments starting with $tokens.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
176
177
        // Only thing still missing are the methods, so ramp up our FunctionParser
178
        $functionParser = new FunctionParser(
179
            $this->file,
180
            $this->config,
181
            $this->structureDefinitionHierarchy,
182
            $this->structureMap,
183
            $this->currentDefinition,
184
            $this->tokens
185
        );
186
187
        $this->currentDefinition->setFunctionDefinitions($functionParser->getDefinitionListFromTokens($tokens));
0 ignored issues
show
Security Bug introduced by
It seems like $functionParser->getDefi...ListFromTokens($tokens) 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...
188
189
        return $this->currentDefinition;
190
    }
191
}
192