NikitaSergeev /
structure-generate
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SchemaGenerate\StructureGenerate\Parsers; |
||
| 4 | |||
| 5 | use Faker\Generator; |
||
| 6 | |||
| 7 | class ParserStructureJson implements ParserStructure |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | private $schema; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var Generator |
||
| 16 | */ |
||
| 17 | private $faker; |
||
| 18 | |||
| 19 | public function __construct(Generator $faker) |
||
| 20 | { |
||
| 21 | $this->faker = $faker; |
||
| 22 | } |
||
| 23 | |||
| 24 | public function setSchema(array $schema): ParserStructure |
||
| 25 | { |
||
| 26 | $this->schema = $schema; |
||
| 27 | return $this; |
||
| 28 | } |
||
| 29 | |||
| 30 | public function process(array $keysIndex): array |
||
| 31 | { |
||
| 32 | $el = []; |
||
| 33 | foreach ($this->schema as $key => $value) { |
||
| 34 | if (preg_match('/{.+}/', $value)) { |
||
| 35 | if (preg_match('/{data\.(.+)}/s', $value, $matches)) { |
||
| 36 | $el[$key] = $this->faker->randomElement($keysIndex[$matches[1]]); |
||
|
0 ignored issues
–
show
|
|||
| 37 | continue; |
||
| 38 | } |
||
| 39 | |||
| 40 | $method = str_replace(['{', '}'], '', $value); |
||
| 41 | $re = '/{(.*)\((.*)\)}/s'; |
||
| 42 | $str = $value; |
||
| 43 | |||
| 44 | preg_match_all($re, $str, $matches, PREG_SET_ORDER); |
||
| 45 | |||
| 46 | if ($matches === []) { |
||
| 47 | $el[$key] = $this->faker->{$method}; |
||
| 48 | continue; |
||
| 49 | } |
||
| 50 | |||
| 51 | $method = $matches[0][1]; |
||
| 52 | $params = json_decode($matches[0][2], true); |
||
| 53 | $el[$key] = $this->faker->{$method}(...$params); |
||
| 54 | continue; |
||
| 55 | } |
||
| 56 | |||
| 57 | $el[$key] = $value; |
||
| 58 | } |
||
| 59 | |||
| 60 | return $el; |
||
| 61 | } |
||
| 62 | } |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.