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