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

SniffDispatcher   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 95.83%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 53
ccs 23
cts 24
cp 0.9583
rs 10
c 2
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addSniffListeners() 0 9 3
A doDispatch() 0 11 3
A addTokenSniffListener() 0 12 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