Completed
Push — master ( 1ff8f1...5e8208 )
by Michael
01:48
created

sugaredRimSetFinderByConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace SugaredRim\PHP\CodeSniffer;
4
5
use Schnittstabil\ComposerExtra\ComposerExtra;
6
use Schnittstabil\FinderByConfig\FinderByConfig;
7
8
trait SugaredRimConfigAwareTrait
9
{
10
    protected $sugaredRimNamespace = 'sugared-rim/php_codesniffer';
11
    protected $sugaredRimfinderByConfig;
12
    protected $sugaredRimDefaultConfig;
13
    protected $sugaredRimConfig;
14
15
    public function sugaredRimSetFinderByConfig(callable $finderByConfig = null)
16
    {
17
        if ($finderByConfig === null) {
18
            $finderByConfig = new FinderByConfig();
19
        }
20
        $this->sugaredRimfinderByConfig = $finderByConfig;
21
22
        $this->sugaredRimDefaultConfig = new \stdClass();
23
        $this->sugaredRimDefaultConfig->presets = [
24
            'SugaredRim\\PHP\\CodeSniffer\\DefaultPreset::get',
25
        ];
26
    }
27
28
    public function sugaredRimGetConfig($path = null, $default = null)
29
    {
30
        if ($this->sugaredRimConfig === null) {
31
            $this->sugaredRimConfig = new ComposerExtra(
32
                $this->sugaredRimNamespace,
33
                $this->sugaredRimDefaultConfig,
34
                'presets'
35
            );
36
        }
37
38
        if ($path === null) {
39
            $path = array();
40
        }
41
42
        return $this->sugaredRimConfig->get($path, $default);
43
    }
44
45
    public function processLongArgument($arg, $pos)
46
    {
47
        if (substr($arg, 0, 10) === 'namespace=') {
48
            $this->sugaredRimNamespace = substr($arg, 10);
49
50
            return;
51
        }
52
53
        parent::processLongArgument($arg, $pos);
54
    }
55
56
    public function setCommandLineValues($args)
57
    {
58
        parent::setCommandLineValues($args);
59
60
        if (isset($this->values) && !empty($this->values['files'])) {
0 ignored issues
show
Bug introduced by
The property values does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61
            // v2.*
62
            return;
63
        }elseif (isset($this->files) && !empty($this->files)) {
0 ignored issues
show
Bug introduced by
The property files does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
64
            // v3.*
65
            return;
66
        }
67
68
        $files = $this->sugaredRimGetConfig('files', []);
69
70
        foreach (call_user_func($this->sugaredRimfinderByConfig, $files) as $file) {
71
            $this->processUnknownArgument($file->getPathname(), -1);
0 ignored issues
show
Bug introduced by
It seems like processUnknownArgument() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
72
        }
73
    }
74
75
    /**
76
     * @SuppressWarnings(PHPMD.StaticAccess)
77
     */
78
    public function sugaredRimProcessConfig()
79
    {
80
        foreach ($this->sugaredRimGetConfig() as $key => $value) {
81
            switch ($key) {
82
                case 'presets':
83
                case 'files':
84
                    continue 2;
85
                case 'default_standard':
86
                    if (is_array($value)) {
87
                        $value = implode(',', $value);
88
                    }
89
            }
90
91
            if (method_exists($this, 'setConfigData')) {
92
                $this->setConfigData($key, $value, true);
0 ignored issues
show
Bug introduced by
It seems like setConfigData() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
93
            } else {
94
                \PHP_CodeSniffer::setConfigData($key, $value, true);
95
            }
96
        }
97
    }
98
}
99