EvilFreelancer /
composer-json-generator
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace ComposerJson; |
||
| 4 | |||
| 5 | use ComposerJson\Schemas\Author; |
||
| 6 | use ComposerJson\Schemas\Composer; |
||
| 7 | use ComposerJson\Schemas\Repository; |
||
| 8 | use ComposerJson\Schemas\Support; |
||
| 9 | use ErrorException; |
||
| 10 | use JsonException; |
||
| 11 | use InvalidArgumentException; |
||
| 12 | |||
| 13 | class Generator |
||
| 14 | { |
||
| 15 | use Helper; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Main root object of composer |
||
| 19 | * |
||
| 20 | * @var Composer |
||
| 21 | */ |
||
| 22 | private Composer $composer; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 23 | |||
| 24 | /** |
||
| 25 | * Check if file is JSON |
||
| 26 | * |
||
| 27 | * @param string $source |
||
| 28 | * |
||
| 29 | * @return JsonException|array |
||
| 30 | * @throws JsonException |
||
| 31 | */ |
||
| 32 | private function isJson(string &$source) |
||
| 33 | { |
||
| 34 | return json_decode($source, true, 512, JSON_THROW_ON_ERROR); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Generator constructor. |
||
| 39 | * |
||
| 40 | * @param \ComposerJson\Schemas\Composer|null $composer |
||
| 41 | */ |
||
| 42 | public function __construct(Composer $composer = null) |
||
| 43 | { |
||
| 44 | if (null !== $composer) { |
||
| 45 | $this->composer = $composer; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Load composer to memory |
||
| 51 | * |
||
| 52 | * @param \ComposerJson\Schemas\Composer $composer |
||
| 53 | * |
||
| 54 | * @return $this |
||
| 55 | */ |
||
| 56 | public function load(Composer $composer): self |
||
| 57 | { |
||
| 58 | $this->composer = $composer; |
||
| 59 | return $this; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Return array of values |
||
| 64 | * |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | public function toArray(): array |
||
| 68 | { |
||
| 69 | return $this->composer->toArray(); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Return JSON with composer |
||
| 74 | * |
||
| 75 | * @return string |
||
| 76 | * @throws JsonException |
||
| 77 | */ |
||
| 78 | public function toJson(): string |
||
| 79 | { |
||
| 80 | return json_encode($this->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Compolomus believe what is a simplification of code... |
||
| 85 | * |
||
| 86 | * @param string $rule |
||
| 87 | * @param mixed $value |
||
| 88 | */ |
||
| 89 | private function parseFields(string $rule, $value): void |
||
| 90 | { |
||
| 91 | $rule = $rule !== 'autoload-dev' ? $rule : 'autoload'; |
||
| 92 | $classRule = $this->denormalize($rule); |
||
| 93 | |||
| 94 | if (!empty($classRule)) { |
||
| 95 | if (is_array($this->composer->$classRule)) { |
||
| 96 | $method = 'parse' . $this->normalizeMethodName($rule); |
||
| 97 | if (method_exists($this, $method)) { |
||
| 98 | $this->composer->$classRule = $this->$method($value); |
||
| 99 | } else { |
||
| 100 | $this->composer->$classRule = $value; |
||
| 101 | } |
||
| 102 | } else { |
||
| 103 | $this->composer->$classRule = $value; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Read JSON conde and return Composer object |
||
| 111 | * |
||
| 112 | * @param string $file |
||
| 113 | * |
||
| 114 | * @return Composer |
||
| 115 | * @throws ErrorException |
||
| 116 | * @throws JsonException |
||
| 117 | */ |
||
| 118 | public function read(string $file): Composer |
||
| 119 | { |
||
| 120 | // Read file from string |
||
| 121 | if (!$source = file_get_contents($file)) { |
||
| 122 | throw new ErrorException('Provided file could not to be read'); |
||
| 123 | } |
||
| 124 | |||
| 125 | // Convert JSON to array or throw JsonException |
||
| 126 | $array = $this->isJson($source); |
||
| 127 | |||
| 128 | // Initiate composer object |
||
| 129 | $this->composer = new Composer(); |
||
| 130 | |||
| 131 | // Parse values |
||
| 132 | foreach ($array as $key => $value) { |
||
| 133 | $this->parseFields($key, $value); |
||
| 134 | } |
||
| 135 | |||
| 136 | return $this->composer; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Parse autoload parts of composer.json |
||
| 141 | * |
||
| 142 | * @param array $autoload |
||
| 143 | * |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | private function parseAutoload(array $autoload): array |
||
| 147 | { |
||
| 148 | $objects = []; |
||
| 149 | |||
| 150 | $methodsArray = [ |
||
| 151 | 'psr-0', |
||
| 152 | 'psr-4', |
||
| 153 | 'classmap', |
||
| 154 | 'files' |
||
| 155 | ]; |
||
| 156 | |||
| 157 | foreach ($autoload as $type => $options) { |
||
| 158 | if (in_array($type, $methodsArray, true)) { |
||
| 159 | $class = 'ComposerJson\\Schemas\\' . $this->normalizeMethodName($type); |
||
| 160 | $object = new $class; |
||
| 161 | |||
| 162 | // Autoload object options |
||
| 163 | $object->options = $options; |
||
| 164 | |||
| 165 | // Save object |
||
| 166 | $objects[$type] = $object; |
||
| 167 | } else { |
||
| 168 | throw new InvalidArgumentException('Incorrect type of autoloader provided'); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | return $objects; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Convert array of repositories to array of repository objects |
||
| 177 | * |
||
| 178 | * @param array $repositories |
||
| 179 | * |
||
| 180 | * @return \ComposerJson\Schemas\Repository[] |
||
| 181 | */ |
||
| 182 | private function parseRepositories(array $repositories): array |
||
| 183 | { |
||
| 184 | $objects = []; |
||
| 185 | foreach ($repositories as $repository) { |
||
| 186 | $object = new Repository(); |
||
| 187 | |||
| 188 | foreach ($repository as $key => $value) { |
||
| 189 | |||
| 190 | if ($key === 'packagist.org') { |
||
| 191 | $key = 'packagistOrg'; |
||
| 192 | } |
||
| 193 | |||
| 194 | $object->$key = $value; |
||
| 195 | } |
||
| 196 | |||
| 197 | $objects[] = $object; |
||
| 198 | } |
||
| 199 | |||
| 200 | return $objects; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Convert support array to object of schema |
||
| 205 | * |
||
| 206 | * @param array $support |
||
| 207 | * |
||
| 208 | * @return \ComposerJson\Schemas\Support |
||
| 209 | */ |
||
| 210 | private function parseSupport(array $support): Support |
||
| 211 | { |
||
| 212 | $object = new Support(); |
||
| 213 | |||
| 214 | foreach ($support as $key => $value) { |
||
| 215 | $object->$key = $value; |
||
| 216 | } |
||
| 217 | |||
| 218 | return $object; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Convert authors array to array of objects |
||
| 223 | * |
||
| 224 | * @param array $authors |
||
| 225 | * |
||
| 226 | * @return \ComposerJson\Schemas\Author[] |
||
| 227 | */ |
||
| 228 | private function parseAuthors(array $authors): array |
||
| 229 | { |
||
| 230 | $objects = []; |
||
| 231 | foreach ($authors as $author) { |
||
| 232 | $object = new Author(); |
||
| 233 | |||
| 234 | foreach ($author as $key => $value) { |
||
| 235 | $object->$key = $value; |
||
| 236 | } |
||
| 237 | |||
| 238 | $objects[] = $object; |
||
| 239 | } |
||
| 240 | |||
| 241 | return $objects; |
||
| 242 | } |
||
| 243 | } |
||
| 244 |