GreyLogException   B
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 448
Duplicated Lines 19.64 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 37
lcom 1
cbo 4
dl 88
loc 448
ccs 0
cts 181
cp 0
rs 8.6
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 38 4
B validateExceptionDetails() 0 20 7
A formatErrorMessage() 0 9 2
A getNumberOfVariables() 0 6 2
A getExceptionCode() 0 8 2
A innerNotice() 0 14 1
A emergency() 11 11 1
A alert() 11 11 1
A critical() 11 11 1
A error() 11 11 1
A warning() 11 11 1
A notice() 11 11 1
A info() 11 11 1
A debug() 11 11 1
A setShortMessage() 0 3 1
A setMessage() 0 3 1
A setLevel() 0 3 1
A setFacility() 0 3 1
A setAdditionals() 0 9 1
A addParameters() 0 21 4
A publishMessage() 0 3 1
A loggerPanic() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * Copyright (C) 2016 luc <[email protected]>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace GreyLogException;
21
22
/**
23
 * This is the main class for the GreyLogException module.
24
 * @author luc <[email protected]>
25
 * @version 0.2
26
 */
27
class GreyLogException extends \Exception {
28
29
    /**
30
     * The application name that is logging the exception (facility in GrayLog).
31
     */
32
    private $sApplication = "SampleApplicationName";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal SampleApplicationName does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
33
34
    /**
35
     * The IP the GrayLog2-Server listens to.
36
     */
37
    private $sLogServerIp = "127.0.0.1";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal 127.0.0.1 does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
38
39
    /**
40
     * PSR LogLevel 8
41
     */
42
    const EMERGENCY = \Psr\Log\LogLevel::EMERGENCY;
43
44
    /**
45
     * PSR LogLevel 7
46
     */
47
    const ALERT = \Psr\Log\LogLevel::ALERT;
48
49
    /**
50
     * PSR LogLevel 6
51
     */
52
    const CRITICAL = \Psr\Log\LogLevel::CRITICAL;
53
54
    /**
55
     * PSR LogLevel 5
56
     */
57
    const ERROR = \Psr\Log\LogLevel::ERROR;
58
59
    /**
60
     * PSR LogLevel 4
61
     */
62
    const WARNING = \Psr\Log\LogLevel::WARNING;
63
64
    /**
65
     * PSR LogLevel 3
66
     */
67
    const NOTICE = \Psr\Log\LogLevel::NOTICE;
68
69
    /**
70
     * PSR LogLevel 2
71
     */
72
    const INFO = \Psr\Log\LogLevel::INFO;
73
74
    /**
75
     * PSR LogLevel 1
76
     */
77
    const DEBUG = \Psr\Log\LogLevel::DEBUG;
78
79
    /**
80
     * The publisher object for GreyLog2-Server.
81
     * @var \Gelf\Publisher $publisher Holds the publisher object. 
82
     */
83
    private $publisher;
84
85
    /**
86
     * The message object to send to GreyLog2-Server.
87
     * @var \Gelf\Message $message Holds the message object.
88
     */
89
    private $gelfMessage;
90
91
    /**
92
     * The actual exception message.
93
     * @var String  $sExceptionMessage  The message for the exception.
94
     */
95
    public $sExceptionMessage;
96
97
    /**
98
     * The actual exception code.
99
     * @var Integer $iExceptionCode Holds the exception code.
100
     */
101
    public $iExceptionCode;
102
103
    /**
104
     * The actual exception identifier/shortMessage.
105
     * @var String  $sExceptionShortMessage Holds the exceptionShortMessage. 
106
     */
107
    public $sExceptionShortMessage;
108
109
    /**
110
     * The actual exception level. Possible values:
111
     * EMERGENCY, ALERT, CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG
112
     * @var String  $sExceptionLevel    Holds the exceptionLevel.
113
     */
114
    private $sExceptionLevel;
115
116
    /**
117
     * The actual class where the exception was thrown.
118
     * @var String  $sExceptionClass    Holds the exceptionClass.
119
     */
120
    private $sExceptionClass;
121
122
    /**
123
     * The actual function/method where the exception was thrown.
124
     * @var String  $sExceptionFunction Holds the exceptionFunction.
125
     */
126
    private $sExceptionFunction;
127
128
    /**
129
     * The actual exception class the exception came from.
130
     * @var String  $sModuleName    Holds the moduleName.
131
     */
132
    private $sModuleName;
133
134
    /**
135
     * Sends a new exception with the help of an exceptionDetails array and aditional parameters.
136
     * @param Array $_aExceptionDetails The exceptionDetails in form: "[int ExceptionCode, lfException::LOGLEVEL, string ExceptionMessage]".
137
     * @param Mixed $_aAdditionalInformations The array of all other given parameters for setting them in the exceptionMessage.
138
     */
139
    public function __construct(Array $_aExceptionDetails, ...$_aAdditionalInformations) {
140
        //get configruation
141
        $this->sApplication = GreyLogExceptionConfig::$sApplicationNameToLog;
142
        $this->sLogServerIp = GreyLogExceptionConfig::$sGreyLogServerIp;
143
144
        try {
145
            //call user-code for exception if there is any
146
            $oClass = new \ReflectionClass(get_called_class());
147
            $sExceptionArrayKey = array_search($_aExceptionDetails, $oClass->getConstants());
148
            if ($sExceptionArrayKey !== false && method_exists(get_called_class(), $sExceptionArrayKey)) {
149
                $sClassToCall = get_called_class();
150
                $sClassToCall::$sExceptionArrayKey();
151
            }
152
153
            //prepare transport objects to GreyLog2
154
            $oTransportObject = new \Gelf\Transport\UdpTransport($this->sLogServerIp, 12201, \Gelf\Transport\UdpTransport::CHUNK_SIZE_LAN);
155
            $this->publisher = new \Gelf\Publisher();
156
            $this->publisher->addTransport($oTransportObject);
157
158
            //validate exception details
159
            $this->validateExceptionDetails($_aExceptionDetails);
160
            //format/create the exception message
161
            $this->sExceptionMessage = $this->formatErrorMessage($_aAdditionalInformations);
162
            //create standard exception
163
            parent::__construct($this->sExceptionMessage, $this->iExceptionCode);
164
            //set additional exception details
165
            $this->sExceptionShortMessage = $this->getExceptionCode($_aExceptionDetails, get_called_class());
166
            $this->sExceptionClass = $this->getTrace()[0]['class'];
167
            $this->sExceptionFunction = $this->getTrace()[0]['function'];
168
            $this->sModuleName = get_called_class();
169
            //log exception
170
            $sFunctionToCall = $this->sExceptionLevel;
171
            $this->$sFunctionToCall();
172
        } catch (\Exception $ex) {
173
            //use your own code in loggerPanic to do something when message cant be logged
174
            $this->loggerPanic($this->sExceptionShortMessage);
175
        }
176
    }
177
178
    /**
179
     * Checks the exceptionArray for valid contents and sets its values.
180
     * Sets a standard value if details are missing in array.
181
     * Sends a notice exception if there are missing details.
182
     * @param array $_aExceptionDetails The exceptionDetails in form: "[int ExceptionCode, lfException::LOGLEVEL, string ExceptionMessage]".
183
     */
184
    private function validateExceptionDetails(Array $_aExceptionDetails) {
185
        if (isset($_aExceptionDetails[0]) && is_numeric($_aExceptionDetails[0])) {
186
            $this->iExceptionCode = $_aExceptionDetails[0];
0 ignored issues
show
Documentation Bug introduced by
It seems like $_aExceptionDetails[0] can also be of type double or string. However, the property $iExceptionCode is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
187
            if (isset($_aExceptionDetails[1]) && method_exists($this, $_aExceptionDetails[1])) {
188
                $this->sExceptionLevel = $_aExceptionDetails[1];
0 ignored issues
show
Documentation Bug introduced by
It seems like $_aExceptionDetails[1] can also be of type integer or double. However, the property $sExceptionLevel is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
189
                if (isset($_aExceptionDetails[2]) && is_string($_aExceptionDetails[2])) {
190
                    $this->sExceptionMessage = $_aExceptionDetails[2];
191
                } else {
192
                    $this->sExceptionMessage = "__ERR_NOT_FOUND__";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal __ERR_NOT_FOUND__ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
193
                    $this->innerNotice("WrongConfig", "The exeption thrown, has no exceptionMessage.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal WrongConfig does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal The exeption thrown, has no exceptionMessage. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
194
                }
195
            } else {
196
                $this->sExceptionLevel = self::ERROR;
197
                $this->innerNotice("WrongConfig", "The exeption thrown, has no or invalid exceptionLevel, using ERROR instead.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal WrongConfig does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal The exeption thrown, has...l, using ERROR instead. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
198
            }
199
        } else {
200
            $this->iExceptionCode = 000000;
201
            $this->innerNotice("WrongConfig", "The exeption thrown, has no exceptionCode. Using '000000' instead.");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal WrongConfig does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
202
        }
203
    }
204
205
    /**
206
     * Formats the errorMessage and fills the variables with content if there are any. 
207
     * @param array $_aAdditionalInformations   An array containing all values for variables that has to be replaced in errorMessage.
208
     * @return string   The formated errorMessage.
209
     */
210
    public function formatErrorMessage(array $_aAdditionalInformations) {
211
        if (count($_aAdditionalInformations) > 0) {
212
            $sFormatedExceptionMessage = vsprintf($this->sExceptionMessage, $_aAdditionalInformations);
213
            $sFormatedExceptionMessage .= "." . PHP_EOL . "Call-Stack:" . PHP_EOL . $this->getTraceAsString();
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal . does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal Call-Stack: does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
214
        } else {
215
            $sFormatedExceptionMessage = $this->sExceptionMessage . "." . PHP_EOL . "Call-Stack:" . PHP_EOL . $this->getTraceAsString();
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal . does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal Call-Stack: does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
216
        }
217
        return $sFormatedExceptionMessage;
218
    }
219
220
    /**
221
     * Gets the number of variables that can be replaces by sprintf function in a string.
222
     * @param string $_sReplaceableString The string to search for replacable variables.
223
     * @return int Returns the number of valriables that can be relaced via sprintf.
224
     */
225
    public function getNumberOfVariables(string $_sReplaceableString) {
226
        if (preg_match_all("~%(?:(\d+)[$])?[-+]?(?:[ 0]|['].)?(?:[-]?\d+)?(?:[.]\d+)?[%bcdeEufFgGosxX]~", $_sReplaceableString, $iNumberOfVariables) > 0) {
227
            return count($iNumberOfVariables[1]);
228
        }
229
        return 0;
230
    }
231
232
    /**
233
     * Seaches the key of the given class constant value.
234
     * @param array $_aExceptionDetails The value of an exception constant.
235
     * @param string $_sExceptionClass The class the exception constant has to be searched.
236
     * @return mixed the key for <i>needle</i> if it is found in the array, <b>FALSE</b> otherwise.
237
     * @todo Could be cached with storing all exceptions in opCache
238
     * @todo Inner exception should be added when getExceptionCode fails. But at the moment innerNotice function has circular reference to ExceptinShortMessage
239
     */
240
    public function getExceptionCode($_aExceptionDetails, $_sExceptionClass) {
241
        $oClass = new \ReflectionClass($_sExceptionClass);
242
        $sExceptionCode = array_search($_aExceptionDetails, $oClass->getConstants());
243
        if($sExceptionCode === false){
244
            $sExceptionCode = "UNKNOWN";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal UNKNOWN does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
245
        }
246
        return $sExceptionCode;
247
    }
248
249
    /**
250
     * Sends an notice to GreyLog-Server before sending the actual exception.
251
     * @param string $sShortMessage The notice identifier.
252
     * @param string $sMessage  The message for the notice.
253
     */
254
    private function innerNotice(string $sShortMessage, string $sMessage) {
255
        $message = new \Gelf\Message();
256
        $message->setShortMessage($sShortMessage . "_" . $this->iExceptionCode . ":" . $this->sExceptionShortMessage)
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal _ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal : does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
257
                ->setLevel(self::NOTICE)
258
                ->setFullMessage($sMessage)
259
                ->setFacility($this->sApplication)
260
                ->setAdditional('ModuleName', $this->sModuleName)
261
                ->setAdditional('ExceptionClassName', $this->sExceptionClass)
262
                ->setAdditional('ExceptionFunctionName', $this->sExceptionFunction)
263
                ->setAdditional('ExceptionCode', $this->code)
264
                ->setAdditional('ExceptionFileName', $this->file)
265
                ->setAdditional('ExceptionLineNumber', $this->line);
266
        $this->publisher->publish($message);
267
    }
268
269
    /**
270
     * Sends an execption of type "EMERGENCY" to configured GreyLog2-Server.
271
     */
272 View Code Duplication
    private function emergency() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
273
        $this->gelfMessage = new \Gelf\Message();
274
        $this->setShortMessage();
275
        $this->setMessage();
276
        $this->setLevel();
277
        $this->setFacility();
278
        $this->setAdditionals();
279
        $this->addParameters();
280
        //send message
281
        $this->publishMessage();
282
    }
283
284
    /**
285
     * Sends an execption of type "ALERT" to configured GreyLog2-Server.
286
     */
287 View Code Duplication
    private function alert() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
288
        $this->gelfMessage = new \Gelf\Message();
289
        $this->setShortMessage();
290
        $this->setMessage();
291
        $this->setLevel();
292
        $this->setFacility();
293
        $this->setAdditionals();
294
        $this->addParameters();
295
        //send message
296
        $this->publishMessage();
297
    }
298
299
    /**
300
     * Sends an execption of type "CRITICAL" to configured GreyLog2-Server.
301
     */
302 View Code Duplication
    private function critical() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
303
        $this->gelfMessage = new \Gelf\Message();
304
        $this->setShortMessage();
305
        $this->setMessage();
306
        $this->setLevel();
307
        $this->setFacility();
308
        $this->setAdditionals();
309
        $this->addParameters();
310
        //send message
311
        $this->publishMessage();
312
    }
313
314
    /**
315
     * Sends an execption of type "ERROR" to configured GreyLog2-Server.
316
     */
317 View Code Duplication
    private function error() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
318
        $this->gelfMessage = new \Gelf\Message();
319
        $this->setShortMessage();
320
        $this->setMessage();
321
        $this->setLevel();
322
        $this->setFacility();
323
        $this->setAdditionals();
324
        $this->addParameters();
325
        //send message
326
        $this->publishMessage();
327
    }
328
329
    /**
330
     * Sends an execption of type "WARNING" to configured GreyLog2-Server.
331
     */
332 View Code Duplication
    private function warning() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
333
        $this->gelfMessage = new \Gelf\Message();
334
        $this->setShortMessage();
335
        $this->setMessage();
336
        $this->setLevel();
337
        $this->setFacility();
338
        $this->setAdditionals();
339
        $this->addParameters();
340
        //send message
341
        $this->publishMessage();
342
    }
343
344
    /**
345
     * Sends an execption of type "NOTICE" to configured GreyLog2-Server.
346
     */
347 View Code Duplication
    private function notice() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
348
        $this->gelfMessage = new \Gelf\Message();
349
        $this->setShortMessage();
350
        $this->setMessage();
351
        $this->setLevel();
352
        $this->setFacility();
353
        $this->setAdditionals();
354
        $this->addParameters();
355
        //send message
356
        $this->publishMessage();
357
    }
358
359
    /**
360
     * Sends an execption of type "INFO" to configured GreyLog2-Server.
361
     */
362 View Code Duplication
    private function info() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
363
        $this->gelfMessage = new \Gelf\Message();
364
        $this->setShortMessage();
365
        $this->setMessage();
366
        $this->setLevel();
367
        $this->setFacility();
368
        $this->setAdditionals();
369
        $this->addParameters();
370
        //send message
371
        $this->publishMessage();
372
    }
373
374
    /**
375
     * Sends an execption of type "DEBUG" to configured GreyLog2-Server.
376
     */
377 View Code Duplication
    private function debug() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
378
        $this->gelfMessage = new \Gelf\Message();
379
        $this->setShortMessage();
380
        $this->setMessage();
381
        $this->setLevel();
382
        $this->setFacility();
383
        $this->setAdditionals();
384
        $this->addParameters();
385
        //send message
386
        $this->publishMessage();
387
    }
388
389
    /**
390
     * Sets the ShortMessage and returns message object.
391
     * @return Gelf\Message Sets the ShortMessage and returns message object.
0 ignored issues
show
Documentation introduced by
Should the return type not be \Gelf\Message?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
392
     */
