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

AbstractConfigurableSniff   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 47
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 31 6
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