Completed
Push — master ( 83b55d...472d84 )
by Justin
03:47 queued 36s
created

Defaults::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 92
Code Lines 85

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 85
dl 0
loc 92
rs 8.3272
c 0
b 0
f 0
cc 3
nc 4
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Rollbar;
2
3
use Rollbar\Utilities;
4
use Rollbar\Payload\Notifier;
5
use Psr\Log\LogLevel;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LogLevel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
class Defaults
8
{
9
    private $utilities;
10
    private $data;
11
    private static $singleton = null;
12
13
    public static function get()
14
    {
15
        if (is_null(self::$singleton)) {
16
            self::$singleton = new Defaults(new Utilities());
17
        }
18
        return self::$singleton;
19
    }
20
21
    public function __construct($utilities)
22
    {
23
        $this->data = array();
24
        
25
        $this->data['psrLevels'] = array(
26
            LogLevel::EMERGENCY => "critical",
27
            "emergency" => "critical",
28
            LogLevel::ALERT => "critical",
29
            "alert" => "critical",
30
            LogLevel::CRITICAL => "critical",
31
            "critical" => "critical",
32
            LogLevel::ERROR => "error",
33
            "error" => "error",
34
            LogLevel::WARNING => "warning",
35
            "warning" => "warning",
36
            LogLevel::NOTICE => "info",
37
            "notice" => "info",
38
            LogLevel::INFO => "info",
39
            "info" => "info",
40
            LogLevel::DEBUG => "debug",
41
            "debug" => "debug"
42
        );
43
        $this->data['errorLevels'] = array(
44
            E_ERROR => "error",
45
            E_WARNING => "warning",
46
            E_PARSE => "critical",
47
            E_NOTICE => "debug",
48
            E_CORE_ERROR => "critical",
49
            E_CORE_WARNING => "warning",
50
            E_COMPILE_ERROR => "critical",
51
            E_COMPILE_WARNING => "warning",
52
            E_USER_ERROR => "error",
53
            E_USER_WARNING => "warning",
54
            E_USER_NOTICE => "debug",
55
            E_STRICT => "info",
56
            E_RECOVERABLE_ERROR => "error",
57
            E_DEPRECATED => "info",
58
            E_USER_DEPRECATED => "info"
59
        );
60
        $this->data['gitHash'] = null;
61
        $this->data['gitBranch'] = null;
62
        $this->data['serverRoot'] = isset($_ENV["HEROKU_APP_DIR"]) ? $_ENV["HEROKU_APP_DIR"] : null;
63
        $this->data['platform'] = php_uname('a');
64
        $this->data['notifier'] = Notifier::defaultNotifier();
65
        $this->data['baseException'] = version_compare(phpversion(), '7.0', '<') ? '\Exception' : '\Throwable';
66
        $this->data['codeVersion'] = "";
67
        $this->data['sendMessageTrace'] = false;
68
        $this->data['includeCodeContext'] = false;
69
        $this->data['includeExcCodeContext'] = false;
70
        $this->data['rawRequestBody'] = false;
71
        $this->data['localVarsDump'] = true;
72
        $this->data['errorSampleRates'] = array();
73
        $this->data['exceptionSampleRates'] = array();
74
        $this->data['includedErrno'] = ROLLBAR_INCLUDED_ERRNO_BITMASK;
75
        $this->data['includeErrorCodeContext'] = null;
76
        $this->data['includeExceptionCodeContext'] = null;
77
        $this->data['agentLogLocation'] = '/var/tmp';
78
        $this->data['allowExec'] = true;
79
        $this->data['messageLevel'] = "warning";
80
        $this->data['exceptionLevel'] = "error";
81
        $this->data['endpoint'] = 'https://api.rollbar.com/api/1/';
82
        $this->data['captureErrorStacktraces'] = true;
83
        $this->data['checkIgnore'] = null;
84
        $this->data['custom'] = null;
85
        $this->data['customDataMethod'] = null;
86
        $this->data['enabled'] = true;
87
        $this->data['environment'] = 'production';
88
        $this->data['fluentHost'] = '127.0.0.1';
89
        $this->data['fluentPort'] = 24224;
90
        $this->data['fluentTag'] = 'rollbar';
91
        $this->data['handler'] = 'blocking';
92
        $this->data['host'] = null;
93
        $this->data['timeout'] = 3;
94
        $this->data['reportSuppressed'] = false;
95
        $this->data['useErrorReporting'] = false;
96
        $this->data['verbosity'] = \Psr\Log\LogLevel::ERROR;
97
        $this->data['captureIP'] = true;
98
        $this->data['captureEmail'] = false;
99
        $this->data['captureUsername'] = false;
100
        $this->data['scrubFields'] = array(
101
            'passwd',
102
            'password',
103
            'secret',
104
            'confirm_password',
105
            'password_confirmation',
106
            'auth_token',
107
            'csrf_token',
108
            'access_token'
109
        );
110
        $this->data['customTruncation'] = null;
111
        
112
        $this->utilities = $utilities;
113
    }
114
    
115
    public function __call($method, $args)
116
    {
117
        if (!array_key_exists($method, $this->data)) {
118
            throw new \Exception('No default value defined for property ' . $method . '.');
119
        }
120
        
121
        return (isset($args[0]) && $args[0] !== null) ? $args[0] : $this->data[$method];
122
    }
123
    
124
    public function fromSnakeCase($option)
125
    {
126
        $spaced = str_replace('_', ' ', $option);
127
        $method = lcfirst(str_replace(' ', '', ucwords($spaced)));
128
        return $this->$method();
129
    }
130
131
    public function gitBranch($gitBranch = null, $allowExec = true)
132
    {
133
        if ($gitBranch) {
134
            return $gitBranch;
135
        }
136
        if ($allowExec) {
137
            static $cachedValue;
138
            static $hasExecuted = false;
139
            if (!$hasExecuted) {
140
                $cachedValue = self::getGitBranch();
141
                $hasExecuted = true;
142
            }
143
            return $cachedValue;
144
        }
145
        return null;
146
    }
147
    
148
    private static function getGitBranch()
149
    {
150
        try {
151
            if (function_exists('shell_exec')) {
152
                $stdRedirCmd = Utilities::isWindows() ? " > NUL" : " 2> /dev/null";
153
                $output = rtrim(shell_exec('git rev-parse --abbrev-ref HEAD' . $stdRedirCmd));
154
                if ($output) {
155
                    return $output;
156
                }
157
            }
158
            return null;
159
        } catch (\Exception $e) {
160
            return null;
161
        }
162
    }
163
}
164