Conditions | 7 |
Paths | 33 |
Total Lines | 113 |
Lines | 9 |
Ratio | 7.96 % |
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 |
||
78 | protected function getDefinitionFromTokens($tokens, $getRecursive = true) |
||
79 | { |
||
80 | // First of all we need a new ClassDefinition to fill |
||
81 | View Code Duplication | if (is_null($this->currentDefinition)) { |
|
82 | $this->currentDefinition = new ClassDefinition(); |
||
83 | |||
|
|||
84 | } elseif (!$this->currentDefinition instanceof ClassDefinition) { |
||
85 | throw new GeneratorException(sprintf( |
||
86 | 'The structure definition %s does not seem to be a class definition.', |
||
87 | $this->currentDefinition->getQualifiedName() |
||
88 | )); |
||
89 | } |
||
90 | |||
91 | // Save the path of the original definition for later use |
||
92 | $this->currentDefinition->setPath($this->file); |
||
93 | |||
94 | // File based namespaces do not make much sense, so hand it over here. |
||
95 | $this->currentDefinition->setNamespace($this->getNamespace()); |
||
96 | $this->currentDefinition->setName($this->getName($tokens)); |
||
97 | $this->currentDefinition->setUsedStructures($this->getUsedStructures()); |
||
98 | |||
99 | // For our next step we would like to get the doc comment (if any) |
||
100 | $this->currentDefinition->setDocBlock($this->getDocBlock($tokens, self::TOKEN)); |
||
101 | |||
102 | // Get start and end line |
||
103 | $this->currentDefinition->setStartLine($this->getStartLine($tokens)); |
||
104 | $this->currentDefinition->setEndLine($this->getEndLine($tokens)); |
||
105 | |||
106 | // Lets get the attributes the class might have |
||
107 | $this->currentDefinition->setAttributeDefinitions($this->getAttributes( |
||
108 | $tokens |
||
109 | )); |
||
110 | |||
111 | // So we got our docBlock, now we can parse the invariant annotations from it |
||
112 | $annotationParser = new AnnotationParser($this->file, $this->config, $this->tokens, $this->currentDefinition); |
||
113 | $invariantConditions = $annotationParser->getConditions( |
||
114 | $this->currentDefinition->getDocBlock(), |
||
115 | Invariant::ANNOTATION |
||
116 | ); |
||
117 | if (!is_bool($invariantConditions)) { |
||
118 | $this->currentDefinition->setInvariantConditions($invariantConditions); |
||
119 | } |
||
120 | |||
121 | // we would be also interested in introductions |
||
122 | $introductions = new IntroductionList(); |
||
123 | $introductionAnnotations = $annotationParser->getAnnotationsByType( |
||
124 | $this->currentDefinition->getDocBlock(), |
||
125 | Introduce::ANNOTATION |
||
126 | ); |
||
127 | foreach ($introductionAnnotations as $introductionAnnotation) { |
||
128 | $introduction = new Introduction(); |
||
129 | $introduction->setTarget($this->currentDefinition->getQualifiedName()); |
||
130 | $introduction->setImplementation($introductionAnnotation->values['implementation']); |
||
131 | $introduction->setInterface($introductionAnnotation->values['interface']); |
||
132 | |||
133 | $introductions->add($introduction); |
||
134 | } |
||
135 | |||
136 | $this->currentDefinition->setIntroductions($introductions); |
||
137 | |||
138 | // Get the class identity |
||
139 | $this->currentDefinition->setIsFinal($this->hasSignatureToken($this->tokens, T_FINAL, self::TOKEN)); |
||
140 | $this->currentDefinition->setIsAbstract($this->hasSignatureToken($this->tokens, T_ABSTRACT, self::TOKEN)); |
||
141 | |||
142 | // Lets check if there is any inheritance, or if we implement any interfaces |
||
143 | $this->currentDefinition->setExtends(trim( |
||
144 | $this->resolveUsedNamespace( |
||
145 | $this->currentDefinition, |
||
146 | $this->getParent($tokens) |
||
147 | ), |
||
148 | '\\' |
||
149 | )); |
||
150 | // Get all the interfaces we have |
||
151 | $this->currentDefinition->setImplements($this->getInterfaces($this->currentDefinition)); |
||
152 | |||
153 | // Get all class constants |
||
154 | $this->currentDefinition->setConstants($this->getConstants($tokens)); |
||
155 | |||
156 | // Only thing still missing are the methods, so ramp up our FunctionParser |
||
157 | $functionParser = new FunctionParser( |
||
158 | $this->file, |
||
159 | $this->config, |
||
160 | $this->structureDefinitionHierarchy, |
||
161 | $this->structureMap, |
||
162 | $this->currentDefinition, |
||
163 | $this->tokens |
||
164 | ); |
||
165 | |||
166 | $functionDefinitions = $functionParser->getDefinitionListFromTokens( |
||
167 | $tokens, |
||
168 | $getRecursive |
||
169 | ); |
||
170 | if ($functionDefinitions !== false) { |
||
171 | $this->currentDefinition->setFunctionDefinitions($functionDefinitions); |
||
172 | } |
||
173 | |||
174 | // If we have to parse the definition in a recursive manner, we have to get the parent invariants |
||
175 | if ($getRecursive === true) { |
||
176 | // Add all the assertions we might get from ancestral dependencies |
||
177 | $this->addAncestralAssertions($this->currentDefinition); |
||
178 | } |
||
179 | |||
180 | // Lets get the attributes the class might have |
||
181 | $this->currentDefinition->setAttributeDefinitions($this->getAttributes( |
||
182 | $tokens, |
||
183 | $this->currentDefinition->getInvariants() |
||
184 | )); |
||
185 | |||
186 | // Before exiting we will add the entry to the current structure definition hierarchy |
||
187 | $this->structureDefinitionHierarchy->insert($this->currentDefinition); |
||
188 | |||
189 | return $this->currentDefinition; |
||
190 | } |
||
191 | |||
304 |