DataDriver::createSetterMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
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 DataDriver.
28
 *
29
 * @author  yuuki.takezawa<[email protected]>
30
 * @license http://opensource.org/licenses/MIT MIT
31
 */
32
class DataDriver extends AbstractDriver
33
{
34
    // getter generator and setter generator
35
    use GetterTrait, SetterTrait, ToStringTrait;
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function generator(): array
41
    {
42
        foreach ($this->reflector->getProperties() as $property) {
43
            $name = $property->getName();
44
            $this->createGetter($name);
45
            $this->createSetter($name);
46
        }
47
        foreach ($this->parsed as $part) {
48
            if ($part instanceof Class_) {
49 View Code Duplication
                foreach ($this->getGetters() as $getter) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
50
                    if ($this->reflector->hasMethod($getter['method'])) {
51
                        continue;
52
                    }
53
                    $part->stmts[] = $this->createGetterMethod($getter);
54
                }
55
                foreach ($this->getSetters() as $setter) {
56
                    if ($this->reflector->hasMethod($setter['method'])) {
57
                        continue;
58
                    }
59
                    $part->stmts[] = $this->createSetterMethod($setter);
60
                }
61
                if (!$this->reflector->hasMethod('__toString')) {
62
                    $part->stmts[] = $this->createToString($this->getGetters());
63
                }
64
            }
65
        }
66
67
        return $this->parsed;
68
    }
69
70
    /**
71
     * @param array $setter
72
     *
73
     * @return ClassMethod
74
     */
75 View Code Duplication
    protected function createSetterMethod(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...
76
    {
77
        return $this->builder->method($setter['method'])
78
            ->setDocComment('')
79
            ->addParam($this->builder->param($setter['property']))
80
            ->addStmt(
81
                new Class_(
82
                    sprintf(Constants::SETTER_FORMAT, $setter['property'], $setter['property'])
83
                )
84
            )->makePublic()->getNode();
85
    }
86
}
87