Passed
Push — master ( 46c3ee...445208 )
by Rafael
04:28
created

PluginConfigAnnotation   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 51
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 5 1
B __construct() 0 25 7
A getConfig() 0 3 1
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Annotation\Plugin;
12
13
use Doctrine\Common\Util\Inflector;
14
15
/**
16
 * Can use this annotation as base to se plugin options.
17
 * Override and define all plugin settings.
18
 */
19
abstract class PluginConfigAnnotation
20
{
21 3
    public function __construct(array $config = [])
22
    {
23 3
        $ref = new \ReflectionClass(get_class($this));
24 3
        $properties = $ref->getProperties();
25
26
        //set default values
27 3
        foreach ($properties as $property) {
28 3
            $property->setAccessible(true);
29 3
            $value = $property->getValue($this);
30 3
            if (null !== $value) {
31 3
                $this->config[Inflector::tableize($property->getName())] = $property->getValue($this);
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32
            }
33
        }
34
35
        //if only one value is given, the first property is set with the given value
36 3
        if (isset($config['value']) && count($config) === 1) {
37 1
            if ($properties) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $properties of type ReflectionProperty[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
38 1
                $propName = $ref->getProperties()[0]->getName();
39 1
                $this->$propName = $config['value'];
40 1
                $this->config[Inflector::tableize($propName)] = $config['value'];
41
            }
42
        } else {
43 2
            foreach ($config as $key => $value) {
44
                //dynamically added to avoid ide auto-complete for this property
45 2
                $this->config[Inflector::tableize($key)] = $value;
46
            }
47
        }
48 3
    }
49
50
    /**
51
     * Must return the array with plugin config
52
     *
53
     * @return array
54
     */
55 12
    public function getConfig(): array
56
    {
57 12
        return $this->config ?? [];
58
    }
59
60
    /**
61
     * Must return the plugin name to apply this config
62
     *
63
     * @return string
64
     */
65 12
    public function getName(): string
66
    {
67 12
        preg_match('/\w+$/', get_class($this), $matches);
68
69 12
        return Inflector::tableize($matches[0]);
70
    }
71
}
72