| Conditions | 9 |
| Paths | 9 |
| Total Lines | 78 |
| Lines | 9 |
| Ratio | 11.54 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 113 | protected function getDefinitionFromTokens($tokens, $getRecursive = true) |
||
| 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 | |||
| 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( |
||
| 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 | |||
| 157 | } else { |
||
| 158 | $this->currentDefinition->getExtends()[] = '\\' . $this->currentDefinition->getNamespace() . '\\' . $parentName; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 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)); |
||
| 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)); |
||
| 188 | |||
| 189 | return $this->currentDefinition; |
||
| 190 | } |
||
| 191 | } |
||
| 192 |
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: