Completed
Branch master (aa8164)
by Tomáš
04:04
created

SniffDispatcher::doDispatch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 6
nc 3
nop 3
crap 3.0261
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\EventDispatcher\Event\CheckFileTokenEvent;
14
15
final class SniffDispatcher extends EventDispatcher
16
{
17
    /**
18
     * @var CurrentListenerSniffCodeProvider
19
     */
20
    private $currentListenerSniffCodeProvider;
21
22 2
    public function __construct(CurrentListenerSniffCodeProvider $currentListenerSniffCodeProvider)
23
    {
24 2
        $this->currentListenerSniffCodeProvider = $currentListenerSniffCodeProvider;
25 2
    }
26
27
    /**
28
     * @param Sniff[] $sniffs
29
     */
30 2
    public function addSniffListeners(array $sniffs)
31
    {
32 2
        foreach ($sniffs as $sniffCode => $sniffObject) {
33 2
            $tokens = $sniffs[$sniffCode]->register();
34 2
            foreach ($tokens as $token) {
35 2
                $this->addTokenSniffListener($token, $sniffObject);
36
            }
37
        }
38 2
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 1
    protected function doDispatch($listeners, $eventName, Event $event)
44
    {
45 1
        foreach ($listeners as $listener) {
46 1
            if ($event->isPropagationStopped()) {
47
                break;
48
            }
49
50 1
            $this->currentListenerSniffCodeProvider->setCurrentListener($listener);
51 1
            call_user_func($listener, $event, $eventName, $this);
52
        }
53 1
    }
54
55 2
    private function addTokenSniffListener(string $token, Sniff $sniffObject)
56
    {
57 2
        $this->addListener(
58
            $token,
59 2
            function (CheckFileTokenEvent $checkFileToken) use ($sniffObject) {
60 1
                $sniffObject->process(
61 1
                    $checkFileToken->getFile(),
62 1
                    $checkFileToken->getStackPointer()
63
                );
64 2
            }
65
        );
66 2
    }
67
}
68