Repository navigation
Expand file tree
/
Copy pathStaticPropertyFetchHandler.cpp
More file actions
589 lines (531 loc) · 28.7 KB
/
Copy pathStaticPropertyFetchHandler.cpp
File metadata and controls
589 lines (531 loc) · 28.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
* PHPStanTurbo\StaticPropertyFetchHandler — native implementation of
* PHPStan\Analyser\ExprHandler\StaticPropertyFetchHandler.
*
* A DI service (#[AutowiredService]): the constructor keeps the twin's
* arginfo so Nette autowires it. processExpr() is registered as the class's
* handler entry and composeResult() — which AssignHandler calls across
* handlers — is exported as pt_static_property_fetch_handler_compose_result()
* (Engine.h conventions). The twin's closures are native closures capturing
* what the PHP closures capture: the issetability descriptor's reflection
* resolver ($this, $expr), the typeCallback ($this, $expr, $classResult,
* $nameResult, $beforeScope) and the specifyTypesCallback ($this, $expr); the
* $shortCircuit / $resolveProperty closures the typeCallback creates and
* calls itself are inlined, as is the private propertyFetchType().
*
* NodeScopeResolver, MutatingScope, ExpressionResult, ExpressionContext,
* VariableFlow(Builder), ImpurePoint, IssetabilityDescriptor,
* DefaultNarrowingHelper, TypeCombinator and the Type kernel are called
* through their direct entries; the property reflections through the readers
* of PropertyHookThrowPointsResolver.cpp; PropertyReflectionFinder stays PHP
* (one cached method site).
*/
#include "support.h"
#include "generated/StaticPropertyFetchHandler.h"
namespace slots = ptdecl::StaticPropertyFetchHandler::slot;
namespace sigs = ptdecl::StaticPropertyFetchHandler::sig;
#include "CallHandlerSupport.h"
zend_class_entry *pt_ce_static_property_fetch_handler = nullptr;
namespace {
using namespace ptcall;
constexpr const char *pt_spfh_closure_name = "PHPStan\\Analyser\\ExprHandler\\StaticPropertyFetchHandler::{closure}";
/* {{{ the PHP collaborators (one site each; switch to their direct entries
* once they are ported) */
pt_method_site pt_spfh_find_property_reflection_from_node_site;
/* $propertyReflectionFinder->findPropertyReflectionFromNode($propertyFetch, $scope) */
zv::Val findPropertyReflectionFromNode(zval *propertyReflectionFinder, zval *propertyFetch, zval *scope)
{
zv::Args argv{propertyFetch, scope};
return pt_call_method_cached(pt_spfh_find_property_reflection_from_node_site, Z_OBJ_P(propertyReflectionFinder), PT_LC("findpropertyreflectionfromnode"), 2, argv);
}
/* }}} */
/* {{{ the PhpParser nodes' properties */
pt_property_site pt_spfh_class_site;
pt_property_site pt_spfh_name_site;
pt_property_site pt_spfh_identifier_name_site;
zval *exprClass(zval *expr) { return nodeProperty(pt_spfh_class_site, expr, PT_LC("class")); }
zval *exprName(zval *expr) { return nodeProperty(pt_spfh_name_site, expr, PT_LC("name")); }
/* $identifier->toString() */
zval *identifierName(zval *identifier) { return nodeProperty(pt_spfh_identifier_name_site, identifier, PT_LC("name")); }
/* }}} */
/* the static property access impure point's literals, permanent interned
* strings (module startup) */
zend_string *pt_spfh_static_property_access = nullptr;
zend_string *pt_spfh_static_property_access_description = nullptr;
} // namespace
namespace phpstanturbo {
/* Mirrors PHPStan\Analyser\ExprHandler\StaticPropertyFetchHandler; UNDEF =
* pending exception. */
class StaticPropertyFetchHandler
{
public:
explicit StaticPropertyFetchHandler(zend_object *self) : self(self) {}
/* the constructor body: the promoted properties */
void construct(zval *propertyReflectionFinder, zval *expressionResultFactory, zval *defaultNarrowingHelper, zval *reflectionProvider) const
{
pt_write_slot(self, slots::propertyReflectionFinder, propertyReflectionFinder);
pt_write_slot(self, slots::expressionResultFactory, expressionResultFactory);
pt_write_slot(self, slots::defaultNarrowingHelper, defaultNarrowingHelper);
pt_write_slot(self, slots::reflectionProvider, reflectionProvider);
}
/* Mirrors supports(); false = pending exception */
[[nodiscard]] bool supports(zval *expr, bool &out) const
{
int is = isInstanceOf(expr, PT_CLASS_STATIC_PROPERTY_FETCH);
if (UNEXPECTED(is < 0)) return false;
out = is == 1;
return true;
}
/* Mirrors processExpr(). */
zv::Val processExpr(zval *nodeScopeResolver, zval *stmt, zval *expr, zval *scope, zval *storage, zval *nodeCallback, zval *context) const
{
zval *beforeScope = scope;
zv::Val currentScope = zv::Val::copyOf(zv::Ref(scope));
zv::Val classResult;
zval *class_ = exprClass(expr);
if (UNEXPECTED(class_ == NULL)) return zv::Val();
int classIsExpr = isInstanceOf(class_, PT_CLASS_EXPR);
if (UNEXPECTED(classIsExpr < 0)) return zv::Val();
if (classIsExpr) {
zv::Val classContext = pt_expression_context_enter_deep(context);
if (UNEXPECTED(classContext.isUndef())) return zv::Val();
classResult = pt_node_scope_resolver_process_expr_node(nodeScopeResolver, stmt, class_, scope, storage, nodeCallback, classContext.raw());
if (UNEXPECTED(classResult.isUndef())) return zv::Val();
zv::Val hold;
zval *classScope = pt_expression_result_scope(classResult.raw(), hold);
if (UNEXPECTED(classScope == NULL)) return zv::Val();
currentScope = zv::Val::copyOf(zv::Ref(classScope));
}
zv::Val nameResult;
zval *name = exprName(expr);
if (UNEXPECTED(name == NULL)) return zv::Val();
int nameIsVarLikeIdentifier = isInstanceOf(name, PT_CLASS_VAR_LIKE_IDENTIFIER);
if (UNEXPECTED(nameIsVarLikeIdentifier < 0)) return zv::Val();
if (!nameIsVarLikeIdentifier) {
zv::Val nameContext = pt_expression_context_enter_deep(context);
if (UNEXPECTED(nameContext.isUndef())) return zv::Val();
nameResult = pt_node_scope_resolver_process_expr_node(nodeScopeResolver, stmt, name, currentScope.raw(), storage, nodeCallback, nameContext.raw());
if (UNEXPECTED(nameResult.isUndef())) return zv::Val();
}
return composeResult(expr, classResult.isUndef() ? NULL : classResult.raw(), nameResult.isUndef() ? NULL : nameResult.raw(), beforeScope);
}
/* Mirrors composeResult(); $classResult / $nameResult NULL (or IS_NULL)
* for null */
zv::Val composeResult(zval *expr, zval *classResult, zval *nameResult, zval *beforeScope) const
{
if (classResult != NULL && Z_TYPE_P(classResult) == IS_NULL) classResult = NULL;
if (nameResult != NULL && Z_TYPE_P(nameResult) == IS_NULL) nameResult = NULL;
zv::Val scope = zv::Val::copyOf(zv::Ref(beforeScope));
bool hasYield = false;
zv::Val throwPoints = zv::Val(zv::Arr::empty());
zv::Val impurePoints;
{
zv::Val impurePoint = pt_impure_point_new(scope.raw(), expr, pt_spfh_static_property_access, pt_spfh_static_property_access_description, true);
if (UNEXPECTED(impurePoint.isUndef())) return zv::Val();
zv::Arr points = zv::Arr::create(1);
points.push(std::move(impurePoint));
impurePoints = zv::Val(std::move(points));
}
bool isAlwaysTerminating = false;
zv::Val hold;
if (classResult != NULL) {
if (UNEXPECTED(!pt_expression_result_has_yield(classResult, hasYield))) return zv::Val();
zval *borrowed = pt_expression_result_throw_points(classResult, hold);
if (UNEXPECTED(borrowed == NULL)) return zv::Val();
throwPoints = zv::Val::copyOf(zv::Ref(borrowed));
borrowed = pt_expression_result_impure_points(classResult, hold);
if (UNEXPECTED(borrowed == NULL)) return zv::Val();
impurePoints = zv::Val::copyOf(zv::Ref(borrowed));
if (UNEXPECTED(!pt_expression_result_is_always_terminating(classResult, isAlwaysTerminating))) return zv::Val();
borrowed = pt_expression_result_scope(classResult, hold);
if (UNEXPECTED(borrowed == NULL)) return zv::Val();
scope = zv::Val::copyOf(zv::Ref(borrowed));
}
bool mayShortCircuit = false;
if (nameResult != NULL) {
// `$a?->b::$$name` is a link in a nullsafe chain and may never run - see
// MethodCallHandler::processExpr(). Only the dynamic name is skipped with
// it, so a plain `::$name` never has to resolve the class expression type.
if (classResult != NULL && UNEXPECTED(!pt_expression_result_may_short_circuit(classResult, NULL, mayShortCircuit))) return zv::Val();
if (!hasYield && UNEXPECTED(!pt_expression_result_has_yield(nameResult, hasYield))) return zv::Val();
zval *borrowed = pt_expression_result_throw_points(nameResult, hold);
if (UNEXPECTED(borrowed == NULL)) return zv::Val();
throwPoints = arrayMerge(throwPoints.raw(), borrowed);
borrowed = pt_expression_result_impure_points(nameResult, hold);
if (UNEXPECTED(borrowed == NULL)) return zv::Val();
impurePoints = arrayMerge(impurePoints.raw(), borrowed);
if (!isAlwaysTerminating && !mayShortCircuit && UNEXPECTED(!pt_expression_result_is_always_terminating(nameResult, isAlwaysTerminating))) return zv::Val();
borrowed = pt_expression_result_scope(nameResult, hold);
if (UNEXPECTED(borrowed == NULL)) return zv::Val();
scope = zv::Val::copyOf(zv::Ref(borrowed));
if (mayShortCircuit) {
// the dynamic name expression was not evaluated in the
// short-circuited world
borrowed = pt_expression_result_scope(classResult, hold);
if (UNEXPECTED(borrowed == NULL)) return zv::Val();
scope = pt_mutating_scope_merge_with(Z_OBJ_P(scope.raw()), borrowed);
if (UNEXPECTED(scope.isUndef())) return zv::Val();
}
}
zv::Val variableFlow;
{
zv::Val classFlow = zv::Val::null();
if (classResult != NULL) {
classFlow = pt_expression_result_variable_flow(classResult);
if (UNEXPECTED(classFlow.isUndef())) return zv::Val();
}
zv::Val nameFlow = zv::Val::null();
if (nameResult != NULL) {
nameFlow = pt_expression_result_variable_flow(nameResult);
if (UNEXPECTED(nameFlow.isUndef())) return zv::Val();
if (mayShortCircuit) {
zv::Args choiceArgv{nameFlow.raw(), zv::null};
zv::Val choice = pt_variable_flow_choice(2, choiceArgv);
if (UNEXPECTED(choice.isUndef())) return zv::Val();
nameFlow = std::move(choice);
}
}
zv::Val throwsFlow = pt_variable_flow_builder_throws(expr, Z_ARRVAL_P(throwPoints.raw()));
if (UNEXPECTED(throwsFlow.isUndef())) return zv::Val();
zv::Args flows{classFlow.raw(), nameFlow.raw(), throwsFlow.raw()};
variableFlow = pt_variable_flow_sequence(3, flows);
if (UNEXPECTED(variableFlow.isUndef())) return zv::Val();
}
bool containsNullsafe = false;
if (classResult != NULL && UNEXPECTED(!pt_expression_result_contains_nullsafe(classResult, containsNullsafe))) return zv::Val();
zv::Val reflectionResolver = pt_native_closure(&reflectionResolverBody, self, expr);
zv::Val issetabilityDescriptor = pt_issetability_descriptor_property(classResult, reflectionResolver.raw(), expr);
if (UNEXPECTED(issetabilityDescriptor.isUndef())) return zv::Val();
zval null;
ZVAL_NULL(&null);
zv::Val typeCallback = pt_native_closure(&typeCallbackBody, self, expr, classResult != NULL ? classResult : &null, nameResult != NULL ? nameResult : &null, beforeScope);
zv::Val specifyTypesCallback = pt_native_closure(&specifyTypesCallbackBody, self, expr);
pt_expression_result_args args(scope.raw(), beforeScope, expr, hasYield, isAlwaysTerminating, throwPoints.raw(), impurePoints.raw(), typeCallback.raw(), specifyTypesCallback.raw());
args.withVariableFlow(variableFlow.raw()).withContainsNullsafe(containsNullsafe).withIssetabilityDescriptor(issetabilityDescriptor.raw());
zv::Val result = pt_expression_result_create(OBJ_PROP_NUM(self, slots::expressionResultFactory), args);
if (UNEXPECTED(result.isUndef())) return zv::Val();
zval *classDependencies = NULL;
zv::Val classDependenciesHold;
if (classResult != NULL) {
classDependencies = pt_expression_result_dependencies(classResult, classDependenciesHold);
if (UNEXPECTED(classDependencies == NULL)) return zv::Val();
}
zval *nameDependencies = NULL;
zv::Val nameDependenciesHold;
if (nameResult != NULL) {
nameDependencies = pt_expression_result_dependencies(nameResult, nameDependenciesHold);
if (UNEXPECTED(nameDependencies == NULL)) return zv::Val();
}
zv::Val ownDependencies = getDependencies(beforeScope, expr, classResult, result.raw());
if (UNEXPECTED(ownDependencies.isUndef())) return zv::Val();
zv::Val dependencies = pt_dependencies_merge({classDependencies, nameDependencies, ownDependencies.raw()});
if (UNEXPECTED(dependencies.isUndef())) return zv::Val();
return pt_expression_result_with_dependencies(result.raw(), dependencies.raw());
}
/* the handler entry (Engine.h) */
static zv::Val processExprEntry(zend_object *handler, zval *nodeScopeResolver, zval *stmt, zval *expr, zval *scope, zval *storage, zval *nodeCallback, zval *context)
{
return StaticPropertyFetchHandler(handler).processExpr(nodeScopeResolver, stmt, expr, scope, storage, nodeCallback, context);
}
private:
zend_object *self;
/* Mirrors getDependencies(): the class the property is fetched from, the
* class declaring it, and the classes in its type ($classResult NULL for
* null) */
zv::Val getDependencies(zval *scope, zval *expr, zval *classResult, zval *result) const
{
zv::Arr types = zv::Arr::create(2);
zv::Arr classNames = zv::Arr::empty();
zv::Val resultType = pt_expression_result_get_type(result);
if (UNEXPECTED(resultType.isUndef())) return zv::Val();
types.push(std::move(resultType));
zval *class_ = exprClass(expr);
if (UNEXPECTED(class_ == NULL)) return zv::Val();
int classIsName = 0;
if (classResult != NULL) {
zv::Val classType = pt_expression_result_get_type(classResult);
if (UNEXPECTED(classType.isUndef())) return zv::Val();
types.push(std::move(classType));
} else {
classIsName = isInstanceOf(class_, PT_CLASS_NAME);
if (UNEXPECTED(classIsName < 0)) return zv::Val();
if (classIsName) {
zv::Val className = pt_mutating_scope_resolve_name(Z_OBJ_P(scope), Z_OBJ_P(class_));
if (UNEXPECTED(className.isUndef())) return zv::Val();
classNames.push(std::move(className));
}
}
zval *name = exprName(expr);
if (UNEXPECTED(name == NULL)) return zv::Val();
int nameIsVarLikeIdentifier = isInstanceOf(name, PT_CLASS_VAR_LIKE_IDENTIFIER);
if (UNEXPECTED(nameIsVarLikeIdentifier < 0)) return zv::Val();
if (nameIsVarLikeIdentifier) {
zval *propertyName = identifierName(name);
if (UNEXPECTED(propertyName == NULL)) return zv::Val();
if (UNEXPECTED(Z_TYPE_P(propertyName) != IS_STRING)) {
zend_type_error("PhpParser\\Node\\Identifier::toString(): Return value must be of type string, %s returned", zend_zval_value_name(propertyName));
return zv::Val();
}
if (classResult != NULL) {
zv::Val classType = pt_expression_result_get_type(classResult);
if (UNEXPECTED(classType.isUndef())) return zv::Val();
zv::Val propertyReflection = pt_mutating_scope_get_static_property_reflection(Z_OBJ_P(scope), classType.raw(), Z_STR_P(propertyName));
if (UNEXPECTED(propertyReflection.isUndef())) return zv::Val();
if (!propertyReflection.isNull()) {
zv::Val declaringClassName = declaringClassNameOf(propertyReflection.raw());
if (UNEXPECTED(declaringClassName.isUndef())) return zv::Val();
classNames.push(std::move(declaringClassName));
}
} else if (classIsName) {
zv::Val className = pt_mutating_scope_resolve_name(Z_OBJ_P(scope), Z_OBJ_P(class_));
if (UNEXPECTED(className.isUndef())) return zv::Val();
zend_object *reflectionProvider = Z_OBJ_P(OBJ_PROP_NUM(self, slots::reflectionProvider));
bool hasClass;
if (UNEXPECTED(!pt_reflection_provider_has_class(reflectionProvider, className.raw(), hasClass))) return zv::Val();
if (hasClass) {
zv::Val propertyClassReflection = pt_reflection_provider_get_class(reflectionProvider, className.raw());
if (UNEXPECTED(propertyClassReflection.isUndef())) return zv::Val();
bool hasStaticProperty;
if (UNEXPECTED(!pt_type_call_bool(Z_OBJ_P(propertyClassReflection.raw()), PT_LC("hasstaticproperty"), 1, propertyName, hasStaticProperty))) return zv::Val();
if (hasStaticProperty) {
zv::Val property = pt_type_call(Z_OBJ_P(propertyClassReflection.raw()), PT_LC("getstaticproperty"), 1, propertyName);
if (UNEXPECTED(property.isUndef())) return zv::Val();
zv::Val declaringClassName = declaringClassNameOf(property.raw());
if (UNEXPECTED(declaringClassName.isUndef())) return zv::Val();
classNames.push(std::move(declaringClassName));
}
}
}
}
return pt_dependencies_create_in(scope, types.raw(), classNames.raw());
}
/* $propertyReflection->getDeclaringClass()->getName(); UNDEF = pending
* exception */
static zv::Val declaringClassNameOf(zval *propertyReflection)
{
zv::Val declaringClass = pt_property_reflection_get_declaring_class(propertyReflection);
if (UNEXPECTED(declaringClass.isUndef())) return zv::Val();
return pt_class_reflection_get_name(Z_OBJ_P(declaringClass.raw()));
}
/* $resolveProperty($propertyName) of the typeCallback */
static zv::Val resolveProperty(bool nativeTypesPromoted, zval *reflectionScope, zval *fetchedOnType, zval *classType, zval *classResult, zval *expr, zend_string *propertyName)
{
if (nativeTypesPromoted) {
// unlike getStaticPropertyReflection(), do not pick only the union
// members that have the property - a fetch on e.g. a null member
// does not produce the property's native type
if (classType != NULL && Z_TYPE_P(classResult) != IS_NULL) {
bool containsNullsafe;
if (UNEXPECTED(!pt_expression_result_contains_nullsafe(classResult, containsNullsafe))) return zv::Val();
if (!containsNullsafe) {
bool containsNull;
if (UNEXPECTED(!pt_type_combinator_contains_null(classType, containsNull))) return zv::Val();
if (containsNull) return pt_type_new_error_type();
}
}
zval propertyNameZv;
ZVAL_STR(&propertyNameZv, propertyName);
zend_long hasStaticProperty = pt_type_call_trinary(Z_OBJ_P(fetchedOnType), PT_LC("hasstaticproperty"), 1, &propertyNameZv);
if (UNEXPECTED(hasStaticProperty < 0)) return zv::Val();
if (hasStaticProperty != PT_TRI_YES) return pt_type_new_error_type();
}
zv::Val propertyReflection = pt_mutating_scope_get_static_property_reflection(Z_OBJ_P(reflectionScope), fetchedOnType, propertyName);
if (UNEXPECTED(propertyReflection.isUndef())) return zv::Val();
if (propertyReflection.isNull()) return pt_type_new_error_type();
if (nativeTypesPromoted) {
bool hasNativeType;
if (UNEXPECTED(!pt_property_reflection_has_native_type(propertyReflection.raw(), hasNativeType))) return zv::Val();
if (!hasNativeType) return pt_type_new_mixed_type();
return pt_property_reflection_get_native_type(propertyReflection.raw());
}
// propertyFetchType()
bool inWriteExpressionAssign;
if (UNEXPECTED(!pt_mutating_scope_is_in_write_expression_assign(Z_OBJ_P(reflectionScope), Z_OBJ_P(expr), inWriteExpressionAssign))) return zv::Val();
if (inWriteExpressionAssign) return pt_property_reflection_get_writable_type(propertyReflection.raw());
return pt_property_reflection_get_readable_type(propertyReflection.raw());
}
/* the typeCallback's body */
static zv::Val resolveType(bool nativeTypesPromoted, zval *expr, zval *classResult, zval *nameResult, zval *beforeScope)
{
zv::Val classType;
if (Z_TYPE_P(classResult) != IS_NULL) {
classType = nativeTypesPromoted ? pt_expression_result_get_native_type(classResult) : pt_expression_result_get_type(classResult);
if (UNEXPECTED(classType.isUndef())) return zv::Val();
}
// the property's class/visibility/assign context is lexical, so it
// comes from beforeScope
zval *reflectionScope = beforeScope;
zv::Val staticPropertyFetchedOnType;
zval *class_ = exprClass(expr);
if (UNEXPECTED(class_ == NULL)) return zv::Val();
int classIsName = isInstanceOf(class_, PT_CLASS_NAME);
if (UNEXPECTED(classIsName < 0)) return zv::Val();
if (classIsName) {
staticPropertyFetchedOnType = pt_mutating_scope_resolve_type_by_name(Z_OBJ_P(reflectionScope), Z_OBJ_P(class_));
if (UNEXPECTED(staticPropertyFetchedOnType.isUndef())) return zv::Val();
} else {
// every caller walks a non-Name class and passes its result
if (classType.isUndef()) {
pt_throw_should_not_happen();
return zv::Val();
}
zv::Val withoutNull = pt_type_combinator_remove_null(classType.raw());
if (UNEXPECTED(withoutNull.isUndef())) return zv::Val();
staticPropertyFetchedOnType = pt_type_call(Z_OBJ_P(withoutNull.raw()), PT_LC("getobjecttypeorclassstringobjecttype"), 0, NULL);
if (UNEXPECTED(staticPropertyFetchedOnType.isUndef())) return zv::Val();
}
zval *name = exprName(expr);
if (UNEXPECTED(name == NULL)) return zv::Val();
int nameIsVarLikeIdentifier = isInstanceOf(name, PT_CLASS_VAR_LIKE_IDENTIFIER);
if (UNEXPECTED(nameIsVarLikeIdentifier < 0)) return zv::Val();
if (nameIsVarLikeIdentifier) {
zval *propertyName = identifierName(name);
if (UNEXPECTED(propertyName == NULL)) return zv::Val();
if (UNEXPECTED(Z_TYPE_P(propertyName) != IS_STRING)) {
zend_type_error("PhpParser\\Node\\Identifier::toString(): Return value must be of type string, %s returned", zend_zval_value_name(propertyName));
return zv::Val();
}
zv::Val type = resolveProperty(nativeTypesPromoted, reflectionScope, staticPropertyFetchedOnType.raw(), classType.isUndef() ? NULL : classType.raw(), classResult, expr, Z_STR_P(propertyName));
if (UNEXPECTED(type.isUndef())) return zv::Val();
// $shortCircuit($type)
if (Z_TYPE_P(classResult) != IS_NULL) {
bool containsNullsafe;
if (UNEXPECTED(!pt_expression_result_contains_nullsafe(classResult, containsNullsafe))) return zv::Val();
if (containsNullsafe && !classType.isUndef()) {
bool containsNull;
if (UNEXPECTED(!pt_type_combinator_contains_null(classType.raw(), containsNull))) return zv::Val();
if (containsNull) return pt_type_combinator_add_null(type.raw());
}
}
return type;
}
// dynamic static property fetch Foo::${$name}: resolve each possible
// name from beforeScope; every caller walks a non-VarLikeIdentifier
// name and passes its result
if (Z_TYPE_P(nameResult) == IS_NULL) {
pt_throw_should_not_happen();
return zv::Val();
}
zv::Val nameType = nativeTypesPromoted ? pt_expression_result_get_native_type(nameResult) : pt_expression_result_get_type(nameResult);
if (UNEXPECTED(nameType.isUndef())) return zv::Val();
zv::Val constantStrings = getConstantStrings(nameType.raw());
if (UNEXPECTED(constantStrings.isUndef())) return zv::Val();
if (UNEXPECTED(Z_TYPE_P(constantStrings.raw()) != IS_ARRAY)) {
zend_type_error("count(): Argument #1 ($value) must be of type Countable|array, %s given", zend_zval_value_name(constantStrings.raw()));
return zv::Val();
}
if (zend_hash_num_elements(Z_ARRVAL_P(constantStrings.raw())) == 0) return pt_type_new_mixed_type();
zv::Val iterated = getConstantStrings(nameType.raw());
if (UNEXPECTED(iterated.isUndef())) return zv::Val();
if (UNEXPECTED(Z_TYPE_P(iterated.raw()) != IS_ARRAY)) {
zend_type_error("array_map(): Argument #2 ($array) must be of type array, %s given", zend_zval_value_name(iterated.raw()));
return zv::Val();
}
zv::Arr types = zv::Arr::create(zend_hash_num_elements(Z_ARRVAL_P(iterated.raw())));
for (auto entry : zv::TableRef(Z_ARRVAL_P(iterated.raw()))) {
zval *constantString = entry.value().deref().raw();
if (UNEXPECTED(Z_TYPE_P(constantString) != IS_OBJECT)) {
zend_throw_error(NULL, "Call to a member function getValue() on %s", zend_zval_value_name(constantString));
return zv::Val();
}
zv::Val value = pt_type_op(Z_OBJ_P(constantString), PT_OP_GET_VALUE, 0, NULL);
if (UNEXPECTED(value.isUndef())) return zv::Val();
if (Z_TYPE_P(value.raw()) == IS_STRING && ZSTR_LEN(Z_STR_P(value.raw())) == 0) {
zv::Val error = pt_type_new_error_type();
if (UNEXPECTED(error.isUndef())) return zv::Val();
types.push(std::move(error));
continue;
}
if (UNEXPECTED(Z_TYPE_P(value.raw()) != IS_STRING)) {
zend_type_error("%s(): Argument #1 ($propertyName) must be of type string, %s given", pt_spfh_closure_name, zend_zval_value_name(value.raw()));
return zv::Val();
}
zv::Val type = resolveProperty(nativeTypesPromoted, reflectionScope, staticPropertyFetchedOnType.raw(), classType.isUndef() ? NULL : classType.raw(), classResult, expr, Z_STR_P(value.raw()));
if (UNEXPECTED(type.isUndef())) return zv::Val();
types.push(std::move(type));
}
HashTable *typesTable = types.table();
return HT_IS_PACKED(typesTable) && typesTable->nNumUsed == zend_hash_num_elements(typesTable)
? pt_type_combinator_union(zend_hash_num_elements(typesTable), typesTable->arPacked)
: pt_type_combinator_union(0, NULL);
}
/* fn (MutatingScope $s): ?FoundPropertyReflection =>
* $this->propertyReflectionFinder->findPropertyReflectionFromNode($expr, $s)
* — captures: $this, $expr */
static void reflectionResolverBody(zval *captures, uint32_t argc, zval *argv, zval *return_value)
{
if (UNEXPECTED(!requireArguments(argc, 1, pt_spfh_closure_name))) return;
zv::Val found = findPropertyReflectionFromNode(OBJ_PROP_NUM(Z_OBJ(captures[0]), slots::propertyReflectionFinder), &captures[1], &argv[0]);
if (UNEXPECTED(found.isUndef())) return;
found.intoReturnValue(return_value);
}
/* function (bool $nativeTypesPromoted) use ($expr, $classResult, $nameResult,
* $beforeScope): Type — captures: $this, $expr, $classResult, $nameResult,
* $beforeScope */
static void typeCallbackBody(zval *captures, uint32_t argc, zval *argv, zval *return_value)
{
if (UNEXPECTED(!requireArguments(argc, 1, pt_spfh_closure_name))) return;
zv::Val type = resolveType(zend_is_true(&argv[0]), &captures[1], &captures[2], &captures[3], &captures[4]);
if (UNEXPECTED(type.isUndef())) return;
type.intoReturnValue(return_value);
}
/* fn (TypeSpecifierContext $context, bool $nativeTypesPromoted): SpecifiedTypes =>
* $this->defaultNarrowingHelper->specifyDefaultTypes($expr, $context) —
* captures: $this, $expr */
static void specifyTypesCallbackBody(zval *captures, uint32_t argc, zval *argv, zval *return_value)
{
if (UNEXPECTED(!requireArguments(argc, 2, pt_spfh_closure_name))) return;
zv::Val specifiedTypes = pt_default_narrowing_helper_specify_default_types(OBJ_PROP_NUM(Z_OBJ(captures[0]), slots::defaultNarrowingHelper), &captures[1], &argv[0]);
if (UNEXPECTED(specifiedTypes.isUndef())) return;
specifiedTypes.intoReturnValue(return_value);
}
};
} // namespace phpstanturbo
using phpstanturbo::StaticPropertyFetchHandler;
zv::Val pt_static_property_fetch_handler_compose_result(zval *handler, zval *expr, zval *classResult, zval *nameResult, zval *beforeScope)
{
if (EXPECTED(Z_OBJCE_P(handler) == pt_ce_static_property_fetch_handler)) return StaticPropertyFetchHandler(Z_OBJ_P(handler)).composeResult(expr, classResult, nameResult, beforeScope);
zval null;
ZVAL_NULL(&null);
zv::Args argv{expr, classResult != NULL ? classResult : &null, nameResult != NULL ? nameResult : &null, beforeScope};
return pt_type_call(Z_OBJ_P(handler), PT_LC("composeresult"), 4, argv);
}
/* {{{ engine ABI glue: parameter parsing + registration */
#include "reg.h"
PT_MINIT_REGISTRATION(pt_register_static_property_fetch_handler)
{
pt_spfh_static_property_access = zend_string_init_interned(PT_LC("staticPropertyAccess"), 1);
pt_spfh_static_property_access_description = zend_string_init_interned(PT_LC("static property access"), 1);
reg::Class cls("PHPStan\\Analyser\\ExprHandler\\StaticPropertyFetchHandler");
ptdecl::StaticPropertyFetchHandler::declareClass(cls);
ptdecl::StaticPropertyFetchHandler::declareProperties(cls);
/* the real parameter class names: the DI container autowires the
* service by reflecting the constructor */
cls.method(sigs::__construct, [](INTERNAL_FUNCTION_PARAMETERS) {
zval *propertyReflectionFinder, *expressionResultFactory, *defaultNarrowingHelper, *reflectionProvider;
if (!zp::parse<zp::Obj, zp::Obj, zp::Obj, zp::Obj>(execute_data, propertyReflectionFinder, expressionResultFactory, defaultNarrowingHelper, reflectionProvider)) RETURN_THROWS();
StaticPropertyFetchHandler(Z_OBJ_P(ZEND_THIS)).construct(propertyReflectionFinder, expressionResultFactory, defaultNarrowingHelper, reflectionProvider);
});
cls.method<&StaticPropertyFetchHandler::supports, zp::Obj>(sigs::supports);
cls.method(sigs::processExpr, [](INTERNAL_FUNCTION_PARAMETERS) {
zval *nodeScopeResolver, *stmt, *expr, *scope, *storage, *nodeCallback, *context;
ZEND_PARSE_PARAMETERS_START(7, 7)
Z_PARAM_OBJECT(nodeScopeResolver)
Z_PARAM_OBJECT(stmt)
Z_PARAM_OBJECT(expr)
Z_PARAM_OBJECT(scope)
Z_PARAM_OBJECT(storage)
Z_PARAM_ZVAL(nodeCallback)
Z_PARAM_OBJECT(context)
ZEND_PARSE_PARAMETERS_END();
PT_RETURN_VAL(StaticPropertyFetchHandler(Z_OBJ_P(ZEND_THIS)).processExpr(nodeScopeResolver, stmt, expr, scope, storage, nodeCallback, context));
});
cls.method(sigs::composeResult, [](INTERNAL_FUNCTION_PARAMETERS) {
zval *expr, *classResult, *nameResult, *beforeScope;
if (!zp::parse<zp::Obj, zp::ObjOrNull, zp::ObjOrNull, zp::Obj>(execute_data, expr, classResult, nameResult, beforeScope)) RETURN_THROWS();
PT_RETURN_VAL(StaticPropertyFetchHandler(Z_OBJ_P(ZEND_THIS)).composeResult(expr, classResult, nameResult, beforeScope));
});
cls.shadow(&pt_ce_static_property_fetch_handler);
pt_expr_handler_entry_register(&pt_ce_static_property_fetch_handler, &StaticPropertyFetchHandler::processExprEntry);
}
/* }}} */