Completed
Push — master ( 74ff7b...2fd8e1 )
by Tomáš
03:25
created

SingleSniffFactory::setCustomSniffPropertyValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
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\Factory;
9
10
use PHP_CodeSniffer\Sniffs\Sniff;
11
use Symplify\PHP7_CodeSniffer\Sniff\Xml\DataCollector\SniffPropertyValueDataCollector;
12
use Symplify\PHP7_CodeSniffer\Sniff\Xml\DataCollector\ExcludedSniffDataCollector;
13
14
final class SingleSniffFactory
15
{
16
    /**
17
     * @var ExcludedSniffDataCollector
18
     */
19
    private $excludedSniffDataCollector;
20
21
    /**
22
     * @var SniffPropertyValueDataCollector
23
     */
24
    private $sniffPropertyValueDataCollector;
25
26 15
    public function __construct(
27
        ExcludedSniffDataCollector $excludedSniffDataCollector,
28
        SniffPropertyValueDataCollector $customSniffPropertyDataCollector
29
    ) {
30 15
        $this->excludedSniffDataCollector = $excludedSniffDataCollector;
31 15
        $this->sniffPropertyValueDataCollector = $customSniffPropertyDataCollector;
32 15
    }
33
34
    /**
35
     * @return Sniff|null
36
     */
37 10
    public function create(string $sniffClassName)
38
    {
39 10
        if ($this->excludedSniffDataCollector->isSniffClassExcluded($sniffClassName)) {
40 1
            return null;
41
        }
42
43 9
        $sniff = new $sniffClassName;
44 9
        return $this->setCustomSniffPropertyValues($sniff);
45
    }
46
47 9
    private function setCustomSniffPropertyValues(Sniff $sniff) : Sniff
48
    {
49 9
        $sniffPropertyValues = $this->sniffPropertyValueDataCollector->getForSniff($sniff);
50 9
        foreach ($sniffPropertyValues as $property => $value) {
51 1
            $sniff->$property = $value;
52
        }
53
54 9
        return $sniff;
55
    }
56
}
57