Passed
Push — master ( 74b95b...af185e )
by Plamen
01:33
created

trait_config::config()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 16
rs 9.5555
1
<?php
2
3
class trait_config
4
{
5
    /** @param array Re-settable values for current table call */
6
    protected static $config = [
7
        'APP' => 'App',
8
        'DB_COLLATION_CI' => 'utf8mb4_general_ci', //UTF8_GENERAL_CI
9
        'EMPTY_TABLE_MSG' => 'No results found.',
10
        'EXPORT_FILE_NAME' => ':app - :items export (:datetime)',
11
        'FILTER_ACTIVE' => true,
12
        'FILTER_CASE_SENSITIVE' => false,
13
        'SAVES' => ['CSV', 'Excel'],
14
        'UTF8_ASC_SYMBOL' => '&#9650;',
15
        'UTF8_DESC_SYMBOL' => '&#9660;',
16
        'UTF8_LEFT_SYMBOL' => '&lsaquo;',
17
        'UTF8_RIGHT_SYMBOL' => '&rsaquo;'
18
    ];
19
20
    /** Set/Get config value
21
     * @param mixed $value (string) Get if exists, (array) Set if valid
22
     * @return mixed */
23
    public static function config($value)
24
    {
25
        try {
26
            switch (gettype($value)) {
27
                case 'array':
28
                    foreach ($value as $k => $v) {
29
                        self::$config[$k] = self::valid((string) $k, $v);
30
                    }
31
                    break;
32
                case 'string':
33
                    return self::valid($value);
34
                default:
35
                    throw new Exception("Invalid value type.");
36
            }
37
        } catch (Exception $e) {
38
            self::error('ERROR: ' . $e->getMessage());
0 ignored issues
show
Bug introduced by
The method error() does not exist on trait_config. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
            self::/** @scrutinizer ignore-call */ 
39
                  error('ERROR: ' . $e->getMessage());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
        }
40
    }
41
42
    private static function valid($key, $val = null)
43
    {
44
        if (!array_key_exists($key, self::$config)) {
45
            throw new Exception('Request to undefined value: ' . $key);
46
        }
47
48
        if ($val !== null) {
49
            if (empty($val) || gettype($val) !== gettype(self::$config[$key])) {
50
                throw new Exception("Setting invalid value: $val (:$key)");
51
            }
52
        }
53
54
        return $val === null ? self::$config[$key] : $val;
55
    }
56
}
57