GetterTrait::createGetter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
7
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
9
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
10
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
11
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
12
 * THE SOFTWARE.
13
 *
14
 * This software consists of voluntary contributions made by many individuals
15
 * and is licensed under the MIT license.
16
 *
17
 * Copyright (c) 2018 Yuuki Takezawa
18
 */
19
20
namespace Ytake\Lom\Factory;
21
22
use PhpParser\Node\Expr\PropertyFetch;
23
use PhpParser\Node\Expr\Variable;
24
use PhpParser\Node\Stmt\ClassMethod;
25
use PhpParser\Node\Stmt\Return_;
26
use Ytake\Lom\Exception\TraitMethodCallException;
27
28
/**
29
 * Class GetterTrait.
30
 *
31
 * @author  yuuki.takezawa<[email protected]>
32
 * @license http://opensource.org/licenses/MIT MIT
33
 */
34
trait GetterTrait
35
{
36
    /** @var string[] */
37
    protected $getters = [];
38
39
    /**
40
     * @param string $name
41
     */
42
    protected function createGetter(string $name)
43
    {
44
        $this->getters[] = [
45
            'method'   => 'get' . ucfirst($name),
46
            'property' => $name,
47
        ];
48
    }
49
50
    /**
51
     * @return \string[]
52
     */
53
    protected function getGetters(): array
54
    {
55
        return $this->getters;
56
    }
57
58
    /**
59
     * @param array $getter
60
     *
61
     * @return ClassMethod
62
     */
63
    protected function createGetterMethod(array $getter): ClassMethod
64
    {
65
        if (!$this instanceof AbstractDriver) {
66
            throw new TraitMethodCallException("should extend " . AbstractDriver::class);
67
        }
68
        /** @var \PhpParser\BuilderFactory $builder */
69
        $builder = $this->builder;
0 ignored issues
show
Bug introduced by
The property builder cannot be accessed from this context as it is declared protected in class Ytake\Lom\Factory\AbstractDriver.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
70
71
        return $builder->method($getter['method'])
72
            ->setDocComment('')
73
            ->addStmt(
74
                new Return_(
75
                    new PropertyFetch(
76
                        new Variable('this'), $getter['property']
77
                    )
78
                )
79
            )->makePublic()->getNode();
80
    }
81
}
82