Conditions | 18 |
Paths | 17 |
Total Lines | 142 |
Code Lines | 84 |
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 |
||
45 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
46 | { |
||
47 | $quiet = (bool) $input->getOption(name: 'quiet'); |
||
48 | $list = (bool) $input->getOption(name: 'list'); |
||
49 | $delete = (bool) $input->getOption(name: 'delete'); |
||
50 | |||
51 | /** @var string $tree_name */ |
||
52 | $tree_name = $input->getArgument(name: 'tree-name'); |
||
53 | |||
54 | /** @var string|null $setting_name */ |
||
55 | $setting_name = $input->getArgument(name: 'setting-name'); |
||
56 | |||
57 | /** @var string|null $setting_value */ |
||
58 | $setting_value = $input->getArgument(name: 'setting-value'); |
||
59 | |||
60 | $io = new SymfonyStyle(input: $input, output: $output); |
||
61 | |||
62 | $tree_id = DB::table('gedcom') |
||
63 | ->where(column: 'gedcom_name', operator: '=', value: $tree_name) |
||
64 | ->value(column: 'gedcom_id'); |
||
65 | |||
66 | if ($tree_id === null) { |
||
67 | $io->error(message: 'Tree ‘' . $tree_name . '’ not found.'); |
||
68 | |||
69 | return Command::FAILURE; |
||
70 | } |
||
71 | |||
72 | if ($list) { |
||
73 | if ($delete) { |
||
74 | $io->error(message: 'Cannot specify --list and --delete together.'); |
||
75 | |||
76 | return Command::FAILURE; |
||
77 | } |
||
78 | |||
79 | if ($setting_value !== null) { |
||
80 | $io->error(message: 'Cannot specify --list and a new value together.'); |
||
81 | |||
82 | return Command::FAILURE; |
||
83 | } |
||
84 | |||
85 | $table = new Table(output: $output); |
||
86 | $table->setHeaders(headers: ['Setting name', 'Setting value']); |
||
87 | |||
88 | /** @var array<object{setting_name:string,setting_value:string}> $settings */ |
||
89 | $settings = DB::table(table: 'gedcom_setting') |
||
90 | ->where(column: 'gedcom_id', operator: '=', value: $tree_id) |
||
91 | ->orderBy(column: 'setting_name') |
||
92 | ->select(columns: ['setting_name', 'setting_value']) |
||
93 | ->get() |
||
94 | ->all(); |
||
95 | |||
96 | foreach ($settings as $setting) { |
||
97 | if ($setting_name === null || str_contains(haystack: $setting->setting_name, needle: $setting_name)) { |
||
98 | $table->addRow(row: [$setting->setting_name, $setting->setting_value]); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | $table->render(); |
||
103 | |||
104 | return Command::SUCCESS; |
||
105 | } |
||
106 | |||
107 | /** @var string|null $old_setting_value */ |
||
108 | $old_setting_value = DB::table('gedcom_setting') |
||
109 | ->where(column: 'gedcom_id', operator: '=', value: $tree_id) |
||
110 | ->where(column: 'setting_name', operator: '=', value: $setting_name) |
||
111 | ->value(column: 'setting_value'); |
||
112 | |||
113 | if ($delete) { |
||
114 | if ($setting_name === null) { |
||
115 | $io->error(message: 'Setting name must be specified for --delete.'); |
||
116 | |||
117 | return Command::FAILURE; |
||
118 | } |
||
119 | |||
120 | if ($setting_value !== null) { |
||
121 | $io->error(message: 'Cannot specify --delete and a new value together.'); |
||
122 | |||
123 | return Command::FAILURE; |
||
124 | } |
||
125 | |||
126 | if ($old_setting_value === null) { |
||
127 | $io->warning(message: 'Tree setting ‘' . $setting_name . '’ not found. Nothing to delete.'); |
||
128 | } else { |
||
129 | DB::table('gedcom_setting') |
||
130 | ->where(column: 'gedcom_id', operator: '=', value: $tree_id) |
||
131 | ->where('setting_name', '=', $setting_name) |
||
132 | ->delete(); |
||
133 | |||
134 | $io->success(message: 'Tree setting ‘' . $setting_name . '’ deleted. Previous value was ‘' . $old_setting_value . '’.'); |
||
135 | } |
||
136 | |||
137 | return Command::SUCCESS; |
||
138 | } |
||
139 | |||
140 | |||
141 | if ($setting_name === null) { |
||
142 | $io->error(message: 'A setting name is required, unless the --list option is used.'); |
||
143 | |||
144 | return Command::FAILURE; |
||
145 | } |
||
146 | |||
147 | if ($setting_value === null) { |
||
148 | if ($old_setting_value === null) { |
||
149 | $io->info(message: 'Tree setting ‘' . $setting_name . '’ is not currently set.'); |
||
150 | } elseif ($quiet) { |
||
151 | $verbosity = $io->getVerbosity(); |
||
152 | $io->setVerbosity(level: OutputInterface::VERBOSITY_NORMAL); |
||
153 | $io->writeln(messages: $old_setting_value); |
||
154 | $io->setVerbosity(level: $verbosity); |
||
155 | } else { |
||
156 | $io->info(message: 'Tree setting ‘' . $setting_name . '’ is currently set to ‘' . $old_setting_value . '’.'); |
||
157 | } |
||
158 | |||
159 | return Command::SUCCESS; |
||
160 | } |
||
161 | |||
162 | if ($old_setting_value === $setting_value) { |
||
163 | $io->warning(message: 'Tree setting ' . $setting_name . ' is already set to ' . $setting_value); |
||
164 | |||
165 | return Command::SUCCESS; |
||
166 | } |
||
167 | |||
168 | if ($old_setting_value === null) { |
||
169 | DB::table(table: 'gedcom_setting') |
||
170 | ->insert(values: [ |
||
171 | 'gedcom_id' => $tree_id, |
||
172 | 'setting_name' => $setting_name, |
||
173 | 'setting_value' => $setting_value, |
||
174 | ]); |
||
175 | |||
176 | $io->success(message: 'gedcom setting ‘' . $setting_name . '’ was created as ‘' . $setting_value . '’.'); |
||
177 | } else { |
||
178 | DB::table(table: 'gedcom_setting') |
||
179 | ->where(column: 'gedcom_id', operator: '=', value: $tree_id) |
||
180 | ->where(column: 'setting_name', operator: '=', value: $setting_name) |
||
181 | ->update(values: ['setting_value' => $setting_value]); |
||
182 | |||
183 | $io->success(message: 'Tree setting ‘' . $setting_name . '’ was changed from ‘' . $old_setting_value . '’ to ‘' . $setting_value . '’.'); |
||
184 | } |
||
185 | |||
186 | return Command::SUCCESS; |
||
187 | } |
||
189 |