AbstractDriver::removeMethod()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 7
Ratio 70 %

Importance

Changes 0
Metric Value
dl 7
loc 10
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 4
nop 2
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
11
 * THE SOFTWARE.
12
 *
13
 * This software consists of voluntary contributions made by many individuals
14
 * and is licensed under the MIT license.
15
 *
16
 * Copyright (c) 2018 Yuuki Takezawa
17
 */
18
19
namespace Ytake\Lom\Factory;
20
21
use PhpParser\BuilderFactory;
22
use PhpParser\Node\Stmt\Class_;
23
use PhpParser\Node\Stmt\ClassMethod;
24
use ReflectionClass;
25
use ReflectionMethod;
26
use ReflectionProperty;
27
use Ytake\Lom\Access;
28
29
/**
30
 * Class AbstractDriver.
31
 *
32
 * @author  yuuki.takezawa<[email protected]>
33
 * @license http://opensource.org/licenses/MIT MIT
34
 */
35
abstract class AbstractDriver implements FactoryInterface
36
{
37
    /** @var ReflectionClass */
38
    protected $reflector;
39
40
    /** @var array */
41
    protected $parsed = [];
42
43
    /** @var BuilderFactory */
44
    protected $builder;
45
46
    /** @var */
47
    protected $annotation;
48
49
    /** @var ReflectionProperty */
50
    protected $property;
51
52
    /** @var ReflectionMethod */
53
    protected $method;
54
55
    /**
56
     * @param array          $parsed
57
     * @param BuilderFactory $builder
58
     */
59
    public function __construct(array $parsed, BuilderFactory $builder)
60
    {
61
        $this->parsed = $parsed;
62
        $this->builder = $builder;
63
    }
64
65
    /**
66
     * set ReflectionClass.
67
     *
68
     * @param ReflectionClass $reflection
69
     *
70
     * @return $this
71
     */
72
    public function setReflector(ReflectionClass $reflection): AbstractDriver
73
    {
74
        $this->reflector = $reflection;
75
76
        return $this;
77
    }
78
79
    /**
80
     * @param $annotation
81
     *
82
     * @return $this
83
     */
84
    public function setAnnotationInstance($annotation): AbstractDriver
85
    {
86
        $this->annotation = $annotation;
87
88
        return $this;
89
    }
90
91
    /**
92
     * @param ReflectionProperty $name
93
     *
94
     * @return $this
95
     */
96
    public function setProperty(ReflectionProperty $name): AbstractDriver
97
    {
98
        $this->property = $name;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @param Class_ $part
105
     */
106
    protected function removeConstructor(Class_ $part)
107
    {
108
        if (!is_null($this->reflector->getConstructor())) {
109
            $this->removeMethod($part, '__construct');
110
        }
111
    }
112
113
    /**
114
     * @param Class_ $part
115
     * @param string $name
116
     */
117
    protected function removeMethod(Class_ $part, string $name)
118
    {
119 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...
120
            if ($statement instanceof ClassMethod) {
121
                if ($statement->name === $name) {
122
                    unset($part->stmts[$key]);
123
                }
124
            }
125
        }
126
    }
127
128
    /**
129
     * detect constructor access level.
130
     *
131
     * @return string
132
     */
133
    protected function setAccessLevel(): string
134
    {
135
        switch ($this->annotation->access) {
136
            case Access::LEVEL_PRIVATE:
137
                return 'makePrivate';
138
            case Access::LEVEL_PROTECTED:
139
                return 'makeProtected';
140
            default:
141
                return 'makePublic';
142
        }
143
    }
144
}
145