1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Symplify |
5
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Symplify\PHP7_CodeSniffer\EventDispatcher; |
9
|
|
|
|
10
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
11
|
|
|
use Symfony\Component\EventDispatcher\Event; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
13
|
|
|
use Symplify\PHP7_CodeSniffer\Event\CheckFileTokenEvent; |
14
|
|
|
|
15
|
|
|
final class SniffDispatcher extends EventDispatcher |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var CurrentListenerSniffCodeProvider |
19
|
|
|
*/ |
20
|
|
|
private $currentListenerSniffCodeProvider; |
21
|
|
|
|
22
|
|
|
public function __construct(CurrentListenerSniffCodeProvider $currentListenerSniffCodeProvider) |
23
|
|
|
{ |
24
|
|
|
$this->currentListenerSniffCodeProvider = $currentListenerSniffCodeProvider; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array|string[] $sniffs |
29
|
|
|
*/ |
30
|
|
|
public function addSniffListeners(array $sniffs) |
31
|
|
|
{ |
32
|
|
|
foreach ($sniffs as $sniffCode => $sniffObject) { |
33
|
|
|
$tokens = $sniffs[$sniffCode]->register(); |
34
|
|
|
foreach ($tokens as $token) { |
35
|
|
|
$this->addTokenSniffListener($token, $sniffObject); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
protected function doDispatch($listeners, $eventName, Event $event) |
44
|
|
|
{ |
45
|
|
|
foreach ($listeners as $listener) { |
46
|
|
|
if ($event->isPropagationStopped()) { |
47
|
|
|
break; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->currentListenerSniffCodeProvider->setCurrentListener($listener); |
51
|
|
|
call_user_func($listener, $event, $eventName, $this); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function addTokenSniffListener(string $token, Sniff $sniffObject) |
56
|
|
|
{ |
57
|
|
|
$this->addListener( |
58
|
|
|
$token, |
59
|
|
|
function (CheckFileTokenEvent $checkFileToken) use ($sniffObject) { |
60
|
|
|
$sniffObject->process( |
61
|
|
|
$checkFileToken->getFile(), |
|
|
|
|
62
|
|
|
$checkFileToken->getStackPointer() |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.