Completed
Branch master (f7eb77)
by Tomáš
03:26
created

Ruleset::decorateSniffsWithCustomRules()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 15
ccs 0
cts 12
cp 0
rs 9.2
c 1
b 0
f 0
cc 4
eloc 7
nc 3
nop 1
crap 20
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\Ruleset;
9
10
use PHP_CodeSniffer\Sniffs\Sniff;
11
12
final class Ruleset
13
{
14
    /**
15
     * @var RulesetBuilder
16
     */
17
    private $rulesetBuilder;
18
19
    public function __construct(RulesetBuilder $rulesetBuilder)
20
    {
21
        $this->rulesetBuilder = $rulesetBuilder;
22
    }
23
24
    /**
25
     * @param array|Sniff[] $sniffs
26
     */
27
    public function decorateSniffsWithCustomRules(array $sniffs)
28
    {
29
        // todo: put to SniffDispatcher on sniff loading?
30
        $ruleset = $this->rulesetBuilder->getRuleset();
31
32
        foreach ($sniffs as $sniffCode => $sniffObject) {
33
            if (!isset($ruleset[$sniffCode]['properties'])) {
34
                continue;
35
            }
36
37
            foreach ($ruleset[$sniffCode]['properties'] as $name => $value) {
38
                $this->setSniffProperty($sniffCode, $name, $value);
39
            }
40
        }
41
    }
42
43
    /**
44
     * @param string $sniffCode
45
     * @param string $name
46
     * @param string|array $value
47
     */
48
    private function setSniffProperty(string $sniffCode, string $name, $value)
49
    {
50
        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...
51
            return;
52
        }
53
54
        $name = trim($name);
55
        if (is_string($value)) {
56
            $value = trim($value);
57
        }
58
59
        // Special case for booleans.
60
        if ($value === 'true') {
61
            $value = true;
62
        } elseif ($value === 'false') {
63
            $value = false;
64
        }
65
66
        $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...
67
    }
68
}
69