Completed
Pull Request — master (#5)
by Valentin
03:04
created

expressions.php ➔ exprUnsetFunc()   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\Stmt\Expression
20
 */
21
function exprUnsetFunc(string $object, string $property): Node\Stmt\Expression
22
{
23
    return new Node\Stmt\Expression(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\Stmt\Return_
32
 */
33
function returnIssetFunc(string $object, string $property): Node\Stmt\Return_
34
{
35
    return new Node\Stmt\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\Stmt\If_
45
 */
46
function ifInConstArray(string $name, string $object, string $haystackConst): Node\Stmt\If_
47
{
48
    return new Node\Stmt\If_(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 = ifNotNull($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
 * @param array     $subNodes
76
 * @return  Node\Stmt\If_
77
 */
78
function ifNotNull(Node\Expr $expr, array $subNodes = []): Node\Stmt\If_
79
{
80
    return new Node\Stmt\If_(new Node\Expr\BinaryOp\NotIdentical($expr, constFetch('null')), $subNodes);
81
}
82
83
/**
84
 * @param string $var
85
 * @param string $object
86
 * @param string $property
87
 * @param string $method
88
 * @return Node\Stmt\Expression
89
 */
90
function resolveIntoVar(
91
    string $var,
92
    string $object,
93
    string $property,
94
    string $method
95
): Node\Stmt\Expression {
96
    return new Node\Stmt\Expression(
97
        new Node\Expr\Assign(
98
            new Node\Expr\Variable($var),
99
            resolveMethodCall($object, $property, $method)
100
        )
101
    );
102
}
103
104
/**
105
 * @param string $object
106
 * @param string $property
107
 * @param string $method
108
 * @return Node\Expr\MethodCall
109
 */
110
function resolveMethodCall(string $object, string $property, string $method): Node\Expr\MethodCall
111
{
112
    return new Node\Expr\MethodCall(resolvePropertyFetch($object, $property), $method);
113
}
114
115
/**
116
 * @param Node\Expr $expr
117
 * @return Node\Stmt\If_
118
 */
119
function ifEqualsFalse(Node\Expr $expr): Node\Stmt\If_
120
{
121
    return new Node\Stmt\If_(new Node\Expr\BinaryOp\Identical($expr, constFetch('false')));
122
}
123
124
/**
125
 * @param string $name
126
 * @return Node\Expr\ConstFetch
127
 */
128
function constFetch(string $name): Node\Expr\ConstFetch
129
{
130
    return new Node\Expr\ConstFetch(new Node\Name($name));
131
}
132
133
/**
134
 * @param string $property
135
 * @return Node\Stmt\Expression
136
 */
137
function exprClone(string $property): Node\Stmt\Expression
138
{
139
    $fetchedProperty = resolvePropertyFetch('this', $property);
140
141
    return new Node\Stmt\Expression(
142
        new Node\Expr\Assign($fetchedProperty, new Node\Expr\Clone_($fetchedProperty))
143
    );
144
}
145
146
/**
147
 * @param string $object
148
 * @param string $property
149
 * @return Node\Expr\PropertyFetch
150
 */
151
function resolvePropertyFetch(string $object, string $property): Node\Expr\PropertyFetch
152
{
153
    return new Node\Expr\PropertyFetch(new Node\Expr\Variable($object), $property);
154
}
155
156
/**
157
 * @param string $name
158
 * @param array  $args
159
 * @param array  $attributes
160
 * @return Node\Expr\FuncCall
161
 * @internal
162
 */
163
function funcCall(string $name, array $args = [], array $attributes = []): Node\Expr\FuncCall
164
{
165
    return new Node\Expr\FuncCall(new Node\Name($name), $args, $attributes);
166
}
167
168
/**
169
 * @param string $class
170
 * @param string $message
171
 * @return Node\Stmt\Throw_
172
 * @internal
173
 */
174
function throwException(string $class, string $message): Node\Stmt\Throw_
175
{
176
    return new Node\Stmt\Throw_(
177
        new Node\Expr\New_(new Node\Name($class), [new Node\Arg(new Node\Scalar\String_($message))])
178
    );
179
}
180