Repository navigation
Expand file tree
/
Copy pathPhpDocsResolver.cpp
More file actions
1026 lines (950 loc) · 45.7 KB
/
Copy pathPhpDocsResolver.cpp
File metadata and controls
1026 lines (950 loc) · 45.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
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\PhpDocsResolver — native implementation of
* PHPStan\Analyser\PhpDocsResolver.
*
* A DI service (#[AutowiredService]): the constructor keeps the twin's
* arginfo so Nette autowires it. getPhpDocs() — asked by ClassMethodHandler,
* FunctionHandler, PropertyHandler, PropertyHooksProcessor and
* PhpClassReflectionExtension for every declaration — is exported as
* pt_php_docs_resolver_get_php_docs(), which hands the 22 values over
* without building the twin's array; the PHP method builds it.
*
* FileTypeMapper, PhpDocInheritanceResolver, ResolvedPhpDocBlock, the PHPDoc
* tags and Assertions stay PHP (the PHPDoc infrastructure) and are called
* through the cached sites below. MutatingScope, ClassReflection,
* TemplateTypeMap, TemplateTypeHelper, TypeTraverser, TypeCombinator and the
* Type family are called through their direct entries; transformStaticType()'s
* TypeTraverser callback is a native closure. The php-parser getters the twin
* calls (getDocComment()->getText(), getParams(), getReturnType()) read the
* node's properties while the node's class inherits the vendored method,
* and call it otherwise.
*/
#include "support.h"
#include "generated/PhpDocsResolver.h"
namespace slots = ptdecl::PhpDocsResolver::slot;
namespace sigs = ptdecl::PhpDocsResolver::sig;
#include "zv.h"
#include "TypeTraits.h"
#include "TypeOps.h"
#include "Engine.h"
#include "StmtHandlerCalls.h"
zend_class_entry *pt_ce_php_docs_resolver = nullptr;
namespace {
/* {{{ the PHP collaborators (one site each) */
pt_method_site pt_pdr_get_resolved_php_doc_site;
pt_method_site pt_pdr_resolve_php_doc_for_method_site;
pt_method_site pt_pdr_var_tag_get_type_site;
pt_method_site pt_pdr_param_tag_get_type_site;
pt_method_site pt_pdr_param_closure_this_tag_get_type_site;
pt_method_site pt_pdr_param_out_tag_get_type_site;
pt_method_site pt_pdr_return_tag_get_type_site;
pt_method_site pt_pdr_return_tag_is_explicit_site;
pt_method_site pt_pdr_throws_tag_get_type_site;
pt_method_site pt_pdr_deprecated_tag_get_message_site;
pt_method_site pt_pdr_self_out_tag_get_type_site;
/* $object->method() of a PHP collaborator */
inline zv::Val call0(pt_method_site &site, zval *object, const char *lcname, size_t len)
{
return pt_call_method_cached(site, Z_OBJ_P(object), lcname, len, 0, NULL);
}
/* $fileTypeMapper->getResolvedPhpDoc($file, $class, $trait, $functionName, $docComment) */
zv::Val getResolvedPhpDoc(zval *fileTypeMapper, zval *file, zval *className, zval *traitName, zval *functionName, zval *docComment)
{
zv::Args argv{file, className, traitName, functionName, docComment};
return pt_call_method_cached(pt_pdr_get_resolved_php_doc_site, Z_OBJ_P(fileTypeMapper), PT_LC("getresolvedphpdoc"), 5, argv);
}
/* $phpDocInheritanceResolver->resolvePhpDocForMethod($classReflection,
* $methodName, $resolvedPhpDoc, $positionalParameterNames) */
zv::Val resolvePhpDocForMethod(zval *phpDocInheritanceResolver, zval *classReflection, zval *methodName, zval *resolvedPhpDoc, zval *positionalParameterNames)
{
zv::Args argv{classReflection, methodName, resolvedPhpDoc, positionalParameterNames};
return pt_call_method_cached(pt_pdr_resolve_php_doc_for_method_site, Z_OBJ_P(phpDocInheritanceResolver), PT_LC("resolvephpdocformethod"), 4, argv);
}
/* Assertions::createFromResolvedPhpDocBlock($resolvedPhpDoc) / Assertions::createEmpty() */
zv::Val createAssertions(zval *resolvedPhpDoc)
{
return pt_assertions_create_from_resolved_php_doc_block(resolvedPhpDoc);
}
zv::Val createEmptyAssertions()
{
return pt_assertions_create_empty();
}
/* }}} */
pt_property_site pt_pdr_name_site;
pt_property_site pt_pdr_identifier_name_site;
pt_property_site pt_pdr_params_site;
pt_property_site pt_pdr_param_flags_site;
pt_property_site pt_pdr_param_var_site;
pt_property_site pt_pdr_variable_name_site;
pt_property_site pt_pdr_return_type_site;
pt_property_site pt_pdr_comment_text_site;
/* the per-class resolutions of the php-parser getters */
pt_method_site pt_pdr_get_doc_comment_fn_site;
pt_method_site pt_pdr_get_doc_comment_call_site;
pt_method_site pt_pdr_comment_get_text_fn_site;
pt_method_site pt_pdr_comment_get_text_call_site;
pt_method_site pt_pdr_get_params_fn_site;
pt_method_site pt_pdr_get_params_call_site;
pt_method_site pt_pdr_get_return_type_fn_site;
pt_method_site pt_pdr_get_return_type_call_site;
/* the '__construct' literal (module startup) */
zend_string *pt_pdr_construct = nullptr;
zend_never_inline ZEND_COLD void memberCallOnNonObject(const char *method, zval *value)
{
zend_throw_error(NULL, "Call to a member function %s() on %s", method, zend_zval_value_name(value));
}
/* the method a class resolves the name to (cached per site), NULL when it
* has none (nothing thrown) */
zend_function *resolveMethod(pt_method_site &site, zend_class_entry *ce, const char *lcname, size_t len)
{
if (EXPECTED(site.ce == ce && site.generation == pt_engine_generation && site.fn != NULL)) return site.fn;
zend_function *fn = (zend_function *) zend_hash_str_find_ptr(&PT_CE(ce, function_table), lcname, len);
if (fn != NULL) site = { ce, fn, pt_engine_generation };
return fn;
}
/* whether the object's class inherits the method from exactly `declaring` */
bool inheritsMethod(pt_method_site &site, zend_object *object, const char *lcname, size_t len, zend_class_entry *declaring)
{
if (UNEXPECTED(declaring == NULL)) return false;
zend_function *fn = resolveMethod(site, object->ce, lcname, len);
return fn != NULL && fn->common.scope == declaring;
}
/* $comment->getText() */
zv::Val commentText(zval *comment)
{
zend_class_entry *docCe = pt_class(PT_CLASS_DOC_COMMENT);
if (UNEXPECTED(docCe == NULL)) return zv::Val();
/* Comment::getText(): return $this->text; */
if (EXPECTED(docCe->parent != NULL && inheritsMethod(pt_pdr_comment_get_text_fn_site, Z_OBJ_P(comment), PT_LC("gettext"), docCe->parent))) {
zval *text = pt_property_cached(pt_pdr_comment_text_site, Z_OBJ_P(comment), PT_LC("text"));
if (EXPECTED(text != NULL)) {
ZVAL_DEREF(text);
if (EXPECTED(Z_TYPE_P(text) == IS_STRING)) return zv::Val::copyOf(zv::Ref(text));
}
}
return call0(pt_pdr_comment_get_text_call_site, comment, PT_LC("gettext"));
}
/* $node->getParams() */
zv::Val nodeParams(zval *node)
{
zend_class_entry *classMethodCe = pt_class(PT_CLASS_CLASS_METHOD_STMT);
if (UNEXPECTED(classMethodCe == NULL)) return zv::Val();
if (EXPECTED(inheritsMethod(pt_pdr_get_params_fn_site, Z_OBJ_P(node), PT_LC("getparams"), classMethodCe))) {
zval *params = pt_property_cached(pt_pdr_params_site, Z_OBJ_P(node), PT_LC("params"));
if (EXPECTED(params != NULL)) {
ZVAL_DEREF(params);
if (EXPECTED(Z_TYPE_P(params) == IS_ARRAY)) return zv::Val::copyOf(zv::Ref(params));
}
}
return call0(pt_pdr_get_params_call_site, node, PT_LC("getparams"));
}
/* $node->getReturnType() of a FunctionLike */
zv::Val nodeReturnType(zval *node)
{
zend_class_entry *classMethodCe = pt_class(PT_CLASS_CLASS_METHOD_STMT);
if (UNEXPECTED(classMethodCe == NULL)) return zv::Val();
zend_class_entry *functionCe = pt_class(PT_CLASS_FUNCTION_STMT);
if (UNEXPECTED(functionCe == NULL)) return zv::Val();
zend_function *fn = resolveMethod(pt_pdr_get_return_type_fn_site, Z_OBJCE_P(node), PT_LC("getreturntype"));
if (EXPECTED(fn != NULL && (fn->common.scope == classMethodCe || fn->common.scope == functionCe))) {
/* ClassMethod / Function_::getReturnType(): return $this->returnType; */
zval *returnType = pt_property_cached(pt_pdr_return_type_site, Z_OBJ_P(node), PT_LC("returnType"));
if (EXPECTED(returnType != NULL && Z_TYPE_P(returnType) != IS_UNDEF)) return zv::Val::copyOf(zv::Ref(returnType).deref());
}
return call0(pt_pdr_get_return_type_call_site, node, PT_LC("getreturntype"));
}
/* $identifier->name of an Identifier node (dereferenced), the twin's
* warning for a missing property; NULL = pending exception */
zval *identifierName(zval *identifier, const char *property)
{
if (UNEXPECTED(Z_TYPE_P(identifier) != IS_OBJECT)) {
zend_error(E_WARNING, "Attempt to read property \"%s\" on %s", property, zend_zval_value_name(identifier));
if (UNEXPECTED(EG(exception))) return NULL;
return &EG(uninitialized_zval);
}
return ptsh::readNodeProperty(pt_pdr_identifier_name_site, identifier, PT_LC("name"));
}
/* $node->name (dereferenced; NULL = pending exception) */
zval *nodeName(zval *node)
{
return ptsh::readNodeProperty(pt_pdr_name_site, node, PT_LC("name"));
}
/* $identifier->toString() / ->toLowerString() === '__construct' of an
* Identifier (Identifier::toString() returns $this->name) */
zv::Val identifierToString(zval *identifier)
{
if (UNEXPECTED(Z_TYPE_P(identifier) != IS_OBJECT)) {
memberCallOnNonObject("toString", identifier);
return zv::Val();
}
return pt_name_node_to_string(identifier);
}
} // namespace
/* $node->getDocComment()?->getText() */
zv::Val pt_node_doc_comment_text(zval *node)
{
zend_class_entry *nodeAbstractCe = pt_class(PT_CLASS_NODE_ABSTRACT);
if (UNEXPECTED(nodeAbstractCe == NULL)) return zv::Val();
zend_class_entry *docCe = pt_class(PT_CLASS_DOC_COMMENT);
if (UNEXPECTED(docCe == NULL)) return zv::Val();
if (EXPECTED(inheritsMethod(pt_pdr_get_doc_comment_fn_site, Z_OBJ_P(node), PT_LC("getdoccomment"), nodeAbstractCe))) {
/* NodeAbstract::getDocComment(): the last Comment\Doc of getComments() */
zv::Val comments = pt_engine_node_get_comments(Z_OBJ_P(node));
if (UNEXPECTED(comments.isUndef())) return zv::Val();
if (EXPECTED(Z_TYPE_P(comments.raw()) == IS_ARRAY)) {
HashTable *table = Z_ARRVAL_P(comments.raw());
for (zend_long i = (zend_long) zend_hash_num_elements(table) - 1; i >= 0; i--) {
zval *comment = zend_hash_index_find(table, (zend_ulong) i);
if (UNEXPECTED(comment == NULL)) {
zend_error(E_WARNING, "Undefined array key " ZEND_LONG_FMT, i);
if (UNEXPECTED(EG(exception))) return zv::Val();
continue;
}
ZVAL_DEREF(comment);
if (Z_TYPE_P(comment) == IS_OBJECT && instanceof_function(Z_OBJCE_P(comment), docCe)) {
zval hold;
ZVAL_COPY(&hold, comment);
zv::Val owned = zv::Val::adopt(hold);
return commentText(owned.raw());
}
}
return zv::Val::null();
}
}
zv::Val docComment = call0(pt_pdr_get_doc_comment_call_site, node, PT_LC("getdoccomment"));
if (UNEXPECTED(docComment.isUndef()) || docComment.isNull()) return docComment;
if (UNEXPECTED(Z_TYPE_P(docComment.raw()) != IS_OBJECT)) {
memberCallOnNonObject("getText", docComment.raw());
return zv::Val();
}
return commentText(docComment.raw());
}
namespace phpstanturbo {
/* Mirrors PHPStan\Analyser\PhpDocsResolver; false / UNDEF = pending
* exception. */
class PhpDocsResolver
{
public:
explicit PhpDocsResolver(zend_object *self) : self(self) {}
/* the constructor body: the promoted properties */
void construct(zval *fileTypeMapper, zval *phpDocInheritanceResolver)
{
zv::ObjRef object(self);
object.propAtWrite(slots::fileTypeMapper, zv::Val::copyOf(zv::Ref(fileTypeMapper)));
object.propAtWrite(slots::phpDocInheritanceResolver, zv::Val::copyOf(zv::Ref(phpDocInheritanceResolver)));
}
/* Mirrors getPhpDocs(): the list's items into out.items */
[[nodiscard]] bool getPhpDocs(zval *scope, zval *node, pt_php_docs &out) const
{
zval *items = out.items;
zv::Arr phpDocParameterTypes = zv::Arr::empty();
zv::Arr phpDocImmediatelyInvokedCallableParameters = zv::Arr::empty();
zv::Arr phpDocClosureThisTypeParameters = zv::Arr::empty();
zv::Val phpDocReturnType = zv::Val::null();
zv::Val phpDocThrowType = zv::Val::null();
zv::Val deprecatedDescription = zv::Val::null();
zv::Val isDeprecated = zv::Val::boolean(false);
zv::Val isInternal = zv::Val::boolean(false);
zv::Val isFinal = zv::Val::boolean(false);
zv::Val isPure = zv::Val::null();
zv::Val isAllowedPrivateMutation = zv::Val::boolean(false);
zv::Val acceptsNamedArguments = zv::Val::boolean(true);
zv::Val asserts;
zv::Val selfOutType = zv::Val::null();
zv::Arr phpDocParameterOutTypes = zv::Arr::empty();
zv::Arr phpDocPureUnlessCallableIsImpureParameters = zv::Arr::empty();
zv::Arr phpDocPureUnlessParameterPassedParameters = zv::Arr::empty();
zv::Val templateTypeMap;
{
zval empty;
if (UNEXPECTED(!pt_template_type_map_empty(&empty))) return false;
templateTypeMap = zv::Val::adopt(empty);
}
bool inClass;
if (UNEXPECTED(!pt_scope_is_in_class(Z_OBJ_P(scope), inClass))) return false;
bool isReadOnly = false;
/* $scope->getClassReflection(), read once: the scope's context holds it */
zv::Val classReflection;
if (inClass) {
classReflection = pt_scope_get_class_reflection(Z_OBJ_P(scope));
if (UNEXPECTED(classReflection.isUndef())) return false;
if (UNEXPECTED(Z_TYPE_P(classReflection.raw()) != IS_OBJECT)) {
memberCallOnNonObject("isImmutable", classReflection.raw());
return false;
}
if (UNEXPECTED(!pt_class_reflection_is_immutable(Z_OBJ_P(classReflection.raw()), isReadOnly))) return false;
}
zv::Val docComment = pt_node_doc_comment_text(node);
if (UNEXPECTED(docComment.isUndef())) return false;
zv::Val file = pt_mutating_scope_get_file(Z_OBJ_P(scope));
if (UNEXPECTED(file.isUndef())) return false;
zv::Val className = zv::Val::null();
if (inClass) {
className = pt_class_reflection_get_name(Z_OBJ_P(classReflection.raw()));
if (UNEXPECTED(className.isUndef())) return false;
}
zv::Val traitName = zv::Val::null();
{
bool inTrait;
if (UNEXPECTED(!pt_mutating_scope_is_in_trait(Z_OBJ_P(scope), inTrait))) return false;
if (inTrait) {
zv::Val traitReflection = pt_mutating_scope_get_trait_reflection(Z_OBJ_P(scope));
if (UNEXPECTED(traitReflection.isUndef())) return false;
if (UNEXPECTED(Z_TYPE_P(traitReflection.raw()) != IS_OBJECT)) {
memberCallOnNonObject("getName", traitReflection.raw());
return false;
}
traitName = pt_class_reflection_get_name(Z_OBJ_P(traitReflection.raw()));
if (UNEXPECTED(traitName.isUndef())) return false;
}
}
zv::Val resolvedPhpDoc = zv::Val::null();
zv::Val functionName = zv::Val::null();
bool error = false;
bool isClassMethod = ptsh::isInstanceOf(node, PT_CLASS_CLASS_METHOD_STMT, error);
if (UNEXPECTED(error)) return false;
if (isClassMethod) {
if (UNEXPECTED(!resolveClassMethodPhpDoc(scope, node, inClass, classReflection, file.raw(), className.raw(), traitName.raw(), docComment.raw(), phpDocParameterTypes, functionName, resolvedPhpDoc))) return false;
} else if (ptsh::isInstanceOf(node, PT_CLASS_FUNCTION_STMT, error)) {
/* trim($scope->getNamespace() . '\\' . $node->name->name, '\\') */
zv::Val ns = pt_mutating_scope_get_namespace(Z_OBJ_P(scope));
if (UNEXPECTED(ns.isUndef())) return false;
zval *name = nodeName(node);
if (UNEXPECTED(name == NULL)) return false;
zval *identifier = identifierName(name, "name");
if (UNEXPECTED(identifier == NULL)) return false;
zend_string *nsString = zval_try_get_string(ns.raw());
if (UNEXPECTED(nsString == NULL)) return false;
zv::Str nsOwned = zv::Str::adopt(nsString);
zend_string *identifierString = zval_try_get_string(identifier);
if (UNEXPECTED(identifierString == NULL)) return false;
zv::Str identifierOwned = zv::Str::adopt(identifierString);
size_t len = ZSTR_LEN(nsString) + 1 + ZSTR_LEN(identifierString);
zend_string *joined = zend_string_alloc(len, 0);
memcpy(ZSTR_VAL(joined), ZSTR_VAL(nsString), ZSTR_LEN(nsString));
ZSTR_VAL(joined)[ZSTR_LEN(nsString)] = '\\';
memcpy(ZSTR_VAL(joined) + ZSTR_LEN(nsString) + 1, ZSTR_VAL(identifierString), ZSTR_LEN(identifierString));
ZSTR_VAL(joined)[len] = '\0';
const char *start = ZSTR_VAL(joined);
const char *end = start + len;
while (start < end && *start == '\\') start++;
while (end > start && end[-1] == '\\') end--;
if (start == ZSTR_VAL(joined) && end == ZSTR_VAL(joined) + len) {
functionName = zv::Val::adoptString(joined);
} else {
functionName = zv::Val::string(start, (size_t) (end - start));
zend_string_release(joined);
}
} else if (UNEXPECTED(error)) {
return false;
} else if (ptsh::isInstanceOf(node, PT_CLASS_PROPERTY_HOOK, error)) {
zv::Val propertyName = pt_engine_node_get_attribute(Z_OBJ_P(node), PT_LC("propertyName"));
if (UNEXPECTED(propertyName.isUndef())) return false;
if (!propertyName.isNull()) {
zval *name = nodeName(node);
if (UNEXPECTED(name == NULL)) return false;
zv::Val hookName = identifierToString(name);
if (UNEXPECTED(hookName.isUndef())) return false;
zend_string *propertyNameString = zval_try_get_string(propertyName.raw());
if (UNEXPECTED(propertyNameString == NULL)) return false;
zend_string *hookNameString = zval_try_get_string(hookName.raw());
if (UNEXPECTED(hookNameString == NULL)) {
zend_string_release(propertyNameString);
return false;
}
functionName = zv::Val::adoptString(zend_strpprintf(0, "$%s::%s", ZSTR_VAL(propertyNameString), ZSTR_VAL(hookNameString)));
zend_string_release(propertyNameString);
zend_string_release(hookNameString);
}
} else if (UNEXPECTED(error)) {
return false;
}
if (!docComment.isNull() && resolvedPhpDoc.isNull()) {
resolvedPhpDoc = getResolvedPhpDoc(OBJ_PROP_NUM(self, slots::fileTypeMapper), file.raw(), className.raw(), traitName.raw(), functionName.raw(), docComment.raw());
if (UNEXPECTED(resolvedPhpDoc.isUndef())) return false;
}
bool isFunctionLike = ptsh::isInstanceOf(node, PT_CLASS_FUNCTION_LIKE, error);
if (UNEXPECTED(error)) return false;
zv::Val varTags = zv::Val(zv::Arr::empty());
if (!resolvedPhpDoc.isNull()) {
zval *doc = resolvedPhpDoc.raw();
if (UNEXPECTED(Z_TYPE_P(doc) != IS_OBJECT)) {
memberCallOnNonObject("getTemplateTypeMap", doc);
return false;
}
templateTypeMap = pt_resolved_php_doc_block_call(doc, PT_RPD_GET_TEMPLATE_TYPE_MAP);
if (UNEXPECTED(templateTypeMap.isUndef())) return false;
{
zv::Val value = pt_resolved_php_doc_block_call(doc, PT_RPD_GET_PARAMS_IMMEDIATELY_INVOKED_CALLABLE);
if (UNEXPECTED(value.isUndef())) return false;
phpDocImmediatelyInvokedCallableParameters = zv::Arr::adoptVal(std::move(value));
}
if (UNEXPECTED(!collectTagTypes(pt_resolved_php_doc_block_call(doc, PT_RPD_GET_PARAM_TAGS), pt_pdr_param_tag_get_type_site, true, scope, classReflection, phpDocParameterTypes))) return false;
if (UNEXPECTED(!collectTagTypes(pt_resolved_php_doc_block_call(doc, PT_RPD_GET_PARAM_CLOSURE_THIS_TAGS), pt_pdr_param_closure_this_tag_get_type_site, true, scope, classReflection, phpDocClosureThisTypeParameters))) return false;
if (UNEXPECTED(!collectTagTypes(pt_resolved_php_doc_block_call(doc, PT_RPD_GET_PARAM_OUT_TAGS), pt_pdr_param_out_tag_get_type_site, false, scope, classReflection, phpDocParameterOutTypes))) return false;
if (isFunctionLike) {
zv::Val returnTypeNode = nodeReturnType(node);
if (UNEXPECTED(returnTypeNode.isUndef())) return false;
zv::Val nativeReturnType = pt_mutating_scope_get_function_type(Z_OBJ_P(scope), returnTypeNode.raw(), false, false);
if (UNEXPECTED(nativeReturnType.isUndef())) return false;
phpDocReturnType = getPhpDocReturnType(doc, nativeReturnType.raw());
if (UNEXPECTED(phpDocReturnType.isUndef())) return false;
if (!phpDocReturnType.isNull()) {
bool stillInClass;
if (UNEXPECTED(!pt_scope_is_in_class(Z_OBJ_P(scope), stillInClass))) return false;
if (stillInClass) {
phpDocReturnType = transformStaticType(scope, classReflection, phpDocReturnType.raw());
if (UNEXPECTED(phpDocReturnType.isUndef())) return false;
}
}
}
phpDocThrowType = tagType(PT_RPD_GET_THROWS_TAG, doc, pt_pdr_throws_tag_get_type_site, PT_LC("gettype"));
if (UNEXPECTED(phpDocThrowType.isUndef())) return false;
deprecatedDescription = tagType(PT_RPD_GET_DEPRECATED_TAG, doc, pt_pdr_deprecated_tag_get_message_site, PT_LC("getmessage"));
if (UNEXPECTED(deprecatedDescription.isUndef())) return false;
isDeprecated = pt_resolved_php_doc_block_call(doc, PT_RPD_IS_DEPRECATED);
if (UNEXPECTED(isDeprecated.isUndef())) return false;
isInternal = pt_resolved_php_doc_block_call(doc, PT_RPD_IS_INTERNAL);
if (UNEXPECTED(isInternal.isUndef())) return false;
isFinal = pt_resolved_php_doc_block_call(doc, PT_RPD_IS_FINAL);
if (UNEXPECTED(isFinal.isUndef())) return false;
isPure = pt_resolved_php_doc_block_call(doc, PT_RPD_IS_PURE);
if (UNEXPECTED(isPure.isUndef())) return false;
isAllowedPrivateMutation = pt_resolved_php_doc_block_call(doc, PT_RPD_IS_ALLOWED_PRIVATE_MUTATION);
if (UNEXPECTED(isAllowedPrivateMutation.isUndef())) return false;
acceptsNamedArguments = pt_resolved_php_doc_block_call(doc, PT_RPD_ACCEPTS_NAMED_ARGUMENTS);
if (UNEXPECTED(acceptsNamedArguments.isUndef())) return false;
if (!isReadOnly) {
zv::Val readOnly = pt_resolved_php_doc_block_call(doc, PT_RPD_IS_READ_ONLY);
if (UNEXPECTED(readOnly.isUndef())) return false;
isReadOnly = zend_is_true(readOnly.raw());
}
asserts = createAssertions(doc);
if (UNEXPECTED(asserts.isUndef())) return false;
selfOutType = tagType(PT_RPD_GET_SELF_OUT_TAG, doc, pt_pdr_self_out_tag_get_type_site, PT_LC("gettype"));
if (UNEXPECTED(selfOutType.isUndef())) return false;
varTags = pt_resolved_php_doc_block_call(doc, PT_RPD_GET_VAR_TAGS);
if (UNEXPECTED(varTags.isUndef())) return false;
{
zv::Val value = pt_resolved_php_doc_block_call(doc, PT_RPD_GET_PARAMS_PURE_UNLESS_CALLABLE_IS_IMPURE);
if (UNEXPECTED(value.isUndef())) return false;
phpDocPureUnlessCallableIsImpureParameters = zv::Arr::adoptVal(std::move(value));
}
{
zv::Val value = pt_resolved_php_doc_block_call(doc, PT_RPD_GET_PARAMS_PURE_UNLESS_PARAMETER_PASSED);
if (UNEXPECTED(value.isUndef())) return false;
phpDocPureUnlessParameterPassedParameters = zv::Arr::adoptVal(std::move(value));
}
} else {
asserts = createEmptyAssertions();
if (UNEXPECTED(asserts.isUndef())) return false;
}
if (zend_is_true(acceptsNamedArguments.raw())) {
bool stillInClass;
if (UNEXPECTED(!pt_scope_is_in_class(Z_OBJ_P(scope), stillInClass))) return false;
if (stillInClass) {
bool accepts;
if (UNEXPECTED(!classReflectionOf(scope, classReflection, "acceptsNamedArguments"))) return false;
if (UNEXPECTED(!pt_class_reflection_accepts_named_arguments(Z_OBJ_P(classReflection.raw()), accepts))) return false;
acceptsNamedArguments = zv::Val::boolean(accepts);
}
}
// A @pure-unless-* tag on the method, written or inherited, states its purity
// just like @phpstan-pure does, so the class-level tags do not override it.
if (
isPure.isNull()
&& zend_hash_num_elements(phpDocPureUnlessCallableIsImpureParameters.table()) == 0
&& zend_hash_num_elements(phpDocPureUnlessParameterPassedParameters.table()) == 0
&& isFunctionLike
) {
bool stillInClass;
if (UNEXPECTED(!pt_scope_is_in_class(Z_OBJ_P(scope), stillInClass))) return false;
if (stillInClass && UNEXPECTED(!resolveClassPurity(scope, node, classReflection, functionName.raw(), phpDocReturnType.raw(), isPure))) return false;
}
moveInto(items[PT_PHP_DOCS_TEMPLATE_TYPE_MAP], templateTypeMap);
moveInto(items[PT_PHP_DOCS_PARAMETER_TYPES], phpDocParameterTypes);
moveInto(items[PT_PHP_DOCS_IMMEDIATELY_INVOKED_CALLABLE_PARAMETERS], phpDocImmediatelyInvokedCallableParameters);
moveInto(items[PT_PHP_DOCS_CLOSURE_THIS_TYPE_PARAMETERS], phpDocClosureThisTypeParameters);
moveInto(items[PT_PHP_DOCS_RETURN_TYPE], phpDocReturnType);
moveInto(items[PT_PHP_DOCS_THROW_TYPE], phpDocThrowType);
moveInto(items[PT_PHP_DOCS_DEPRECATED_DESCRIPTION], deprecatedDescription);
moveInto(items[PT_PHP_DOCS_IS_DEPRECATED], isDeprecated);
moveInto(items[PT_PHP_DOCS_IS_INTERNAL], isInternal);
moveInto(items[PT_PHP_DOCS_IS_FINAL], isFinal);
moveInto(items[PT_PHP_DOCS_IS_PURE], isPure);
moveInto(items[PT_PHP_DOCS_ACCEPTS_NAMED_ARGUMENTS], acceptsNamedArguments);
ZVAL_BOOL(&items[PT_PHP_DOCS_IS_READ_ONLY], isReadOnly);
moveInto(items[PT_PHP_DOCS_DOC_COMMENT], docComment);
moveInto(items[PT_PHP_DOCS_ASSERTS], asserts);
moveInto(items[PT_PHP_DOCS_SELF_OUT_TYPE], selfOutType);
moveInto(items[PT_PHP_DOCS_PARAMETER_OUT_TYPES], phpDocParameterOutTypes);
moveInto(items[PT_PHP_DOCS_VAR_TAGS], varTags);
moveInto(items[PT_PHP_DOCS_IS_ALLOWED_PRIVATE_MUTATION], isAllowedPrivateMutation);
moveInto(items[PT_PHP_DOCS_RESOLVED_PHP_DOC], resolvedPhpDoc);
moveInto(items[PT_PHP_DOCS_PURE_UNLESS_CALLABLE_IS_IMPURE_PARAMETERS], phpDocPureUnlessCallableIsImpureParameters);
moveInto(items[PT_PHP_DOCS_PURE_UNLESS_PARAMETER_PASSED_PARAMETERS], phpDocPureUnlessParameterPassedParameters);
return true;
}
private:
zend_object *self;
static void moveInto(zval &item, zv::Val &value)
{
zval_ptr_dtor(&item);
item = value.take();
}
/* $scope->getClassReflection() into `classReflection` unless already
* read, checked to be an object for the method the twin calls on it */
[[nodiscard]] static bool classReflectionOf(zval *scope, zv::Val &classReflection, const char *method)
{
if (classReflection.isUndef()) {
classReflection = pt_scope_get_class_reflection(Z_OBJ_P(scope));
if (UNEXPECTED(classReflection.isUndef())) return false;
}
if (UNEXPECTED(Z_TYPE_P(classReflection.raw()) != IS_OBJECT)) {
memberCallOnNonObject(method, classReflection.raw());
return false;
}
return true;
}
/* the ClassMethod branch: $functionName, the constructor's promoted
* parameter @var types and the inherited $resolvedPhpDoc */
[[nodiscard]] bool resolveClassMethodPhpDoc(zval *scope, zval *node, bool inClass, zv::Val &classReflection, zval *file, zval *className, zval *traitName, zval *docComment, zv::Arr &phpDocParameterTypes, zv::Val &functionName, zv::Val &resolvedPhpDoc) const
{
if (!inClass) {
pt_throw_should_not_happen();
return false;
}
zval *name = nodeName(node);
if (UNEXPECTED(name == NULL)) return false;
{
zval *identifier = identifierName(name, "name");
if (UNEXPECTED(identifier == NULL)) return false;
functionName = zv::Val::copyOf(zv::Ref(identifier));
}
/* array_map(static function (Node\Param $param): string { ... }, $node->getParams()) */
zv::Val params = nodeParams(node);
if (UNEXPECTED(params.isUndef())) return false;
zv::Val positionalParameterNames = positionalNames(params.raw());
if (UNEXPECTED(positionalParameterNames.isUndef())) return false;
zv::Val currentResolvedPhpDoc = zv::Val::null();
if (Z_TYPE_P(docComment) != IS_NULL) {
name = nodeName(node);
if (UNEXPECTED(name == NULL)) return false;
zval *identifier = identifierName(name, "name");
if (UNEXPECTED(identifier == NULL)) return false;
zv::Val methodName = zv::Val::copyOf(zv::Ref(identifier));
currentResolvedPhpDoc = getResolvedPhpDoc(OBJ_PROP_NUM(self, slots::fileTypeMapper), file, className, traitName, methodName.raw(), docComment);
if (UNEXPECTED(currentResolvedPhpDoc.isUndef())) return false;
}
zv::Val methodNameForInheritance = pt_engine_node_get_attribute(Z_OBJ_P(node), PT_LC("originalTraitMethodName"));
if (UNEXPECTED(methodNameForInheritance.isUndef())) return false;
if (methodNameForInheritance.isNull()) {
name = nodeName(node);
if (UNEXPECTED(name == NULL)) return false;
zval *identifier = identifierName(name, "name");
if (UNEXPECTED(identifier == NULL)) return false;
methodNameForInheritance = zv::Val::copyOf(zv::Ref(identifier));
}
if (UNEXPECTED(!classReflectionOf(scope, classReflection, "getName"))) return false;
resolvedPhpDoc = resolvePhpDocForMethod(OBJ_PROP_NUM(self, slots::phpDocInheritanceResolver), classReflection.raw(), methodNameForInheritance.raw(), currentResolvedPhpDoc.raw(), positionalParameterNames.raw());
if (UNEXPECTED(resolvedPhpDoc.isUndef())) return false;
name = nodeName(node);
if (UNEXPECTED(name == NULL)) return false;
if (UNEXPECTED(Z_TYPE_P(name) != IS_OBJECT)) {
memberCallOnNonObject("toLowerString", name);
return false;
}
zval *identifier = identifierName(name, "name");
if (UNEXPECTED(identifier == NULL)) return false;
if (Z_TYPE_P(identifier) != IS_STRING || !zend_string_equals_literal_ci(Z_STR_P(identifier), "__construct")) return true;
zval *nodeParamsValue = ptsh::readNodeProperty(pt_pdr_params_site, node, PT_LC("params"));
if (UNEXPECTED(nodeParamsValue == NULL)) return false;
if (UNEXPECTED(Z_TYPE_P(nodeParamsValue) != IS_ARRAY)) {
zend_error(E_WARNING, "foreach() argument must be of type array|object, %s given", zend_zval_value_name(nodeParamsValue));
return !EG(exception);
}
zv::Arr iterated = zv::Arr::copyOfTable(Z_ARRVAL_P(nodeParamsValue));
for (auto entry : zv::TableRef(iterated.table())) {
if (UNEXPECTED(!constructorParamVarType(entry.value().deref().raw(), file, className, traitName, phpDocParameterTypes))) return false;
}
return true;
}
/* the positional parameter names: each param's $var->name, the twin's
* ShouldNotHappenException for anything else */
static zv::Val positionalNames(zval *params)
{
if (UNEXPECTED(Z_TYPE_P(params) != IS_ARRAY)) {
zend_type_error("array_map(): Argument #2 ($array) must be of type array, %s given", zend_zval_value_name(params));
return zv::Val();
}
zend_class_entry *paramCe = pt_class(PT_CLASS_PARAM);
if (UNEXPECTED(paramCe == NULL)) return zv::Val();
zend_class_entry *variableCe = pt_class(PT_CLASS_VARIABLE);
if (UNEXPECTED(variableCe == NULL)) return zv::Val();
HashTable *source = Z_ARRVAL_P(params);
if (zend_hash_num_elements(source) == 0) return zv::Val(zv::Arr::empty());
/* array_map() over one array keeps its keys */
zv::Arr names = zv::Arr::create(zend_hash_num_elements(source));
for (auto entry : zv::TableRef(source)) {
zval *param = entry.value().deref().raw();
if (UNEXPECTED(Z_TYPE_P(param) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(param), paramCe))) {
zend_type_error("PHPStan\\Analyser\\PhpDocsResolver::{closure}(): Argument #1 ($param) must be of type PhpParser\\Node\\Param, %s given", zend_zval_value_name(param));
return zv::Val();
}
zval *var = ptsh::readNodeProperty(pt_pdr_param_var_site, param, PT_LC("var"));
if (UNEXPECTED(var == NULL)) return zv::Val();
if (Z_TYPE_P(var) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(var), variableCe)) {
pt_throw_should_not_happen();
return zv::Val();
}
zval *name = ptsh::readNodeProperty(pt_pdr_variable_name_site, var, PT_LC("name"));
if (UNEXPECTED(name == NULL)) return zv::Val();
if (Z_TYPE_P(name) != IS_STRING) {
pt_throw_should_not_happen();
return zv::Val();
}
names.separate();
Z_TRY_ADDREF_P(name);
zend_string *key = entry.stringKeyOrNull();
if (key != NULL) {
zend_hash_update(names.table(), key, name);
} else {
zend_hash_index_update(names.table(), entry.indexKey(), name);
}
}
return zv::Val(std::move(names));
}
/* the constructor loop's body: a promoted parameter's @var type */
[[nodiscard]] bool constructorParamVarType(zval *param, zval *file, zval *className, zval *traitName, zv::Arr &phpDocParameterTypes) const
{
if (UNEXPECTED(Z_TYPE_P(param) != IS_OBJECT)) {
zend_error(E_WARNING, "Attempt to read property \"flags\" on %s", zend_zval_value_name(param));
if (UNEXPECTED(EG(exception))) return false;
/* null === 0 is false: the loop goes on to getDocComment() */
memberCallOnNonObject("getDocComment", param);
return false;
}
zval *flags = ptsh::readNodeProperty(pt_pdr_param_flags_site, param, PT_LC("flags"));
if (UNEXPECTED(flags == NULL)) return false;
if (Z_TYPE_P(flags) == IS_LONG && Z_LVAL_P(flags) == 0) return true;
zv::Val paramDocComment = pt_node_doc_comment_text(param);
if (UNEXPECTED(paramDocComment.isUndef())) return false;
if (paramDocComment.isNull()) return true;
zend_class_entry *variableCe = pt_class(PT_CLASS_VARIABLE);
if (UNEXPECTED(variableCe == NULL)) return false;
zval *var = ptsh::readNodeProperty(pt_pdr_param_var_site, param, PT_LC("var"));
if (UNEXPECTED(var == NULL)) return false;
if (Z_TYPE_P(var) != IS_OBJECT || !instanceof_function(Z_OBJCE_P(var), variableCe)) {
pt_throw_should_not_happen();
return false;
}
zval *varName = ptsh::readNodeProperty(pt_pdr_variable_name_site, var, PT_LC("name"));
if (UNEXPECTED(varName == NULL)) return false;
if (Z_TYPE_P(varName) != IS_STRING) {
pt_throw_should_not_happen();
return false;
}
zv::Val parameterName = zv::Val::copyOf(zv::Ref(varName));
zval functionName;
ZVAL_INTERNED_STR(&functionName, pt_pdr_construct);
zv::Val paramPhpDoc = getResolvedPhpDoc(OBJ_PROP_NUM(self, slots::fileTypeMapper), file, className, traitName, &functionName, paramDocComment.raw());
if (UNEXPECTED(paramPhpDoc.isUndef())) return false;
if (UNEXPECTED(Z_TYPE_P(paramPhpDoc.raw()) != IS_OBJECT)) {
memberCallOnNonObject("getVarTags", paramPhpDoc.raw());
return false;
}
zv::Val varTags = pt_resolved_php_doc_block_call(paramPhpDoc.raw(), PT_RPD_GET_VAR_TAGS);
if (UNEXPECTED(varTags.isUndef())) return false;
zval *varTag = NULL;
if (EXPECTED(Z_TYPE_P(varTags.raw()) == IS_ARRAY)) {
HashTable *tags = Z_ARRVAL_P(varTags.raw());
zval *first = zend_hash_index_find(tags, 0);
if (first != NULL) ZVAL_DEREF(first);
if (first != NULL && Z_TYPE_P(first) != IS_NULL && zend_hash_num_elements(tags) == 1) {
varTag = first;
} else {
zval *named = zend_symtable_find(tags, Z_STR_P(parameterName.raw()));
if (named != NULL) ZVAL_DEREF(named);
if (named != NULL && Z_TYPE_P(named) != IS_NULL) varTag = named;
}
}
if (varTag == NULL) return true;
if (UNEXPECTED(Z_TYPE_P(varTag) != IS_OBJECT)) {
memberCallOnNonObject("getType", varTag);
return false;
}
zv::Val phpDocType = call0(pt_pdr_var_tag_get_type_site, varTag, PT_LC("gettype"));
if (UNEXPECTED(phpDocType.isUndef())) return false;
phpDocParameterTypes.set(Z_STR_P(parameterName.raw()), std::move(phpDocType));
return true;
}
/* foreach ($tags as $paramName => $tag) — $types[$paramName] =
* $tag->getType(); for the @param / @param-closure-this tags (`paramTypes`)
* an existing key is kept and a static type transformed in a class */
[[nodiscard]] bool collectTagTypes(zv::Val tags, pt_method_site &getTypeSite, bool paramTypes, zval *scope, zv::Val &classReflection, zv::Arr &types) const
{
if (UNEXPECTED(tags.isUndef())) return false;
if (UNEXPECTED(Z_TYPE_P(tags.raw()) != IS_ARRAY)) {
zend_error(E_WARNING, "foreach() argument must be of type array|object, %s given", zend_zval_value_name(tags.raw()));
return !EG(exception);
}
for (auto entry : zv::ArrRef(tags.raw())) {
zend_string *key = entry.stringKeyOrNull();
zend_ulong index = entry.indexKey();
if (paramTypes) {
/* array_key_exists($paramName, $types) */
bool exists = key != NULL ? zend_hash_exists(types.table(), key) : zend_hash_index_exists(types.table(), index);
if (exists) continue;
}
zval *tag = entry.value().deref().raw();
if (UNEXPECTED(Z_TYPE_P(tag) != IS_OBJECT)) {
memberCallOnNonObject("getType", tag);
return false;
}
zv::Val type = call0(getTypeSite, tag, PT_LC("gettype"));
if (UNEXPECTED(type.isUndef())) return false;
if (paramTypes) {
bool inClass;
if (UNEXPECTED(!pt_scope_is_in_class(Z_OBJ_P(scope), inClass))) return false;
if (inClass) {
type = transformStaticType(scope, classReflection, type.raw());
if (UNEXPECTED(type.isUndef())) return false;
}
}
types.separate();
zval stored = type.take();
if (key != NULL) {
zend_hash_update(types.table(), key, &stored);
} else {
zend_hash_index_update(types.table(), index, &stored);
}
}
return true;
}
/* $doc->getXTag() !== null ? $doc->getXTag()->getY() : null (the getter
* memoizes, so it is asked once) */
static zv::Val tagType(pt_resolved_php_doc_member tagMember, zval *doc, pt_method_site &valueSite, const char *valueLcname, size_t valueLen)
{
zv::Val tag = pt_resolved_php_doc_block_call(doc, tagMember);
if (UNEXPECTED(tag.isUndef()) || tag.isNull()) return tag;
if (UNEXPECTED(Z_TYPE_P(tag.raw()) != IS_OBJECT)) {
memberCallOnNonObject(valueLcname, tag.raw());
return zv::Val();
}
return pt_call_method_cached(valueSite, Z_OBJ_P(tag.raw()), valueLcname, valueLen, 0, NULL);
}
/* Mirrors getPhpDocReturnType(). */
static zv::Val getPhpDocReturnType(zval *resolvedPhpDoc, zval *nativeReturnType)
{
zv::Val returnTag = pt_resolved_php_doc_block_call(resolvedPhpDoc, PT_RPD_GET_RETURN_TAG);
if (UNEXPECTED(returnTag.isUndef()) || returnTag.isNull()) return returnTag;
if (UNEXPECTED(Z_TYPE_P(returnTag.raw()) != IS_OBJECT)) {
memberCallOnNonObject("getType", returnTag.raw());
return zv::Val();
}
zv::Val phpDocReturnType = call0(pt_pdr_return_tag_get_type_site, returnTag.raw(), PT_LC("gettype"));
if (UNEXPECTED(phpDocReturnType.isUndef())) return zv::Val();
zv::Val isExplicit = call0(pt_pdr_return_tag_is_explicit_site, returnTag.raw(), PT_LC("isexplicit"));
if (UNEXPECTED(isExplicit.isUndef())) return zv::Val();
if (zend_is_true(isExplicit.raw())) return phpDocReturnType;
if (UNEXPECTED(Z_TYPE_P(nativeReturnType) != IS_OBJECT)) {
memberCallOnNonObject("isSuperTypeOf", nativeReturnType);
return zv::Val();
}
{
zv::Val bounds = pt_type_template_type_helper_resolve_to_bounds(phpDocReturnType.raw());
if (UNEXPECTED(bounds.isUndef())) return zv::Val();
zv::Val result = pt_type_op(Z_OBJ_P(nativeReturnType), PT_OP_IS_SUPER_TYPE_OF, 1, bounds.raw());
if (UNEXPECTED(result.isUndef())) return zv::Val();
zend_long trinary = pt_type_result_trinary(result.raw());
if (UNEXPECTED(trinary < 0)) return zv::Val();
if (trinary == PT_TRI_YES) return phpDocReturnType;
}
if (Z_TYPE_P(phpDocReturnType.raw()) == IS_OBJECT && instanceof_function(Z_OBJCE_P(phpDocReturnType.raw()), pt_ce_union_type)) {
zv::Val innerTypes = pt_union_type_get_types(Z_OBJ_P(phpDocReturnType.raw()));
if (UNEXPECTED(innerTypes.isUndef())) return zv::Val();
if (UNEXPECTED(Z_TYPE_P(innerTypes.raw()) != IS_ARRAY)) {
zend_error(E_WARNING, "foreach() argument must be of type array|object, %s given", zend_zval_value_name(innerTypes.raw()));
if (UNEXPECTED(EG(exception))) return zv::Val();
return zv::Val::null();
}
zv::Arr types = zv::Arr::empty();
for (auto entry : zv::ArrRef(innerTypes.raw())) {
zval *innerType = entry.value().deref().raw();
zv::Val result = pt_type_op(Z_OBJ_P(nativeReturnType), PT_OP_IS_SUPER_TYPE_OF, 1, innerType);
if (UNEXPECTED(result.isUndef())) return zv::Val();
zend_long trinary = pt_type_result_trinary(result.raw());
if (UNEXPECTED(trinary < 0)) return zv::Val();
if (trinary != PT_TRI_YES) continue;
types.push(zv::Ref(innerType));
}
if (zend_hash_num_elements(types.table()) == 0) return zv::Val::null();
return pt_type_combinator_call_spread(PT_LC("union"), types.table());
}
return zv::Val::null();
}
/* Mirrors transformStaticType($scope->getClassReflection(), $type). */
static zv::Val transformStaticType(zval *scope, zv::Val &classReflection, zval *type)
{
if (UNEXPECTED(!classReflectionOf(scope, classReflection, "isFinal"))) return zv::Val();
zv::Val callback = pt_native_closure(&transformStaticTypeBody, classReflection.raw());
return pt_type_traverser_map_memoized_of(type, callback.raw());
}
/* static function (Type $type, callable $traverse) use ($declaringClass): Type */
static void transformStaticTypeBody(zval *captures, uint32_t argc, zval *argv, zval *return_value)
{
if (UNEXPECTED(argc < 2)) {
zend_throw_error(zend_ce_argument_count_error, "Too few arguments to function PHPStan\\Analyser\\PhpDocsResolver::{closure}(), %u passed and exactly 2 expected", argc);
return;
}
zval *type = &argv[0];
zval *traverse = &argv[1];
zval *declaringClass = &captures[0];
if (Z_TYPE_P(type) == IS_OBJECT && instanceof_function(Z_OBJCE_P(type), pt_ce_static_type)) {
zv::Val changedType = pt_type_op(Z_OBJ_P(type), PT_OP_CHANGE_BASE_CLASS, 1, declaringClass);
if (UNEXPECTED(changedType.isUndef())) return;
bool isFinal;
if (UNEXPECTED(!pt_class_reflection_is_final(Z_OBJ_P(declaringClass), isFinal))) return;
if (isFinal && !instanceof_function(Z_OBJCE_P(type), pt_ce_this_type)) {
if (UNEXPECTED(Z_TYPE_P(changedType.raw()) != IS_OBJECT)) {
memberCallOnNonObject("getStaticObjectType", changedType.raw());
return;
}
changedType = pt_type_call(Z_OBJ_P(changedType.raw()), PT_LC("getstaticobjecttype"), 0, NULL);
if (UNEXPECTED(changedType.isUndef())) return;
}
zv::Val traversed = pt_type_call_callable(traverse, 1, changedType.raw());
if (UNEXPECTED(traversed.isUndef())) return;
traversed.intoReturnValue(return_value);
return;
}
zv::Val traversed = pt_type_call_callable(traverse, 1, type);
if (UNEXPECTED(traversed.isUndef())) return;
traversed.intoReturnValue(return_value);
}
/* the class-level purity of a method without its own @phpstan-pure /
* @phpstan-impure */
[[nodiscard]] static bool resolveClassPurity(zval *scope, zval *node, zv::Val &classReflection, zval *functionName, zval *phpDocReturnType, zv::Val &isPure)
{
// a set hook has no return type node of its own, but it always returns
// void - the class-level @phpstan-pure must not make it pure
bool error = false;
bool isSetHook = ptsh::isInstanceOf(node, PT_CLASS_PROPERTY_HOOK, error);
if (UNEXPECTED(error)) return false;
if (isSetHook) {
zval *hookName = nodeName(node);
if (UNEXPECTED(hookName == NULL)) return false;
zval *hookNameString = identifierName(hookName, "name");
if (UNEXPECTED(hookNameString == NULL)) return false;
/* $node->name->toLowerString() === 'set' */
isSetHook = Z_TYPE_P(hookNameString) == IS_STRING && zend_string_equals_literal_ci(Z_STR_P(hookNameString), "set");
}
if (UNEXPECTED(!classReflectionOf(scope, classReflection, "getResolvedPhpDoc"))) return false;
zv::Val classResolvedPhpDoc = pt_class_reflection_get_resolved_php_doc(Z_OBJ_P(classReflection.raw()));
if (UNEXPECTED(classResolvedPhpDoc.isUndef())) return false;
if (classResolvedPhpDoc.isNull()) return true;
if (UNEXPECTED(Z_TYPE_P(classResolvedPhpDoc.raw()) != IS_OBJECT)) {
memberCallOnNonObject("areAllMethodsPure", classResolvedPhpDoc.raw());
return false;
}
zv::Val allPure = pt_resolved_php_doc_block_call(classResolvedPhpDoc.raw(), PT_RPD_ARE_ALL_METHODS_PURE);
if (UNEXPECTED(allPure.isUndef())) return false;
if (zend_is_true(allPure.raw())) {
/* strtolower($functionName ?? '') === '__construct' */
bool isConstructor = false;
if (Z_TYPE_P(functionName) != IS_NULL) {
zend_string *name = zval_try_get_string(functionName);
if (UNEXPECTED(name == NULL)) return false;
isConstructor = zend_string_equals_literal_ci(name, "__construct");
zend_string_release(name);
}
if (isConstructor) {
isPure = zv::Val::boolean(true);
return true;
}
if (isSetHook) return true;
bool phpDocIsVoid = false;
if (Z_TYPE_P(phpDocReturnType) != IS_NULL) {
zend_long isVoid = pt_type_op_trinary(Z_OBJ_P(phpDocReturnType), PT_OP_IS_VOID, 0, NULL);
if (UNEXPECTED(isVoid < 0)) return false;
phpDocIsVoid = isVoid == PT_TRI_YES;
}
if (phpDocIsVoid) return true;
zv::Val returnTypeNode = nodeReturnType(node);
if (UNEXPECTED(returnTypeNode.isUndef())) return false;
zv::Val nativeReturnType = pt_mutating_scope_get_function_type(Z_OBJ_P(scope), returnTypeNode.raw(), false, false);
if (UNEXPECTED(nativeReturnType.isUndef())) return false;
if (UNEXPECTED(Z_TYPE_P(nativeReturnType.raw()) != IS_OBJECT)) {
memberCallOnNonObject("isVoid", nativeReturnType.raw());
return false;
}
zend_long isVoid = pt_type_op_trinary(Z_OBJ_P(nativeReturnType.raw()), PT_OP_IS_VOID, 0, NULL);
if (UNEXPECTED(isVoid < 0)) return false;
if (isVoid != PT_TRI_YES) isPure = zv::Val::boolean(true);
return true;
}
zv::Val allImpure = pt_resolved_php_doc_block_call(classResolvedPhpDoc.raw(), PT_RPD_ARE_ALL_METHODS_IMPURE);
if (UNEXPECTED(allImpure.isUndef())) return false;
if (zend_is_true(allImpure.raw())) isPure = zv::Val::boolean(false);
return true;
}
};
} // namespace phpstanturbo
using phpstanturbo::PhpDocsResolver;
bool pt_php_docs_resolver_get_php_docs(zval *resolver, zval *scope, zval *node, uint32_t destructured, pt_php_docs &out)
{
if (EXPECTED(Z_OBJCE_P(resolver) == pt_ce_php_docs_resolver)) return PhpDocsResolver(Z_OBJ_P(resolver)).getPhpDocs(scope, node, out);
zv::Args argv{scope, node};
zv::Val phpDocs = pt_type_call(Z_OBJ_P(resolver), PT_LC("getphpdocs"), 2, argv);
if (UNEXPECTED(phpDocs.isUndef())) return false;
for (uint32_t index = 0; index < PT_PHP_DOCS_COUNT; index++) {
zval *item = NULL;
if (EXPECTED(Z_TYPE_P(phpDocs.raw()) == IS_ARRAY)) {
item = zend_hash_index_find(Z_ARRVAL_P(phpDocs.raw()), index);
if (item == NULL && (destructured & (1u << index)) != 0) {
zend_error(E_WARNING, "Undefined array key %u", index);
if (UNEXPECTED(EG(exception))) return false;
}
}
if (item != NULL) {
ZVAL_COPY_DEREF(&out.items[index], item);
} else if ((destructured & (1u << index)) != 0) {
ZVAL_NULL(&out.items[index]);
}
}
return true;
}
/* {{{ engine ABI glue: parameter parsing + registration */
#include "reg.h"
PT_MINIT_REGISTRATION(pt_register_php_docs_resolver)
{
pt_pdr_construct = zend_string_init_interned(PT_LC("__construct"), 1);
reg::Class cls("PHPStan\\Analyser\\PhpDocsResolver");
ptdecl::PhpDocsResolver::declareClass(cls);
ptdecl::PhpDocsResolver::declareProperties(cls);
/* the real parameter class names: the DI container autowires the
* service by reflecting the constructor */
cls.method(sigs::__construct, [](INTERNAL_FUNCTION_PARAMETERS) {