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
|
|||
63 | ->setContext($parameters) |
||
64 | ->setMessage($message) |
||
65 | ->setPriority($priority) |
||
66 | ); |
||
67 | } |
||
68 | } |
||
69 |
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: