Skip to content

Commit 9215858

Browse files
author
Faraz Samapoor
committed
Uses early returns.
To improve code readability. Signed-off-by: Faraz Samapoor <f.samapoor@gmail.com> Signed-off-by: Faraz Samapoor <fsa@adlas.at>
1 parent a79391b commit 9215858

6 files changed

Lines changed: 43 additions & 41 deletions

File tree

apps/user_ldap/lib/Command/CheckGroup.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107107
}
108108
}
109109
return self::SUCCESS;
110-
} elseif ($wasMapped) {
110+
}
111+
112+
if ($wasMapped) {
111113
$output->writeln('The group does not exist on LDAP anymore.');
112114
if ($input->getOption('update')) {
113115
$this->backend->getLDAPAccess($gid)->connection->clearCache();
114116
$this->service->handleRemovedGroups([$gid]);
115117
}
116118
return self::SUCCESS;
117-
} else {
118-
throw new \Exception('The given group is not a recognized LDAP group.');
119119
}
120+
121+
throw new \Exception('The given group is not a recognized LDAP group.');
120122
} catch (\Exception $e) {
121123
$output->writeln('<error>' . $e->getMessage(). '</error>');
122124
return self::FAILURE;

apps/user_ldap/lib/Command/CheckUser.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9191
$this->updateUser($uid, $output);
9292
}
9393
return self::SUCCESS;
94-
} elseif ($wasMapped) {
94+
}
95+
96+
if ($wasMapped) {
9597
$this->dui->markUser($uid);
9698
$output->writeln('The user does not exists on LDAP anymore.');
9799
$output->writeln('Clean up the user\'s remnants by: ./occ user:delete "'
98100
. $uid . '"');
99101
return self::SUCCESS;
100-
} else {
101-
throw new \Exception('The given user is not a recognized LDAP user.');
102102
}
103+
104+
throw new \Exception('The given user is not a recognized LDAP user.');
103105
} catch (\Exception $e) {
104106
$output->writeln('<error>' . $e->getMessage(). '</error>');
105107
return self::FAILURE;

apps/user_ldap/lib/Command/DeleteConfig.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,17 @@ protected function configure(): void {
4949
;
5050
}
5151

52-
5352
protected function execute(InputInterface $input, OutputInterface $output): int {
5453
$configPrefix = $input->getArgument('configID');
5554

5655
$success = $this->helper->deleteServerConfiguration($configPrefix);
5756

58-
if ($success) {
59-
$output->writeln("Deleted configuration with configID '{$configPrefix}'");
60-
return self::SUCCESS;
61-
} else {
57+
if (!$success) {
6258
$output->writeln("Cannot delete configuration with configID '{$configPrefix}'");
6359
return self::FAILURE;
6460
}
61+
62+
$output->writeln("Deleted configuration with configID '{$configPrefix}'");
63+
return self::SUCCESS;
6564
}
6665
}

apps/user_ldap/lib/Command/Search.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,20 @@ protected function configure(): void {
8181

8282
/**
8383
* Tests whether the offset and limit options are valid
84-
* @param int $offset
85-
* @param int $limit
84+
*
8685
* @throws \InvalidArgumentException
8786
*/
88-
protected function validateOffsetAndLimit($offset, $limit): void {
89-
if ($limit < 0) {
90-
throw new \InvalidArgumentException('limit must be 0 or greater');
91-
}
92-
if ($offset < 0) {
93-
throw new \InvalidArgumentException('offset must be 0 or greater');
94-
}
95-
if ($limit === 0 && $offset !== 0) {
96-
throw new \InvalidArgumentException('offset must be 0 if limit is also set to 0');
97-
}
98-
if ($offset > 0 && ($offset % $limit !== 0)) {
99-
throw new \InvalidArgumentException('offset must be a multiple of limit');
87+
protected function validateOffsetAndLimit(int $offset, int $limit): void {
88+
$message = match (true) {
89+
($limit < 0) => 'limit must be 0 or greater',
90+
($offset < 0) => 'offset must be 0 or greater',
91+
($limit === 0 && $offset !== 0) => 'offset must be 0 if limit is also set to 0',
92+
($offset > 0 && ($offset % $limit !== 0)) => 'offset must be a multiple of limit',
93+
default => '',
94+
};
95+
96+
if (!empty($message)) {
97+
throw new \InvalidArgumentException($message);
10098
}
10199
}
102100

apps/user_ldap/lib/Command/SetConfig.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7676

7777
/**
7878
* save the configuration value as provided
79-
* @param string $configID
80-
* @param string $configKey
81-
* @param string $configValue
8279
*/
83-
protected function setValue($configID, $key, $value): void {
80+
protected function setValue(string $configID, string $key, string $value): void {
8481
$configHolder = new Configuration($configID);
8582
$configHolder->$key = $value;
8683
$configHolder->saveConfiguration();

apps/user_ldap/lib/Command/ShowConfig.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8686

8787
/**
8888
* prints the LDAP configuration(s)
89-
* @param string[] configID(s)
90-
* @param InputInterface $input
91-
* @param OutputInterface $output
89+
*
90+
* @param string[] $configIDs
9291
*/
93-
protected function renderConfigs($configIDs, $input, $output): void {
92+
protected function renderConfigs(
93+
array $configIDs,
94+
InputInterface $input,
95+
OutputInterface $output,
96+
): void {
9497
$renderTable = $input->getOption('output') === 'table' or $input->getOption('output') === null;
9598
$showPassword = $input->getOption('show-password');
9699

@@ -116,16 +119,17 @@ protected function renderConfigs($configIDs, $input, $output): void {
116119
$table->setHeaders(['Configuration', $id]);
117120
$table->setRows($rows);
118121
$table->render();
119-
} else {
120-
foreach ($configuration as $key => $value) {
121-
if ($key === 'ldapAgentPassword' && !$showPassword) {
122-
$rows[$key] = '***';
123-
} else {
124-
$rows[$key] = $value;
125-
}
122+
continue;
123+
}
124+
125+
foreach ($configuration as $key => $value) {
126+
if ($key === 'ldapAgentPassword' && !$showPassword) {
127+
$rows[$key] = '***';
128+
} else {
129+
$rows[$key] = $value;
126130
}
127-
$configs[$id] = $rows;
128131
}
132+
$configs[$id] = $rows;
129133
}
130134
if (!$renderTable) {
131135
$this->writeArrayInOutputFormat($input, $output, $configs);

0 commit comments

Comments
 (0)