Completed
Push — master ( e8a810...7ca800 )
by Andrew
07:51 queued 05:38
created

MonologResetter::resetHandler()   C

Complexity

Conditions 7
Paths 13

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 20
cts 20
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 13
nop 1
crap 7
1
<?php
2
3
namespace AndrewCarterUK\NoMoreLeaksBundle\Resetter;
4
5
use AndrewCarterUK\NoMoreLeaksBundle\Util;
6
use Monolog\Logger;
7
use Monolog\Handler\HandlerInterface;
8
9
class MonologResetter implements ResetterInterface
10
{
11
    /**
12
     * @var Logger
13
     */
14
    private $logger;
15
16
    /**
17
     * Constructor.
18
     * 
19
     * @param Logger $logger
20
     */
21 2
    public function __construct(Logger $logger)
22
    {
23 2
        $this->logger = $logger;
24 2
    }
25
26
    /**
27
     * {@inheritDoc}
28
     */
29 2
    public function reset()
30
    {
31 2
        $handlers = $this->logger->getHandlers();
32 2
        $this->resetHandlers($handlers);
33 2
    }
34
35 2
    private function resetHandlers(array $handlers)
36
    {
37 2
        foreach($handlers as $handler) {
38 2
            $this->resetHandler($handler);
39 2
        }
40 2
    }
41
42 2
    private function resetHandler(HandlerInterface $handler)
43
    {
44 2
        $class = get_class($handler);
45
46 2
        $prefix = 'Monolog\\Handler\\';
47
48 2
        if (substr($class, 0, strlen($prefix)) == $prefix) {
49 2
            $handlerName = substr($class, strlen($prefix));
50
51 2
            if ('FingersCrossedHandler' == $handlerName) {
52 1
                $handler->clear();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Monolog\Handler\HandlerInterface as the method clear() does only exist in the following implementations of said interface: Monolog\Handler\BufferHandler, Monolog\Handler\DeduplicationHandler, Monolog\Handler\ExceptionTestHandler, Monolog\Handler\FingersCrossedHandler, Monolog\Handler\TestHandler.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
53 2
            } elseif ('BufferHandler' == $handlerName || 'StreamHandler' == $handlerName) {
54 1
                $handler->close();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Monolog\Handler\HandlerInterface as the method close() does only exist in the following implementations of said interface: Monolog\Handler\AbstractHandler, Monolog\Handler\AbstractProcessingHandler, Monolog\Handler\AbstractSyslogHandler, Monolog\Handler\AmqpHandler, Monolog\Handler\BrowserConsoleHandler, Monolog\Handler\BufferHandler, Monolog\Handler\ChromePHPHandler, Monolog\Handler\CouchDBHandler, Monolog\Handler\CubeHandler, Monolog\Handler\DeduplicationHandler, Monolog\Handler\DoctrineCouchDBHandler, Monolog\Handler\DynamoDbHandler, Monolog\Handler\ElasticSearchHandler, Monolog\Handler\ErrorLogHandler, Monolog\Handler\ExceptionTestHandler, Monolog\Handler\FilterHandler, Monolog\Handler\FingersCrossedHandler, Monolog\Handler\FirePHPHandler, Monolog\Handler\FleepHookHandler, Monolog\Handler\FlowdockHandler, Monolog\Handler\GelfHandler, Monolog\Handler\GroupHandler, Monolog\Handler\HipChatHandler, Monolog\Handler\IFTTTHandler, Monolog\Handler\LogEntriesHandler, Monolog\Handler\LogglyHandler, Monolog\Handler\MailHandler, Monolog\Handler\MandrillHandler, Monolog\Handler\MongoDBHandler, Monolog\Handler\NativeMailerHandler, Monolog\Handler\NewRelicHandler, Monolog\Handler\NullHandler, Monolog\Handler\PHPConsoleHandler, Monolog\Handler\PsrHandler, Monolog\Handler\PushoverHandler, Monolog\Handler\RavenHandler, Monolog\Handler\RedisHandler, Monolog\Handler\RollbarHandler, Monolog\Handler\RotatingFileHandler, Monolog\Handler\SamplingHandler, Monolog\Handler\SlackHandler, Monolog\Handler\SlackWebhookHandler, Monolog\Handler\SlackbotHandler, Monolog\Handler\SocketHandler, Monolog\Handler\StreamHandler, Monolog\Handler\StubNewRelicHandler, Monolog\Handler\StubNewR...HandlerWithoutExtension, Monolog\Handler\SwiftMailerHandler, Monolog\Handler\SyslogHandler, Monolog\Handler\SyslogUdpHandler, Monolog\Handler\TestChromePHPHandler, Monolog\Handler\TestFirePHPHandler, Monolog\Handler\TestHandler, Monolog\Handler\WhatFailureGroupHandler, Monolog\Handler\ZendMonitorHandler.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
55 1
            }
56
57 2
            if (property_exists($handler, 'handler')) {
58 2
                $subHandler = Util::readProperty($handler, 'handler');
59 2
                $this->resetHandler($subHandler);
60 2
            }
61
62 2
            if (property_exists($handler, 'handlers')) {
63 1
                $subHandlers = Util::readProperty($handler, 'handlers');
64 1
                $this->resetHandlers($subHandlers);
65 1
            }
66 2
        }
67 2
    }
68
}
69