Completed
Branch develop (ab1b2a)
by Steve
07:43
created

NamespaceFieldKey::getBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
nc 1
cc 1
eloc 2
nop 0
1
<?php
2
3
namespace StoutLogic\AcfBuilder\Transform;
4
5
/**
6
 * Namespace a field key by appending the namespace consisting of 'field'
7
 * and the Builder's name before the defined key.
8
 *
9
 * Ensure all lowercase.
10
 */
11
class NamespaceFieldKey extends RecursiveTransform
12
{
13
    protected $keys = ['key', 'field'];
14
15
    /**
16
     * @param \StoutLogic\AcfBuilder\NamedBuilder $builder
17
     */
18
    public function __construct(\StoutLogic\AcfBuilder\NamedBuilder $builder)
19
    {
20
        parent::__construct($builder);
0 ignored issues
show
Documentation introduced by
$builder is of type object<StoutLogic\AcfBuilder\NamedBuilder>, but the function expects a object<StoutLogic\AcfBuilder\Builder>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
21
    }
22
23
    /**
24
     * @return \StoutLogic\AcfBuilder\NamedBuilder
25
     */
26
    public function getBuilder()
27
    {
28
        return parent::getBuilder();
29
    }
30
31
    /**
32
     * @param  string $value Key
33
     * @return string Namedspaced key
34
     */
35
    public function transformValue($value)
36
    {
37
        $namespace = 'field_';
38
        $groupName = $this->getBuilder()->getName();
0 ignored issues
show
Documentation Bug introduced by
The method getName does not exist on object<StoutLogic\AcfBuilder\Builder>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
39
40
        if ($groupName) {
41
            // remove field_ or group_ if already at the begining of the key
42
            $value = preg_replace('/^field_|^group_/', '', $value);
43
            $namespace .= str_replace(' ', '_', $groupName).'_';
44
        }
45
        return strtolower($namespace.$value);
46
    }
47
}
48