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

SingleSniffFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 45
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A create() 0 9 2
A setCustomSniffPropertyValues() 0 11 3
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 14
    public function __construct(
27
        ExcludedSniffDataCollector $excludedSniffDataCollector,
28
        SniffPropertyValueDataCollector $customSniffPropertyDataCollector
29
    ) {
30 14
        $this->excludedSniffDataCollector = $excludedSniffDataCollector;
31 14
        $this->sniffPropertyValueDataCollector = $customSniffPropertyDataCollector;
32 14
    }
33
34
    /**
35
     * @return Sniff|null
36
     */
37 9
    public function create(string $sniffClassName)
38
    {
39 9
        if ($this->excludedSniffDataCollector->isSniffClassExcluded($sniffClassName)) {
40 1
            return null;
41
        }
42
43 8
        $sniff = new $sniffClassName;
44 8
        return $this->setCustomSniffPropertyValues($sniff);
45
    }
46
47 8
    private function setCustomSniffPropertyValues(Sniff $sniff) : Sniff
48
    {
49 8
        $sniffPropertyValues = $this->sniffPropertyValueDataCollector->getForSniff($sniff);
50 8
        if ($sniffPropertyValues) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sniffPropertyValues of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
51
            foreach ($sniffPropertyValues as $property => $value) {
52
                $sniff->$property = $value;
53
            }
54
        }
55
56 8
        return $sniff;
57
    }
58
}
59