Conditions | 21 |
Paths | 12 |
Total Lines | 87 |
Lines | 0 |
Ratio | 0 % |
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 |
||
54 | public function getAttributes(array $tokens, TypedListList $invariants = null) |
||
55 | { |
||
56 | // Check the tokens |
||
57 | $attributes = new AttributeDefinitionList(); |
||
58 | for ($i = 0; $i < count($tokens); $i++) { |
||
|
|||
59 | // If we got a variable we will check if there is any function definition above it. |
||
60 | // If not, we got an attribute, if so we will check if there is an even number of closing and opening |
||
61 | // brackets above it, which would mean we are not in the function. |
||
62 | if (is_array($tokens[$i]) && $tokens[$i][0] === T_VARIABLE) { |
||
63 | for ($j = $i - 1; $j >= 0; $j--) { |
||
64 | if (is_array($tokens[$j]) && $tokens[$j][0] === T_FUNCTION) { |
||
65 | // Initialize our counter and also the check if we even started counting |
||
66 | $bracketCounter = 0; |
||
67 | $usedCounter = false; |
||
68 | |||
69 | // We got something, lets count the brackets between it and our variable's position |
||
70 | for ($k = $j + 1; $k < $i; $k++) { |
||
71 | if ($tokens[$k] === '{' || $tokens[$k][0] === T_CURLY_OPEN) { |
||
72 | $usedCounter = true; |
||
73 | $bracketCounter++; |
||
74 | |||
75 | } elseif ($tokens[$k] === '}') { |
||
76 | $usedCounter = true; |
||
77 | $bracketCounter--; |
||
78 | } |
||
79 | } |
||
80 | |||
81 | // If we got an even number of brackets (the counter is 0 and got used), we got an attribute |
||
82 | if ($bracketCounter === 0 && $usedCounter === true) { |
||
83 | $attributes->set($tokens[$i][1], $this->getAttributeProperties($tokens, $i)); |
||
84 | } |
||
85 | |||
86 | break; |
||
87 | |||
88 | } elseif (is_array($tokens[$j]) && $tokens[$j][0] === $this->getToken()) { |
||
89 | // If we reach the class definition without passing a function we definitely got an attribute |
||
90 | $attributes->set($tokens[$i][1], $this->getAttributeProperties($tokens, $i)); |
||
91 | break; |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | |||
97 | // If we got invariants we will check if our attributes are used in invariants |
||
98 | if ($invariants !== null) { |
||
99 | // Lets iterate over all the attributes and check them against the invariants we got |
||
100 | $listIterator = $invariants->getIterator(); |
||
101 | $listCount = $listIterator->count(); |
||
102 | $attributeIterator = $attributes->getIterator(); |
||
103 | $attributeCount = $attributeIterator->count(); |
||
104 | for ($i = 0; $i < $attributeCount; $i++) { |
||
105 | // Do we have any of these attributes in our invariants? |
||
106 | $listIterator = $invariants->getIterator(); |
||
107 | for ($j = 0; $j < $listCount; $j++) { |
||
108 | // Did we get anything useful? |
||
109 | if ($listIterator->current() === null) { |
||
110 | continue; |
||
111 | } |
||
112 | |||
113 | /** @var \AppserverIo\Doppelgaenger\Interfaces\TypedListInterface|\Iterator $invariantIterator */ |
||
114 | $invariantIterator = $listIterator->current()->getIterator(); |
||
115 | $invariantCount = $invariantIterator->count(); |
||
116 | for ($k = 0; $k < $invariantCount; $k++) { |
||
117 | $attributePosition = strpos( |
||
118 | $invariantIterator->current()->getString(), |
||
119 | '$this->' . ltrim( |
||
120 | $attributeIterator->current()->getName(), |
||
121 | '$' |
||
122 | ) |
||
123 | ); |
||
124 | |||
125 | if ($attributePosition !== false |
||
126 | ) { |
||
127 | // Tell them we were mentioned and persist it |
||
128 | $attributeIterator->current()->setInInvariant(true); |
||
129 | } |
||
130 | |||
131 | $invariantIterator->next(); |
||
132 | } |
||
133 | $listIterator->next(); |
||
134 | } |
||
135 | $attributeIterator->next(); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | return $attributes; |
||
140 | } |
||
141 | |||
219 |
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: