1 | <?php |
||||||||
2 | namespace SimpleLogger; |
||||||||
3 | |||||||||
4 | use SimpleLogger\Colours; |
||||||||
5 | use Psr\Log\AbstractLogger; |
||||||||
6 | |||||||||
7 | class SimpleLogger extends AbstractLogger |
||||||||
8 | { |
||||||||
9 | /** |
||||||||
10 | * Configuration class object. |
||||||||
11 | * |
||||||||
12 | * @var object |
||||||||
13 | */ |
||||||||
14 | private $configuration; |
||||||||
15 | |||||||||
16 | /** |
||||||||
17 | * Constructor. |
||||||||
18 | * |
||||||||
19 | * @param object $configuration Configuration class object |
||||||||
20 | * @return void |
||||||||
21 | */ |
||||||||
22 | public function __construct(Configuration $configuration = null) |
||||||||
23 | { |
||||||||
24 | $this->configuration = ($configuration instanceof Configuration) ? $configuration : new Configuration(); |
||||||||
25 | } |
||||||||
26 | |||||||||
27 | /** |
||||||||
28 | * Format log line output. |
||||||||
29 | * |
||||||||
30 | * @param string $level log level |
||||||||
31 | * @param string $message log message |
||||||||
32 | * @return void |
||||||||
33 | */ |
||||||||
34 | final protected function formatter($level, $message, $format) |
||||||||
35 | { |
||||||||
36 | $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
||||||||
37 | |||||||||
38 | $timestamp = date('c'); |
||||||||
39 | $class = isset($backtrace[3]['class']) ? $backtrace[3]['class'] : ""; |
||||||||
40 | $file = isset($backtrace[2]['file']) ? $backtrace[2]['file'] : ""; |
||||||||
41 | $function = isset($backtrace[3]['function']) ? $backtrace[3]['function'] : ""; |
||||||||
42 | $line = isset($backtrace[2]['line']) ? $backtrace[2]['line'] : ""; |
||||||||
43 | $pid = getmypid(); |
||||||||
44 | $tag = $this->configuration->getTag(); |
||||||||
45 | |||||||||
46 | $identifier = ['%t', '%c', '%f', '%F', '%l', '%L', '%p', '%m', '%T']; |
||||||||
47 | $values = [$timestamp, $class, $function, $file, strtoupper($level), $line, $pid, $message, $tag]; |
||||||||
48 | $format = str_replace($identifier, $values, $format); |
||||||||
49 | |||||||||
50 | return $format; |
||||||||
51 | } |
||||||||
52 | |||||||||
53 | /** |
||||||||
54 | * {@inheritdoc} |
||||||||
55 | */ |
||||||||
56 | public function log($level, $message, array $context = []) |
||||||||
57 | { |
||||||||
58 | $stdout = fopen('php://stdout', 'w'); |
||||||||
59 | if ($this->configuration->getLevel($level) >= $this->configuration->getVerbosity()) |
||||||||
60 | { |
||||||||
61 | if ($this->configuration->getLevel($level) >= $this->configuration->getConsoleVerbosity()) |
||||||||
62 | { |
||||||||
63 | $console_format = $this->configuration->getLoggerConsoleFormat(); |
||||||||
64 | $logline = $this->formatter($level, $message, $console_format); |
||||||||
0 ignored issues
–
show
|
|||||||||
65 | list($foreground, $background) = $this->configuration->getColours($level); |
||||||||
66 | fwrite($stdout, Colours::setColour($logline, $foreground, $background) . PHP_EOL); |
||||||||
0 ignored issues
–
show
$logline of type void is incompatible with the type string expected by parameter $string of SimpleLogger\Colours::setColour() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() The method
SimpleLogger\Colours::setColour() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() It seems like
$stdout can also be of type false ; however, parameter $handle of fwrite() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
67 | } |
||||||||
68 | |||||||||
69 | if (($logfile = $this->configuration->getLogfile()) !== null) |
||||||||
70 | { |
||||||||
71 | if (is_writable($logfile) === false) |
||||||||
72 | { |
||||||||
73 | throw new \RuntimeException("Log file {$logfile} is not writable."); |
||||||||
74 | } |
||||||||
75 | |||||||||
76 | $file_format = $this->configuration->getLoggerFileFormat(); |
||||||||
77 | $logline = $this->formatter($level, $message, $file_format); |
||||||||
0 ignored issues
–
show
Are you sure the assignment to
$logline is correct as $this->formatter($level, $message, $file_format) targeting SimpleLogger\SimpleLogger::formatter() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||||||
78 | $handle = fopen($logfile, "a+"); |
||||||||
79 | fwrite($handle, $logline . PHP_EOL); |
||||||||
0 ignored issues
–
show
Are you sure
$logline of type void can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
80 | fclose($handle); |
||||||||
0 ignored issues
–
show
It seems like
$handle can also be of type false ; however, parameter $handle of fclose() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
81 | } |
||||||||
82 | } |
||||||||
83 | } |
||||||||
84 | } |
||||||||
85 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.