Completed
Push — master ( 5e8208...81c8fa )
by Michael
01:45
created

SugaredRimConfigAwareTrait   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 2
dl 0
loc 96
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A sugaredRimSetFinderByConfig() 0 12 2
A sugaredRimGetConfig() 0 16 3
A processLongArgument() 0 10 2
B setCommandLineValues() 0 18 6
B sugaredRimProcessConfig() 0 16 6
A sugaredRimSetConfigData() 0 8 2
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
    protected 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
    protected 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
    protected 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
            $this->sugaredRimSetConfigData($key, $value, true);
92
        }
93
    }
94
95
    protected function sugaredRimSetConfigData($key, $value, $temp = false)
0 ignored issues
show
Unused Code introduced by
The parameter $temp is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
    {
97
        if (method_exists($this, 'setConfigData')) {
98
            $this->setConfigData($key, $value, true);
0 ignored issues
show
Bug introduced by
The method setConfigData() does not exist on SugaredRim\PHP\CodeSniff...aredRimConfigAwareTrait. Did you maybe mean sugaredRimSetConfigData()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
99
        } else {
100
            \PHP_CodeSniffer::setConfigData($key, $value, true);
101
        }
102
    }
103
}
104