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

Ruleset::setSniffProperty()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 20
ccs 0
cts 16
cp 0
rs 8.8571
c 2
b 0
f 0
cc 5
eloc 11
nc 7
nop 3
crap 30
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;
9
10
use PHP_CodeSniffer\Sniffs\Sniff;
11
use Symplify\PHP7_CodeSniffer\Ruleset\RulesetBuilder;
12
13
final class Ruleset
14
{
15
    /**
16
     * @var RulesetBuilder
17
     */
18
    private $rulesetBuilder;
19
20
    public function __construct(RulesetBuilder $rulesetBuilder)
21
    {
22
        $this->rulesetBuilder = $rulesetBuilder;
23
    }
24
25
    /**
26
     * @param array|Sniff[] $sniffs
27
     */
28
    public function decorateSniffsWithCustomRules(array $sniffs)
29
    {
30
        // todo: put to SniffDispatcher on sniff loading?
31
        $ruleset = $this->rulesetBuilder->getRuleset();
32
33
        foreach ($sniffs as $sniffCode => $sniffObject) {
34
            if (!isset($ruleset[$sniffCode]['properties'])) {
35
                continue;
36
            }
37
38
            foreach ($ruleset[$sniffCode]['properties'] as $name => $value) {
39
                $this->setSniffProperty($sniffCode, $name, $value);
40
            }
41
        }
42
    }
43
44
    /**
45
     * @param string $sniffCode
46
     * @param string $name
47
     * @param string|array $value
48
     */
49
    private function setSniffProperty(string $sniffCode, string $name, $value)
50
    {
51
        if (isset($sniffs[$sniffCode]) === false) {
0 ignored issues
show
Bug introduced by
The variable $sniffs seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
52
            return;
53
        }
54
55
        $name = trim($name);
56
        if (is_string($value)) {
57
            $value = trim($value);
58
        }
59
60
        // Special case for booleans.
61
        if ($value === 'true') {
62
            $value = true;
63
        } elseif ($value === 'false') {
64
            $value = false;
65
        }
66
67
        $sniffs[$sniffCode]->$name = $value;
0 ignored issues
show
Bug introduced by
The variable $sniffs does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
68
    }
69
}
70