| Conditions | 20 |
| Paths | 164 |
| Total Lines | 106 |
| Code Lines | 71 |
| 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 |
||
| 84 | public function performAction() |
||
| 85 | { |
||
| 86 | $schema = new Schema(); |
||
| 87 | |||
| 88 | if ($this->schema) { |
||
| 89 | $schemaDataOrig = $this->readData($this->schema); |
||
| 90 | $schemaData = $schemaDataOrig; |
||
| 91 | |||
| 92 | $resolver = new ResolverMux(); |
||
| 93 | |||
| 94 | if (!empty($this->ptrInSchema)) { |
||
| 95 | $baseName = basename($this->schema); |
||
| 96 | $preloaded = new Preloaded(); |
||
| 97 | $preloaded->setSchemaData($baseName, $schemaData); |
||
| 98 | $resolver->resolvers[] = $preloaded; |
||
| 99 | $schemaData = (object)[Schema::PROP_REF => $baseName . $this->ptrInSchema]; |
||
| 100 | } |
||
| 101 | |||
| 102 | $resolver->resolvers[] = new BasicFetcher(); |
||
| 103 | |||
| 104 | try { |
||
| 105 | $schemaContract = Schema::import($schemaData, new Context($resolver)); |
||
| 106 | if ($schemaContract instanceof Schema) { |
||
| 107 | $schema = $schemaContract; |
||
| 108 | } |
||
| 109 | } catch (InvalidValue $e) { |
||
| 110 | $this->response->error('Invalid schema'); |
||
| 111 | $this->response->addContent($e->getMessage()); |
||
| 112 | throw new ExitCode('', 1); |
||
| 113 | } catch (\Exception $e) { |
||
| 114 | $this->response->error('Failed to import schema:' . $e->getMessage()); |
||
| 115 | throw new ExitCode('', 1); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | $maker = new SchemaMaker($schema); |
||
| 120 | $maker->options->useXNullable = $this->useXNullable; |
||
| 121 | $maker->options->useNullable = $this->useNullable; |
||
| 122 | $maker->options->defsPtr = $this->defsPtr; |
||
| 123 | $maker->options->collectExamples = $this->collectExamples; |
||
| 124 | $maker->options->heuristicRequired = $this->heuristicRequired; |
||
| 125 | |||
| 126 | if ($this->jsonl) { |
||
| 127 | $pathInData = []; |
||
| 128 | if ($this->ptrInData) { |
||
| 129 | $pathInData = JsonPointer::splitPath($this->ptrInData); |
||
| 130 | } |
||
| 131 | |||
| 132 | $handle = fopen($this->data, "r"); |
||
| 133 | if ($handle) { |
||
| 134 | while (($buffer = fgets($handle)) !== false) { |
||
| 135 | $item = json_decode($buffer); |
||
| 136 | // Tolerate and skip malformed JSON line. |
||
| 137 | if (null === $item) { |
||
| 138 | continue; |
||
| 139 | } |
||
| 140 | if ($this->ptrInData) { |
||
| 141 | $item = JsonPointer::get($item, $pathInData); |
||
| 142 | } |
||
| 143 | $maker->addInstanceValue($item); |
||
| 144 | } |
||
| 145 | if (!feof($handle)) { |
||
| 146 | echo "Error: unexpected fgets() fail\n"; |
||
| 147 | } |
||
| 148 | fclose($handle); |
||
| 149 | } |
||
| 150 | } else { |
||
| 151 | $data = $this->readData($this->data); |
||
| 152 | $maker->addInstanceValue($data); |
||
| 153 | |||
| 154 | if (!empty($this->additionalData)) { |
||
| 155 | foreach ($this->additionalData as $path) { |
||
| 156 | $data = $this->readData($path); |
||
| 157 | $maker->addInstanceValue($data); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | |||
| 163 | $s = Schema::export($schema); |
||
| 164 | $this->out = $s; |
||
| 165 | |||
| 166 | if ($this->ptrInSchema && isset($schemaDataOrig)) { |
||
| 167 | $tmp = json_encode($schemaDataOrig); |
||
| 168 | if ($tmp === false) { |
||
| 169 | throw new ExitCode('Failed to encode JSON', 1); |
||
| 170 | } |
||
| 171 | $schemaDataResult = json_decode($tmp); |
||
| 172 | |||
| 173 | $defs = JsonPointer::get($s, JsonPointer::splitPath(rtrim($this->defsPtr, '/'))); |
||
| 174 | foreach ($defs as $name => $def) { |
||
| 175 | JsonPointer::add($schemaDataResult, JsonPointer::splitPath($this->defsPtr . $name), $def); |
||
| 176 | } |
||
| 177 | JsonPointer::remove($s, JsonPointer::splitPath(rtrim($this->defsPtr, '/'))); |
||
| 178 | JsonPointer::add($schemaDataResult, JsonPointer::splitPath($this->ptrInSchema), $s); |
||
| 179 | |||
| 180 | $tmp = json_encode($schemaDataResult); |
||
| 181 | if ($tmp === false) { |
||
| 182 | throw new ExitCode('Failed to encode JSON', 1); |
||
| 183 | } |
||
| 184 | $schemaDataResult = json_decode($tmp); |
||
| 185 | $diff = new JsonDiff($schemaDataOrig, $schemaDataResult, JsonDiff::REARRANGE_ARRAYS); |
||
| 186 | $this->out = $diff->getRearranged(); |
||
| 187 | } |
||
| 188 | |||
| 189 | $this->postPerform(); |
||
| 190 | } |
||
| 191 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..