393
    private function setShortMessage() {
394
        return $this->gelfMessage->setShortMessage($this->iExceptionCode . ":" . $this->sExceptionShortMessage);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal : does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
395
    }
396
397
    /**
398
     * Sets the Message and returns message object.
399
     * @return Gelf\Message Sets the Message and returns message object.
0 ignored issues
show
Documentation introduced by
Should the return type not be \Gelf\Message?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
400
     */
401
    private function setMessage() {
402
        return $this->gelfMessage->setFullMessage($this->sExceptionMessage);
403
    }
404
405
    /**
406
     * Sets the Level and returns message object.
407
     * @return Gelf\Message Sets the Level and returns message object.
0 ignored issues
show
Documentation introduced by
Should the return type not be \Gelf\Message?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
408
     */
409
    private function setLevel() {
410
        return $this->gelfMessage->setLevel($this->sExceptionLevel);
411
    }
412
413
    /**
414
     * Sets the Facility and returns message object.
415
     * @return Gelf\Message Sets the Facility and returns message object.
0 ignored issues
show
Documentation introduced by
Should the return type not be \Gelf\Message?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
416
     */
417
    private function setFacility() {
418
        return $this->gelfMessage->setFacility($this->sApplication);
419
    }
420
421
    /**
422
     * Sets additional details from exception object and returns message object.
423
     * @return Gelf\Message Sets the additional details from exception object and returns message object.
0 ignored issues
show
Documentation introduced by
Should the return type not be \Gelf\Message?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
424
     */
