| Conditions | 45 | 
| Paths | > 20000 | 
| Total Lines | 196 | 
| Code Lines | 122 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 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 | ||
| 38 | public function parse(): array | ||
| 39 |     { | ||
| 40 | $lines = explode(PHP_EOL, $this->rawSchema); | ||
| 41 | |||
| 42 | $description = ''; | ||
| 43 | $currentClassName = ''; | ||
| 44 | $currentClass = new ClassDefinition(); | ||
| 45 | $isFunction = false; | ||
| 46 | $needClassDescription = false; | ||
| 47 | $this->currentLineNr = 0; | ||
| 48 | |||
| 49 |         foreach ($lines as $line) { | ||
| 50 | $this->currentLine = $line; | ||
| 51 | $this->currentLineNr++; | ||
| 52 | |||
| 53 |             if ('---types---' === $line) { | ||
| 54 | $isFunction = false; | ||
| 55 |             } elseif ('---functions---' === $line) { | ||
| 56 | $isFunction = true; | ||
| 57 | $currentClassName = ''; | ||
| 58 | $currentClass = new ClassDefinition(); | ||
| 59 | $needClassDescription = false; | ||
| 60 |             } elseif (($line[0] ?? '') === '/') { | ||
| 61 |                 if (($line[1] ?? '') !== '/') { | ||
| 62 |                     $this->printError('Wrong comment'); | ||
| 63 | |||
| 64 | continue; | ||
| 65 | } | ||
| 66 | |||
| 67 |                 if (($line[2] ?? '') === '@' || ($line[2] ?? '') === '-') { | ||
| 68 |                     $description .= trim(substr($line, 2 + intval('-' === $line[2]))) . ' '; | ||
| 69 |                 } else { | ||
| 70 |                     $this->printError('Unexpected comment'); | ||
| 71 | } | ||
| 72 | } elseif (strpos($line, '? =') || strpos($line, ' = Vector t;') || 'boolFalse = Bool;' === $line || | ||
| 73 | 'boolTrue = Bool;' === $line || 'bytes = Bytes;' === $line || 'int32 = Int32;' === $line || | ||
| 74 |                 'int53 = Int53;' === $line || 'int64 = Int64;' === $line) { | ||
| 75 |                 $this->printDebug('skip built-in types'); | ||
| 76 | |||
| 77 | continue; | ||
| 78 |             } else { | ||
| 79 | $description = trim($description); | ||
| 80 | |||
| 81 |                 if ('' === $description) { | ||
| 82 | //                    $this->printError('Empty description', ['description' => $description]); | ||
| 83 | } | ||
| 84 | |||
| 85 |                 if (($description[0] ?? '') !== '@') { | ||
| 86 | //                    $this->printError('Wrong description begin', ['description' => $description]); | ||
| 87 | } | ||
| 88 | |||
| 89 |                 $docs = explode('@', $description); | ||
| 90 | array_shift($docs); | ||
| 91 | |||
| 92 | $info = []; | ||
| 93 |                 foreach ($docs as $doc) { | ||
| 94 |                     [$key, $value] = explode(' ', $doc, 2); | ||
| 95 | $value = trim($value); | ||
| 96 | |||
| 97 |                     if ($needClassDescription) { | ||
| 98 |                         if ('description' === $key) { | ||
| 99 | $needClassDescription = false; | ||
| 100 | |||
| 101 | $currentClass->classDocs = $value; | ||
| 102 | $currentClass->parentClass = 'Object'; | ||
| 103 | $currentClass->typeName = $currentClass->className; | ||
| 104 | |||
| 105 | $this->classes[$value] = $currentClass; | ||
| 106 | $currentClass = new ClassDefinition(); | ||
| 107 | continue; | ||
| 108 |                         } else { | ||
| 109 |                             $this->printError('Expected abstract class description', ['description' => $description]); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 |                     if ('class' === $key) { | ||
| 114 | $currentClassName = $this->getClassName($value); | ||
| 115 | $currentClass->className = $currentClassName; | ||
| 116 | |||
| 117 | $needClassDescription = true; | ||
| 118 | |||
| 119 |                         if ($isFunction) { | ||
| 120 |                             $this->printError('Unexpected class definition'); | ||
| 121 | } | ||
| 122 |                     } else { | ||
| 123 |                         if (isset($info[$key])) { | ||
| 124 | //                            $this->printError("Duplicate info about `$key`"); | ||
| 125 | } | ||
| 126 | $info[$key] = trim($value); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 |                 if (1 !== substr_count($line, '=')) { | ||
| 131 | //                    $this->printError("Wrong '=' count"); | ||
| 132 | continue; | ||
| 133 | } | ||
| 134 | |||
| 135 |                 [$fields, $type] = explode('=', $line); | ||
| 136 | $type = $this->getClassName($type); | ||
| 137 |                 $fields          = explode(' ', trim($fields)); | ||
| 138 | $typeName = array_shift($fields); | ||
| 139 | $className = $this->getClassName($typeName); | ||
| 140 | |||
| 141 |                 if ($type !== $currentClassName) { | ||
| 142 | $currentClassName = ''; | ||
| 143 | $currentClass = new ClassDefinition(); | ||
| 144 | $needClassDescription = false; | ||
| 145 | } | ||
| 146 | |||
| 147 |                 if (!$isFunction) { | ||
| 148 | $typeLower = strtolower($type); | ||
| 149 | $classNameLower = strtolower($className); | ||
| 150 | |||
| 151 |                     if (empty($currentClassName) === ($typeLower !== $classNameLower)) { | ||
| 152 |                         $this->printError('Wrong constructor name'); | ||
| 153 | } | ||
| 154 | |||
| 155 |                     if (0 !== strpos($classNameLower, $typeLower)) { | ||
| 156 |                         // $this->printError('Wrong constructor name'); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | $knownFields = []; | ||
| 161 |                 foreach ($fields as $field) { | ||
| 162 |                     [$fieldName, $fieldType] = explode(':', $field); | ||
| 163 | |||
| 164 |                     if (isset($info['param_' . $fieldName])) { | ||
| 165 | $knownFields['param_' . $fieldName] = $fieldType; | ||
| 166 | |||
| 167 | continue; | ||
| 168 | } | ||
| 169 | |||
| 170 |                     if (isset($info[$fieldName])) { | ||
| 171 | $knownFields[$fieldName] = $fieldType; | ||
| 172 | |||
| 173 | continue; | ||
| 174 | } | ||
| 175 | |||
| 176 |                     $this->printError("Have no info about field `$fieldName`"); | ||
| 177 | } | ||
| 178 | |||
| 179 |                 foreach ($info as $name => $value) { | ||
| 180 |                     if (!$value) { | ||
| 181 |                         $this->printError("info[$name] for $className is empty"); | ||
| 182 |                     } elseif (($value[0] < 'A' || $value[0] > 'Z') && ($value[0] < '0' || $value[0] > '9')) { | ||
| 183 |                         $this->printError("info[$name] for $className doesn't begins with capital letter"); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 |                 foreach (array_diff_key($info, $knownFields) as $fieldName => $fieldInfo) { | ||
| 188 |                     if ('description' !== $fieldName) { | ||
| 189 |                         $this->printError("Have info about unexisted field `$fieldName`"); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 |                 if (!isset($info['description'])) { | ||
| 194 |                     $this->printError("Have no description for class `$className`"); | ||
| 195 | } | ||
| 196 | |||
| 197 | $baseClassName = $currentClassName ?: $this->getBaseClassName($isFunction); | ||
| 198 | $classDescription = $info['description']; | ||
| 199 | |||
| 200 |                 if ($isFunction) { | ||
| 201 | $currentClass->returnType = $this->getTypeName($type); | ||
| 202 | } | ||
| 203 | |||
| 204 | $currentClass->className = $className; | ||
| 205 | $currentClass->parentClass = $baseClassName; | ||
| 206 | $currentClass->classDocs = $classDescription; | ||
| 207 | $currentClass->typeName = $typeName; | ||
| 208 | |||
| 209 |                 foreach ($knownFields as $name => $fieldType) { | ||
| 210 | $mayBeNull = false !== stripos($info[$name], 'may be null'); | ||
| 211 | $fieldName = $this->getFieldName($name, $className); | ||
| 212 | $fieldTypeName = $this->getTypeName($fieldType); | ||
| 213 | |||
| 214 | $rawName = $name; | ||
| 215 |                     if ('param_' === substr($rawName, 0, 6)) { | ||
| 216 | $rawName = substr($rawName, 6); | ||
| 217 | } | ||
| 218 | |||
| 219 | $field = $currentClass->getField($name); | ||
| 220 | $field->rawName = $rawName; | ||
| 221 | $field->name = $fieldName; | ||
| 222 | $field->type = $fieldTypeName; | ||
| 223 | $field->doc = $info[$name]; | ||
| 224 | $field->mayBeNull = $mayBeNull; | ||
| 225 | } | ||
| 226 | |||
| 227 | $this->classes[$typeName] = $currentClass; | ||
| 228 | $currentClass = new ClassDefinition(); | ||
| 229 | $description = ''; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | return $this->classes; | ||
| 234 | } | ||
| 326 |