4
4
5
5
use Faker \Generator as FakerGenerator ;
6
6
use PhpParser \Node ;
7
+ use PhpParser \Node \Scalar \String_ ;
7
8
use PhpParser \Node \Stmt ;
8
9
use PhpParser \Node \Stmt \Class_ ;
10
+ use PhpParser \Node \Stmt \ClassMethod ;
11
+ use PhpParser \Node \Stmt \Property ;
9
12
use PhpParser \NodeFinder ;
10
13
use PhpParser \Parser ;
11
14
use PhpSchool \PhpWorkshop \Check \FileComparisonCheck ;
@@ -79,6 +82,7 @@ public function getArgs(): array
79
82
return [
80
83
json_encode (
81
84
[
85
+ 'id ' => random_int (0 , 100 ),
82
86
'comment ' => $ this ->faker ->sentence (4 ),
83
87
'rating ' => $ this ->faker ->numberBetween (0 , 5 ),
84
88
'reviewer ' => $ this ->faker ->userName (),
@@ -99,17 +103,108 @@ public function check(Input $input): ResultInterface
99
103
return $ node instanceof Class_ && $ node ->name && $ node ->name ->name === 'Review ' ;
100
104
});
101
105
102
- //not even sure we need this the var_dump will cover it
103
106
if ($ classStmt === null ) {
104
107
return new Failure ($ this ->getName (), 'A class named Review was not found ' );
105
108
}
106
109
110
+ /** @var ClassMethod|null $method */
111
+ $ method = (new NodeFinder ())->findFirst ($ statements , function (Node $ node ) {
112
+ return $ node instanceof ClassMethod && $ node ->name ->name === 'obfuscateReviewer ' ;
113
+ });
114
+
115
+ if ($ method === null ) {
116
+ return new Failure ($ this ->getName (), 'A method named obfuscateReviewer was not found ' );
117
+ }
118
+
119
+ if (!isset ($ method ->attrGroups [0 ]->attrs [0 ])) {
120
+ return new Failure ($ this ->getName (), 'No attributes found on method obfuscateReviewer ' );
121
+ }
122
+
123
+ $ attribute = $ method ->attrGroups [0 ]->attrs [0 ];
124
+
125
+ if ($ attribute ->name ->toString () !== 'Obfuscate ' ) {
126
+ return new Failure ($ this ->getName (), 'No attribute named Obfuscate found on method obfuscateReviewer ' );
127
+ }
128
+
129
+ if (!isset ($ attribute ->args [0 ])) {
130
+ return new Failure ($ this ->getName (), 'No property name argument was passed to the Obfuscate attribute ' );
131
+ }
132
+
133
+ if (!$ attribute ->args [0 ]->value instanceof String_ || $ attribute ->args [0 ]->value ->value !== 'reviewer ' ) {
134
+ return new Failure ($ this ->getName (), 'The Obfuscate attribute was not passed the correct data property ' );
135
+ }
136
+
137
+ /** @var Class_|null $attributeClass */
138
+ $ attributeClass = (new NodeFinder ())->findFirst ($ statements , function (Node $ node ) {
139
+ return $ node instanceof Class_ && $ node ->name && $ node ->name ->name === 'Obfuscate ' ;
140
+ });
141
+
142
+ if ($ attributeClass === null ) {
143
+ return new Failure ($ this ->getName (), 'A class named Obfuscate was not found ' );
144
+ }
145
+
146
+ if (!isset ($ attributeClass ->attrGroups [0 ]->attrs [0 ])) {
147
+ return new Failure ($ this ->getName (), 'No attributes found on class Obfuscate ' );
148
+ }
149
+
150
+ $ attribute = $ attributeClass ->attrGroups [0 ]->attrs [0 ];
151
+
152
+ if ($ attribute ->name ->toString () !== 'Attribute ' ) {
153
+ return new Failure ($ this ->getName (), 'The Obfuscate class was not defined as an Attribute ' );
154
+ }
155
+
156
+ if (!isset ($ attribute ->args [0 ])) {
157
+ return new Failure ($ this ->getName (), 'No flags were passed to Obfuscate Attribute definition ' );
158
+ }
159
+
160
+ /** @var \PhpParser\Node\Expr\ClassConstFetch $value */
161
+ $ value = $ attribute ->args [0 ]->value ;
162
+
163
+ if ($ value ->class ->toString () !== 'Attribute ' || $ value ->name ->name !== 'TARGET_METHOD ' ) {
164
+ return new Failure (
165
+ $ this ->getName (),
166
+ 'The Obfuscate Attribute was not configured as Attribute::TARGET_METHOD '
167
+ );
168
+ }
169
+
170
+ $ prop = (new NodeFinder ())->findFirst ($ attributeClass ->getProperties (), function (Node $ node ) {
171
+ return $ node instanceof Property
172
+ && $ node ->isPublic ()
173
+ && $ node ->type instanceof \PhpParser \Node \Identifier
174
+ && $ node ->type ->name === 'string '
175
+ && $ node ->props [0 ] instanceof \PhpParser \Node \Stmt \PropertyProperty
176
+ && $ node ->props [0 ]->name instanceof \PhpParser \Node \VarLikeIdentifier
177
+ && $ node ->props [0 ]->name ->name === 'key ' ;
178
+ });
179
+
180
+ $ promotedProp = (new NodeFinder ())->findFirst ($ attributeClass ->getMethods (), function (Node $ node ) {
181
+ return $ node instanceof ClassMethod
182
+ && $ node ->name ->name === '__construct '
183
+ && isset ($ node ->params [0 ])
184
+ && $ node ->params [0 ]->flags === 1
185
+ && $ node ->params [0 ]->var ->name === 'key '
186
+ && $ node ->params [0 ]->type instanceof \PhpParser \Node \Identifier
187
+ && $ node ->params [0 ]->type ->name === 'string ' ;
188
+
189
+ });
190
+
191
+ if ($ prop === null && $ promotedProp === null ) {
192
+ return new Failure (
193
+ $ this ->getName (),
194
+ 'The Obfuscate Attribute has no public property named "key" '
195
+ );
196
+ }
197
+
198
+
107
199
return new Success ($ this ->getName ());
108
200
}
109
201
110
202
public function getRequiredFunctions (): array
111
203
{
112
- return ['deserialize ' ];
204
+ return [
205
+ 'deserialize ' ,
206
+ 'var_dump '
207
+ ];
113
208
}
114
209
115
210
public function getBannedFunctions (): array
0 commit comments