These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Spiral Framework. |
||
4 | * |
||
5 | * @license MIT |
||
6 | * @author Valentin V (Vvval) |
||
7 | */ |
||
8 | declare(strict_types=1); |
||
9 | |||
10 | namespace Cycle\ORM\Promise\Visitor; |
||
11 | |||
12 | use Cycle\ORM\Promise\PHPDoc; |
||
13 | use Cycle\ORM\Promise\Utils; |
||
14 | use PhpParser\Builder\Property; |
||
15 | use PhpParser\Node; |
||
16 | use PhpParser\NodeVisitorAbstract; |
||
17 | |||
18 | /** |
||
19 | * Add resolver property |
||
20 | */ |
||
21 | final class AddResolverProperty extends NodeVisitorAbstract |
||
22 | { |
||
23 | /** @var string */ |
||
24 | private $property; |
||
25 | |||
26 | /** @var string */ |
||
27 | private $type; |
||
28 | |||
29 | /** @var string */ |
||
30 | private $class; |
||
31 | |||
32 | /** |
||
33 | * @param string $property |
||
34 | * @param string $type |
||
35 | * @param string $class |
||
36 | */ |
||
37 | public function __construct(string $property, string $type, string $class) |
||
38 | { |
||
39 | $this->property = $property; |
||
40 | $this->type = $type; |
||
41 | $this->class = $class; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | View Code Duplication | public function leaveNode(Node $node) |
|
0 ignored issues
–
show
|
|||
48 | { |
||
49 | if ($node instanceof Node\Stmt\Class_) { |
||
50 | $node->stmts = Utils::injectValues($node->stmts, $this->definePlacementID($node), [$this->buildProperty()]); |
||
51 | } |
||
52 | |||
53 | return null; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param Node\Stmt\Class_ $node |
||
58 | * @return int |
||
59 | */ |
||
60 | private function definePlacementID(Node\Stmt\Class_ $node): int |
||
61 | { |
||
62 | foreach ($node->stmts as $index => $child) { |
||
63 | if ($child instanceof Node\Stmt\ClassMethod) { |
||
64 | return $index; |
||
65 | } |
||
66 | } |
||
67 | |||
68 | return 0; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @return Node\Stmt\Property |
||
73 | */ |
||
74 | private function buildProperty(): Node\Stmt\Property |
||
75 | { |
||
76 | $property = new Property($this->property); |
||
77 | $property->makePrivate(); |
||
78 | $property->setDocComment(PHPDoc::writeProperty("{$this->type}|{$this->class}")); |
||
79 | |||
80 | return $property->getNode(); |
||
81 | } |
||
82 | } |
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.