Completed
Push — develop ( 4b49c4...89d32a )
by Jaap
09:06 queued 05:30
created

Parser/Middleware/ErrorHandlingMiddleware.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 *  For the full copyright and license information, please view the LICENSE
6
 *  file that was distributed with this source code.
7
 *
8
 *  @copyright 2010-2017 Mike van Riel<[email protected]>
9
 *  @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 *  @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Parser\Middleware;
14
15
use Exception;
16
use phpDocumentor\Event\Dispatcher;
17
use phpDocumentor\Event\LogEvent;
18
use phpDocumentor\Reflection\Middleware\Middleware;
19
use Psr\Log\LogLevel;
20
21
final class ErrorHandlingMiddleware implements Middleware
22
{
23
    /**
24
     * Executes this middle ware class.
25
     *
26
     * @param $command
27
     * @param callable $next
28
     *
29
     * @return object
30
     */
31
    public function execute($command, callable $next)
32
    {
33
        $filename = $command->getFile()->path();
34
        $this->log('Starting to parse file: ' . $filename);
35
36
        try {
37
            return $next($command);
38
        } catch (Exception $e) {
39
            $this->log(
40
                '  Unable to parse file "' . $filename . '", an error was detected: ' . $e->getMessage(),
41
                LogLevel::ALERT
42
            );
43
        }
44
45
        return null;
46
    }
47
48
49
    /**
50
     * Dispatches a logging request.
51
     *
52
     * @param string   $message  The message to log.
53
     * @param string   $priority The logging priority as declared in the LogLevel PSR-3 class.
54
     * @param string[] $parameters
55
     *
56
     * @return void
57
     */
58
    private function log($message, $priority = LogLevel::INFO, $parameters = array())
59
    {
60
        Dispatcher::getInstance()->dispatch(
61
            'system.log',
62
            LogEvent::createInstance($this)
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class phpDocumentor\Event\DebugEvent as the method setPriority() does only exist in the following sub-classes of phpDocumentor\Event\DebugEvent: phpDocumentor\Event\LogEvent. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
63
                ->setContext($parameters)
64
                ->setMessage($message)
65
                ->setPriority($priority)
66
        );
67
    }
68
}
69