PropertyReference::createPropertyMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
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\Stmt\Class_;
23
use PhpParser\Node\Stmt\ClassMethod;
24
use Ytake\Lom\Constants;
25
26
/**
27
 * Class PropertyReference.
28
 *
29
 * @author yuuki.takezawa<[email protected]>
30
 */
31
abstract class PropertyReference extends AbstractDriver
32
{
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function generator(): array
37
    {
38
        foreach ($this->parsed as $part) {
39
            if ($part instanceof Class_) {
40
                $methodName = $this->resolveMethodName();
41
                $this->removeMethod($part, $methodName);
42
                $part->stmts[] = $this->createPropertyMethod([
43
                    'method' => $methodName,
44
                    'property' => $this->property->getName(),
45
                ]);
46
            }
47
        }
48
49
        return $this->parsed;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    abstract protected function resolveMethodName(): string;
56
57
    /**
58
     * @param array $setter
59
     *
60
     * @return ClassMethod
61
     */
62 View Code Duplication
    protected function createPropertyMethod(array $setter): ClassMethod
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64
        $detectAccessLevel = $this->setAccessLevel();
65
66
        return $this->builder->method($setter['method'])
67
            ->setDocComment('')
68
            ->addParam($this->builder->param($setter['property']))
69
            ->addStmt(
70
                new \PhpParser\Node\Stmt\Class_(
71
                    sprintf(Constants::SETTER_FORMAT, $setter['property'], $setter['property'])
72
                )
73
            )->$detectAccessLevel()->getNode();
74
    }
75
}
76