Completed
Push — master ( 393329...74ff7b )
by Tomáš
10:49
created

SniffSetFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 64
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addSniffFactory() 0 7 2
A createFromStandardsAndSniffs() 0 11 1
A create() 0 15 4
A toArray() 0 8 2
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\Sniff;
9
10
use PHP_CodeSniffer\Sniffs\Sniff;
11
use Symplify\PHP7_CodeSniffer\Contract\Sniff\Factory\SniffFactoryInterface;
12
use Symplify\PHP7_CodeSniffer\Contract\Sniff\SniffSetFactoryAwareInterface;
13
use Symplify\PHP7_CodeSniffer\Sniff\Sorter\SniffSorter;
14
15
final class SniffSetFactory
16
{
17
    /**
18
     * @var SniffFactoryInterface[]
19
     */
20
    private $sniffFactories;
21
22 10
    public function addSniffFactory(SniffFactoryInterface $sniffFactory)
23
    {
24 10
        $this->sniffFactories[] = $sniffFactory;
25 10
        if ($sniffFactory instanceof SniffSetFactoryAwareInterface) {
26 10
            $sniffFactory->setSniffSetFactory($this);
27
        }
28 10
    }
29
30
    /**
31
     * @param string[] $standardNames
32
     * @param string[] $sniffCodes
33
     * @return Sniff[]
34
     */
35 5
    public function createFromStandardsAndSniffs(
36
        array $standardNames,
37
        array $sniffCodes
38
    ) : array {
39 5
        $sniffs = array_merge(
40 5
            $this->create($standardNames),
41 5
            $this->create($sniffCodes)
42
        );
43
44 5
        return SniffSorter::sort($sniffs);
45
    }
46
47
    /**
48
     * @param string|array $source
49
     * @return Sniff[]
50
     */
51 8
    public function create($source) : array
52
    {
53 8
        $sources = $this->toArray($source);
54
55 8
        $sniffs = [];
56 8
        foreach ($this->sniffFactories as $sniffFactory) {
57 8
            foreach ($sources as $source) {
58 6
                if ($sniffFactory->isMatch($source)) {
59 8
                    $sniffs = array_merge($sniffs, $sniffFactory->create($source));
60
                }
61
            }
62
        }
63
64 8
        return $sniffs;
65
    }
66
67
    /**
68
     * @param string|array $source
69
     */
70 8
    private function toArray($source) : array
71
    {
72 8
        if (is_array($source)) {
73 5
            return $source;
74
        }
75
76 6
        return [$source];
77
    }
78
}
79