1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. Cycle ProxyFactory |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Valentin V (Vvval) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\ORM\Promise\Visitor; |
13
|
|
|
|
14
|
|
|
use PhpParser\Builder; |
15
|
|
|
use PhpParser\Node; |
16
|
|
|
use PhpParser\NodeVisitorAbstract; |
17
|
|
|
|
18
|
|
|
use function Cycle\ORM\Promise\ifInConstArray; |
19
|
|
|
use function Cycle\ORM\Promise\resolveIntoVar; |
20
|
|
|
use function Cycle\ORM\Promise\returnIssetFunc; |
21
|
|
|
use function Cycle\ORM\Promise\throwExceptionOnNull; |
22
|
|
|
|
23
|
|
View Code Duplication |
final class AddMagicIssetMethod extends NodeVisitorAbstract |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
/** @var string */ |
26
|
|
|
private $class; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private $resolverProperty; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
private $resolveMethod; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
private $unsetPropertiesProperty; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $class |
39
|
|
|
* @param string $resolverProperty |
40
|
|
|
* @param string $resolveMethod |
41
|
|
|
* @param string $unsetPropertiesProperty |
42
|
|
|
*/ |
43
|
|
|
public function __construct( |
44
|
|
|
string $class, |
45
|
|
|
string $resolverProperty, |
46
|
|
|
string $resolveMethod, |
47
|
|
|
string $unsetPropertiesProperty |
48
|
|
|
) { |
49
|
|
|
$this->class = $class; |
50
|
|
|
$this->unsetPropertiesProperty = $unsetPropertiesProperty; |
51
|
|
|
$this->resolveMethod = $resolveMethod; |
52
|
|
|
$this->resolverProperty = $resolverProperty; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Node $node |
57
|
|
|
* @return int|Node|Node[]|null |
58
|
|
|
*/ |
59
|
|
|
public function leaveNode(Node $node) |
60
|
|
|
{ |
61
|
|
|
if ($node instanceof Node\Stmt\Class_) { |
62
|
|
|
$method = new Builder\Method('__isset'); |
63
|
|
|
$method->makePublic(); |
64
|
|
|
$method->addParam(new Builder\Param('name')); |
65
|
|
|
$method->addStmt($this->buildIssetExpression()); |
66
|
|
|
|
67
|
|
|
$node->stmts[] = $method->getNode(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return Node\Stmt\If_ |
75
|
|
|
*/ |
76
|
|
|
private function buildIssetExpression(): Node\Stmt\If_ |
77
|
|
|
{ |
78
|
|
|
$if = ifInConstArray('name', 'self', $this->unsetPropertiesProperty); |
79
|
|
|
$if->stmts[] = resolveIntoVar('entity', 'this', $this->resolverProperty, $this->resolveMethod); |
80
|
|
|
$if->stmts[] = throwExceptionOnNull( |
81
|
|
|
new Node\Expr\Variable('entity'), |
82
|
|
|
returnIssetFunc('entity', '{$name}'), |
83
|
|
|
'Property `%s` not loaded in `__isset()` method for `%s`', |
84
|
|
|
[ |
85
|
|
|
new Node\Arg(new Node\Expr\Variable('name')), |
86
|
|
|
$this->class |
87
|
|
|
] |
88
|
|
|
); |
89
|
|
|
$if->else = new Node\Stmt\Else_([ |
90
|
|
|
returnIssetFunc('this', '{$name}') |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
return $if; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.