Issues (9)

src/Log.php (1 issue)

1
<?php
2
3
namespace Nilnice\Payment;
4
5
use Monolog\Formatter\LineFormatter;
6
use Monolog\Handler\RotatingFileHandler;
7
use Monolog\Logger;
8
use Psr\Log\LoggerInterface;
9
10
/**
11
 * @method static Logger getLevels()
12
 * @method static Logger getLevelName($level)
13
 * @method static Logger toMonologLevel($level)
14
 * @method static Logger log($level, $message, array $context = [])
15
 * @method static Logger debug($message, array $context = [])
16
 * @method static Logger info($message, array $context = [])
17
 * @method static Logger notice($message, array $context = [])
18
 * @method static Logger warn($message, array $context = [])
19
 * @method static Logger warning($message, array $context = [])
20
 * @method static Logger err($message, array $context = [])
21
 * @method static Logger error($message, array $context = [])
22
 * @method static Logger crit($message, array $context = [])
23
 * @method static Logger critical($message, array $context = [])
24
 * @method static Logger alert($message, array $context = [])
25
 * @method static Logger emerg($message, array $context = [])
26
 * @method static Logger emergency($message, array $context = [])
27
 */
28
class Log
29
{
30
    /**
31
     * @var \Monolog\Logger
32
     */
33
    protected static $logger;
34
35
    /**
36
     * @param \Psr\Log\LoggerInterface $logger
37
     */
38
    public static function setLogger(LoggerInterface $logger) : void
39
    {
40
        self::$logger = $logger;
0 ignored issues
show
Documentation Bug introduced by
$logger is of type Psr\Log\LoggerInterface, but the property $logger was declared to be of type Monolog\Logger. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
41
    }
42
43
    /**
44
     * @return \Monolog\Logger
45
     */
46 6
    public static function getLogger() : Logger
47
    {
48 6
        return self::$logger ?? self::$logger = self::getLoggerInstance();
49
    }
50
51
    /**
52
     * @param string $method
53
     * @param mixed  $arguments
54
     *
55
     * @return mixed
56
     */
57 6
    public static function __callStatic(string $method, $arguments)
58
    {
59 6
        $function = [self::getLogger(), $method];
60
61 6
        return \forward_static_call_array($function, $arguments);
62
    }
63
64
    /**
65
     * @param string $method
66
     * @param mixed  $arguments
67
     *
68
     * @return mixed
69
     */
70
    public function __call(string $method, $arguments)
71
    {
72
        $function = [self::getLogger(), $method];
73
74
        return \call_user_func_array($function, $arguments);
75
    }
76
77
    /**
78
     * @return \Monolog\Logger
79
     */
80 6
    protected static function getLoggerInstance() : Logger
81
    {
82 6
        $maxFiles = 7;
83 6
        $filename = sys_get_temp_dir() . '/logs/payment.log';
84 6
        $handler = new RotatingFileHandler($filename, $maxFiles);
85 6
        $handler->setFilenameFormat('{date}-{filename}', 'Y-m-d');
86 6
        $formatter = new LineFormatter(null, null, true, true);
87 6
        $handler->setFormatter($formatter);
88
89 6
        $logger = new Logger('monolog');
90 6
        $logger->pushHandler($handler);
91
92 6
        return $logger;
93
    }
94
}
95