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\Node\Stmt\Class_; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class ValueDriver. |
25
|
|
|
* |
26
|
|
|
* @author yuuki.takezawa<[email protected]> |
27
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
28
|
|
|
*/ |
29
|
|
|
class ValueDriver extends AbstractDriver |
30
|
|
|
{ |
31
|
|
|
// getter generator |
32
|
|
|
use GetterTrait, ToStringTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function generator(): array |
38
|
|
|
{ |
39
|
|
|
foreach ($this->reflector->getProperties() as $property) { |
40
|
|
|
$name = $property->getName(); |
41
|
|
|
$this->createGetter($name); |
42
|
|
|
} |
43
|
|
|
foreach ($this->parsed as $part) { |
44
|
|
|
if ($part instanceof Class_) { |
45
|
|
View Code Duplication |
foreach ($this->getGetters() as $getter) { |
|
|
|
|
46
|
|
|
if ($this->reflector->hasMethod($getter['method'])) { |
47
|
|
|
continue; |
48
|
|
|
} |
49
|
|
|
$part->stmts[] = $this->createGetterMethod($getter); |
50
|
|
|
} |
51
|
|
|
if (!$this->reflector->hasMethod('__toString')) { |
52
|
|
|
$part->stmts[] = $this->createToString($this->getGetters()); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->parsed; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
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.