Conditions | 11 |
Paths | 2 |
Total Lines | 60 |
Code Lines | 40 |
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 declare(strict_types=1); |
||
29 | public function __construct(string $rootPath, string $cachePath = null, bool $reclone) |
||
30 | { |
||
31 | if (!\file_exists($configFile = ($this->config['path'] = $rootPath).'/.monorepo')) { |
||
32 | throw new \RuntimeException(\sprintf('Config file "%s" does not exist.', $configFile)); |
||
33 | } |
||
34 | |||
35 | $workers = []; |
||
36 | $this->config['reclone'] = $reclone; |
||
37 | $options = new OptionsResolver(); |
||
38 | $options |
||
39 | ->define('base_url')->default(null)->allowedTypes('string', 'null') |
||
40 | ->define('branch_filter')->default(null)->allowedTypes('string', 'null') |
||
41 | ->define('extra')->default([])->allowedTypes('array') |
||
42 | ->define('workers')->default([])->allowedTypes('array') |
||
43 | ->normalize(function (Options $options, array $value): array { |
||
44 | if (\array_is_list($value)) { |
||
|
|||
45 | return ['main' => $value]; |
||
46 | } |
||
47 | |||
48 | foreach ($value as $k => $v) { |
||
49 | $v = \is_string($v) ? [$v] : $v; |
||
50 | |||
51 | if (!\is_string($k)) { |
||
52 | throw new InvalidOptionsException('Expected workers config to begin with a string key.'); |
||
53 | } |
||
54 | |||
55 | if (!\is_array($v) || !\array_is_list($v)) { |
||
56 | throw new InvalidOptionsException('Expected workers config\'s key value be a list of workers classes.'); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | return $value; |
||
61 | }) |
||
62 | ->define('repositories') |
||
63 | ->default(function (OptionsResolver $options, Options $parent) use (&$workers): void { |
||
64 | $workers = \array_keys($parent['workers']); |
||
65 | $options->setPrototype(true) |
||
66 | ->define('url')->default(null)->allowedTypes('string', 'null')->required() |
||
67 | ->define('merge')->default(false)->allowedTypes('bool') |
||
68 | ->define('path') |
||
69 | ->allowedTypes('string') |
||
70 | ->default(\Closure::bind(fn (Options $options): string => $options->prototypeIndex, null, $options)) |
||
71 | ->normalize(fn (Options $options, string $value): string => \trim($value, '/')) |
||
72 | ->define('workers')->default($workers)->allowedTypes('array') |
||
73 | ->normalize(function (Options $options, array $value) use ($workers): array { |
||
74 | foreach ($value as $worker) { |
||
75 | if (!\in_array($worker, $workers, true)) { |
||
76 | throw new InvalidOptionsException(\sprintf('The worker "%s" for monorepo\'s path "%s" does not exist.', $worker, $options['path'])); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | return $value; |
||
81 | }) |
||
82 | ; |
||
83 | })->allowedTypes('array') |
||
84 | ; |
||
85 | |||
86 | $options->setRequired(['base_url', 'branch_filter', 'workers', 'repositories']); |
||
87 | $this->config += $options->resolve(\function_exists('yaml_parse_file') ? yaml_parse_file($configFile) : Yaml::parseFile($configFile)); |
||
88 | $this->config['cache'] = $cachePath ?? $this->config['path'].'/.monorepo-cache'; |
||
89 | } |
||
143 |