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

SniffDispatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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