Skip to content

Commit 5a1182b

Browse files
Move constants to separate class
1 parent 5efb853 commit 5a1182b

File tree

6 files changed

+64
-49
lines changed

6 files changed

+64
-49
lines changed

src/Config.php

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,6 @@
2626
*/
2727
class Config
2828
{
29-
public const SETTING_ALLOW_FAILURE = 'allow-failure';
30-
public const SETTING_BOOTSTRAP = 'bootstrap';
31-
public const SETTING_COLORS = 'ansi-colors';
32-
public const SETTING_CUSTOM = 'custom';
33-
public const SETTING_GIT_DIR = 'git-directory';
34-
public const SETTING_INCLUDES = 'includes';
35-
public const SETTING_INCLUDES_LEVEL = 'includes-level';
36-
public const SETTING_LABEL = 'label';
37-
public const SETTING_RUN_EXEC = 'run-exec';
38-
public const SETTING_RUN_MODE = 'run-mode';
39-
public const SETTING_RUN_PATH = 'run-path';
40-
public const SETTING_RUN_GIT = 'run-git';
41-
public const SETTING_PHP_PATH = 'php-path';
42-
public const SETTING_VERBOSITY = 'verbosity';
43-
public const SETTING_FAIL_ON_FIRST_ERROR = 'fail-on-first-error';
44-
4529
/**
4630
* Path to the config file
4731
*
@@ -161,10 +145,10 @@ private function setupRunConfig(array $settings): array
161145
{
162146
// extract the legacy settings
163147
$settingsToMove = [
164-
self::SETTING_RUN_MODE,
165-
self::SETTING_RUN_EXEC,
166-
self::SETTING_RUN_PATH,
167-
self::SETTING_RUN_GIT
148+
Config\Settings::RUN_MODE,
149+
Config\Settings::RUN_EXEC,
150+
Config\Settings::RUN_PATH,
151+
Config\Settings::RUN_GIT
168152
];
169153
$config = [];
170154
foreach ($settingsToMove as $setting) {
@@ -199,7 +183,7 @@ public function isLoadedFromFile(): bool
199183
*/
200184
public function isFailureAllowed(): bool
201185
{
202-
return (bool) ($this->settings[self::SETTING_ALLOW_FAILURE] ?? false);
186+
return (bool) ($this->settings[Config\Settings::ALLOW_FAILURE] ?? false);
203187
}
204188

205189
/**
@@ -243,14 +227,14 @@ public function getPath(): string
243227
*/
244228
public function getGitDirectory(): string
245229
{
246-
if (empty($this->settings[self::SETTING_GIT_DIR])) {
230+
if (empty($this->settings[Config\Settings::GIT_DIR])) {
247231
return getcwd() . '/.git';
248232
}
249233

250234
// if repo path is absolute use it otherwise create an absolute path relative to the configuration file
251-
return Check::isAbsolutePath($this->settings[self::SETTING_GIT_DIR])
252-
? $this->settings[self::SETTING_GIT_DIR]
253-
: dirname($this->path) . '/' . $this->settings[self::SETTING_GIT_DIR];
235+
return Check::isAbsolutePath($this->settings[Config\Settings::GIT_DIR])
236+
? $this->settings[Config\Settings::GIT_DIR]
237+
: dirname($this->path) . '/' . $this->settings[Config\Settings::GIT_DIR];
254238
}
255239

256240
/**
@@ -261,8 +245,8 @@ public function getGitDirectory(): string
261245
*/
262246
public function getBootstrap(string $default = 'vendor/autoload.php'): string
263247
{
264-
return !empty($this->settings[self::SETTING_BOOTSTRAP])
265-
? $this->settings[self::SETTING_BOOTSTRAP]
248+
return !empty($this->settings[Config\Settings::BOOTSTRAP])
249+
? $this->settings[Config\Settings::BOOTSTRAP]
266250
: $default;
267251
}
268252

@@ -273,8 +257,8 @@ public function getBootstrap(string $default = 'vendor/autoload.php'): string
273257
*/
274258
public function getVerbosity(): string
275259
{
276-
return !empty($this->settings[self::SETTING_VERBOSITY])
277-
? $this->settings[self::SETTING_VERBOSITY]
260+
return !empty($this->settings[Config\Settings::VERBOSITY])
261+
? $this->settings[Config\Settings::VERBOSITY]
278262
: 'normal';
279263
}
280264

@@ -285,7 +269,7 @@ public function getVerbosity(): string
285269
*/
286270
public function useAnsiColors(): bool
287271
{
288-
return (bool) ($this->settings[self::SETTING_COLORS] ?? true);
272+
return (bool) ($this->settings[Config\Settings::COLORS] ?? true);
289273
}
290274

291275
/**
@@ -295,7 +279,7 @@ public function useAnsiColors(): bool
295279
*/
296280
public function getPhpPath(): string
297281
{
298-
return (string) ($this->settings[self::SETTING_PHP_PATH] ?? '');
282+
return (string) ($this->settings[Config\Settings::PHP_PATH] ?? '');
299283
}
300284

301285
/**
@@ -327,7 +311,7 @@ public function getCustomSettings(): array
327311
*/
328312
public function failOnFirstError(): bool
329313
{
330-
return (bool) ($this->settings[self::SETTING_FAIL_ON_FIRST_ERROR] ?? true);
314+
return (bool) ($this->settings[Config\Settings::FAIL_ON_FIRST_ERROR] ?? true);
331315
}
332316

333317
/**

src/Config/Action.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ class Action
5757
* @var string[]
5858
*/
5959
private static array $availableSettings = [
60-
Config::SETTING_ALLOW_FAILURE,
61-
Config::SETTING_LABEL
60+
Config\Settings::ALLOW_FAILURE,
61+
Config\Settings::LABEL
6262
];
6363

6464
/**
@@ -149,7 +149,7 @@ public function isIncluded(): bool
149149
*/
150150
public function isFailureAllowed(bool $default = false): bool
151151
{
152-
return (bool) ($this->settings[Config::SETTING_ALLOW_FAILURE] ?? $default);
152+
return (bool) ($this->settings[Config\Settings::ALLOW_FAILURE] ?? $default);
153153
}
154154

155155
/**
@@ -159,7 +159,7 @@ public function isFailureAllowed(bool $default = false): bool
159159
*/
160160
public function getLabel(): string
161161
{
162-
return (string) ($this->settings[Config::SETTING_LABEL] ?? $this->getAction());
162+
return (string) ($this->settings[Config\Settings::LABEL] ?? $this->getAction());
163163
}
164164

165165
/**

src/Config/Factory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ private function appendIncludedConfigurations(Config $config, array $json): void
271271
private function readMaxIncludeLevel(array $json): void
272272
{
273273
// read the include-level setting only for the actual configuration
274-
if ($this->includeLevel === 0 && isset($json['config'][Config::SETTING_INCLUDES_LEVEL])) {
275-
$this->maxIncludeLevel = (int) $json['config'][Config::SETTING_INCLUDES_LEVEL];
274+
if ($this->includeLevel === 0 && isset($json['config'][Config\Settings::INCLUDES_LEVEL])) {
275+
$this->maxIncludeLevel = (int) $json['config'][Config\Settings::INCLUDES_LEVEL];
276276
}
277277
}
278278

@@ -312,7 +312,7 @@ protected function loadIncludedConfigs(array $json, string $path): array
312312
{
313313
$includes = [];
314314
$directory = dirname($path);
315-
$files = Util::extractListFromJson(Util::extractListFromJson($json, 'config'), Config::SETTING_INCLUDES);
315+
$files = Util::extractListFromJson(Util::extractListFromJson($json, 'config'), Config\Settings::INCLUDES);
316316

317317
foreach ($files as $file) {
318318
$includes[] = $this->includeConfig($directory . DIRECTORY_SEPARATOR . $file);

src/Config/Settings.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CaptainHook.
5+
*
6+
* (c) Sebastian Feldmann <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CaptainHook\App\Config;
13+
14+
class Settings
15+
{
16+
public const ALLOW_FAILURE = 'allow-failure';
17+
public const BOOTSTRAP = 'bootstrap';
18+
public const COLORS = 'ansi-colors';
19+
public const CUSTOM = 'custom';
20+
public const GIT_DIR = 'git-directory';
21+
public const INCLUDES = 'includes';
22+
public const INCLUDES_LEVEL = 'includes-level';
23+
public const LABEL = 'label';
24+
public const RUN_EXEC = 'run-exec';
25+
public const RUN_MODE = 'run-mode';
26+
public const RUN_PATH = 'run-path';
27+
public const RUN_GIT = 'run-git';
28+
public const PHP_PATH = 'php-path';
29+
public const VERBOSITY = 'verbosity';
30+
public const FAIL_ON_FIRST_ERROR = 'fail-on-first-error';
31+
}

src/Config/Util.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,14 @@ public static function writeToDisk(Config $config): void
161161
*/
162162
public static function mergeSettings(array ...$settings): array
163163
{
164-
$includes = array_column($settings, Config::SETTING_INCLUDES);
165-
$custom = array_column($settings, Config::SETTING_CUSTOM);
164+
$includes = array_column($settings, Config\Settings::INCLUDES);
165+
$custom = array_column($settings, Config\Settings::CUSTOM);
166166
$mergedSettings = array_merge(...$settings);
167167
if (!empty($includes)) {
168-
$mergedSettings[Config::SETTING_INCLUDES] = array_merge(...$includes);
168+
$mergedSettings[Config\Settings::INCLUDES] = array_merge(...$includes);
169169
}
170170
if (!empty($custom)) {
171-
$mergedSettings[Config::SETTING_CUSTOM] = array_merge(...$custom);
171+
$mergedSettings[Config\Settings::CUSTOM] = array_merge(...$custom);
172172
}
173173

174174
return $mergedSettings;

tests/unit/Config/UtilTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,22 +219,22 @@ public function testPluginEmpty(): void
219219
public function testMergeSettings()
220220
{
221221
$s1 = [
222-
Config::SETTING_INCLUDES => [
222+
Config\Settings::INCLUDES => [
223223
'foo',
224224
'bar'
225225
],
226-
CONFIG::SETTING_COLORS => true,
226+
Config\Settings::COLORS => true,
227227
];
228228
$s2 = [
229-
Config::SETTING_INCLUDES => [
229+
Config\Settings::INCLUDES => [
230230
'baz'
231231
],
232-
CONFIG::SETTING_GIT_DIR => '/var/.git'
232+
Config\Settings::GIT_DIR => '/var/.git'
233233
];
234234

235235
$merged = Util::mergeSettings($s2, $s1);
236236

237-
$this->assertEquals(3, count($merged[Config::SETTING_INCLUDES]));
238-
$this->assertContains('baz', $merged[Config::SETTING_INCLUDES]);
237+
$this->assertEquals(3, count($merged[Config\Settings::INCLUDES]));
238+
$this->assertContains('baz', $merged[Config\Settings::INCLUDES]);
239239
}
240240
}

0 commit comments

Comments
 (0)