AllArgsConstructorDriver::generator()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 11

Duplication

Lines 8
Ratio 38.1 %

Importance

Changes 0
Metric Value
dl 8
loc 21
rs 8.7624
c 0
b 0
f 0
cc 6
eloc 11
nc 6
nop 0
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 AllArgsConstructorDriver.
28
 *
29
 * @author  yuuki.takezawa<[email protected]>
30
 * @license http://opensource.org/licenses/MIT MIT
31
 */
32
class AllArgsConstructorDriver extends AbstractDriver
33
{
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function generator(): array
38
    {
39
        foreach ($this->parsed as $part) {
40
            // auto generate for constructor
41
            if ($part instanceof Class_) {
42
                // remove constructor
43
                $this->removeConstructor($part);
44
                $part->stmts[] = $this->createConstructor();
45 View Code Duplication
                foreach ($part->stmts as $key => $statement) {
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...
46
                    if ($statement instanceof ClassMethod) {
47
                        if ('__construct' !== $statement->name) {
48
                            unset($part->stmts[$key]);
49
                            $part->stmts[] = $statement;
50
                        }
51
                    }
52
                }
53
            }
54
        }
55
56
        return $this->parsed;
57
    }
58
59
    /**
60
     * @return ClassMethod
61
     */
62
    protected function createConstructor(): ClassMethod
63
    {
64
        $properties = [];
65
        $sets = [];
66
        foreach ($this->reflector->getProperties() as $property) {
67
            $properties[] = $this->builder->param($property->getName());
68
            $sets[] = new Class_(
69
                sprintf(Constants::SETTER_FORMAT, $property->getName(), $property->getName())
70
            );
71
        }
72
        $detectAccessLevel = $this->setAccessLevel();
73
74
        return $this->builder->method('__construct')
75
            ->setDocComment("\n/**
76
                              *
77
                              */")
78
            ->addParams($properties)->$detectAccessLevel()
79
            ->addStmts($sets)
80
            ->getNode();
81
    }
82
}
83