Repository navigation
Expand file tree
/
Copy pathConstantStringType.cpp
More file actions
1510 lines (1328 loc) · 59.7 KB
/
Copy pathConstantStringType.cpp
File metadata and controls
1510 lines (1328 loc) · 59.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\ConstantStringType — native implementation of
* PHPStan\Type\Constant\ConstantStringType.
*
* Declared as PHPStan\Type\Constant\ConstantStringType itself at
* activation: not final (the PHP TemplateConstantStringType extends it),
* extending the native StringType (declared first — Shadow.cpp
* materialises a parent plan before its child) and implementing
* PHPStan\Type\ConstantScalarType. State is the twin's five private
* properties, declared typed slots in the twin's order: the three memo
* properties with their defaults and the two promoted constructor
* parameters (IS_PROP_UNINIT until the constructor writes them), so the
* std object handlers do GC/clone.
*
* The class body's methods live here; the two traits
* (ConstantScalarTypeTrait, ConstantScalarToBooleanTrait) come from the
* shared registrars in TypeTraits.cpp, run after the class's own methods
* so the class body's isSuperTypeOf() wins. Everything else is inherited
* from StringType.
*
* Every `$this->method()` the twin makes goes through the object's class
* entry — a subclass may have overridden it — with a direct C++ call when
* the object is exactly a ConstantStringType; `parent::` calls go to
* StringType's native bodies directly. String conversions the twin leaves
* to PHP ((int)/(float)/(bool) casts, the unary plus, `~`, is_numeric(),
* the array-key coercion of key()) go through the engine's own operators so
* the outcome is the twin's.
*/
#include "TypeTraits.h"
#include "generated/ConstantStringType.h"
namespace slots = ptdecl::ConstantStringType::slot;
namespace sigs = ptdecl::ConstantStringType::sig;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wunknown-warning-option"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#pragma GCC diagnostic ignored "-Wattributes"
extern "C" {
#include "ext/standard/php_string.h" /* php_addcslashes_str */
}
#pragma GCC diagnostic pop
#include <cstring>
zend_class_entry *pt_ce_constant_string_type = nullptr;
/* the twin's private const DESCRIBE_LIMIT */
#define PT_CST_DESCRIBE_LIMIT 20
/* Class::NAME — a literal class constant, borrowed; NULL = pending
* exception */
static zval *pt_class_constant(zend_class_entry *ce, const char *name, size_t len)
{
zend_class_constant *constant = (zend_class_constant *) zend_hash_str_find_ptr(&PT_CE(ce, constants_table), name, len);
if (UNEXPECTED(constant == NULL)) {
zend_throw_error(NULL, "phpstan_turbo: %s::%s not found", ZSTR_VAL(ce->name), name);
return NULL;
}
if (UNEXPECTED(Z_TYPE(constant->value) == IS_CONSTANT_AST && zval_update_constant_ex(&constant->value, ce) != SUCCESS)) return NULL;
return &constant->value;
}
namespace phpstanturbo {
/* the character classes of the 'MyClass::myStaticFunction' pattern
* (#^([a-zA-Z_\x7f-\xff\\][a-zA-Z0-9_\x7f-\xff\\]*)::([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\z#) */
static bool isIdentifierStart(unsigned char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c >= 0x7f;
}
static bool isIdentifierChar(unsigned char c)
{
return isIdentifierStart(c) || (c >= '0' && c <= '9');
}
/* Strings::match($value, '#^(class)::(method)\z#'): neither class allows
* a colon, so the greedy groups never backtrack — the match is the
* maximal class run followed by "::" and a method run reaching the end */
static bool matchStaticCallable(const unsigned char *s, size_t len, size_t &classLen, size_t &methodStart)
{
if (len == 0 || !(isIdentifierStart(s[0]) || s[0] == '\\')) return false;
size_t i = 1;
while (i < len && (isIdentifierChar(s[i]) || s[i] == '\\')) {
i++;
}
if (i + 2 > len || s[i] != ':' || s[i + 1] != ':') return false;
size_t m = i + 2;
if (m >= len || !isIdentifierStart(s[m])) return false;
size_t j = m + 1;
while (j < len && isIdentifierChar(s[j])) {
j++;
}
if (j != len) return false;
classLen = i;
methodStart = m;
return true;
}
/* Mirrors PHPStan\Type\Constant\ConstantStringType. State lives in the PHP
* object's slots. */
class ConstantStringType
{
public:
explicit ConstantStringType(zend_object *self) : self(self) {}
/* __construct(private string $value, private bool $isClassString = false):
* initializes the typed slots; parent::__construct() is StringType's
* empty constructor */
void construct(zend_string *value, bool isClassString)
{
zval *valueSlot = OBJ_PROP_NUM(self, slots::value);
zval *flagSlot = OBJ_PROP_NUM(self, slots::isClassString);
ZVAL_STR_COPY(valueSlot, value);
ZVAL_BOOL(flagSlot, isClassString);
Z_PROP_FLAG_P(valueSlot) = 0; /* no longer IS_PROP_UNINIT */
Z_PROP_FLAG_P(flagSlot) = 0;
}
/* new self($value, $isClassString); UNDEF = pending exception */
static zv::Val create(zend_string *value, bool isClassString = false)
{
zval object;
if (UNEXPECTED(object_init_ex(&object, pt_ce_constant_string_type) != SUCCESS)) return zv::Val();
ConstantStringType(Z_OBJ(object)).construct(value, isClassString);
return zv::Val::adopt(object);
}
/* $this->value (borrowed); NULL with an Error pending when the
* constructor never ran (ReflectionClass::newInstanceWithoutConstructor())
* — the twin's typed-property read raises the same */
[[nodiscard]] zend_string *value() const { return valueOf(self); }
/* $object->value — the private slot declared here, read directly on a
* subclass too (as `$type->value` does) */
static zend_string *valueOf(zend_object *object)
{
zval *slot = OBJ_PROP_NUM(object, slots::value);
if (UNEXPECTED(Z_TYPE_P(slot) != IS_STRING)) {
zend_throw_error(NULL, "Typed property %s::$value must not be accessed before initialization", ZSTR_VAL(pt_ce_constant_string_type->name));
return NULL;
}
return Z_STR_P(slot);
}
/* $this->isClassString; false with an Error pending when uninitialized */
[[nodiscard]] bool isClassStringFlag(bool &out) const
{
zval *slot = OBJ_PROP_NUM(self, slots::isClassString);
if (UNEXPECTED(Z_TYPE_P(slot) != IS_TRUE && Z_TYPE_P(slot) != IS_FALSE)) {
zend_throw_error(NULL, "Typed property %s::$isClassString must not be accessed before initialization", ZSTR_VAL(pt_ce_constant_string_type->name));
return false;
}
out = Z_TYPE_P(slot) == IS_TRUE;
return true;
}
/* an owned copy of $this->value; UNDEF = pending exception */
zv::Val getValue() const
{
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return zv::Val();
return zv::Val::string(v);
}
/* [$this] */
zv::Val getConstantStrings() const
{
zv::Arr types = zv::Arr::create(1);
zval selfZv;
ZVAL_OBJ(&selfZv, self);
types.push(zv::Ref(&selfZv));
return zv::Val(std::move(types));
}
/* yes when constructed as a class-string, else whether the reflection
* provider knows a class of that name; -1 = pending exception */
[[nodiscard]] zend_long isClassString() const
{
bool flag = false;
if (UNEXPECTED(!isClassStringFlag(flag))) return -1;
if (flag) return PT_TRI_YES;
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return -1;
zv::Val reflectionProvider = reflectionProviderInstance();
if (UNEXPECTED(reflectionProvider.isUndef())) return -1;
zval valueZv;
ZVAL_STR(&valueZv, v);
zv::Val hasClass = pt_reflection_provider_has_class_zv(Z_OBJ_P(reflectionProvider.raw()), &valueZv);
if (UNEXPECTED(hasClass.isUndef())) return -1;
return zend_is_true(hasClass.raw()) ? PT_TRI_YES : PT_TRI_NO;
}
/* new ObjectType($this->value) when $this->isClassString()->yes(), new
* ErrorType() otherwise; UNDEF = pending exception */
zv::Val getClassStringObjectType() const
{
zend_long isClass = thisIsClassString();
if (UNEXPECTED(isClass < 0)) return zv::Val();
if (isClass == PT_TRI_YES) return newObjectTypeOfValue();
return pt_type_new_error_type();
}
/* $this->getClassStringObjectType() */
zv::Val getObjectTypeOrClassStringObjectType() const
{
if (EXPECTED(isExact())) return getClassStringObjectType();
return pt_type_call(self, PT_LC("getclassstringobjecttype"), 0, NULL);
}
/* $level->handle(): 'string' for the type-only level; the exported
* value, truncated to DESCRIBE_LIMIT unless a class-string, for the
* value level; the exported full value for the precise and cache
* levels — each memoized per level in $cachedDescriptions; UNDEF =
* pending exception */
zv::Val describe(zval *level) const
{
/* $level->getLevelValue() — the shadowing VerbosityLevel's slot, or
* the PHP twin's method (VerbosityLevel.cpp) */
zend_long levelValue;
if (UNEXPECTED(!pt_verbosity_level_value_of(level, levelValue))) return zv::Val();
zv::ArrRef cached(OBJ_PROP_NUM(self, slots::cachedDescriptions));
if (UNEXPECTED(!cached.isArray())) {
zend_throw_error(NULL, "Typed property %s::$cachedDescriptions must not be accessed before initialization", ZSTR_VAL(pt_ce_constant_string_type->name));
return zv::Val();
}
zv::Ref hit = cached.findIndex((zend_ulong) levelValue);
if (hit.raw() != NULL && !hit.isNull()) return zv::Val::copyOf(hit);
if (levelValue == PT_VERBOSITY_LEVEL_TYPE_ONLY) return zv::Val::string("string", sizeof("string") - 1);
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return zv::Val();
zv::Val exported;
if (levelValue == PT_VERBOSITY_LEVEL_VALUE) {
bool flag = false;
if (UNEXPECTED(!isClassStringFlag(flag))) return zv::Val();
zv::Str truncated;
if (!flag) {
truncated = truncate(v);
if (UNEXPECTED(truncated.isNull())) return zv::Val();
v = truncated.get();
}
exported = zv::Val::adoptString(exportValue(v));
} else {
/* the precise callback, which the cache level falls through to */
exported = zv::Val::adoptString(exportValue(v));
}
cached.setIndex((zend_ulong) levelValue, zv::Ref(exported.raw()));
return exported;
}
/* no for a GenericClassStringType unless this is a class-string, else
* maybe/no by whether its generic type (a StaticType's object type, a
* TemplateType's bound) is a supertype of this value's ObjectType;
* maybe/no for a
* ClassStringType by whether this is a class-string; yes/no against
* another ConstantStringType's value; maybe for any other StringType;
* the CompoundType callback; no otherwise; UNDEF = pending exception */
zv::Val isSuperTypeOf(zval *type) const
{
zend_class_entry *typeCe = Z_OBJCE_P(type);
if (instanceof_function(typeCe, pt_ce_generic_class_string_type)) {
zend_long isClass = thisIsClassString();
if (UNEXPECTED(isClass < 0)) return zv::Val();
if (isClass != PT_TRI_YES) return pt_type_is_super_type_of_result(PT_TRI_NO);
zv::Val genericType = pt_type_call(Z_OBJ_P(type), PT_LC("getgenerictype"), 0, NULL);
if (UNEXPECTED(genericType.isUndef())) return zv::Val();
/* $genericType instanceof StaticType — the shadowing class */
bool isStatic = zv::Ref(genericType.raw()).instanceOf(pt_ce_static_type);
if (isStatic) {
genericType = pt_type_call(Z_OBJ_P(genericType.raw()), PT_LC("getstaticobjecttype"), 0, NULL);
if (UNEXPECTED(genericType.isUndef())) return zv::Val();
}
/* We are transforming constant class-string to ObjectType. But
* we need to filter out an uncertainty originating in possible
* ObjectType's class subtypes. */
zval *objectType = getObjectType();
if (UNEXPECTED(objectType == NULL)) return zv::Val();
/* Do not use TemplateType's isSuperTypeOf handling directly
* because it takes ObjectType uncertainty into account. */
bool isTemplate;
if (UNEXPECTED(!pt_type_instanceof(genericType.raw(), PT_CLASS_TEMPLATE_TYPE, isTemplate))) return zv::Val();
zv::Val isSuperType;
if (isTemplate) {
zv::Val bound = pt_type_call(Z_OBJ_P(genericType.raw()), PT_LC("getbound"), 0, NULL);
if (UNEXPECTED(bound.isUndef())) return zv::Val();
isSuperType = pt_type_op(Z_OBJ_P(bound.raw()), PT_OP_IS_SUPER_TYPE_OF, 1, objectType);
} else {
isSuperType = pt_type_op(Z_OBJ_P(genericType.raw()), PT_OP_IS_SUPER_TYPE_OF, 1, objectType);
}
if (UNEXPECTED(isSuperType.isUndef())) return zv::Val();
zend_long verdict = pt_type_result_trinary(isSuperType.raw());
if (UNEXPECTED(verdict < 0)) return zv::Val();
/* Explicitly handle the uncertainty for Yes & Maybe. */
if (verdict == PT_TRI_YES) return pt_type_is_super_type_of_result(PT_TRI_MAYBE);
return pt_type_is_super_type_of_result(PT_TRI_NO);
}
if (instanceof_function(typeCe, pt_ce_class_string_type)) {
zend_long isClass = thisIsClassString();
if (UNEXPECTED(isClass < 0)) return zv::Val();
return pt_type_is_super_type_of_result(isClass == PT_TRI_YES ? PT_TRI_MAYBE : PT_TRI_NO);
}
/* $type instanceof self: $this->value === $type->value */
if (instanceof_function(typeCe, pt_ce_constant_string_type)) {
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return zv::Val();
zend_string *typeValue = valueOf(Z_OBJ_P(type));
if (UNEXPECTED(typeValue == NULL)) return zv::Val();
return pt_type_is_super_type_of_result(zend_string_equals(v, typeValue) ? PT_TRI_YES : PT_TRI_NO);
}
/* $type instanceof parent */
if (instanceof_function(typeCe, pt_ce_string_type)) return pt_type_is_super_type_of_result(PT_TRI_MAYBE);
bool compound;
if (UNEXPECTED(!pt_type_instanceof(type, PT_CLASS_COMPOUND_TYPE, compound))) return zv::Val();
if (compound) {
zval selfZv;
ZVAL_OBJ(&selfZv, self);
return pt_type_op(Z_OBJ_P(type), PT_OP_IS_SUB_TYPE_OF, 1, &selfZv);
}
return pt_type_is_super_type_of_result(PT_TRI_NO);
}
/* no for the empty string; yes for a known function name; for
* 'MyClass::myStaticFunction' maybe when the class is unknown, yes when
* it has the method (no for an instance method before PHP 8.2's
* callable instance methods), maybe when the class is not final by
* keyword, no otherwise; no for anything else; -1 = pending exception */
[[nodiscard]] zend_long isCallable() const
{
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return -1;
if (ZSTR_LEN(v) == 0) return PT_TRI_NO;
zv::Val reflectionProvider = reflectionProviderInstance();
if (UNEXPECTED(reflectionProvider.isUndef())) return -1;
zend_object *provider = Z_OBJ_P(reflectionProvider.raw());
/* 'my_function' */
zv::Val name = newName(v);
if (UNEXPECTED(name.isUndef())) return -1;
bool hasFunction;
if (UNEXPECTED(!pt_reflection_provider_has_function(reflectionProvider.raw(), name.raw(), NULL, hasFunction))) return -1;
if (hasFunction) return PT_TRI_YES;
/* 'MyClass::myStaticFunction' */
size_t classLen, methodStart;
if (matchStaticCallable((const unsigned char *) ZSTR_VAL(v), ZSTR_LEN(v), classLen, methodStart)) {
zv::Val className = zv::Val::string(ZSTR_VAL(v), classLen);
zv::Val methodName = zv::Val::string(ZSTR_VAL(v) + methodStart, ZSTR_LEN(v) - methodStart);
zv::Val hasClass = pt_reflection_provider_has_class_zv(provider, className.raw());
if (UNEXPECTED(hasClass.isUndef())) return -1;
if (!zend_is_true(hasClass.raw())) return PT_TRI_MAYBE;
zv::Val classRef = pt_reflection_provider_get_class(provider, className.raw());
if (UNEXPECTED(classRef.isUndef())) return -1;
bool hasMethod;
if (UNEXPECTED(!pt_class_reflection_has_method(Z_OBJ_P(classRef.raw()), methodName.raw(), hasMethod))) return -1;
if (hasMethod) {
zv::Val phpVersion = pt_type_call_static(PT_CLASS_PHP_VERSION_STATIC_ACCESSOR, PT_LC("getinstance"), 0, NULL);
if (UNEXPECTED(phpVersion.isUndef())) return -1;
bool supportsInstanceMethods;
if (UNEXPECTED(!pt_php_version_answer(phpVersion.raw(), PT_PHP_VERSION_SUPPORTS_CALLABLE_INSTANCE_METHODS, supportsInstanceMethods))) return -1;
if (!supportsInstanceMethods) {
zv::Val scope = pt_type_new(PT_CLASS_OUT_OF_CLASS_SCOPE, 0, NULL);
if (UNEXPECTED(scope.isUndef())) return -1;
zv::Args methodArgs{methodName.raw(), scope.raw()};
zv::Val method = pt_type_call(Z_OBJ_P(classRef.raw()), PT_LC("getmethod"), 2, methodArgs);
if (UNEXPECTED(method.isUndef())) return -1;
zv::Val isStatic = Z_TYPE_P(method.raw()) == IS_OBJECT ? pt_extended_method_reflection_call(method.raw(), PT_MR_IS_STATIC) : pt_type_call(Z_OBJ_P(method.raw()), PT_LC("isstatic"), 0, NULL);
if (UNEXPECTED(isStatic.isUndef())) return -1;
if (!zend_is_true(isStatic.raw())) return PT_TRI_NO;
}
return PT_TRI_YES;
}
zv::Val isFinalByKeyword = pt_type_call(Z_OBJ_P(classRef.raw()), PT_LC("isfinalbykeyword"), 0, NULL);
if (UNEXPECTED(isFinalByKeyword.isUndef())) return -1;
if (!zend_is_true(isFinalByKeyword.raw())) return PT_TRI_MAYBE;
return PT_TRI_NO;
}
return PT_TRI_NO;
}
/* [] for the empty string; a known function's callable variants; for
* 'MyClass::myStaticFunction' a trivial acceptor when the class is
* unknown, the method's variants (an InaccessibleMethod when the scope
* cannot call it) when it has the method, a trivial acceptor when the
* class is not final by keyword; ShouldNotHappenException otherwise;
* UNDEF = pending exception */
zv::Val getCallableParametersAcceptors(zval *scope) const
{
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return zv::Val();
if (ZSTR_LEN(v) == 0) return zv::Val(zv::Arr::empty());
zv::Val reflectionProvider = reflectionProviderInstance();
if (UNEXPECTED(reflectionProvider.isUndef())) return zv::Val();
zend_object *provider = Z_OBJ_P(reflectionProvider.raw());
/* 'my_function' */
zv::Val functionName = newName(v);
if (UNEXPECTED(functionName.isUndef())) return zv::Val();
bool hasFunction;
if (UNEXPECTED(!pt_reflection_provider_has_function(reflectionProvider.raw(), functionName.raw(), NULL, hasFunction))) return zv::Val();
if (hasFunction) {
zv::Val function = pt_reflection_provider_get_function(reflectionProvider.raw(), functionName.raw(), NULL);
if (UNEXPECTED(function.isUndef())) return zv::Val();
return callableVariants(function.raw());
}
/* 'MyClass::myStaticFunction' */
size_t classLen, methodStart;
if (matchStaticCallable((const unsigned char *) ZSTR_VAL(v), ZSTR_LEN(v), classLen, methodStart)) {
zv::Val className = zv::Val::string(ZSTR_VAL(v), classLen);
zv::Val methodName = zv::Val::string(ZSTR_VAL(v) + methodStart, ZSTR_LEN(v) - methodStart);
zv::Val hasClass = pt_reflection_provider_has_class_zv(provider, className.raw());
if (UNEXPECTED(hasClass.isUndef())) return zv::Val();
if (!zend_is_true(hasClass.raw())) return trivialAcceptors();
zv::Val classReflection = pt_reflection_provider_get_class(provider, className.raw());
if (UNEXPECTED(classReflection.isUndef())) return zv::Val();
bool hasMethod;
if (UNEXPECTED(!pt_class_reflection_has_method(Z_OBJ_P(classReflection.raw()), methodName.raw(), hasMethod))) return zv::Val();
if (hasMethod) {
zv::Args methodArgs{methodName.raw(), scope};
zv::Val method = pt_type_call(Z_OBJ_P(classReflection.raw()), PT_LC("getmethod"), 2, methodArgs);
if (UNEXPECTED(method.isUndef())) return zv::Val();
zv::Val canCall = pt_type_call(Z_OBJ_P(scope), PT_LC("cancallmethod"), 1, method.raw());
if (UNEXPECTED(canCall.isUndef())) return zv::Val();
if (!zend_is_true(canCall.raw())) {
/* [new InaccessibleMethod($method)] */
zv::Val inaccessible = pt_type_new(PT_CLASS_INACCESSIBLE_METHOD, 1, method.raw());
if (UNEXPECTED(inaccessible.isUndef())) return zv::Val();
zv::Arr acceptors = zv::Arr::create(1);
acceptors.push(std::move(inaccessible));
return zv::Val(std::move(acceptors));
}
return callableVariants(method.raw());
}
zv::Val isFinalByKeyword = pt_type_call(Z_OBJ_P(classReflection.raw()), PT_LC("isfinalbykeyword"), 0, NULL);
if (UNEXPECTED(isFinalByKeyword.isUndef())) return zv::Val();
if (!zend_is_true(isFinalByKeyword.raw())) return trivialAcceptors();
}
pt_throw_should_not_happen();
return zv::Val();
}
/* the value's number (`+$value`: the engine's numeric-string
* conversion, a float on overflow) as a ConstantFloatType or
* ConstantIntegerType when is_numeric(), new ErrorType() otherwise;
* UNDEF = pending exception */
zv::Val toNumber() const
{
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return zv::Val();
if (!is_numeric_string(ZSTR_VAL(v), ZSTR_LEN(v), NULL, NULL, false)) return pt_type_new_error_type();
/* +$value compiles to `$value * 1` */
zval valueZv, one, number;
ZVAL_STR(&valueZv, v);
ZVAL_LONG(&one, 1);
if (UNEXPECTED(mul_function(&number, &valueZv, &one) != SUCCESS || EG(exception))) return zv::Val();
if (Z_TYPE(number) == IS_DOUBLE) return pt_type_new_constant_float(Z_DVAL(number));
return pt_type_new_constant_integer(Z_LVAL(number));
}
/* new ConstantStringType(~$this->value) */
zv::Val toBitwiseNotType() const
{
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return zv::Val();
zval valueZv, negated;
ZVAL_STR(&valueZv, v);
if (UNEXPECTED(bitwise_not_function(&negated, &valueZv) != SUCCESS || EG(exception))) return zv::Val();
zv::Val result = create(Z_STR(negated));
zval_ptr_dtor(&negated);
return result;
}
/* new ClassNameToObjectTypeResult(new ObjectType($this->value), false) */
zv::Val toObjectTypeForInstanceofCheck() const
{
zv::Val objectType = newObjectTypeOfValue();
if (UNEXPECTED(objectType.isUndef())) return zv::Val();
return classNameToObjectTypeResult(objectType.raw(), false);
}
/* the ObjectType of the value — or, when strings are allowed, its
* union with the GenericClassStringType of it — with the uncertainty
* the twin derives from the compared type's class names (and never
* when the compared type is exactly this one final class); UNDEF =
* pending exception */
zv::Val toObjectTypeForIsACheck(zval *objectOrClassType, bool allowString, bool allowSameClass) const
{
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return zv::Val();
zv::Val classNamesRaw = pt_type_op(Z_OBJ_P(objectOrClassType), PT_OP_GET_OBJECT_CLASS_NAMES, 0, NULL);
if (UNEXPECTED(classNamesRaw.isUndef())) return zv::Val();
if (UNEXPECTED(!zv::Ref(classNamesRaw.raw()).isArray())) {
zend_type_error("phpstan_turbo: %s::getObjectClassNames() must return array", ZSTR_VAL(Z_OBJCE_P(objectOrClassType)->name));
return zv::Val();
}
zv::Arr classNames = zv::Arr::adoptVal(std::move(classNamesRaw));
if (allowString) {
zv::Val constantStrings = pt_type_call(Z_OBJ_P(objectOrClassType), PT_LC("getconstantstrings"), 0, NULL);
if (UNEXPECTED(constantStrings.isUndef())) return zv::Val();
if (UNEXPECTED(!zv::Ref(constantStrings.raw()).isArray())) {
zend_type_error("phpstan_turbo: %s::getConstantStrings() must return array", ZSTR_VAL(Z_OBJCE_P(objectOrClassType)->name));
return zv::Val();
}
for (zv::ArrayEntry entry : zv::ArrRef(constantStrings.raw())) {
if (UNEXPECTED(!entry.value().isObject())) {
zend_type_error("phpstan_turbo: %s::getConstantStrings() must return ConstantStringType instances", ZSTR_VAL(Z_OBJCE_P(objectOrClassType)->name));
return zv::Val();
}
zv::Val constantValue = pt_constant_string_get_value(entry.value().asObject());
if (UNEXPECTED(constantValue.isUndef())) return zv::Val();
classNames.push(std::move(constantValue));
}
/* array_values(array_unique($objectOrClassTypeClassNames)):
* the first occurrence of each string, re-indexed */
zv::Arr unique = zv::Arr::create(classNames.arrRef().size());
zv::ScratchTable seen(classNames.arrRef().size());
for (zv::ArrayEntry entry : classNames.arrRef()) {
zval *item = entry.value().raw();
zend_string *tmp;
zend_string *key = zval_get_tmp_string(item, &tmp);
if (UNEXPECTED(EG(exception))) {
zend_tmp_string_release(tmp);
return zv::Val();
}
bool first = zend_hash_add_empty_element(seen.table(), key) != NULL;
zend_tmp_string_release(tmp);
if (first) {
unique.push(entry.value());
}
}
classNames = std::move(unique);
}
bool uncertainty = false;
if (!allowSameClass) {
/* $objectOrClassTypeClassNames === [$this->value] */
zv::Arr onlyThis = zv::Arr::create(1);
onlyThis.push(zv::Val::string(v));
if (zend_is_identical(classNames.raw(), onlyThis.raw())) {
bool isSameClass = true;
zv::Val reflections = pt_type_call(Z_OBJ_P(objectOrClassType), PT_LC("getobjectclassreflections"), 0, NULL);
if (UNEXPECTED(reflections.isUndef())) return zv::Val();
if (UNEXPECTED(!zv::Ref(reflections.raw()).isArray())) {
zend_type_error("phpstan_turbo: %s::getObjectClassReflections() must return array", ZSTR_VAL(Z_OBJCE_P(objectOrClassType)->name));
return zv::Val();
}
for (zv::ArrayEntry entry : zv::ArrRef(reflections.raw())) {
if (UNEXPECTED(!entry.value().isObject())) {
zend_type_error("phpstan_turbo: %s::getObjectClassReflections() must return ClassReflection instances", ZSTR_VAL(Z_OBJCE_P(objectOrClassType)->name));
return zv::Val();
}
zv::Val isFinal = pt_type_call(entry.value().asObject(), PT_LC("isfinal"), 0, NULL);
if (UNEXPECTED(isFinal.isUndef())) return zv::Val();
if (!zend_is_true(isFinal.raw())) {
isSameClass = false;
break;
}
}
if (isSameClass) {
zv::Val never = pt_type_new_never_type();
if (UNEXPECTED(never.isUndef())) return zv::Val();
return classNameToObjectTypeResult(never.raw(), false);
}
}
/* in_array($this->value, $objectOrClassTypeClassNames, true) —
* for object, as soon as the exact same type is provided in
* the list we cannot be sure of the result */
bool contained = false;
for (zv::ArrayEntry entry : classNames.arrRef()) {
if (entry.value().isString() && zend_string_equals(entry.value().asString(), v)) {
contained = true;
break;
}
}
if (contained) {
uncertainty = true;
} else if (allowString && classNames.arrRef().size() == 0) {
/* this also occurs for generic class string:
* $objectOrClassType->isSuperTypeOf($this)->yes() */
zval selfZv;
ZVAL_OBJ(&selfZv, self);
zv::Val isSuperType = pt_type_op(Z_OBJ_P(objectOrClassType), PT_OP_IS_SUPER_TYPE_OF, 1, &selfZv);
if (UNEXPECTED(isSuperType.isUndef())) return zv::Val();
zend_long verdict = pt_type_result_trinary(isSuperType.raw());
if (UNEXPECTED(verdict < 0)) return zv::Val();
if (verdict == PT_TRI_YES) {
uncertainty = true;
}
}
}
zv::Val objectType = newObjectTypeOfValue();
if (UNEXPECTED(objectType.isUndef())) return zv::Val();
if (allowString) {
/* new UnionType([new ObjectType($this->value), new GenericClassStringType(new ObjectType($this->value))]) */
zv::Val innerObjectType = newObjectTypeOfValue();
if (UNEXPECTED(innerObjectType.isUndef())) return zv::Val();
zval genericRaw;
if (UNEXPECTED(object_init_ex(&genericRaw, pt_ce_generic_class_string_type) != SUCCESS)) return zv::Val();
zv::Val generic = zv::Val::adopt(genericRaw);
zend_call_known_instance_method(PT_CE(pt_ce_generic_class_string_type, constructor), Z_OBJ_P(generic.raw()), NULL, 1, innerObjectType.raw());
if (UNEXPECTED(EG(exception))) return zv::Val();
zv::Arr types = zv::Arr::create(2);
types.push(std::move(objectType));
types.push(std::move(generic));
zv::Val unionType = pt_type_new_union(std::move(types));
if (UNEXPECTED(unionType.isUndef())) return zv::Val();
return classNameToObjectTypeResult(unionType.raw(), uncertainty);
}
return classNameToObjectTypeResult(objectType.raw(), uncertainty);
}
/* $this->toNumber()->toAbsoluteNumber() */
zv::Val toAbsoluteNumber() const
{
zv::Val number = isExact() ? toNumber() : pt_type_call(self, PT_LC("tonumber"), 0, NULL);
if (UNEXPECTED(number.isUndef())) return zv::Val();
return pt_type_call(Z_OBJ_P(number.raw()), PT_LC("toabsolutenumber"), 0, NULL);
}
/* new ConstantIntegerType((int) $this->value) */
zv::Val toInteger() const
{
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return zv::Val();
zval valueZv;
ZVAL_STR(&valueZv, v);
return pt_type_new_constant_integer(zval_get_long(&valueZv));
}
/* new ConstantFloatType((float) $this->value) */
zv::Val toFloat() const
{
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return zv::Val();
zval valueZv;
ZVAL_STR(&valueZv, v);
return pt_type_new_constant_float(zval_get_double(&valueZv));
}
/* $this when the value stays a string key (key([$this->value => null])),
* the memoized ConstantIntegerType of its integer key otherwise; UNDEF
* = pending exception */
zv::Val toArrayKey() const
{
zval *memo = OBJ_PROP_NUM(self, slots::arrayKeyType);
if (Z_TYPE_P(memo) == IS_OBJECT) return zv::Val::copyOf(zv::Ref(memo));
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return zv::Val();
zend_ulong index;
if (!_zend_handle_numeric_str(ZSTR_VAL(v), ZSTR_LEN(v), &index)) {
/* $offsetValue === $this->value */
return thisValue();
}
/* is_int($offsetValue) ? new ConstantIntegerType($offsetValue) :
* new ConstantStringType($offsetValue) — a numeric key is always
* the int */
zv::Val arrayKeyType = pt_type_new_constant_integer((zend_long) index);
if (UNEXPECTED(arrayKeyType.isUndef())) return zv::Val();
zv::ObjRef(self).propAtWrite(slots::arrayKeyType, zv::Val::copyOf(zv::Ref(arrayKeyType.raw())));
return arrayKeyType;
}
static zend_long isString() { return PT_TRI_YES; }
/* is_numeric($this->getValue()); -1 = pending exception */
[[nodiscard]] zend_long isNumericString() const
{
zv::Val v = thisGetValue();
if (UNEXPECTED(v.isUndef())) return -1;
zend_string *str = zv::Ref(v.raw()).asString();
return is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), NULL, NULL, false) ? PT_TRI_YES : PT_TRI_NO;
}
/* (string) (int) $this->value === $this->value; -1 = pending exception */
[[nodiscard]] zend_long isDecimalIntegerString() const
{
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return -1;
zval valueZv;
ZVAL_STR(&valueZv, v);
zend_string *roundTrip = zend_long_to_str(zval_get_long(&valueZv));
bool equal = zend_string_equals(roundTrip, v);
zend_string_release(roundTrip);
return equal ? PT_TRI_YES : PT_TRI_NO;
}
/* $this->getValue() !== ''; -1 = pending exception */
[[nodiscard]] zend_long isNonEmptyString() const
{
zv::Val v = thisGetValue();
if (UNEXPECTED(v.isUndef())) return -1;
return ZSTR_LEN(zv::Ref(v.raw()).asString()) != 0 ? PT_TRI_YES : PT_TRI_NO;
}
/* !in_array($this->getValue(), ['', '0'], true); -1 = pending exception */
[[nodiscard]] zend_long isNonFalsyString() const
{
zv::Val v = thisGetValue();
if (UNEXPECTED(v.isUndef())) return -1;
zend_string *str = zv::Ref(v.raw()).asString();
bool falsy = ZSTR_LEN(str) == 0 || (ZSTR_LEN(str) == 1 && ZSTR_VAL(str)[0] == '0');
return falsy ? PT_TRI_NO : PT_TRI_YES;
}
static zend_long isLiteralString() { return PT_TRI_YES; }
/* strtolower($this->value) === $this->value; -1 = pending exception */
[[nodiscard]] zend_long isLowercaseString() const
{
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return -1;
zend_string *lower = zend_string_tolower(v);
bool equal = zend_string_equals(lower, v);
zend_string_release(lower);
return equal ? PT_TRI_YES : PT_TRI_NO;
}
/* strtoupper($this->value) === $this->value; -1 = pending exception */
[[nodiscard]] zend_long isUppercaseString() const
{
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return -1;
zend_string *upper = zend_string_toupper(v);
bool equal = zend_string_equals(upper, v);
zend_string_release(upper);
return equal ? PT_TRI_YES : PT_TRI_NO;
}
/* for an integer offset whether int<-strlen, strlen-1> is a supertype
* of it, parent::hasOffsetValueType() (StringType's) otherwise; UNDEF
* = pending exception */
zv::Val hasOffsetValueType(zval *offsetType) const
{
zend_long isInteger = pt_type_op_trinary(Z_OBJ_P(offsetType), PT_OP_IS_INTEGER, 0, NULL);
if (UNEXPECTED(isInteger < 0)) return zv::Val();
if (isInteger == PT_TRI_YES) {
zv::Val strLenType = offsetRangeType();
if (UNEXPECTED(strLenType.isUndef())) return zv::Val();
zv::Val isSuperType = pt_type_op(Z_OBJ_P(strLenType.raw()), PT_OP_IS_SUPER_TYPE_OF, 1, offsetType);
if (UNEXPECTED(isSuperType.isUndef())) return zv::Val();
/* ->result */
zend_long verdict = pt_type_result_trinary(isSuperType.raw());
if (UNEXPECTED(verdict < 0)) return zv::Val();
return pt_type_trinary(verdict);
}
return pt_string_type_has_offset_value_type(self, offsetType);
}
/* for an integer offset: the character at a constant offset within
* int<-strlen, strlen-1> (new ErrorType() outside it), the union of the
* characters at the finitely many offsets of the range's intersection
* with the offset type (plus '' when the offset may fall outside);
* parent::getOffsetValueType() (StringType's) otherwise; UNDEF =
* pending exception */
zv::Val getOffsetValueType(zval *offsetType) const
{
zend_long isInteger = pt_type_op_trinary(Z_OBJ_P(offsetType), PT_OP_IS_INTEGER, 0, NULL);
if (UNEXPECTED(isInteger < 0)) return zv::Val();
if (isInteger == PT_TRI_YES) {
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return zv::Val();
zv::Val strLenType = offsetRangeType();
if (UNEXPECTED(strLenType.isUndef())) return zv::Val();
if (instanceof_function(Z_OBJCE_P(offsetType), pt_ce_constant_integer_type)) {
zend_long verdict = superTypeVerdict(strLenType.raw(), offsetType);
if (UNEXPECTED(verdict < 0)) return zv::Val();
if (verdict == PT_TRI_YES) {
zend_long offset;
if (UNEXPECTED(!pt_constant_integer_get_value(Z_OBJ_P(offsetType), offset))) return zv::Val();
return charAt(v, offset);
}
return pt_type_new_error_type();
}
zv::Args args{strLenType.raw(), offsetType};
zv::Val intersected = pt_type_combinator_call(PT_LC("intersect"), 2, args);
if (UNEXPECTED(intersected.isUndef())) return zv::Val();
if (zv::Ref(intersected.raw()).instanceOf(pt_ce_integer_range_type)) {
zv::Val finiteTypes = pt_type_call(Z_OBJ_P(intersected.raw()), PT_LC("getfinitetypes"), 0, NULL);
if (UNEXPECTED(finiteTypes.isUndef())) return zv::Val();
if (UNEXPECTED(!zv::Ref(finiteTypes.raw()).isArray())) {
zend_type_error("phpstan_turbo: %s::getFiniteTypes() must return array", ZSTR_VAL(Z_OBJCE_P(intersected.raw())->name));
return zv::Val();
}
zv::ArrRef finite(finiteTypes.raw());
if (finite.size() == 0) return pt_string_type_get_offset_value_type(self, offsetType);
zv::Arr chars = zv::Arr::create(finite.size() + 1);
for (zv::ArrayEntry entry : finite) {
if (UNEXPECTED(!entry.value().isObject())) {
zend_type_error("phpstan_turbo: %s::getFiniteTypes() must return ConstantIntegerType instances", ZSTR_VAL(Z_OBJCE_P(intersected.raw())->name));
return zv::Val();
}
zend_long offset;
if (UNEXPECTED(!pt_constant_integer_get_value(entry.value().asObject(), offset))) return zv::Val();
zv::Val chr = charAt(v, offset);
if (UNEXPECTED(chr.isUndef())) return zv::Val();
chars.push(std::move(chr));
}
zend_long verdict = superTypeVerdict(strLenType.raw(), offsetType);
if (UNEXPECTED(verdict < 0)) return zv::Val();
if (verdict != PT_TRI_YES) {
zv::Val empty = create(ZSTR_EMPTY_ALLOC());
if (UNEXPECTED(empty.isUndef())) return zv::Val();
chars.push(std::move(empty));
}
return pt_type_combinator_call_spread(PT_LC("union"), chars.table());
}
}
return pt_string_type_get_offset_value_type(self, offsetType);
}
/* new ErrorType() for a value with no string form; for a constant
* integer offset and a constant string value, the value written into
* the string at that offset (`$value[$offset] = $char`: new ErrorType()
* for a negative offset or a value that is not one character, spaces
* padding a gap); parent::setOffsetValueType() (StringType's)
* otherwise; offsetType NULL = the twin's null; UNDEF = pending
* exception */
zv::Val setOffsetValueType(zval *offsetType, zval *valueType) const
{
zv::Val valueStringType = pt_type_call(Z_OBJ_P(valueType), PT_LC("tostring"), 0, NULL);
if (UNEXPECTED(valueStringType.isUndef())) return zv::Val();
bool isError;
if (UNEXPECTED(!pt_type_instanceof_ce(valueStringType.raw(), pt_ce_error_type, isError))) return zv::Val();
if (isError) return pt_type_new_error_type();
if (offsetType != NULL
&& instanceof_function(Z_OBJCE_P(offsetType), pt_ce_constant_integer_type)
&& zv::Ref(valueStringType.raw()).instanceOf(pt_ce_constant_string_type)
) {
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return zv::Val();
zend_long offsetValue;
if (UNEXPECTED(!pt_constant_integer_get_value(Z_OBJ_P(offsetType), offsetValue))) return zv::Val();
if (offsetValue < 0) return pt_type_new_error_type();
zv::Val stringValue = pt_constant_string_get_value(Z_OBJ_P(valueStringType.raw()));
if (UNEXPECTED(stringValue.isUndef())) return zv::Val();
zend_string *chr = zv::Ref(stringValue.raw()).asString();
if (ZSTR_LEN(chr) != 1) return pt_type_new_error_type();
/* $value[$offsetValue] = $stringValue — the engine pads a gap
* with spaces */
size_t len = ZSTR_LEN(v);
size_t newLen = (size_t) offsetValue < len ? len : (size_t) offsetValue + 1;
zend_string *written = zend_string_alloc(newLen, 0);
memcpy(ZSTR_VAL(written), ZSTR_VAL(v), len);
if (newLen > len) {
memset(ZSTR_VAL(written) + len, ' ', newLen - len);
}
ZSTR_VAL(written)[offsetValue] = ZSTR_VAL(chr)[0];
ZSTR_VAL(written)[newLen] = '\0';
zv::Val result = create(written);
zend_string_release(written);
return result;
}
return pt_string_type_set_offset_value_type(self, offsetType, valueType);
}
/* parent::setOffsetValueType($offsetType, $valueType) — StringType's */
zv::Val setExistingOffsetValueType(zval *offsetType, zval *valueType) const
{
return pt_string_type_set_offset_value_type(self, offsetType, valueType);
}
/* new self($this->getValue() . $otherString->getValue()) */
zv::Val append(zval *otherString) const
{
zv::Val own = thisGetValue();
if (UNEXPECTED(own.isUndef())) return zv::Val();
zv::Val other = pt_constant_string_get_value(Z_OBJ_P(otherString));
if (UNEXPECTED(other.isUndef())) return zv::Val();
zend_string *joined = zend_string_concat2(
ZSTR_VAL(zv::Ref(own.raw()).asString()), ZSTR_LEN(zv::Ref(own.raw()).asString()),
ZSTR_VAL(zv::Ref(other.raw()).asString()), ZSTR_LEN(zv::Ref(other.raw()).asString())
);
zv::Val result = create(joined);
zend_string_release(joined);
return result;
}
/* class-string / string for a class-string by precision; for a
* non-empty value at the more specific precision the intersection of
* string with the literal, numeric (when numeric), non-falsy (or
* non-empty for '0'), lowercase and uppercase accessories that hold;
* literal-string at the more specific precision otherwise, plain string
* else; UNDEF = pending exception */
zv::Val generalize(zval *precision) const
{
bool flag = false;
if (UNEXPECTED(!isClassStringFlag(flag))) return zv::Val();
zv::Val moreSpecificZv = pt_type_call(Z_OBJ_P(precision), PT_LC("ismorespecific"), 0, NULL);
if (UNEXPECTED(moreSpecificZv.isUndef())) return zv::Val();
bool moreSpecific = zend_is_true(moreSpecificZv.raw());
if (flag) {
zval result;
if (moreSpecific) {
if (UNEXPECTED(!pt_class_string_type_new(&result))) return zv::Val();
return zv::Val::adopt(result);
}
if (UNEXPECTED(!pt_string_type_new(&result))) return zv::Val();
return zv::Val::adopt(result);
}
zv::Val valueZv = thisGetValue();
if (UNEXPECTED(valueZv.isUndef())) return zv::Val();
zend_string *v = zv::Ref(valueZv.raw()).asString();
if (ZSTR_LEN(v) != 0 && moreSpecific) {
zv::Arr accessories = zv::Arr::create(6);
if (UNEXPECTED(!pushString(accessories) || !pushNew(accessories, pt_accessory_literal_string_type_new))) return zv::Val();
if (is_numeric_string(ZSTR_VAL(v), ZSTR_LEN(v), NULL, NULL, false)) {
if (UNEXPECTED(!pushNew(accessories, pt_accessory_numeric_string_type_new))) return zv::Val();
}
bool isZero = ZSTR_LEN(v) == 1 && ZSTR_VAL(v)[0] == '0';
if (UNEXPECTED(!pushNew(accessories, isZero ? pt_accessory_non_empty_string_type_new : pt_accessory_non_falsy_string_type_new))) return zv::Val();
zend_string *lower = zend_string_tolower(v);
bool isLower = zend_string_equals(lower, v);
zend_string_release(lower);
if (isLower && UNEXPECTED(!pushNew(accessories, pt_accessory_lowercase_string_type_new))) return zv::Val();
zend_string *upper = zend_string_toupper(v);
bool isUpper = zend_string_equals(upper, v);
zend_string_release(upper);
if (isUpper && UNEXPECTED(!pushNew(accessories, pt_accessory_uppercase_string_type_new))) return zv::Val();
return pt_intersection_of(std::move(accessories));
}
if (moreSpecific) {
zv::Arr accessories = zv::Arr::create(2);
if (UNEXPECTED(!pushString(accessories) || !pushNew(accessories, pt_accessory_literal_string_type_new))) return zv::Val();
return pt_intersection_of(std::move(accessories));
}
return pt_val_of<pt_string_type_new>();
}
/* mixed minus [true, int>=(float) $value] (+ [null, string] for '',
* + [false] when falsy) */
zv::Val getSmallerType() const
{
zend_string *v = value();
if (UNEXPECTED(v == NULL)) return zv::Val();
zval asFloat;
ZVAL_DOUBLE(&asFloat, floatValue(v));
zv::Arr subtractedTypes = zv::Arr::create(5);
if (UNEXPECTED(!pushBoolean(subtractedTypes, true) || !pushRange(subtractedTypes, pt_integer_range_create_all_greater_than_or_equal_to(&asFloat)))) {
return zv::Val();
}
if (ZSTR_LEN(v) == 0) {
if (UNEXPECTED(!pushNull(subtractedTypes) || !pushString(subtractedTypes))) return zv::Val();
}
if (!truthy(v) && UNEXPECTED(!pushBoolean(subtractedTypes, false))) return zv::Val();
return pt_type_mixed_minus(subtractedTypes.table());
}
/* mixed minus [int>(float) $value] (+ [true] when falsy) */
zv::Val getSmallerOrEqualType() const
{
zend_string *v = value();