PluginConfigAnnotation::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 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\Inflector\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 14
    public function __construct(array $config = [])
22
    {
23 14
        $ref = new \ReflectionClass(get_class($this));
24 14
        $properties = $ref->getProperties();
25
26
        //if only one value is given, the first property is set with the given value
27 14
        if ($properties && isset($config['value']) && \count($config) === 1) {
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...
28 2
            $propName = $properties[0]->getName();
29 2
            $this->$propName = $config['value'];
30
        } else {
31 13
            foreach ($config as $key => $value) {
32 2
                $this->$key = $value;
33
            }
34
        }
35 14
    }
36
37
    /**
38
     * Must return the array with plugin config
39
     *
40
     * @return array
41
     */
42 16
    public function getConfig(): array
43
    {
44 16
        $ref = new \ReflectionClass(get_class($this));
45 16
        $properties = $ref->getProperties();
46 16
        $config = [];
47
48
        //set default values
49 16
        foreach ($properties as $property) {
50 16
            $value = $property->getValue($this);
51 16
            if (null !== $value) {
52 16
                $config[Inflector::tableize($property->getName())] = $property->getValue($this);
53
            }
54
        }
55
56 16
        return $config;
57
    }
58
59
    /**
60
     * Must return the plugin name to apply this config
61
     *
62
     * @return string
63
     */
64 16
    public function getName(): string
65
    {
66 16
        preg_match('/\w+$/', get_class($this), $matches);
67
68 16
        return Inflector::tableize($matches[0]);
69
    }
70
}
71