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