Config   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 50
rs 10
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
B getConfigFromFile() 0 33 11
1
<?php
2
/**
3
 * Twigfield for Craft CMS
4
 *
5
 * Provides a twig editor field with Twig & Craft API autocomplete
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2022 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\twigfield\helpers;
12
13
use Craft;
14
use craft\helpers\ArrayHelper;
15
use craft\helpers\StringHelper;
16
17
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
18
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
19
 * @package   Twigfield
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
20
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
21
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
22
class Config
23
{
24
    // Constants
25
    // =========================================================================
26
27
    const PHP_SUFFIX = '.php';
28
29
    // Static Methods
30
    // =========================================================================
31
32
    /**
33
     * Loads a config file from, trying @craft/config first, then from @nystudio107/twigfield
34
     *
35
     * @param string $fileName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
36
     *
37
     * @return array
38
     */
39
    public static function getConfigFromFile(string $fileName): array
40
    {
41
        $fileName .= self::PHP_SUFFIX;
42
        $currentEnv = Craft::$app->getConfig()->env;
43
        // Try craft/config first
44
        $path = Craft::getAlias('@config/' . $fileName, false);
45
        if ($path === false || !file_exists($path)) {
46
            // Now try our own internal config
47
            $path = Craft::getAlias('@nystudio107/twigfield/config.php', false);
48
            if ($path === false || !file_exists($path)) {
49
                return [];
50
            }
51
        }
52
        // Make sure we got a config file
53
        if (!is_array($config = @include $path)) {
54
            return [];
55
        }
56
        // If it's not a multi-environment config, return the whole thing
57
        if (!array_key_exists('*', $config)) {
58
            return $config;
59
        }
60
        // If no environment was specified, just look in the '*' array
61
        if ($currentEnv === null) {
62
            return $config['*'];
63
        }
64
        $mergedConfig = [];
65
        foreach ($config as $env => $envConfig) {
66
            if ($env === '*' || StringHelper::contains($currentEnv, $env)) {
67
                $mergedConfig = ArrayHelper::merge($mergedConfig, $envConfig);
68
            }
69
        }
70
71
        return $mergedConfig;
72
    }
73
}
74