425
    private function setAdditionals() {
426
        $this->gelfMessage->setAdditional('ModuleName', $this->sModuleName);
427
        $this->gelfMessage->setAdditional('ExceptionClassName', $this->sExceptionClass);
428
        $this->gelfMessage->setAdditional('ExceptionFunctionName', $this->sExceptionFunction);
429
        $this->gelfMessage->setAdditional('ExceptionCode', $this->code);
430
        $this->gelfMessage->setAdditional('ExceptionFileName', $this->file);
431
        $this->gelfMessage->setAdditional('ExceptionLineNumber', $this->line);
432
        return $this->gelfMessage;
433
    }
434
435
    /**
436
     * Checks if function/method parameters are equal to function declaration and logs warning if not.
437
     * Gets all names of function/method parameters and its values and adds it to message object as additional.
438
     * @return Gelf\Message Sets additional details to message object and returns the message object.
0 ignored issues
show
Documentation introduced by
Should the return type not be \Gelf\Message?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
439
     */
440
    private function addParameters() {
441
        //get function/method parameters and parameters function was called
442
        $ref = new \ReflectionMethod($this->sExceptionClass, $this->sExceptionFunction);
443
        $aFunctionParameters = $ref->getParameters();
444
        $aFunctionParametersSetted = $this->getTrace()[0]['args'];
445
        //log notice if number of parameters does not match
446
        if (count($aFunctionParameters) < count($aFunctionParametersSetted)) {
447
            $this->innerNotice("MissUse", "Function was called with '" . count($aFunctionParametersSetted) . "' parameters, but defined on function is only '" . count($aFunctionParameters) . "'");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal MissUse does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
448
        }
449
        //add parameters and values to exception to log
450
        $i = 0;
451
        foreach ($aFunctionParametersSetted as $mParameterValue) {
452
            if (!isset($aFunctionParameters[$i])) {
453
                $this->gelfMessage->setAdditional("Param_UNKNOWN" . $i, serialize($mParameterValue));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Param_UNKNOWN does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
454
            } else {
455
                $this->gelfMessage->setAdditional("Param_" . $aFunctionParameters[$i]->name, serialize($mParameterValue));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Param_ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
456
            }
457
            $i++;
458
        }
459
        return $this->gelfMessage;
460
    }
461
462
    /**
463
     * Publishes the message object to configured GreyLog2-Server instance.
464
     * @return Gelf\publisher The publisher object.
0 ignored issues
show
Documentation introduced by
Should the return type not be Gelf\publisher|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
465
     */
466
    private function publishMessage() {
467
        $this->publisher->publish($this->gelfMessage);
468
    }
469
470
    private function loggerPanic(string $_sMessage) {
471
        die("<html><head><title>Error</title></head><body><h1>Error!</h1><h3>Error while logging the error {$_sMessage}</h3><h4><style 'font-color:red'>Application stoped!</font></h4></body></html>");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $_sMessage instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
472
    }
473
474
}
475