Skip to content

Commit 214d96b

Browse files
authored
Merge pull request #87 from nextcloud/backport/77/stable26
[stable26] code cleanup
2 parents 54ea169 + 94a63f1 commit 214d96b

22 files changed

+197
-258
lines changed

lib/Command/Add.php

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use OC\Core\Command\Base;
2929
use OCA\LDAPContactsBackend\Service\Configuration;
3030
use OCA\LDAPContactsBackend\Service\ConnectionImporter;
31+
use RuntimeException;
3132
use Symfony\Component\Console\Helper\QuestionHelper;
3233
use Symfony\Component\Console\Input\InputArgument;
3334
use Symfony\Component\Console\Input\InputInterface;
@@ -37,10 +38,8 @@
3738
use Symfony\Component\Console\Question\Question;
3839

3940
class Add extends Base {
40-
/** @var Configuration */
41-
private $configurationService;
42-
/** @var ConnectionImporter|null */
43-
private $connectionImporter;
41+
private Configuration $configurationService;
42+
private ?ConnectionImporter $connectionImporter;
4443

4544
public function __construct(Configuration $configurationService, ?ConnectionImporter $connectionImporter = null) {
4645
parent::__construct();
@@ -147,7 +146,7 @@ protected function interact(InputInterface $input, OutputInterface $output) {
147146
$helper = $this->getHelper('question');
148147

149148
$q = new Question('Address book display name: ');
150-
$q->setNormalizer(function ($input) {
149+
$q->setNormalizer(function (string $input) {
151150
return $this->stringNormalizer($input);
152151
});
153152

@@ -173,16 +172,11 @@ protected function interact(InputInterface $input, OutputInterface $output) {
173172
$q = new Question('Transport encryption: ');
174173
$q->setAutocompleterValues(['StartTLS', 'LDAPS', 'none']);
175174

176-
switch ($helper->ask($input, $output, $q)) {
177-
case 'StartTLS':
178-
$v = 'tls';
179-
break;
180-
case 'LDAPS':
181-
$v = 'ssl';
182-
break;
183-
default:
184-
$v = 'none';
185-
}
175+
$v = match ($helper->ask($input, $output, $q)) {
176+
'StartTLS' => 'tls',
177+
'LDAPS' => 'ssl',
178+
default => 'none',
179+
};
186180

187181
$input->setOption('trans_enc', $v);
188182
}
@@ -306,18 +300,6 @@ protected function importConnection(InputInterface $input) {
306300
$wasRun = true;
307301
}
308302

309-
protected function askArrayOfString(string $subject, string $label, InputInterface $input, OutputInterface $output): void {
310-
/** @var QuestionHelper $helper */
311-
$helper = $this->getHelper('question');
312-
313-
$q = new Question($label);
314-
$q->setNormalizer(function ($input) {
315-
return $this->arrayOfStringNormalizer($input);
316-
});
317-
318-
$input->setOption($subject, $helper->ask($input, $output, $q));
319-
}
320-
321303
protected function askImport(InputInterface $input, OutputInterface $output): void {
322304
$availableConnections = $this->connectionImporter ? $this->connectionImporter->getAvailableConnections() : [];
323305
if (count($availableConnections) === 0) {
@@ -393,15 +375,8 @@ protected function askUnsignedInt(string $subject, string $label, InputInterface
393375
$input->setOption($subject, $helper->ask($input, $output, $q));
394376
}
395377

396-
protected function stringNormalizer($input) {
397-
return $input ? trim($input) : '';
398-
}
399-
400-
protected function arrayOfStringNormalizer(string $input) {
401-
foreach ($input as &$item) {
402-
$item = $this->stringNormalizer($item);
403-
}
404-
return $input;
378+
protected function stringNormalizer(?string $input): string {
379+
return ($input !== null) ? trim($input) : '';
405380
}
406381

407382
protected function askStrings(string $subject, string $label, string $followUpLabel, InputInterface $input, OutputInterface $output): void {
@@ -435,7 +410,7 @@ protected function posNumberNormalizer(?string $input): ?int {
435410
$input = (int)$input;
436411
}
437412
if (is_int($input) && $input < 0) {
438-
throw new \RuntimeException('Port must not be negative');
413+
throw new RuntimeException('Port must not be negative');
439414
}
440415
return $input;
441416
}

lib/Command/Delete.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
use Symfony\Component\Console\Output\OutputInterface;
3333

3434
class Delete extends Base {
35-
/** @var Configuration */
36-
private $configurationService;
35+
private Configuration $configurationService;
3736

3837
public function __construct(Configuration $configurationService) {
3938
parent::__construct();

lib/Command/Edit.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525

2626
namespace OCA\LDAPContactsBackend\Command;
2727

28+
use Generator;
2829
use OC\Core\Command\Base;
2930
use OCA\LDAPContactsBackend\Model\Configuration as ConfigurationModel;
3031
use OCA\LDAPContactsBackend\Service\Configuration;
32+
use RuntimeException;
3133
use Symfony\Component\Console\Helper\QuestionHelper;
3234
use Symfony\Component\Console\Input\InputArgument;
3335
use Symfony\Component\Console\Input\InputInterface;
@@ -38,8 +40,7 @@
3840
class Edit extends Base {
3941
use TConfigurationDetail;
4042

41-
/** @var Configuration */
42-
private $configurationService;
43+
private Configuration $configurationService;
4344

4445
public function __construct(Configuration $configurationService) {
4546
parent::__construct();
@@ -64,7 +65,7 @@ protected function configure() {
6465
$this->configureOptions();
6566
}
6667

67-
protected function getListOfOptions(ConfigurationModel $model): \Generator {
68+
protected function getListOfOptions(ConfigurationModel $model): Generator {
6869
yield [
6970
'key' => 'addressBookName',
7071
'type' => 'string',
@@ -200,16 +201,10 @@ public function execute(InputInterface $input, OutputInterface $output): int {
200201

201202
foreach ($this->getListOfOptions($config) as $optionData) {
202203
if (!empty($input->getOption($optionData['key']))) {
203-
switch ($optionData['type']) {
204-
case 'uint':
205-
$v = max((int)$input->getOption($optionData['key']), 0);
206-
break;
207-
case 'string':
208-
case 'array-string':
209-
case 'cs-string':
210-
default:
211-
$v = $input->getOption($optionData['key']);
212-
}
204+
$v = match ($optionData['type']) {
205+
'uint' => max((int)$input->getOption($optionData['key']), 0),
206+
default => $input->getOption($optionData['key']),
207+
};
213208
$optionData['setter']($v);
214209
}
215210
}
@@ -233,20 +228,20 @@ private function yesOrNoNormalizer(string $input): ?bool {
233228
return null;
234229
}
235230

236-
private function stringNormalizer($input) {
237-
return $input ? trim($input) : '';
231+
private function stringNormalizer(?string $input): string {
232+
return ($input !== null) ? trim($input) : '';
238233
}
239234

240235
private function autoCompleteNormalizer($input, array $autoComplete) {
241-
return $v = array_change_key_case(array_flip($autoComplete))[strtolower($input)] ?? array_pop($autoComplete);
236+
return array_change_key_case(array_flip($autoComplete))[strtolower($input)] ?? array_pop($autoComplete);
242237
}
243238

244239
private function uIntNormalizer(?string $input): ?int {
245240
if (is_string($input)) {
246241
$input = (int)$input;
247242
}
248243
if (is_int($input) && $input < 0) {
249-
throw new \RuntimeException('Port must not be negative');
244+
throw new RuntimeException('Port must not be negative');
250245
}
251246
return $input;
252247
}

lib/Command/ListConfigs.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
use Symfony\Component\Console\Output\OutputInterface;
3333

3434
class ListConfigs extends Base {
35-
/** @var Configuration */
36-
private $configurationService;
35+
private Configuration $configurationService;
3736

3837
public function __construct(Configuration $configurationService) {
3938
parent::__construct();

lib/Command/TConfigurationDetail.php

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -25,85 +25,84 @@
2525

2626
namespace OCA\LDAPContactsBackend\Command;
2727

28+
use LogicException;
2829
use Symfony\Component\Console\Command\Command;
2930
use Symfony\Component\Console\Input\InputOption;
3031

3132
trait TConfigurationDetail {
32-
public function configureOptions() {
33+
public function configureOptions(): void {
3334
if (!$this instanceof Command) {
34-
throw new \LogicException('Trait applied on wrong base class');
35+
throw new LogicException('Trait applied on wrong base class');
3536
}
3637

37-
if ($this instanceof Command) {
38-
$this
39-
->addOption(
40-
'host',
41-
null,
42-
InputOption::VALUE_REQUIRED,
43-
'LDAP host name'
44-
)
45-
->addOption(
46-
'port',
47-
null,
48-
InputOption::VALUE_REQUIRED,
49-
'LDAP Port'
50-
)
51-
->addOption(
52-
'trans_enc',
53-
null,
54-
InputOption::VALUE_REQUIRED,
55-
'Transport encryption'
56-
)
57-
->addOption(
58-
'bindDN',
59-
null,
60-
InputOption::VALUE_REQUIRED,
61-
'LDAP Bind DN'
62-
)
63-
->addOption(
64-
'bindPwd',
65-
null,
66-
InputOption::VALUE_REQUIRED,
67-
'LDAP Bind Password'
68-
)
69-
->addOption(
70-
'filter',
71-
null,
72-
InputOption::VALUE_REQUIRED,
73-
'LDAP filter'
74-
)
75-
->addOption(
76-
'base',
77-
null,
78-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
79-
'LDAP contacts bases'
80-
)
81-
->addOption(
82-
'attrs',
83-
null,
84-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
85-
'LDAP Search Attributes',
86-
null
87-
)
88-
->addOption(
89-
'mapping',
90-
null,
91-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
92-
'LDAP to VCard mapping (e.g. --mapping=EMAIL:mail --mapping=TEL:mobile,homePhone)',
93-
null
94-
)
95-
->addOption(
96-
'disable',
97-
null,
98-
InputOption::VALUE_NONE,
99-
'keep configuration disabled'
100-
)
101-
->addOption(
102-
'interactive',
103-
'i',
104-
InputOption::VALUE_NONE,
105-
'Interactive mode'
106-
);
107-
}
38+
$this
39+
->addOption(
40+
'host',
41+
null,
42+
InputOption::VALUE_REQUIRED,
43+
'LDAP host name'
44+
)
45+
->addOption(
46+
'port',
47+
null,
48+
InputOption::VALUE_REQUIRED,
49+
'LDAP Port'
50+
)
51+
->addOption(
52+
'trans_enc',
53+
null,
54+
InputOption::VALUE_REQUIRED,
55+
'Transport encryption'
56+
)
57+
->addOption(
58+
'bindDN',
59+
null,
60+
InputOption::VALUE_REQUIRED,
61+
'LDAP Bind DN'
62+
)
63+
->addOption(
64+
'bindPwd',
65+
null,
66+
InputOption::VALUE_REQUIRED,
67+
'LDAP Bind Password'
68+
)
69+
->addOption(
70+
'filter',
71+
null,
72+
InputOption::VALUE_REQUIRED,
73+
'LDAP filter'
74+
)
75+
->addOption(
76+
'base',
77+
null,
78+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
79+
'LDAP contacts bases'
80+
)
81+
->addOption(
82+
'attrs',
83+
null,
84+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
85+
'LDAP Search Attributes',
86+
null
87+
)
88+
->addOption(
89+
'mapping',
90+
null,
91+
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
92+
'LDAP to VCard mapping (e.g. --mapping=EMAIL:mail --mapping=TEL:mobile,homePhone)',
93+
null
94+
)
95+
->addOption(
96+
'disable',
97+
null,
98+
InputOption::VALUE_NONE,
99+
'keep configuration disabled'
100+
)
101+
->addOption(
102+
'interactive',
103+
'i',
104+
InputOption::VALUE_NONE,
105+
'Interactive mode'
106+
);
108107
}
109108
}

0 commit comments

Comments
 (0)