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

SniffSetFactory::addSniffFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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