Completed
Branch master (06cb84)
by Tomáš
06:00
created

SniffDispatcher   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 53
ccs 0
cts 35
cp 0
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\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(),
0 ignored issues
show
Bug introduced by
It seems like $checkFileToken->getFile() targeting Symplify\PHP7_CodeSniffe...leTokenEvent::getFile() can also be of type object<Symplify\PHP7_Cod...act\File\FileInterface>; however, PHP_CodeSniffer\Sniffs\Sniff::process() does only seem to accept object<PHP_CodeSniffer\Files\File>, maybe add an additional type check?

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.

Loading history...
62
                    $checkFileToken->getStackPointer()
63
                );
64
            }
65
        );
66
    }
67
}
68