AllArgsConstructorDriver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 15.69 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 8
loc 51
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B generator() 8 21 6
A createConstructor() 0 20 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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