Completed
Branch feature/functions (11307d)
by Valentin
04:45
created

expressions.php ➔ unsetFuncExpr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Spiral Framework. Cycle ProxyFactory
5
 *
6
 * @license MIT
7
 * @author  Valentin V (Vvval)
8
 */
9
declare(strict_types=1);
10
11
namespace Cycle\ORM\Promise;
12
13
use Cycle\ORM\Promise\Exception\ProxyFactoryException;
14
use PhpParser\Node;
15
16
/**
17
 * @param string $object
18
 * @param string $property
19
 * @return Node\Expr\FuncCall
20
 */
21
function unsetFuncExpr(string $object, string $property): Node\Expr\FuncCall
22
{
23
    return funcCall('unset', [
24
        new Node\Arg(new Node\Expr\PropertyFetch(new Node\Expr\Variable($object), $property))
25
    ]);
26
}
27
28
/**
29
 * @param string $object
30
 * @param string $property
31
 * @return Node\Expr\FuncCall
32
 */
33
function issetFuncExpr(string $object, string $property): Node\Expr\FuncCall
34
{
35
    return funcCall('isset', [
36
        new Node\Arg(new Node\Expr\PropertyFetch(new Node\Expr\Variable($object), $property))
37
    ]);
38
}
39
40
/**
41
 * @param string $name
42
 * @param string $object
43
 * @param string $haystackConst
44
 * @return Node\Expr\FuncCall
45
 */
46
function inConstArrayFunc(string $name, string $object, string $haystackConst): Node\Expr\FuncCall
47
{
48
    return funcCall('in_array', [
49
        new Node\Arg(new Node\Expr\Variable($name)),
50
        new Node\Arg(new Node\Expr\ClassConstFetch(new Node\Name($object), $haystackConst)),
51
        new Node\Arg(constFetch('true'))
52
    ]);
53
}
54
55
/**
56
 * @param Node\Expr $condition
57
 * @param Node\Stmt $stmt
58
 * @return Node\Stmt\If_
59
 */
60
function throwExceptionOnNull(Node\Expr $condition, Node\Stmt $stmt): Node\Stmt\If_
61
{
62
    $if = new Node\Stmt\If_(notNull($condition));
63
    $if->stmts[] = $stmt;
64
    $if->else = new Node\Stmt\Else_();
65
    $if->else->stmts[] = throwException(
66
        shortName(ProxyFactoryException::class),
67
        'Promise not loaded'
0 ignored issues
show
Unused Code introduced by
The call to throwException() has too many arguments starting with 'Promise not loaded'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
68
    );
69
70
    return $if;
71
}
72
73
/**
74
 * @param Node\Expr $expr
75
 * @return Node\Expr\BinaryOp\NotIdentical
76
 */
77
function notNull(Node\Expr $expr): Node\Expr\BinaryOp\NotIdentical
78
{
79
    return new Node\Expr\BinaryOp\NotIdentical($expr, constFetch('null'));
80
}
81
82
/**
83
 * @param string $var
84
 * @param string $object
85
 * @param string $property
86
 * @param string $method
87
 * @return Node\Stmt\Expression
88
 */
89
function resolveIntoVar(
90
    string $var,
91
    string $object,
92
    string $property,
93
    string $method
94
): Node\Stmt\Expression {
95
    return new Node\Stmt\Expression(
96
        new Node\Expr\Assign(
97
            new Node\Expr\Variable($var),
98
            resolveMethodCall($object, $property, $method)
99
        )
100
    );
101
}
102
103
/**
104
 * @param string $object
105
 * @param string $property
106
 * @param string $method
107
 * @return Node\Expr\MethodCall
108
 */
109
function resolveMethodCall(string $object, string $property, string $method): Node\Expr\MethodCall
110
{
111
    return new Node\Expr\MethodCall(resolvePropertyFetch($object, $property), $method);
112
}
113
114
/**
115
 * @param Node\Expr $expr
116
 * @return Node\Expr\BinaryOp\Identical
117
 */
118
function equalsFalse(Node\Expr $expr): Node\Expr\BinaryOp\Identical
119
{
120
    return new Node\Expr\BinaryOp\Identical($expr, constFetch('false'));
121
}
122
123
/**
124
 * @param string $name
125
 * @return Node\Expr\ConstFetch
126
 */
127
function constFetch(string $name): Node\Expr\ConstFetch
128
{
129
    return new Node\Expr\ConstFetch(new Node\Name($name));
130
}
131
132
/**
133
 * @param string $property
134
 * @return Node\Stmt\Expression
135
 */
136
function buildCloneExpression(string $property): Node\Stmt\Expression
137
{
138
    $fetchedProperty = resolvePropertyFetch('this', $property);
139
140
    return new Node\Stmt\Expression(
141
        new Node\Expr\Assign($fetchedProperty, new Node\Expr\Clone_($fetchedProperty))
142
    );
143
}
144
145
/**
146
 * @param string $object
147
 * @param string $property
148
 * @return Node\Expr\PropertyFetch
149
 */
150
function resolvePropertyFetch(string $object, string $property): Node\Expr\PropertyFetch
151
{
152
    return new Node\Expr\PropertyFetch(new Node\Expr\Variable($object), $property);
153
}
154
155
/**
156
 * @param string $name
157
 * @param array  $args
158
 * @param array  $attributes
159
 * @return Node\Expr\FuncCall
160
 * @internal
161
 */
162
function funcCall(string $name, array $args = [], array $attributes = []): Node\Expr\FuncCall
163
{
164
    return new Node\Expr\FuncCall(new Node\Name($name), $args, $attributes);
165
}
166
167
/**
168
 * @param string $class
169
 * @param string $message
170
 * @return Node\Stmt\Throw_
171
 * @internal
172
 */
173
function throwException(string $class, string $message): Node\Stmt\Throw_
174
{
175
    return new Node\Stmt\Throw_(
176
        new Node\Expr\New_(new Node\Name($class), [new Node\Arg(new Node\Scalar\String_($message))])
177
    );
178
}
179