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