Repository navigation
Expand file tree
/
Copy pathPhpClassReflectionExtension.cpp
More file actions
3077 lines (2836 loc) · 150 KB
/
Copy pathPhpClassReflectionExtension.cpp
File metadata and controls
3077 lines (2836 loc) · 150 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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* PHPStanTurbo\PhpClassReflectionExtension — native implementation of
* PHPStan\Reflection\Php\PhpClassReflectionExtension, declared under the
* twin's real name at activation (final, like the twin).
*
* Why. ClassReflection asks this service for every member it is asked for:
* hasMethod() 807K, hasNativeMethod() 746K, touchMemberCacheKey() 399K,
* getNativeMethod() 292K, hasProperty() 156K and getNativeProperty() 78K
* times in a self-analysis of src/Analyser, src/Rules and src/Type. Those
* bodies are three lines each around a memo array, and they call back into
* ClassReflection::getCacheKey() 1.24M and ::getNativeReflection() 1.01M
* times — trivial memo getters that the pt_class_reflection_*() direct
* calls answer without a frame — and into the already
* native LruCache 0.40M times. The member construction below them
* (createProperty 10K, createMethod 22K, createUserlandMethodReflection
* 17K) is memoized and cold, but a shadowed class has no PHP body left to
* fall back to, so it is ported too.
*
* Design
* ------
* Class shape. The twin is final: no PHP subclass exists, so every
* `$this->method()` is a direct C++ call — no Z_OBJCE dispatch. The class
* is an #[AutowiredService]: Nette reflects __construct while it compiles
* the container and pairs #[AutowiredParameter] by name, so the arginfo
* declares the twin's exact parameter names AND class names (README rule
* 6). $memberCacheKeysMax is the one non-promoted parameter.
*
* Layout. The twin's properties are typed property slots in the twin's
* declaration order: the seven class-body properties first (the LruCache
* and the six memo arrays), then the seventeen promoted constructor
* properties in parameter order. The std object handlers do GC/clone/free.
* The names are load-bearing — the differential harness
* (tests/php-class-reflection-family.php) reads the constructor arguments
* and the memo state of both sides by reflection.
*
* Collaborators. The fifteen injected services and the BetterReflection
* adapters ($classReflection->getNativeReflection(), the property and
* method reflections, their tags and ResolvedPhpDocBlocks) are PHP objects
* called by name (pt_type_call). ClassReflection's two hot getters go
* through the direct calls ClassReflection.cpp exports
* (pt_class_reflection_get_cache_key / _get_native_reflection); every
* other ClassReflection method is a by-name call, as is
* ClassMemberAccessAnswerer's isInClass()/getClassReflection() (through
* pt_scope_is_in_class / pt_scope_get_class_reflection, which fast-path a
* MutatingScope and call the method on anything else). The shared member
* LRU is the native LruCache through its exported helpers. The Type kernel
* is reached natively: TypeCombinator::union()/intersect(),
* TemplateTypeHelper::resolveTemplateTypes(),
* TypehintHelper::decideTypeFromReflection()/decideType(),
* ConstantArrayTypeBuilder, TemplateTypeMap::createEmpty(), the
* TemplateTypeVariance singletons and `new` of the shadowing
* ConstantStringType / StringType / MixedType / ArrayType /
* AccessoryNonFalsyStringType / AccessoryDecimalIntegerStringType /
* EnumCaseObjectType; `instanceof` against MixedType / TemplateMixedType /
* ErrorType / NeverType / UnionType uses their class entries directly.
* Everything else (PhpPropertyReflection, NativeMethodReflection,
* ExtendedNativeParameterReflection, EnumCasesMethodReflection,
* ExtendedFunctionVariant, Assertions, InitializerExprContext,
* OutOfClassScope, ShouldNotHappenException, the PrivateProperty /
* ProtectedProperty attribute classes, the BetterReflection adapter's
* ReflectionMethod and the parser nodes read by the constructor-inference
* pass) goes through pt_type_new / pt_type_call_static / pt_type_instanceof
* on class-map keys.
*
* getCacheKey() is called once per method where the twin calls it three or
* four times: it memoizes into $cacheKey and is a pure read afterwards, so
* the repeated calls are the same string by construction.
*
* The logic lives in the PhpClassReflectionExtension handle class below,
* structured to mirror src/Reflection/Php/PhpClassReflectionExtension.php
* method for method and in the same order; the registration at the bottom
* is only the engine ABI glue (parameter parsing + delegation).
*/
#include "support.h"
#include "generated/PhpClassReflectionExtension.h"
namespace slots = ptdecl::PhpClassReflectionExtension::slot;
namespace sigs = ptdecl::PhpClassReflectionExtension::sig;
#include "zv.h"
#include "TypeTraits.h"
#include "TypeOps.h"
#include <cstring>
#include <vector>
zend_class_entry *pt_ce_php_class_reflection_extension = nullptr;
namespace {
/* {{{ small engine helpers */
/* $object->method(...$args); UNDEF = pending exception */
inline zv::Val call(zval *object, const char *lcname, size_t len, uint32_t argc = 0, zval *argv = NULL)
{
return pt_type_call(Z_OBJ_P(object), lcname, len, argc, argv);
}
/* a ResolvedPhpDocBlock getter coerced to bool; false with `ok` cleared on a
* pending exception */
inline bool resolvedPhpDocBool(zval *block, pt_resolved_php_doc_member member, bool &ok)
{
bool out = false;
ok = pt_resolved_php_doc_block_bool(block, member, out);
return ok && out;
}
/* $object->method(...$args) coerced to bool; false with `ok` cleared on a
* pending exception */
inline bool callBool(zval *object, const char *lcname, size_t len, uint32_t argc, zval *argv, bool &ok)
{
zv::Val result = pt_type_call(Z_OBJ_P(object), lcname, len, argc, argv);
if (UNEXPECTED(result.isUndef())) {
ok = false;
return false;
}
ok = true;
return zend_is_true(result.raw());
}
/* new ShouldNotHappenException() — the twin's default message ("Internal
* error.") comes from its own constructor; thrown, never returned */
void throwShouldNotHappen()
{
zv::Val exception = pt_type_new(PT_CLASS_SHOULD_NOT_HAPPEN, 0, NULL);
if (exception.isUndef()) return;
zval thrown = exception.take();
zend_throw_exception_object(&thrown);
}
void throwShouldNotHappenStr(zend_string *message)
{
zend_class_entry *ce = pt_class(PT_CLASS_SHOULD_NOT_HAPPEN);
if (ce == NULL) return;
zend_throw_exception(ce, ZSTR_VAL(message), 0);
}
/* the twin's two "Internal error: Expected to find an ancestor …" messages */
zv::Str ancestorMessage(zend_string *declaringClassName, zend_string *className)
{
return zv::Str::adopt(zend_strpprintf(0, "Internal error: Expected to find an ancestor with class name %s on %s, but none was found.", ZSTR_VAL(declaringClassName), ZSTR_VAL(className)));
}
/* the result of a call declared `: array`; false (an exception pending) when
* the call threw or when the callee broke its signature — the engine's
* return-type TypeError, which a PHP callee would have thrown itself */
[[nodiscard]] bool arrayResult(zv::Val &result, const char *method)
{
if (UNEXPECTED(result.isUndef())) return false;
if (EXPECTED(Z_TYPE_P(result.raw()) == IS_ARRAY)) return true;
zend_type_error("%s(): Return value must be of type array, %s returned", method, zend_zval_value_name(result.raw()));
return false;
}
/* isset($array[$key]) over a symtable-keyed array: the value, or NULL when
* the key is absent or holds null */
zval *issetIn(zval *array, zend_string *key)
{
if (Z_TYPE_P(array) != IS_ARRAY) return NULL;
zval *found = zend_symtable_find(Z_ARRVAL_P(array), key);
if (found == NULL) return NULL;
ZVAL_DEREF(found);
return Z_TYPE_P(found) == IS_NULL ? NULL : found;
}
/* $array[$key] ?? null over an array keyed by strings (array_key_exists
* semantics: a stored null is returned as the null zval) */
zval *keyIn(zval *array, zend_string *key)
{
if (Z_TYPE_P(array) != IS_ARRAY) return NULL;
zval *found = zend_symtable_find(Z_ARRVAL_P(array), key);
if (found != NULL) {
ZVAL_DEREF(found);
}
return found;
}
/* $array[$key] = $value on a property slot holding an array */
void setIn(zval *array, zend_string *key, zv::Val value)
{
SEPARATE_ARRAY(array);
zval v = value.take();
zend_symtable_update(Z_ARRVAL_P(array), key, &v);
}
/* $array[$key][$subKey] = $value, creating the inner array when missing */
void setNested(zval *array, zend_string *key, zend_string *subKey, zv::Val value)
{
SEPARATE_ARRAY(array);
zval *inner = zend_symtable_find(Z_ARRVAL_P(array), key);
if (inner != NULL) {
ZVAL_DEREF(inner);
}
if (inner == NULL || Z_TYPE_P(inner) != IS_ARRAY) {
zval fresh;
array_init(&fresh);
inner = zend_symtable_update(Z_ARRVAL_P(array), key, &fresh);
} else {
SEPARATE_ARRAY(inner);
}
zval v = value.take();
zend_symtable_update(Z_ARRVAL_P(inner), subKey, &v);
}
/* isset($array[$key][$subKey]) — the value or NULL */
zval *issetNested(zval *array, zend_string *key, zend_string *subKey)
{
zval *inner = issetIn(array, key);
if (inner == NULL) return NULL;
return issetIn(inner, subKey);
}
/* unset($array[$key]) */
void unsetIn(zval *array, zend_string *key)
{
if (Z_TYPE_P(array) != IS_ARRAY) return;
SEPARATE_ARRAY(array);
zend_symtable_del(Z_ARRVAL_P(array), key);
}
/* a string-returning call ($x->getName(), ->toString(), …); the Str is
* null with an exception pending */
zv::Str callString(zval *object, const char *lcname, size_t len, uint32_t argc = 0, zval *argv = NULL)
{
zv::Val result = pt_type_call(Z_OBJ_P(object), lcname, len, argc, argv);
if (UNEXPECTED(result.isUndef()) || Z_TYPE_P(result.raw()) != IS_STRING) return zv::Str::adopt(NULL);
return zv::Str::adopt(zend_string_copy(Z_STR_P(result.raw())));
}
/* a string getter's answer as an owned string; null with the exception
* pending when the getter threw */
zv::Str stringOf(zv::Val result)
{
if (UNEXPECTED(result.isUndef()) || Z_TYPE_P(result.raw()) != IS_STRING) return zv::Str::adopt(NULL);
return zv::Str::copyOf(Z_STR_P(result.raw()));
}
/* }}} */
/* {{{ the BetterReflection adapter's member memos
*
* hasMethod() and hasProperty() are what this service is asked for most
* (0.96M calls in a self-analysis of src/Analyser, src/Rules and src/Type)
* and their whole body is
* $classReflection->getNativeReflection()->has*($name) — three PHP frames
* around one array lookup:
*
* Adapter\ReflectionClass::hasMethod() '' => false, else delegate
* ReflectionClass::hasMethod() getMethod() !== null
* ReflectionClass::getMethod() ($this->cachedMethods ?? compute())[strtolower($name)] ?? null
*
* (hasProperty() is the same shape over $cachedProperties, keyed by the
* exact name, and getMethod() adds `new Adapter\ReflectionMethod($m)`.)
*
* The memos are private properties of
* PHPStan\BetterReflection\Reflection\ReflectionClass filled lazily, so
* these readers answer from them once they are filled and leave every
* other case — an unfilled memo, an adapter of some other class, the
* empty name, the exception getMethod() raises for a missing method — to
* the adapter's own methods (the class entries and offsets are the shared
* BetterReflection readers', BetterReflectionAccess.cpp). */
/* $betterReflection->cachedMethods / ->cachedProperties behind an adapter
* the readers know once the library filled it, NULL while it is still null */
zval *adapterMemo(zval *adapter, bool methods)
{
zend_object *betterReflection = pt_better_reflection_class_of_adapter(adapter);
if (betterReflection == NULL) return NULL;
return methods ? pt_better_reflection_class_cached_methods(betterReflection) : pt_better_reflection_class_cached_properties(betterReflection);
}
/* $methods[strtolower($name)] ?? null over the lowercased-name memo */
zval *memoFindLowercased(zval *memo, zend_string *name)
{
size_t len = ZSTR_LEN(name);
char buffer[128];
if (EXPECTED(len < sizeof(buffer))) {
zend_str_tolower_copy(buffer, ZSTR_VAL(name), len);
zval *found = zend_symtable_str_find(Z_ARRVAL_P(memo), buffer, len);
return found != NULL && Z_TYPE_P(found) != IS_NULL ? found : NULL;
}
zend_string *lower = zend_string_tolower(name);
zval *found = zend_symtable_find(Z_ARRVAL_P(memo), lower);
zend_string_release(lower);
return found != NULL && Z_TYPE_P(found) != IS_NULL ? found : NULL;
}
/* }}} */
} // namespace
namespace {
/* the constructor's values, borrowed; $memberCacheKeysMax is the one
* parameter the twin does not promote */
struct ConstructorArgs
{
zval *scopeFactory;
zval *phpDocsResolver;
zval *nodeScopeResolver;
zval *methodReflectionFactory;
zval *phpDocInheritanceResolver;
zval *deprecationProvider;
zval *annotationsMethodsClassReflectionExtension;
zval *annotationsPropertiesClassReflectionExtension;
zval *signatureMapProvider;
zval *parser;
zval *stubPhpDocProvider;
zval *reflectionProviderProvider;
zval *fileTypeMapper;
zval *attributeReflectionFactory;
zval *allowedConstantsMapProvider;
bool inferPrivatePropertyTypeFromConstructor;
zval *phpVersion;
bool backedEnumFromParameterType;
zend_long memberCacheKeysMax;
};
} // namespace
namespace phpstanturbo {
/*
* Mirrors PHPStan\Reflection\Php\PhpClassReflectionExtension. State lives
* in the PHP object's property slots. Methods returning zv::Val use UNDEF
* to signal a pending exception, a legitimate PHP null is zv::Val::null();
* methods returning bool with an `out` parameter return false on a pending
* exception.
*/
class PhpClassReflectionExtension
{
public:
explicit PhpClassReflectionExtension(zend_object *self) : self(self) {}
/* {{{ the slots */
zval *slot(uint32_t index) const { return OBJ_PROP_NUM(self, index); }
/* }}} */
/* Mirrors __construct(): the promoted properties, then
* $this->memberCacheOrder = new LruCache($memberCacheKeysMax).
* false = pending exception */
[[nodiscard]] static bool construct(zend_object *object, const ConstructorArgs &a)
{
static const struct { uint32_t slot; size_t offset; } promoted[] = {
{ slots::scopeFactory, offsetof(ConstructorArgs, scopeFactory) },
{ slots::phpDocsResolver, offsetof(ConstructorArgs, phpDocsResolver) },
{ slots::nodeScopeResolver, offsetof(ConstructorArgs, nodeScopeResolver) },
{ slots::methodReflectionFactory, offsetof(ConstructorArgs, methodReflectionFactory) },
{ slots::phpDocInheritanceResolver, offsetof(ConstructorArgs, phpDocInheritanceResolver) },
{ slots::deprecationProvider, offsetof(ConstructorArgs, deprecationProvider) },
{ slots::annotationsMethodsClassReflectionExtension, offsetof(ConstructorArgs, annotationsMethodsClassReflectionExtension) },
{ slots::annotationsPropertiesClassReflectionExtension, offsetof(ConstructorArgs, annotationsPropertiesClassReflectionExtension) },
{ slots::signatureMapProvider, offsetof(ConstructorArgs, signatureMapProvider) },
{ slots::parser, offsetof(ConstructorArgs, parser) },
{ slots::stubPhpDocProvider, offsetof(ConstructorArgs, stubPhpDocProvider) },
{ slots::reflectionProviderProvider, offsetof(ConstructorArgs, reflectionProviderProvider) },
{ slots::fileTypeMapper, offsetof(ConstructorArgs, fileTypeMapper) },
{ slots::attributeReflectionFactory, offsetof(ConstructorArgs, attributeReflectionFactory) },
{ slots::allowedConstantsMapProvider, offsetof(ConstructorArgs, allowedConstantsMapProvider) },
{ slots::phpVersion, offsetof(ConstructorArgs, phpVersion) },
};
for (const auto &entry : promoted) {
zval *value = *(zval *const *) ((const char *) &a + entry.offset);
ZVAL_COPY(OBJ_PROP_NUM(object, entry.slot), value);
}
ZVAL_BOOL(OBJ_PROP_NUM(object, slots::inferPrivatePropertyTypeFromConstructor), a.inferPrivatePropertyTypeFromConstructor);
ZVAL_BOOL(OBJ_PROP_NUM(object, slots::backedEnumFromParameterType), a.backedEnumFromParameterType);
zval lru;
if (UNEXPECTED(!pt_lru_cache_new(&lru, a.memberCacheKeysMax))) return false;
ZVAL_COPY_VALUE(OBJ_PROP_NUM(object, slots::memberCacheOrder), &lru);
return true;
}
/*
* Mirrors touchMemberCacheKey(): moves the key to the most recently
* used position of the shared LRU and drops the evicted keys' entries
* from all four member caches. false = pending exception
*/
[[nodiscard]] bool touchMemberCacheKey(zend_string *cacheKey)
{
zval *order = slot(slots::memberCacheOrder);
zv::Val current = pt_lru_cache_get(order, cacheKey);
if (UNEXPECTED(current.isUndef())) return false;
if (Z_TYPE_P(current.raw()) != IS_NULL) return true;
zval trueValue;
ZVAL_TRUE(&trueValue);
zv::Val evicted = pt_lru_cache_set(order, cacheKey, &trueValue, 0);
if (UNEXPECTED(evicted.isUndef())) return false;
if (Z_TYPE_P(evicted.raw()) != IS_ARRAY || zend_hash_num_elements(Z_ARRVAL_P(evicted.raw())) == 0) return true;
for (zv::ArrayEntry entry : zv::ArrRef(evicted.raw())) {
zval *evictKey = entry.value().raw();
if (UNEXPECTED(Z_TYPE_P(evictKey) != IS_STRING)) continue;
/* the key lives in the evicted list, which outlives the loop */
zend_string *key = Z_STR_P(evictKey);
unsetIn(slot(slots::methodsIncludingAnnotations), key);
unsetIn(slot(slots::nativeMethods), key);
unsetIn(slot(slots::propertiesIncludingAnnotations), key);
unsetIn(slot(slots::nativeProperties), key);
}
return true;
}
/* Mirrors hasProperty(). false with `ok` cleared = pending exception */
[[nodiscard]] bool hasProperty(zval *classReflection, zend_string *propertyName, bool &ok)
{
zv::Val nativeReflection = pt_class_reflection_get_native_reflection(Z_OBJ_P(classReflection));
if (UNEXPECTED(nativeReflection.isUndef())) {
ok = false;
return false;
}
zval *memo = adapterMemo(nativeReflection.raw(), false);
if (EXPECTED(memo != NULL)) {
ok = true;
if (ZSTR_LEN(propertyName) == 0) return false;
zval *found = zend_symtable_find(Z_ARRVAL_P(memo), propertyName);
return found != NULL && Z_TYPE_P(found) != IS_NULL;
}
zval name;
ZVAL_STR(&name, propertyName);
return callBool(nativeReflection.raw(), PT_LC("hasproperty"), 1, &name, ok);
}
/* Mirrors getProperty(). */
zv::Val getProperty(zval *classReflection, zend_string *propertyName, zval *scope)
{
zv::Val classCacheKey = pt_class_reflection_get_cache_key(Z_OBJ_P(classReflection));
if (UNEXPECTED(classCacheKey.isUndef())) return zv::Val();
zv::Str cacheKey = zv::Str::copyOf(Z_STR_P(classCacheKey.raw()));
bool isInClass;
if (UNEXPECTED(!pt_scope_is_in_class(Z_OBJ_P(scope), isInClass))) return zv::Val();
if (isInClass) {
zv::Val scopeClassReflection = pt_scope_get_class_reflection(Z_OBJ_P(scope));
if (UNEXPECTED(scopeClassReflection.isUndef())) return zv::Val();
zv::Val scopeCacheKey = pt_class_reflection_get_cache_key(Z_OBJ_P(scopeClassReflection.raw()));
if (UNEXPECTED(scopeCacheKey.isUndef())) return zv::Val();
cacheKey = zv::Str::adopt(zend_strpprintf(0, "%s-%s", ZSTR_VAL(cacheKey.get()), Z_STRVAL_P(scopeCacheKey.raw())));
}
if (UNEXPECTED(!touchMemberCacheKey(cacheKey.get()))) return zv::Val();
zval *cache = slot(slots::propertiesIncludingAnnotations);
zval *cached = issetNested(cache, cacheKey.get(), propertyName);
if (cached != NULL) return zv::Val::copyOf(zv::Ref(cached));
zv::Val property = createProperty(classReflection, propertyName, scope, true);
if (UNEXPECTED(property.isUndef())) return zv::Val();
zv::Val result = zv::Val::copyOf(zv::Ref(property.raw()));
setNested(slot(slots::propertiesIncludingAnnotations), cacheKey.get(), propertyName, std::move(property));
return result;
}
/* Mirrors getNativeProperty(). */
zv::Val getNativeProperty(zval *classReflection, zend_string *propertyName)
{
zv::Val classCacheKey = pt_class_reflection_get_cache_key(Z_OBJ_P(classReflection));
if (UNEXPECTED(classCacheKey.isUndef())) return zv::Val();
zv::Str cacheKey = zv::Str::copyOf(Z_STR_P(classCacheKey.raw()));
if (UNEXPECTED(!touchMemberCacheKey(cacheKey.get()))) return zv::Val();
zval *cached = issetNested(slot(slots::nativeProperties), cacheKey.get(), propertyName);
if (cached != NULL) return zv::Val::copyOf(zv::Ref(cached));
zv::Val outOfClassScope = pt_type_new(PT_CLASS_OUT_OF_CLASS_SCOPE, 0, NULL);
if (UNEXPECTED(outOfClassScope.isUndef())) return zv::Val();
zv::Val property = createProperty(classReflection, propertyName, outOfClassScope.raw(), false);
if (UNEXPECTED(property.isUndef())) return zv::Val();
zv::Val result = zv::Val::copyOf(zv::Ref(property.raw()));
setNested(slot(slots::nativeProperties), cacheKey.get(), propertyName, std::move(property));
return result;
}
/*
* `$value instanceof <ShadowedTypeClass>` — the native class entry
* first, and under the prefixed activation of the differential tests
* also the PHP twin that still carries the real name (the container's
* Type objects are the twins there). In a production run the native
* class carries the real name and the second lookup never runs.
*/
static bool instanceOfShadowed(zval *value, zend_class_entry *ce, const char *realName, size_t len)
{
if (Z_TYPE_P(value) != IS_OBJECT) return false;
if (ce != NULL && instanceof_function(Z_OBJCE_P(value), ce)) return true;
if (ce != NULL && zend_string_equals_cstr(ce->name, realName, len)) return false;
zend_string *name = zend_string_init(realName, len, 0);
zend_class_entry *twin = zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
zend_string_release(name);
return twin != NULL && instanceof_function(Z_OBJCE_P(value), twin);
}
/*
* Class::method(...$args) by the class's REAL name: the native class in
* a production run, the PHP twin's under the prefixed activation of the
* differential tests. The singletons below all end up in typed
* parameters of PHP collaborators (PhpMethodReflection's TrinaryLogic
* maps, the factory's TemplateTypeMap, StaticType's positionVariance),
* which would refuse a PHPStanTurbo\* instance there. UNDEF = pending
* exception.
*/
static zv::Val kernelStatic(const char *className, size_t classLen, const char *lcmethod, size_t methodLen, uint32_t argc = 0, zval *argv = NULL)
{
zend_string *name = zend_string_init(className, classLen, 0);
zend_class_entry *ce = zend_lookup_class(name);
zend_string_release(name);
if (UNEXPECTED(ce == NULL)) {
if (!EG(exception)) {
zend_throw_error(NULL, "phpstan_turbo: class %s not found", className);
}
return zv::Val();
}
zend_function *fn = (zend_function *) zend_hash_str_find_ptr(&PT_CE(ce, function_table), lcmethod, methodLen);
if (UNEXPECTED(fn == NULL)) {
zend_throw_error(NULL, "phpstan_turbo: method %s::%s not found", className, lcmethod);
return zv::Val();
}
zval result;
zend_call_known_function(fn, NULL, ce, &result, argc, argv, NULL);
if (UNEXPECTED(EG(exception))) {
zval_ptr_dtor(&result);
return zv::Val();
}
return zv::Val::adopt(result);
}
/*
* new <Class>(...$args) by the class's REAL name, for the same reason as
* kernelStatic(): the native class in a production run, the PHP twin
* under the prefixed activation — the Type instances built here end up
* in PHP collaborators that test them with `instanceof`
* (PhpPropertyReflection::hasNativeType() against MixedType), which a
* PHPStanTurbo\* instance would answer differently there. Every site is
* cold (member construction is memoized). UNDEF = pending exception.
*/
static zv::Val kernelNew(const char *className, size_t classLen, uint32_t argc = 0, zval *argv = NULL)
{
zend_string *name = zend_string_init(className, classLen, 0);
zend_class_entry *ce = zend_lookup_class(name);
zend_string_release(name);
if (UNEXPECTED(ce == NULL)) {
if (!EG(exception)) {
zend_throw_error(NULL, "phpstan_turbo: class %s not found", className);
}
return zv::Val();
}
zval object;
if (UNEXPECTED(object_init_ex(&object, ce) != SUCCESS)) return zv::Val();
if (PT_CE(ce, constructor) != NULL) {
zend_call_known_instance_method(PT_CE(ce, constructor), Z_OBJ(object), NULL, argc, argv);
if (UNEXPECTED(EG(exception))) {
zval_ptr_dtor(&object);
return zv::Val();
}
}
return zv::Val::adopt(object);
}
/* Class::method(...$args) by the real name, arguments spread from a PHP
* list (TypeCombinator::union(...$types)) */
static zv::Val kernelStaticSpread(const char *className, size_t classLen, const char *lcmethod, size_t methodLen, HashTable *args)
{
uint32_t argc = zend_hash_num_elements(args);
std::vector<zval> argv;
argv.reserve(argc);
for (zv::ArrayEntry entry : zv::TableRef(args)) {
argv.push_back(*entry.value().raw());
}
return kernelStatic(className, classLen, lcmethod, methodLen, argc, argv.empty() ? NULL : argv.data());
}
/* TypehintHelper::decideTypeFromReflection($reflectionType, null,
* $selfClass) / ::decideType($type, $phpDocType) by the real name: the
* native helper builds its unions out of the native Type family, which
* the PHP twins next to it cannot describe under the prefixed
* activation (a native VerbosityLevel reaches a PHP Type::describe()).
* In a production run the real name IS the native class. */
static zv::Val decideTypeFromReflection(zval *reflectionType, zval *selfClass)
{
zv::Args args{reflectionType, zv::null, selfClass};
return kernelStatic(PT_LC("PHPStan\\Type\\TypehintHelper"), PT_LC("decidetypefromreflection"), 3, args);
}
static zv::Val mixedType() { return kernelNew(PT_LC("PHPStan\\Type\\MixedType")); }
static zv::Val explicitMixedType()
{
zval isExplicit;
ZVAL_TRUE(&isExplicit);
return kernelNew(PT_LC("PHPStan\\Type\\MixedType"), 1, &isExplicit);
}
static zv::Val stringType() { return kernelNew(PT_LC("PHPStan\\Type\\StringType")); }
static zv::Val constantStringType(zend_string *value)
{
zval arg;
ZVAL_STR(&arg, value);
return kernelNew(PT_LC("PHPStan\\Type\\Constant\\ConstantStringType"), 1, &arg);
}
static zv::Val enumCaseObjectType(zend_string *className, zend_string *caseName)
{
zv::Args args{className, caseName};
return kernelNew(PT_LC("PHPStan\\Type\\Enum\\EnumCaseObjectType"), 2, args);
}
static zv::Val arrayType(zval *keyType, zval *itemType)
{
zv::Args args{keyType, itemType};
return kernelNew(PT_LC("PHPStan\\Type\\ArrayType"), 2, args);
}
static zv::Val trinaryMaybe() { return kernelStatic(PT_LC("PHPStan\\TrinaryLogic"), PT_LC("createmaybe")); }
static zv::Val trinaryNo() { return kernelStatic(PT_LC("PHPStan\\TrinaryLogic"), PT_LC("createno")); }
static zv::Val trinaryFromBoolean(bool value)
{
return value
? kernelStatic(PT_LC("PHPStan\\TrinaryLogic"), PT_LC("createyes"))
: kernelStatic(PT_LC("PHPStan\\TrinaryLogic"), PT_LC("createno"));
}
static zv::Val varianceInvariant() { return kernelStatic(PT_LC("PHPStan\\Type\\Generic\\TemplateTypeVariance"), PT_LC("createinvariant")); }
static zv::Val varianceCovariant() { return kernelStatic(PT_LC("PHPStan\\Type\\Generic\\TemplateTypeVariance"), PT_LC("createcovariant")); }
static zv::Val varianceContravariant() { return kernelStatic(PT_LC("PHPStan\\Type\\Generic\\TemplateTypeVariance"), PT_LC("createcontravariant")); }
static zv::Val templateTypeMapEmpty() { return kernelStatic(PT_LC("PHPStan\\Type\\Generic\\TemplateTypeMap"), PT_LC("createempty")); }
static bool isMixedType(zval *value) { return instanceOfShadowed(value, pt_ce_mixed_type, PT_LC("PHPStan\\Type\\MixedType")); }
static bool isTemplateMixedType(zval *value) { return instanceOfShadowed(value, pt_ce_template_mixed_type, PT_LC("PHPStan\\Type\\Generic\\TemplateMixedType")); }
static bool isErrorType(zval *value) { return instanceOfShadowed(value, pt_ce_error_type, PT_LC("PHPStan\\Type\\ErrorType")); }
static bool isNeverType(zval *value) { return instanceOfShadowed(value, pt_ce_never_type, PT_LC("PHPStan\\Type\\NeverType")); }
static bool isUnionType(zval *value) { return instanceOfShadowed(value, pt_ce_union_type, PT_LC("PHPStan\\Type\\UnionType")); }
/* $type->isSuperTypeOf($other)->yes(); false with `ok` cleared on a
* pending exception */
static bool isSuperTypeOfYes(zval *type, zval *other, bool &ok)
{
zv::Val result = pt_type_op(Z_OBJ_P(type), PT_OP_IS_SUPER_TYPE_OF, 1, other);
if (UNEXPECTED(result.isUndef())) {
ok = false;
return false;
}
zend_long value = pt_type_result_trinary(result.raw());
if (UNEXPECTED(value < 0)) {
ok = false;
return false;
}
ok = true;
return value == PT_TRI_YES;
}
/* $type->isVoid()->yes() / ->isConstantArray()->yes() */
static bool typeOpYes(zval *type, pt_type_op_id op, bool &ok)
{
zend_long value = pt_type_op_trinary(Z_OBJ_P(type), op, 0, NULL);
if (UNEXPECTED(value < 0)) {
ok = false;
return false;
}
ok = true;
return value == PT_TRI_YES;
}
/* the ?string a getDocComment() returns as string|false */
static zv::Val docCommentOf(zval *reflection)
{
zv::Val docComment = pt_better_reflection_method_of_adapter(reflection) != NULL
? pt_method_adapter_get_doc_comment(reflection)
: pt_property_adapter_get_doc_comment(reflection);
if (UNEXPECTED(docComment.isUndef())) return zv::Val();
if (Z_TYPE_P(docComment.raw()) != IS_STRING) return zv::Val::null();
return docComment;
}
/* $this->fileTypeMapper->getResolvedPhpDoc($fileName, $className, $traitName, $functionName, $docComment) */
zv::Val getResolvedPhpDoc(zval *fileName, zval *className, zval *traitName, zval *functionName, zval *docComment)
{
zv::Args args{fileName, className, traitName, functionName, docComment};
return call(slot(slots::fileTypeMapper), PT_LC("getresolvedphpdoc"), 5, args);
}
/* InitializerExprContext::fromClass($className, $fileName) */
static zv::Val initializerExprContextFromClass(zval *className, zval *fileName)
{
return pt_initializer_expr_context_from_class(className, fileName);
}
/* $this->attributeReflectionFactory->fromNativeReflection($reflection->getAttributes(), $context) */
zv::Val attributesOf(zval *reflection, zval *context)
{
zv::Val attributes = call(reflection, PT_LC("getattributes"));
if (UNEXPECTED(attributes.isUndef())) return zv::Val();
zv::Args args{attributes.raw(), context};
return call(slot(slots::attributeReflectionFactory), PT_LC("fromnativereflection"), 2, args);
}
/* the first of a @var tag list the twin picks: $varTags[0] when it is
* the only one, else $varTags[$propertyName]; NULL for neither */
static zval *varTagFor(zval *varTags, zend_string *propertyName)
{
if (Z_TYPE_P(varTags) != IS_ARRAY) return NULL;
zval *first = zend_hash_index_find(Z_ARRVAL_P(varTags), 0);
if (first != NULL) {
ZVAL_DEREF(first);
if (Z_TYPE_P(first) != IS_NULL && zend_hash_num_elements(Z_ARRVAL_P(varTags)) == 1) return first;
}
return issetIn(varTags, propertyName);
}
/* Mirrors createProperty(). */
zv::Val createProperty(zval *classReflection, zend_string *requestedPropertyName, zval *scope, bool includingAnnotations)
{
bool ok;
zv::Val nativeReflection = pt_class_reflection_get_native_reflection(Z_OBJ_P(classReflection));
if (UNEXPECTED(nativeReflection.isUndef())) return zv::Val();
zval requestedNameArg;
ZVAL_STR(&requestedNameArg, requestedPropertyName);
zv::Val propertyReflection = call(nativeReflection.raw(), PT_LC("getproperty"), 1, &requestedNameArg);
if (UNEXPECTED(propertyReflection.isUndef())) return zv::Val();
zv::Str propertyNameStr = stringOf(pt_property_adapter_get_name(propertyReflection.raw()));
if (UNEXPECTED(propertyNameStr.isNull())) return zv::Val();
zend_string *propertyName = propertyNameStr.get();
zval propertyNameArg;
ZVAL_STR(&propertyNameArg, propertyName);
zv::Str declaringClassNameStr = stringOf(pt_member_adapter_get_declaring_class_name(propertyReflection.raw()));
if (UNEXPECTED(declaringClassNameStr.isNull())) return zv::Val();
zval declaringClassNameArg;
ZVAL_STR(&declaringClassNameArg, declaringClassNameStr.get());
zv::Val declaringClassReflection = call(classReflection, PT_LC("getancestorwithclassname"), 1, &declaringClassNameArg);
if (UNEXPECTED(declaringClassReflection.isUndef())) return zv::Val();
if (Z_TYPE_P(declaringClassReflection.raw()) != IS_OBJECT) {
zv::Val className = pt_class_reflection_get_name(Z_OBJ_P(classReflection));
if (UNEXPECTED(className.isUndef())) return zv::Val();
zv::Str message = ancestorMessage(declaringClassNameStr.get(), Z_STR_P(className.raw()));
throwShouldNotHappenStr(message.get());
return zv::Val();
}
zval *declaringClass = declaringClassReflection.raw();
bool supportsEnums;
if (UNEXPECTED(!pt_php_version_answer(slot(slots::phpVersion), PT_PHP_VERSION_SUPPORTS_ENUMS, supportsEnums))) return zv::Val();
bool isNameProperty = zend_string_equals_literal(propertyName, "name");
bool isUnitEnumInterfaceNameProperty = supportsEnums
&& isNameProperty
&& zend_string_equals_literal(declaringClassNameStr.get(), "UnitEnum");
bool declaringIsEnum;
if (UNEXPECTED(!pt_class_reflection_is_enum(Z_OBJ_P(declaringClass), declaringIsEnum))) return zv::Val();
if (declaringIsEnum || isUnitEnumInterfaceNameProperty) {
bool enumMemberProperty = isNameProperty;
if (!enumMemberProperty) {
bool isBackedEnum = callBool(declaringClass, PT_LC("isbackedenum"), 0, NULL, ok);
if (UNEXPECTED(!ok)) return zv::Val();
enumMemberProperty = isBackedEnum && zend_string_equals_literal(propertyName, "value");
}
if (enumMemberProperty) {
zv::Val phpDocType;
zv::Val nativeType;
if (declaringIsEnum) {
zv::Val enumCases = call(classReflection, PT_LC("getenumcases"));
if (UNEXPECTED(!arrayResult(enumCases, "PHPStan\\Reflection\\ClassReflection::getEnumCases"))) return zv::Val();
zv::Arr types = zv::Arr::create(zend_hash_num_elements(Z_ARRVAL_P(enumCases.raw())));
for (zv::ArrayEntry entry : zv::ArrRef(enumCases.raw())) {
if (isNameProperty) {
zend_string *caseName = entry.stringKeyOrNull();
zv::Str owned;
if (caseName == NULL) {
owned = zv::Str::adopt(zend_long_to_str((zend_long) entry.indexKey()));
caseName = owned.get();
}
zv::Val constantString = constantStringType(caseName);
if (UNEXPECTED(constantString.isUndef())) return zv::Val();
types.push(std::move(constantString));
continue;
}
zv::Val backingValue = call(entry.value().raw(), PT_LC("getbackingvaluetype"));
if (UNEXPECTED(backingValue.isUndef())) return zv::Val();
if (Z_TYPE_P(backingValue.raw()) == IS_NULL) {
throwShouldNotHappen();
return zv::Val();
}
types.push(zv::Ref(backingValue.raw()));
}
phpDocType = kernelStaticSpread(PT_LC("PHPStan\\Type\\TypeCombinator"), PT_LC("union"), types.table());
if (UNEXPECTED(phpDocType.isUndef())) return zv::Val();
nativeType = mixedType();
if (UNEXPECTED(nativeType.isUndef())) return zv::Val();
} else {
zv::Val ownedString = stringType();
if (UNEXPECTED(ownedString.isUndef())) return zv::Val();
zv::Val ownedNonFalsy = kernelNew(PT_LC("PHPStan\\Type\\Accessory\\AccessoryNonFalsyStringType"));
if (UNEXPECTED(ownedNonFalsy.isUndef())) return zv::Val();
zval inverse;
ZVAL_TRUE(&inverse);
zv::Val ownedDecimal = kernelNew(PT_LC("PHPStan\\Type\\Accessory\\AccessoryDecimalIntegerStringType"), 1, &inverse);
if (UNEXPECTED(ownedDecimal.isUndef())) return zv::Val();
zv::Args parts{ownedString.raw(), ownedNonFalsy.raw(), ownedDecimal.raw()};
phpDocType = kernelStatic(PT_LC("PHPStan\\Type\\TypeCombinator"), PT_LC("intersect"), 3, parts);
if (UNEXPECTED(phpDocType.isUndef())) return zv::Val();
nativeType = stringType();
if (UNEXPECTED(nativeType.isUndef())) return zv::Val();
}
zv::Val reflectionProperty = call(nativeReflection.raw(), PT_LC("getproperty"), 1, &propertyNameArg);
if (UNEXPECTED(reflectionProperty.isUndef())) return zv::Val();
zval args[20];
ZVAL_COPY_VALUE(&args[0], declaringClass);
ZVAL_NULL(&args[1]);
ZVAL_COPY_VALUE(&args[2], nativeType.raw());
ZVAL_COPY_VALUE(&args[3], phpDocType.raw());
ZVAL_COPY_VALUE(&args[4], phpDocType.raw());
ZVAL_COPY_VALUE(&args[5], reflectionProperty.raw());
ZVAL_NULL(&args[6]);
ZVAL_NULL(&args[7]);
ZVAL_NULL(&args[8]);
ZVAL_NULL(&args[9]);
ZVAL_FALSE(&args[10]);
ZVAL_FALSE(&args[11]);
ZVAL_FALSE(&args[12]);
ZVAL_FALSE(&args[13]);
ZVAL_EMPTY_ARRAY(&args[14]);
ZVAL_FALSE(&args[15]);
ZVAL_TRUE(&args[16]);
ZVAL_FALSE(&args[17]);
ZVAL_FALSE(&args[18]);
ZVAL_TRUE(&args[19]);
return pt_php_property_reflection_new(args);
}
}
zv::Val deprecation = call(slot(slots::deprecationProvider), PT_LC("getpropertydeprecation"), 1, propertyReflection.raw());
if (UNEXPECTED(deprecation.isUndef())) return zv::Val();
bool isDeprecated = Z_TYPE_P(deprecation.raw()) != IS_NULL;
zv::Val deprecatedDescription = zv::Val::null();
if (isDeprecated) {
deprecatedDescription = call(deprecation.raw(), PT_LC("getdescription"));
if (UNEXPECTED(deprecatedDescription.isUndef())) return zv::Val();
}
bool isInternal = false;
bool isReadOnlyByPhpDoc = callBool(classReflection, PT_LC("isimmutable"), 0, NULL, ok);
if (UNEXPECTED(!ok)) return zv::Val();
bool isFinal = callBool(classReflection, PT_LC("isfinal"), 0, NULL, ok);
if (UNEXPECTED(!ok)) return zv::Val();
if (!isFinal) {
if (UNEXPECTED(!pt_property_adapter_is_final(propertyReflection.raw(), isFinal))) return zv::Val();
}
bool isAllowedPrivateMutation = false;
zv::Val docComment = docCommentOf(propertyReflection.raw());
if (UNEXPECTED(docComment.isUndef())) return zv::Val();
zv::Val phpDocType = zv::Val::null();
zv::Val resolvedPhpDoc = zv::Val::null();
zv::Val declaringTraitName = findPropertyTrait(propertyReflection.raw());
if (UNEXPECTED(declaringTraitName.isUndef())) return zv::Val();
zv::Val constructorName = zv::Val::null();
bool isPromoted;
if (UNEXPECTED(!pt_property_adapter_is_promoted(propertyReflection.raw(), isPromoted))) return zv::Val();
if (isPromoted) {
bool hasConstructor = callBool(declaringClass, PT_LC("hasconstructor"), 0, NULL, ok);
if (UNEXPECTED(!ok)) return zv::Val();
if (hasConstructor) {
zv::Val constructor = call(declaringClass, PT_LC("getconstructor"));
if (UNEXPECTED(constructor.isUndef())) return zv::Val();
constructorName = call(constructor.raw(), PT_LC("getname"));
if (UNEXPECTED(constructorName.isUndef())) return zv::Val();
}
}
if (Z_TYPE_P(constructorName.raw()) == IS_NULL) {
zv::Args stubArgs{&declaringClassNameArg, &propertyNameArg};
zv::Val currentResolvedPhpDoc = call(slot(slots::stubPhpDocProvider), PT_LC("findpropertyphpdoc"), 2, stubArgs);
if (UNEXPECTED(currentResolvedPhpDoc.isUndef())) return zv::Val();
if (Z_TYPE_P(currentResolvedPhpDoc.raw()) == IS_NULL && Z_TYPE_P(declaringTraitName.raw()) != IS_NULL) {
ZVAL_COPY_VALUE(&stubArgs[0], declaringTraitName.raw());
currentResolvedPhpDoc = call(slot(slots::stubPhpDocProvider), PT_LC("findpropertyphpdoc"), 2, stubArgs);
if (UNEXPECTED(currentResolvedPhpDoc.isUndef())) return zv::Val();
}
if (Z_TYPE_P(currentResolvedPhpDoc.raw()) == IS_NULL && Z_TYPE_P(docComment.raw()) != IS_NULL) {
zv::Val fileName = call(declaringClass, PT_LC("getfilename"));
if (UNEXPECTED(fileName.isUndef())) return zv::Val();
zval nullArg = {};
ZVAL_NULL(&nullArg);
currentResolvedPhpDoc = getResolvedPhpDoc(fileName.raw(), &declaringClassNameArg, declaringTraitName.raw(), &nullArg, docComment.raw());
if (UNEXPECTED(currentResolvedPhpDoc.isUndef())) return zv::Val();
}
zv::Args resolveArgs{declaringClass, &propertyNameArg, currentResolvedPhpDoc.raw()};
resolvedPhpDoc = call(slot(slots::phpDocInheritanceResolver), PT_LC("resolvephpdocforproperty"), 3, resolveArgs);
if (UNEXPECTED(resolvedPhpDoc.isUndef())) return zv::Val();
} else if (Z_TYPE_P(docComment.raw()) != IS_NULL) {
zv::Val fileName = call(declaringClass, PT_LC("getfilename"));
if (UNEXPECTED(fileName.isUndef())) return zv::Val();
resolvedPhpDoc = getResolvedPhpDoc(fileName.raw(), &declaringClassNameArg, declaringTraitName.raw(), constructorName.raw(), docComment.raw());
if (UNEXPECTED(resolvedPhpDoc.isUndef())) return zv::Val();
}
if (Z_TYPE_P(resolvedPhpDoc.raw()) != IS_NULL) {
zv::Val varTags = pt_resolved_php_doc_block_call(resolvedPhpDoc.raw(), PT_RPD_GET_VAR_TAGS);
if (UNEXPECTED(varTags.isUndef())) return zv::Val();
zval *varTag = varTagFor(varTags.raw(), propertyName);
if (varTag != NULL) {
phpDocType = call(varTag, PT_LC("gettype"));
if (UNEXPECTED(phpDocType.isUndef())) return zv::Val();
}
if (Z_TYPE_P(phpDocType.raw()) != IS_NULL) {
zv::Val activeTemplateTypeMap = call(declaringClass, PT_LC("getactivetemplatetypemap"));
if (UNEXPECTED(activeTemplateTypeMap.isUndef())) return zv::Val();
zv::Val callSiteVarianceMap = call(declaringClass, PT_LC("getcallsitevariancemap"));
if (UNEXPECTED(callSiteVarianceMap.isUndef())) return zv::Val();
zv::Val invariant = varianceInvariant();
if (UNEXPECTED(invariant.isUndef())) return zv::Val();
phpDocType = pt_type_template_type_helper_resolve_template_types(phpDocType.raw(), activeTemplateTypeMap.raw(), callSiteVarianceMap.raw(), invariant.raw(), false);
if (UNEXPECTED(phpDocType.isUndef())) return zv::Val();
}
if (!isDeprecated) {
zv::Val deprecatedTag = pt_resolved_php_doc_block_call(resolvedPhpDoc.raw(), PT_RPD_GET_DEPRECATED_TAG);
if (UNEXPECTED(deprecatedTag.isUndef())) return zv::Val();
if (Z_TYPE_P(deprecatedTag.raw()) != IS_NULL) {
deprecatedDescription = call(deprecatedTag.raw(), PT_LC("getmessage"));
if (UNEXPECTED(deprecatedDescription.isUndef())) return zv::Val();
} else {
deprecatedDescription = zv::Val::null();
}
isDeprecated = resolvedPhpDocBool(resolvedPhpDoc.raw(), PT_RPD_IS_DEPRECATED, ok);
if (UNEXPECTED(!ok)) return zv::Val();
}
isInternal = resolvedPhpDocBool(resolvedPhpDoc.raw(), PT_RPD_IS_INTERNAL, ok);
if (UNEXPECTED(!ok)) return zv::Val();
if (!isReadOnlyByPhpDoc) {
isReadOnlyByPhpDoc = resolvedPhpDocBool(resolvedPhpDoc.raw(), PT_RPD_IS_READ_ONLY, ok);
if (UNEXPECTED(!ok)) return zv::Val();
}
if (!isFinal) {
isFinal = resolvedPhpDocBool(resolvedPhpDoc.raw(), PT_RPD_IS_FINAL, ok);
if (UNEXPECTED(!ok)) return zv::Val();
}
isAllowedPrivateMutation = resolvedPhpDocBool(resolvedPhpDoc.raw(), PT_RPD_IS_ALLOWED_PRIVATE_MUTATION, ok);
if (UNEXPECTED(!ok)) return zv::Val();
}
if (Z_TYPE_P(phpDocType.raw()) == IS_NULL && Z_TYPE_P(constructorName.raw()) != IS_NULL) {
zv::Val constructor = call(declaringClass, PT_LC("getconstructor"));
if (UNEXPECTED(constructor.isUndef())) return zv::Val();
zv::Val resolvedConstructorPhpDoc = call(constructor.raw(), PT_LC("getresolvedphpdoc"));
if (UNEXPECTED(resolvedConstructorPhpDoc.isUndef())) return zv::Val();
if (Z_TYPE_P(resolvedConstructorPhpDoc.raw()) != IS_NULL) {
zv::Val paramTags = pt_resolved_php_doc_block_call(resolvedConstructorPhpDoc.raw(), PT_RPD_GET_PARAM_TAGS);
if (UNEXPECTED(paramTags.isUndef())) return zv::Val();
zv::Str reflectionName = stringOf(pt_property_adapter_get_name(propertyReflection.raw()));
if (UNEXPECTED(reflectionName.isNull())) return zv::Val();
zval *paramTag = issetIn(paramTags.raw(), reflectionName.get());
if (paramTag != NULL) {
phpDocType = call(paramTag, PT_LC("gettype"));
if (UNEXPECTED(phpDocType.isUndef())) return zv::Val();
}
}
}
if (Z_TYPE_P(phpDocType.raw()) == IS_NULL && Z_TYPE_P(slot(slots::inferPrivatePropertyTypeFromConstructor)) == IS_TRUE) {
zv::Val fileName = call(declaringClass, PT_LC("getfilename"));
if (UNEXPECTED(fileName.isUndef())) return zv::Val();
bool eligible = Z_TYPE_P(fileName.raw()) != IS_NULL;
if (eligible) {
if (UNEXPECTED(!pt_property_adapter_is_private(propertyReflection.raw(), eligible))) return zv::Val();
}
if (eligible) {
eligible = !isPromoted;
}
if (eligible) {
bool hasType = callBool(propertyReflection.raw(), PT_LC("hastype"), 0, NULL, ok);
if (UNEXPECTED(!ok)) return zv::Val();
eligible = !hasType;
}
if (eligible) {
eligible = callBool(declaringClass, PT_LC("hasconstructor"), 0, NULL, ok);
if (UNEXPECTED(!ok)) return zv::Val();
}
if (eligible) {
zv::Val constructor = call(declaringClass, PT_LC("getconstructor"));
if (UNEXPECTED(constructor.isUndef())) return zv::Val();
zv::Val constructorDeclaringClass = call(constructor.raw(), PT_LC("getdeclaringclass"));
if (UNEXPECTED(constructorDeclaringClass.isUndef())) return zv::Val();
zv::Val constructorDeclaringName = pt_class_reflection_get_name(Z_OBJ_P(constructorDeclaringClass.raw()));
if (UNEXPECTED(constructorDeclaringName.isUndef())) return zv::Val();
zv::Val declaringName = pt_class_reflection_get_name(Z_OBJ_P(declaringClass));
if (UNEXPECTED(declaringName.isUndef())) return zv::Val();
if (zend_string_equals(Z_STR_P(constructorDeclaringName.raw()), Z_STR_P(declaringName.raw()))) {
zv::Str reflectionName = stringOf(pt_property_adapter_get_name(propertyReflection.raw()));
if (UNEXPECTED(reflectionName.isNull())) return zv::Val();
phpDocType = inferPrivatePropertyType(reflectionName.get(), constructor.raw());
if (UNEXPECTED(phpDocType.isUndef())) return zv::Val();
}
}
}