Completed
Push — master ( 368282...c4d7dc )
by Gytis
03:56
created

AbstractConfigurableSniff::process()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 4
nop 2
dl 0
loc 31
ccs 0
cts 19
cp 0
crap 42
rs 9.0444
c 0
b 0
f 0
1
<?php
2
3
namespace Gskema\TypeSniff\Sniffs;
4
5
use PHP_CodeSniffer\Files\File;
6
use PHP_CodeSniffer\Sniffs\Sniff;
7
8
abstract class AbstractConfigurableSniff implements Sniff
9
{
10
    /** @var bool */
11
    protected $configured = false;
12
13
    /**
14
     * @inheritDoc
15
     */
16
    public function process(File $file, $ptr): void
17
    {
18
        // File path sniffs and not saved using sniff code and their config
19
        // is not processed. This is a workaround.
20
        if (!$this->configured) {
21
            $bits = explode('\\', get_class($this));
22
            $currentClass = end($bits);
23
24
            $opts = [];
25
            foreach ($file->ruleset->ruleset as $ref => $rule) {
26
                $bits = explode('/', $ref);
27
                $cfgClass = rtrim(end($bits), '.php');
28
                if ($currentClass === $cfgClass) {
29
                    $opts = $rule['properties'];
30
                    break;
31
                }
32
            }
33
34
            array_walk_recursive($opts, function (&$val) {
35
                if ('true' === $val) {
36
                    $val = true;
37
                } elseif ('false' === $val) {
38
                    $val = false;
39
                }
40
            });
41
42
            $this->configure($opts);
43
            $this->configured = true;
44
        }
45
46
        $this->run($file, $ptr);
47
    }
48
49
    /**
50
     * @param mixed[] $config
51
     */
52
    abstract protected function configure(array $config): void;
53
54
    abstract protected function run(File $file, int $ptr): void;
55
}
56