AbstractConfigurableSniff::process()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6

Importance

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