Conditions | 363 |
Paths | > 20000 |
Total Lines | 1153 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
769 | public static function isAtomicContainedBy( |
||
770 | Codebase $codebase, |
||
771 | Type\Atomic $input_type_part, |
||
772 | Type\Atomic $container_type_part, |
||
773 | bool $allow_interface_equality = false, |
||
774 | bool $allow_float_int_equality = true, |
||
775 | ?TypeComparisonResult $atomic_comparison_result = null |
||
776 | ) : bool { |
||
777 | if (($container_type_part instanceof TTemplateParam |
||
778 | || ($container_type_part instanceof TNamedObject |
||
779 | && isset($container_type_part->extra_types))) |
||
780 | && ($input_type_part instanceof TTemplateParam |
||
781 | || ($input_type_part instanceof TNamedObject |
||
782 | && isset($input_type_part->extra_types))) |
||
783 | ) { |
||
784 | return self::isObjectContainedByObject( |
||
785 | $codebase, |
||
786 | $input_type_part, |
||
787 | $container_type_part, |
||
788 | $allow_interface_equality, |
||
789 | $atomic_comparison_result |
||
790 | ); |
||
791 | } |
||
792 | |||
793 | if ($container_type_part instanceof TMixed |
||
794 | || ($container_type_part instanceof TTemplateParam |
||
795 | && $container_type_part->as->isMixed() |
||
796 | && !$container_type_part->extra_types |
||
797 | && $input_type_part instanceof TMixed) |
||
798 | ) { |
||
799 | if (get_class($container_type_part) === TEmptyMixed::class |
||
800 | && get_class($input_type_part) === TMixed::class |
||
801 | ) { |
||
802 | if ($atomic_comparison_result) { |
||
803 | $atomic_comparison_result->type_coerced = true; |
||
804 | $atomic_comparison_result->type_coerced_from_mixed = true; |
||
805 | } |
||
806 | |||
807 | return false; |
||
808 | } |
||
809 | |||
810 | return true; |
||
811 | } |
||
812 | |||
813 | if ($input_type_part instanceof TNever || $input_type_part instanceof Type\Atomic\TEmpty) { |
||
814 | return true; |
||
815 | } |
||
816 | |||
817 | if ($input_type_part instanceof TMixed |
||
818 | || ($input_type_part instanceof TTemplateParam |
||
819 | && $input_type_part->as->isMixed() |
||
820 | && !$input_type_part->extra_types) |
||
821 | ) { |
||
822 | if ($atomic_comparison_result) { |
||
823 | $atomic_comparison_result->type_coerced = true; |
||
824 | $atomic_comparison_result->type_coerced_from_mixed = true; |
||
825 | } |
||
826 | |||
827 | return false; |
||
828 | } |
||
829 | |||
830 | if ($input_type_part instanceof TNull) { |
||
831 | if ($container_type_part instanceof TNull) { |
||
832 | return true; |
||
833 | } |
||
834 | |||
835 | if ($container_type_part instanceof TTemplateParam |
||
836 | && ($container_type_part->as->isNullable() || $container_type_part->as->isMixed()) |
||
837 | ) { |
||
838 | return true; |
||
839 | } |
||
840 | |||
841 | return false; |
||
842 | } |
||
843 | |||
844 | if ($container_type_part instanceof TNull) { |
||
845 | return false; |
||
846 | } |
||
847 | |||
848 | if ($container_type_part instanceof ObjectLike |
||
849 | && $input_type_part instanceof TArray |
||
850 | ) { |
||
851 | $all_string_int_literals = true; |
||
852 | |||
853 | $properties = []; |
||
854 | |||
855 | foreach ($input_type_part->type_params[0]->getAtomicTypes() as $atomic_key_type) { |
||
856 | if ($atomic_key_type instanceof TLiteralString || $atomic_key_type instanceof TLiteralInt) { |
||
857 | $properties[$atomic_key_type->value] = $input_type_part->type_params[1]; |
||
858 | } else { |
||
859 | $all_string_int_literals = false; |
||
860 | } |
||
861 | } |
||
862 | |||
863 | if ($all_string_int_literals && $properties) { |
||
864 | $input_type_part = new ObjectLike($properties); |
||
865 | } |
||
866 | } |
||
867 | |||
868 | if ($container_type_part instanceof TNonEmptyString |
||
869 | && get_class($input_type_part) === TString::class |
||
870 | ) { |
||
871 | if ($atomic_comparison_result) { |
||
872 | $atomic_comparison_result->type_coerced = true; |
||
873 | } |
||
874 | |||
875 | return false; |
||
876 | } |
||
877 | |||
878 | if (($input_type_part instanceof Type\Atomic\TLowercaseString |
||
879 | || $input_type_part instanceof Type\Atomic\TNonEmptyLowercaseString) |
||
880 | && get_class($container_type_part) === TString::class |
||
881 | ) { |
||
882 | return true; |
||
883 | } |
||
884 | |||
885 | if (($container_type_part instanceof Type\Atomic\TLowercaseString |
||
886 | || $container_type_part instanceof Type\Atomic\TNonEmptyLowercaseString) |
||
887 | && $input_type_part instanceof TString |
||
888 | ) { |
||
889 | if (($input_type_part instanceof Type\Atomic\TLowercaseString |
||
890 | && $container_type_part instanceof Type\Atomic\TLowercaseString) |
||
891 | || ($input_type_part instanceof Type\Atomic\TNonEmptyLowercaseString |
||
892 | && $container_type_part instanceof Type\Atomic\TNonEmptyLowercaseString) |
||
893 | ) { |
||
894 | return true; |
||
895 | } |
||
896 | |||
897 | if ($input_type_part instanceof Type\Atomic\TNonEmptyLowercaseString |
||
898 | && $container_type_part instanceof Type\Atomic\TLowercaseString |
||
899 | ) { |
||
900 | return true; |
||
901 | } |
||
902 | |||
903 | if ($input_type_part instanceof Type\Atomic\TLowercaseString |
||
904 | && $container_type_part instanceof Type\Atomic\TNonEmptyLowercaseString |
||
905 | ) { |
||
906 | if ($atomic_comparison_result) { |
||
907 | $atomic_comparison_result->type_coerced = true; |
||
908 | } |
||
909 | |||
910 | return false; |
||
911 | } |
||
912 | |||
913 | if ($input_type_part instanceof TLiteralString) { |
||
914 | if (strtolower($input_type_part->value) === $input_type_part->value) { |
||
915 | return $input_type_part->value || $container_type_part instanceof Type\Atomic\TLowercaseString; |
||
916 | } |
||
917 | |||
918 | return false; |
||
919 | } |
||
920 | |||
921 | if ($input_type_part instanceof TClassString) { |
||
922 | return false; |
||
923 | } |
||
924 | |||
925 | if ($atomic_comparison_result) { |
||
926 | $atomic_comparison_result->type_coerced = true; |
||
927 | } |
||
928 | |||
929 | return false; |
||
930 | } |
||
931 | |||
932 | if ($input_type_part->shallowEquals($container_type_part) |
||
933 | || ($input_type_part instanceof Type\Atomic\TCallableObjectLikeArray |
||
934 | && $container_type_part instanceof TArray) |
||
935 | || ($input_type_part instanceof TCallable |
||
936 | && $container_type_part instanceof TCallable) |
||
937 | || ($input_type_part instanceof Type\Atomic\TFn |
||
938 | && $container_type_part instanceof Type\Atomic\TFn) |
||
939 | || (($input_type_part instanceof TNamedObject |
||
940 | || ($input_type_part instanceof TTemplateParam |
||
941 | && $input_type_part->as->hasObjectType()) |
||
942 | || $input_type_part instanceof TIterable) |
||
943 | && ($container_type_part instanceof TNamedObject |
||
944 | || ($container_type_part instanceof TTemplateParam |
||
945 | && $container_type_part->isObjectType()) |
||
946 | || $container_type_part instanceof TIterable) |
||
947 | && self::isObjectContainedByObject( |
||
948 | $codebase, |
||
949 | $input_type_part, |
||
950 | $container_type_part, |
||
951 | $allow_interface_equality, |
||
952 | $atomic_comparison_result |
||
953 | ) |
||
954 | ) |
||
955 | ) { |
||
956 | return self::isMatchingTypeContainedBy( |
||
957 | $codebase, |
||
958 | $input_type_part, |
||
959 | $container_type_part, |
||
960 | $atomic_comparison_result ?: new TypeComparisonResult(), |
||
961 | $allow_interface_equality |
||
962 | ); |
||
963 | } |
||
964 | |||
965 | if ($container_type_part instanceof TTemplateParam && $input_type_part instanceof TTemplateParam) { |
||
966 | return self::isContainedBy( |
||
967 | $codebase, |
||
968 | $input_type_part->as, |
||
969 | $container_type_part->as, |
||
970 | false, |
||
971 | false, |
||
972 | $atomic_comparison_result, |
||
973 | $allow_interface_equality |
||
974 | ); |
||
975 | } |
||
976 | |||
977 | if ($container_type_part instanceof TTemplateParam) { |
||
978 | foreach ($container_type_part->as->getAtomicTypes() as $container_as_type_part) { |
||
979 | if (self::isAtomicContainedBy( |
||
980 | $codebase, |
||
981 | $input_type_part, |
||
982 | $container_as_type_part, |
||
983 | $allow_interface_equality, |
||
984 | $allow_float_int_equality, |
||
985 | $atomic_comparison_result |
||
986 | )) { |
||
987 | if ($allow_interface_equality |
||
988 | || ($input_type_part instanceof TArray |
||
989 | && !$input_type_part->type_params[1]->isEmpty()) |
||
990 | || $input_type_part instanceof ObjectLike |
||
991 | ) { |
||
992 | return true; |
||
993 | } |
||
994 | } |
||
995 | } |
||
996 | |||
997 | return false; |
||
998 | } |
||
999 | |||
1000 | if ($container_type_part instanceof TConditional) { |
||
1001 | $atomic_types = array_merge( |
||
1002 | array_values($container_type_part->if_type->getAtomicTypes()), |
||
1003 | array_values($container_type_part->else_type->getAtomicTypes()) |
||
1004 | ); |
||
1005 | |||
1006 | foreach ($atomic_types as $container_as_type_part) { |
||
1007 | if (self::isAtomicContainedBy( |
||
1008 | $codebase, |
||
1009 | $input_type_part, |
||
1010 | $container_as_type_part, |
||
1011 | $allow_interface_equality, |
||
1012 | $allow_float_int_equality, |
||
1013 | $atomic_comparison_result |
||
1014 | )) { |
||
1015 | return true; |
||
1016 | } |
||
1017 | } |
||
1018 | |||
1019 | return false; |
||
1020 | } |
||
1021 | |||
1022 | if ($input_type_part instanceof TTemplateParam) { |
||
1023 | if ($input_type_part->extra_types) { |
||
1024 | foreach ($input_type_part->extra_types as $extra_type) { |
||
1025 | if (self::isAtomicContainedBy( |
||
1026 | $codebase, |
||
1027 | $extra_type, |
||
1028 | $container_type_part, |
||
1029 | $allow_interface_equality, |
||
1030 | $allow_float_int_equality, |
||
1031 | $atomic_comparison_result |
||
1032 | )) { |
||
1033 | return true; |
||
1034 | } |
||
1035 | } |
||
1036 | } |
||
1037 | |||
1038 | foreach ($input_type_part->as->getAtomicTypes() as $input_as_type_part) { |
||
1039 | if ($input_as_type_part instanceof TNull && $container_type_part instanceof TNull) { |
||
1040 | continue; |
||
1041 | } |
||
1042 | |||
1043 | if (self::isAtomicContainedBy( |
||
1044 | $codebase, |
||
1045 | $input_as_type_part, |
||
1046 | $container_type_part, |
||
1047 | $allow_interface_equality, |
||
1048 | $allow_float_int_equality, |
||
1049 | $atomic_comparison_result |
||
1050 | )) { |
||
1051 | return true; |
||
1052 | } |
||
1053 | } |
||
1054 | |||
1055 | return false; |
||
1056 | } |
||
1057 | |||
1058 | if ($input_type_part instanceof TConditional) { |
||
1059 | $input_atomic_types = array_merge( |
||
1060 | array_values($input_type_part->if_type->getAtomicTypes()), |
||
1061 | array_values($input_type_part->else_type->getAtomicTypes()) |
||
1062 | ); |
||
1063 | |||
1064 | foreach ($input_atomic_types as $input_as_type_part) { |
||
1065 | if (self::isAtomicContainedBy( |
||
1066 | $codebase, |
||
1067 | $input_as_type_part, |
||
1068 | $container_type_part, |
||
1069 | $allow_interface_equality, |
||
1070 | $allow_float_int_equality, |
||
1071 | $atomic_comparison_result |
||
1072 | )) { |
||
1073 | return true; |
||
1074 | } |
||
1075 | } |
||
1076 | |||
1077 | return false; |
||
1078 | } |
||
1079 | |||
1080 | if ($container_type_part instanceof GetClassT) { |
||
1081 | $first_type = array_values($container_type_part->as_type->getAtomicTypes())[0]; |
||
1082 | |||
1083 | $container_type_part = new TClassString( |
||
1084 | 'object', |
||
1085 | $first_type instanceof TNamedObject ? $first_type : null |
||
1086 | ); |
||
1087 | } |
||
1088 | |||
1089 | if ($input_type_part instanceof GetClassT) { |
||
1090 | $first_type = array_values($input_type_part->as_type->getAtomicTypes())[0]; |
||
1091 | |||
1092 | if ($first_type instanceof TTemplateParam) { |
||
1093 | $object_type = array_values($first_type->as->getAtomicTypes())[0]; |
||
1094 | |||
1095 | $input_type_part = new TTemplateParamClass( |
||
1096 | $first_type->param_name, |
||
1097 | $first_type->as->getId(), |
||
1098 | $object_type instanceof TNamedObject ? $object_type : null, |
||
1099 | $first_type->defining_class |
||
1100 | ); |
||
1101 | } else { |
||
1102 | $input_type_part = new TClassString( |
||
1103 | 'object', |
||
1104 | $first_type instanceof TNamedObject ? $first_type : null |
||
1105 | ); |
||
1106 | } |
||
1107 | } |
||
1108 | |||
1109 | if ($input_type_part instanceof GetTypeT) { |
||
1110 | $input_type_part = new TString(); |
||
1111 | |||
1112 | if ($container_type_part instanceof TLiteralString) { |
||
1113 | return isset(ClassLikeAnalyzer::GETTYPE_TYPES[$container_type_part->value]); |
||
1114 | } |
||
1115 | } |
||
1116 | |||
1117 | if ($container_type_part instanceof GetTypeT) { |
||
1118 | $container_type_part = new TString(); |
||
1119 | |||
1120 | if ($input_type_part instanceof TLiteralString) { |
||
1121 | return isset(ClassLikeAnalyzer::GETTYPE_TYPES[$input_type_part->value]); |
||
1122 | } |
||
1123 | } |
||
1124 | |||
1125 | if ($input_type_part->shallowEquals($container_type_part)) { |
||
1126 | return self::isMatchingTypeContainedBy( |
||
1127 | $codebase, |
||
1128 | $input_type_part, |
||
1129 | $container_type_part, |
||
1130 | $atomic_comparison_result ?: new TypeComparisonResult(), |
||
1131 | $allow_interface_equality |
||
1132 | ); |
||
1133 | } |
||
1134 | |||
1135 | if ($input_type_part instanceof TFalse |
||
1136 | && $container_type_part instanceof TBool |
||
1137 | && !($container_type_part instanceof TTrue) |
||
1138 | ) { |
||
1139 | return true; |
||
1140 | } |
||
1141 | |||
1142 | if ($input_type_part instanceof TTrue |
||
1143 | && $container_type_part instanceof TBool |
||
1144 | && !($container_type_part instanceof TFalse) |
||
1145 | ) { |
||
1146 | return true; |
||
1147 | } |
||
1148 | |||
1149 | // from https://wiki.php.net/rfc/scalar_type_hints_v5: |
||
1150 | // |
||
1151 | // > int types can resolve a parameter type of float |
||
1152 | if ($input_type_part instanceof TInt |
||
1153 | && $container_type_part instanceof TFloat |
||
1154 | && !$container_type_part instanceof TLiteralFloat |
||
1155 | && $allow_float_int_equality |
||
1156 | ) { |
||
1157 | return true; |
||
1158 | } |
||
1159 | |||
1160 | if ($input_type_part instanceof TNamedObject |
||
1161 | && $input_type_part->value === 'static' |
||
1162 | && $container_type_part instanceof TNamedObject |
||
1163 | && strtolower($container_type_part->value) === 'self' |
||
1164 | ) { |
||
1165 | return true; |
||
1166 | } |
||
1167 | |||
1168 | if ($container_type_part instanceof TCallable && $input_type_part instanceof Type\Atomic\TFn) { |
||
1169 | $all_types_contain = true; |
||
1170 | |||
1171 | if (self::compareCallable( |
||
1172 | $codebase, |
||
1173 | $input_type_part, |
||
1174 | $container_type_part, |
||
1175 | $atomic_comparison_result ?: new TypeComparisonResult(), |
||
1176 | $all_types_contain |
||
1177 | ) === false |
||
1178 | ) { |
||
1179 | return false; |
||
1180 | } |
||
1181 | |||
1182 | if (!$all_types_contain) { |
||
1183 | return false; |
||
1184 | } |
||
1185 | } |
||
1186 | |||
1187 | if ($input_type_part instanceof TNamedObject && |
||
1188 | $input_type_part->value === 'Closure' && |
||
1189 | $container_type_part instanceof TCallable |
||
1190 | ) { |
||
1191 | return true; |
||
1192 | } |
||
1193 | |||
1194 | if ($input_type_part instanceof TObject && |
||
1195 | $container_type_part instanceof TCallable |
||
1196 | ) { |
||
1197 | return true; |
||
1198 | } |
||
1199 | |||
1200 | if ($input_type_part instanceof Type\Atomic\TCallableObject && |
||
1201 | $container_type_part instanceof TObject |
||
1202 | ) { |
||
1203 | return true; |
||
1204 | } |
||
1205 | |||
1206 | if ($container_type_part instanceof TNumeric && |
||
1207 | $input_type_part->isNumericType() |
||
1208 | ) { |
||
1209 | return true; |
||
1210 | } |
||
1211 | |||
1212 | if ($container_type_part instanceof TArrayKey |
||
1213 | && $input_type_part instanceof TNumeric |
||
1214 | ) { |
||
1215 | return true; |
||
1216 | } |
||
1217 | |||
1218 | if ($container_type_part instanceof TArrayKey |
||
1219 | && ($input_type_part instanceof TInt |
||
1220 | || $input_type_part instanceof TString |
||
1221 | || $input_type_part instanceof Type\Atomic\TTemplateKeyOf) |
||
1222 | ) { |
||
1223 | return true; |
||
1224 | } |
||
1225 | |||
1226 | if ($input_type_part instanceof Type\Atomic\TTemplateKeyOf) { |
||
1227 | foreach ($input_type_part->as->getAtomicTypes() as $atomic_type) { |
||
1228 | if ($atomic_type instanceof TArray) { |
||
1229 | foreach ($atomic_type->type_params[0]->getAtomicTypes() as $array_key_atomic) { |
||
1230 | if (!self::isAtomicContainedBy( |
||
1231 | $codebase, |
||
1232 | $array_key_atomic, |
||
1233 | $container_type_part, |
||
1234 | $allow_interface_equality, |
||
1235 | $allow_float_int_equality, |
||
1236 | $atomic_comparison_result |
||
1237 | )) { |
||
1238 | return false; |
||
1239 | } |
||
1240 | } |
||
1241 | } |
||
1242 | } |
||
1243 | |||
1244 | return true; |
||
1245 | } |
||
1246 | |||
1247 | if ($input_type_part instanceof TArrayKey && |
||
1248 | ($container_type_part instanceof TInt || $container_type_part instanceof TString) |
||
1249 | ) { |
||
1250 | if ($atomic_comparison_result) { |
||
1251 | $atomic_comparison_result->type_coerced = true; |
||
1252 | $atomic_comparison_result->type_coerced_from_mixed = true; |
||
1253 | $atomic_comparison_result->scalar_type_match_found = true; |
||
1254 | } |
||
1255 | |||
1256 | return false; |
||
1257 | } |
||
1258 | |||
1259 | if (($container_type_part instanceof ObjectLike |
||
1260 | && $input_type_part instanceof ObjectLike) |
||
1261 | || ($container_type_part instanceof TObjectWithProperties |
||
1262 | && $input_type_part instanceof TObjectWithProperties) |
||
1263 | ) { |
||
1264 | $all_types_contain = true; |
||
1265 | |||
1266 | foreach ($container_type_part->properties as $key => $container_property_type) { |
||
1267 | if (!isset($input_type_part->properties[$key])) { |
||
1268 | if (!$container_property_type->possibly_undefined) { |
||
1269 | $all_types_contain = false; |
||
1270 | } |
||
1271 | |||
1272 | continue; |
||
1273 | } |
||
1274 | |||
1275 | $input_property_type = $input_type_part->properties[$key]; |
||
1276 | |||
1277 | $property_type_comparison = new TypeComparisonResult(); |
||
1278 | |||
1279 | if (!$input_property_type->isEmpty() |
||
1280 | && !self::isContainedBy( |
||
1281 | $codebase, |
||
1282 | $input_property_type, |
||
1283 | $container_property_type, |
||
1284 | $input_property_type->ignore_nullable_issues, |
||
1285 | $input_property_type->ignore_falsable_issues, |
||
1286 | $property_type_comparison, |
||
1287 | $allow_interface_equality |
||
1288 | ) |
||
1289 | && !$property_type_comparison->type_coerced_from_scalar |
||
1290 | ) { |
||
1291 | $inverse_property_type_comparison = new TypeComparisonResult(); |
||
1292 | |||
1293 | if ($atomic_comparison_result) { |
||
1294 | if (self::isContainedBy( |
||
1295 | $codebase, |
||
1296 | $container_property_type, |
||
1297 | $input_property_type, |
||
1298 | false, |
||
1299 | false, |
||
1300 | $inverse_property_type_comparison, |
||
1301 | $allow_interface_equality |
||
1302 | ) |
||
1303 | || $inverse_property_type_comparison->type_coerced_from_scalar |
||
1304 | ) { |
||
1305 | $atomic_comparison_result->type_coerced = true; |
||
1306 | } |
||
1307 | } |
||
1308 | |||
1309 | $all_types_contain = false; |
||
1310 | } |
||
1311 | } |
||
1312 | |||
1313 | if ($all_types_contain) { |
||
1314 | if ($atomic_comparison_result) { |
||
1315 | $atomic_comparison_result->to_string_cast = false; |
||
1316 | } |
||
1317 | |||
1318 | return true; |
||
1319 | } |
||
1320 | |||
1321 | return false; |
||
1322 | } |
||
1323 | |||
1324 | if ($container_type_part instanceof TIterable) { |
||
1325 | if ($input_type_part instanceof TArray |
||
1326 | || $input_type_part instanceof ObjectLike |
||
1327 | || $input_type_part instanceof TList |
||
1328 | ) { |
||
1329 | if ($input_type_part instanceof ObjectLike) { |
||
1330 | $input_type_part = $input_type_part->getGenericArrayType(); |
||
1331 | } elseif ($input_type_part instanceof TList) { |
||
1332 | $input_type_part = new TArray([Type::getInt(), $input_type_part->type_param]); |
||
1333 | } |
||
1334 | |||
1335 | $all_types_contain = true; |
||
1336 | |||
1337 | foreach ($input_type_part->type_params as $i => $input_param) { |
||
1338 | $container_param_offset = $i - (2 - count($container_type_part->type_params)); |
||
1339 | |||
1340 | if ($container_param_offset === -1) { |
||
1341 | continue; |
||
1342 | } |
||
1343 | |||
1344 | $container_param = $container_type_part->type_params[$container_param_offset]; |
||
1345 | |||
1346 | if ($i === 0 |
||
1347 | && $input_param->hasMixed() |
||
1348 | && $container_param->hasString() |
||
1349 | && $container_param->hasInt() |
||
1350 | ) { |
||
1351 | continue; |
||
1352 | } |
||
1353 | |||
1354 | $array_comparison_result = new TypeComparisonResult(); |
||
1355 | |||
1356 | if (!$input_param->isEmpty() |
||
1357 | && !self::isContainedBy( |
||
1358 | $codebase, |
||
1359 | $input_param, |
||
1360 | $container_param, |
||
1361 | $input_param->ignore_nullable_issues, |
||
1362 | $input_param->ignore_falsable_issues, |
||
1363 | $array_comparison_result, |
||
1364 | $allow_interface_equality |
||
1365 | ) |
||
1366 | && !$array_comparison_result->type_coerced_from_scalar |
||
1367 | ) { |
||
1368 | if ($atomic_comparison_result && $array_comparison_result->type_coerced_from_mixed) { |
||
1369 | $atomic_comparison_result->type_coerced_from_mixed = true; |
||
1370 | } |
||
1371 | $all_types_contain = false; |
||
1372 | } |
||
1373 | } |
||
1374 | |||
1375 | if ($all_types_contain) { |
||
1376 | if ($atomic_comparison_result) { |
||
1377 | $atomic_comparison_result->to_string_cast = false; |
||
1378 | } |
||
1379 | |||
1380 | return true; |
||
1381 | } |
||
1382 | |||
1383 | return false; |
||
1384 | } |
||
1385 | |||
1386 | if ($input_type_part->hasTraversableInterface($codebase)) { |
||
1387 | return true; |
||
1388 | } |
||
1389 | } |
||
1390 | |||
1391 | if ($container_type_part instanceof TScalar && $input_type_part instanceof Scalar) { |
||
1392 | return true; |
||
1393 | } |
||
1394 | |||
1395 | if (get_class($container_type_part) === TInt::class && $input_type_part instanceof TLiteralInt) { |
||
1396 | return true; |
||
1397 | } |
||
1398 | |||
1399 | if (get_class($container_type_part) === TFloat::class && $input_type_part instanceof TLiteralFloat) { |
||
1400 | return true; |
||
1401 | } |
||
1402 | |||
1403 | if ($container_type_part instanceof TNonEmptyString |
||
1404 | && $input_type_part instanceof TLiteralString |
||
1405 | && $input_type_part->value === '' |
||
1406 | ) { |
||
1407 | return false; |
||
1408 | } |
||
1409 | |||
1410 | if ((get_class($container_type_part) === TString::class |
||
1411 | || get_class($container_type_part) === TNonEmptyString::class |
||
1412 | || get_class($container_type_part) === TSingleLetter::class) |
||
1413 | && $input_type_part instanceof TLiteralString |
||
1414 | ) { |
||
1415 | return true; |
||
1416 | } |
||
1417 | |||
1418 | if (get_class($input_type_part) === TInt::class && $container_type_part instanceof TLiteralInt) { |
||
1419 | if ($atomic_comparison_result) { |
||
1420 | $atomic_comparison_result->type_coerced = true; |
||
1421 | $atomic_comparison_result->type_coerced_from_scalar = true; |
||
1422 | } |
||
1423 | |||
1424 | return false; |
||
1425 | } |
||
1426 | |||
1427 | if (get_class($input_type_part) === TFloat::class && $container_type_part instanceof TLiteralFloat) { |
||
1428 | if ($atomic_comparison_result) { |
||
1429 | $atomic_comparison_result->type_coerced = true; |
||
1430 | $atomic_comparison_result->type_coerced_from_scalar = true; |
||
1431 | } |
||
1432 | |||
1433 | return false; |
||
1434 | } |
||
1435 | |||
1436 | if ((get_class($input_type_part) === TString::class |
||
1437 | || get_class($input_type_part) === TSingleLetter::class |
||
1438 | || get_class($input_type_part) === TNonEmptyString::class) |
||
1439 | && $container_type_part instanceof TLiteralString |
||
1440 | ) { |
||
1441 | if ($atomic_comparison_result) { |
||
1442 | $atomic_comparison_result->type_coerced = true; |
||
1443 | $atomic_comparison_result->type_coerced_from_scalar = true; |
||
1444 | } |
||
1445 | |||
1446 | return false; |
||
1447 | } |
||
1448 | |||
1449 | if (($input_type_part instanceof Type\Atomic\TLowercaseString |
||
1450 | || $input_type_part instanceof Type\Atomic\TNonEmptyLowercaseString) |
||
1451 | && $container_type_part instanceof TLiteralString |
||
1452 | && strtolower($container_type_part->value) === $container_type_part->value |
||
1453 | ) { |
||
1454 | if ($atomic_comparison_result |
||
1455 | && ($container_type_part->value || $input_type_part instanceof Type\Atomic\TLowercaseString) |
||
1456 | ) { |
||
1457 | $atomic_comparison_result->type_coerced = true; |
||
1458 | $atomic_comparison_result->type_coerced_from_scalar = true; |
||
1459 | } |
||
1460 | |||
1461 | return false; |
||
1462 | } |
||
1463 | |||
1464 | if (($container_type_part instanceof TClassString || $container_type_part instanceof TLiteralClassString) |
||
1465 | && ($input_type_part instanceof TClassString || $input_type_part instanceof TLiteralClassString) |
||
1466 | ) { |
||
1467 | if ($container_type_part instanceof TLiteralClassString |
||
1468 | && $input_type_part instanceof TLiteralClassString |
||
1469 | ) { |
||
1470 | return $container_type_part->value === $input_type_part->value; |
||
1471 | } |
||
1472 | |||
1473 | if ($container_type_part instanceof TTemplateParamClass |
||
1474 | && get_class($input_type_part) === TClassString::class |
||
1475 | ) { |
||
1476 | if ($atomic_comparison_result) { |
||
1477 | $atomic_comparison_result->type_coerced = true; |
||
1478 | } |
||
1479 | |||
1480 | return false; |
||
1481 | } |
||
1482 | |||
1483 | if ($container_type_part instanceof TClassString |
||
1484 | && $container_type_part->as === 'object' |
||
1485 | && !$container_type_part->as_type |
||
1486 | ) { |
||
1487 | return true; |
||
1488 | } |
||
1489 | |||
1490 | if ($input_type_part instanceof TClassString |
||
1491 | && $input_type_part->as === 'object' |
||
1492 | && !$input_type_part->as_type |
||
1493 | ) { |
||
1494 | if ($atomic_comparison_result) { |
||
1495 | $atomic_comparison_result->type_coerced = true; |
||
1496 | $atomic_comparison_result->type_coerced_from_scalar = true; |
||
1497 | } |
||
1498 | |||
1499 | return false; |
||
1500 | } |
||
1501 | |||
1502 | $fake_container_object = $container_type_part instanceof TClassString |
||
1503 | && $container_type_part->as_type |
||
1504 | ? $container_type_part->as_type |
||
1505 | : new TNamedObject( |
||
1506 | $container_type_part instanceof TClassString |
||
1507 | ? $container_type_part->as |
||
1508 | : $container_type_part->value |
||
1509 | ); |
||
1510 | |||
1511 | $fake_input_object = $input_type_part instanceof TClassString |
||
1512 | && $input_type_part->as_type |
||
1513 | ? $input_type_part->as_type |
||
1514 | : new TNamedObject( |
||
1515 | $input_type_part instanceof TClassString |
||
1516 | ? $input_type_part->as |
||
1517 | : $input_type_part->value |
||
1518 | ); |
||
1519 | |||
1520 | return self::isAtomicContainedBy( |
||
1521 | $codebase, |
||
1522 | $fake_input_object, |
||
1523 | $fake_container_object, |
||
1524 | $allow_interface_equality, |
||
1525 | $allow_float_int_equality, |
||
1526 | $atomic_comparison_result |
||
1527 | ); |
||
1528 | } |
||
1529 | |||
1530 | if ($container_type_part instanceof TString && $input_type_part instanceof TTraitString) { |
||
1531 | return true; |
||
1532 | } |
||
1533 | |||
1534 | if ($container_type_part instanceof TTraitString |
||
1535 | && (get_class($input_type_part) === TString::class |
||
1536 | || get_class($input_type_part) === TNonEmptyString::class) |
||
1537 | ) { |
||
1538 | if ($atomic_comparison_result) { |
||
1539 | $atomic_comparison_result->type_coerced = true; |
||
1540 | } |
||
1541 | |||
1542 | return false; |
||
1543 | } |
||
1544 | |||
1545 | if (($input_type_part instanceof TClassString |
||
1546 | || $input_type_part instanceof TLiteralClassString) |
||
1547 | && (get_class($container_type_part) === TString::class |
||
1548 | || get_class($container_type_part) === TSingleLetter::class |
||
1549 | || get_class($container_type_part) === TNonEmptyString::class) |
||
1550 | ) { |
||
1551 | return true; |
||
1552 | } |
||
1553 | |||
1554 | if ($input_type_part instanceof TCallableString |
||
1555 | && (get_class($container_type_part) === TString::class |
||
1556 | || get_class($container_type_part) === TSingleLetter::class |
||
1557 | || get_class($container_type_part) === TNonEmptyString::class) |
||
1558 | ) { |
||
1559 | return true; |
||
1560 | } |
||
1561 | |||
1562 | if ($container_type_part instanceof TString |
||
1563 | && ($input_type_part instanceof TNumericString |
||
1564 | || $input_type_part instanceof THtmlEscapedString) |
||
1565 | ) { |
||
1566 | if ($container_type_part instanceof TLiteralString) { |
||
1567 | if (\is_numeric($container_type_part->value) && $atomic_comparison_result) { |
||
1568 | $atomic_comparison_result->type_coerced = true; |
||
1569 | } |
||
1570 | |||
1571 | return false; |
||
1572 | } |
||
1573 | |||
1574 | return true; |
||
1575 | } |
||
1576 | |||
1577 | if ($input_type_part instanceof TString |
||
1578 | && ($container_type_part instanceof TNumericString |
||
1579 | || $container_type_part instanceof THtmlEscapedString) |
||
1580 | ) { |
||
1581 | if ($input_type_part instanceof TLiteralString) { |
||
1582 | return \is_numeric($input_type_part->value); |
||
1583 | } |
||
1584 | if ($atomic_comparison_result) { |
||
1585 | $atomic_comparison_result->type_coerced = true; |
||
1586 | } |
||
1587 | |||
1588 | return false; |
||
1589 | } |
||
1590 | |||
1591 | if ($container_type_part instanceof TCallableString |
||
1592 | && $input_type_part instanceof TLiteralString |
||
1593 | ) { |
||
1594 | $input_callable = self::getCallableFromAtomic($codebase, $input_type_part); |
||
1595 | $container_callable = self::getCallableFromAtomic($codebase, $container_type_part); |
||
1596 | |||
1597 | if ($input_callable && $container_callable) { |
||
1598 | $all_types_contain = true; |
||
1599 | |||
1600 | if (self::compareCallable( |
||
1601 | $codebase, |
||
1602 | $input_callable, |
||
1603 | $container_callable, |
||
1604 | $atomic_comparison_result ?: new TypeComparisonResult(), |
||
1605 | $all_types_contain |
||
1606 | ) === false |
||
1607 | ) { |
||
1608 | return false; |
||
1609 | } |
||
1610 | |||
1611 | if (!$all_types_contain) { |
||
1612 | return false; |
||
1613 | } |
||
1614 | } |
||
1615 | |||
1616 | return true; |
||
1617 | } |
||
1618 | |||
1619 | if (($container_type_part instanceof TClassString |
||
1620 | || $container_type_part instanceof TLiteralClassString |
||
1621 | || $container_type_part instanceof TCallableString) |
||
1622 | && $input_type_part instanceof TString |
||
1623 | ) { |
||
1624 | if ($atomic_comparison_result) { |
||
1625 | $atomic_comparison_result->type_coerced = true; |
||
1626 | } |
||
1627 | |||
1628 | return false; |
||
1629 | } |
||
1630 | |||
1631 | if (($container_type_part instanceof TString || $container_type_part instanceof TScalar) |
||
1632 | && $input_type_part instanceof TNamedObject |
||
1633 | ) { |
||
1634 | // check whether the object has a __toString method |
||
1635 | if ($codebase->classOrInterfaceExists($input_type_part->value) |
||
1636 | && $codebase->methods->methodExists( |
||
1637 | new \Psalm\Internal\MethodIdentifier( |
||
1638 | $input_type_part->value, |
||
1639 | '__tostring' |
||
1640 | ) |
||
1641 | ) |
||
1642 | ) { |
||
1643 | if ($atomic_comparison_result) { |
||
1644 | $atomic_comparison_result->to_string_cast = true; |
||
1645 | } |
||
1646 | |||
1647 | return true; |
||
1648 | } |
||
1649 | |||
1650 | // PHP 5.6 doesn't support this natively, so this introduces a bug *just* when checking PHP 5.6 code |
||
1651 | if ($input_type_part->value === 'ReflectionType') { |
||
1652 | if ($atomic_comparison_result) { |
||
1653 | $atomic_comparison_result->to_string_cast = true; |
||
1654 | } |
||
1655 | |||
1656 | return true; |
||
1657 | } |
||
1658 | } |
||
1659 | |||
1660 | if (($container_type_part instanceof TString || $container_type_part instanceof TScalar) |
||
1661 | && $input_type_part instanceof TObjectWithProperties |
||
1662 | && isset($input_type_part->methods['__toString']) |
||
1663 | ) { |
||
1664 | if ($atomic_comparison_result) { |
||
1665 | $atomic_comparison_result->to_string_cast = true; |
||
1666 | } |
||
1667 | |||
1668 | return true; |
||
1669 | } |
||
1670 | |||
1671 | if ($container_type_part instanceof Type\Atomic\TFn && $input_type_part instanceof TCallable) { |
||
1672 | if ($atomic_comparison_result) { |
||
1673 | $atomic_comparison_result->type_coerced = true; |
||
1674 | } |
||
1675 | |||
1676 | return false; |
||
1677 | } |
||
1678 | |||
1679 | if ($container_type_part instanceof TCallable && |
||
1680 | ( |
||
1681 | $input_type_part instanceof TLiteralString |
||
1682 | || $input_type_part instanceof TCallableString |
||
1683 | || $input_type_part instanceof TArray |
||
1684 | || $input_type_part instanceof ObjectLike |
||
1685 | || $input_type_part instanceof TList |
||
1686 | || ( |
||
1687 | $input_type_part instanceof TNamedObject && |
||
1688 | $codebase->classOrInterfaceExists($input_type_part->value) && |
||
1689 | $codebase->methodExists($input_type_part->value . '::__invoke') |
||
1690 | ) |
||
1691 | ) |
||
1692 | ) { |
||
1693 | if ($input_type_part instanceof TList) { |
||
1694 | if ($input_type_part->type_param->isMixed() |
||
1695 | || $input_type_part->type_param->hasScalar() |
||
1696 | ) { |
||
1697 | if ($atomic_comparison_result) { |
||
1698 | $atomic_comparison_result->type_coerced_from_mixed = true; |
||
1699 | $atomic_comparison_result->type_coerced = true; |
||
1700 | } |
||
1701 | |||
1702 | return false; |
||
1703 | } |
||
1704 | |||
1705 | if (!$input_type_part->type_param->hasString()) { |
||
1706 | return false; |
||
1707 | } |
||
1708 | |||
1709 | if (!$input_type_part instanceof Type\Atomic\TCallableList) { |
||
1710 | if ($atomic_comparison_result) { |
||
1711 | $atomic_comparison_result->type_coerced_from_mixed = true; |
||
1712 | $atomic_comparison_result->type_coerced = true; |
||
1713 | } |
||
1714 | |||
1715 | return false; |
||
1716 | } |
||
1717 | } |
||
1718 | |||
1719 | if ($input_type_part instanceof TArray) { |
||
1720 | if ($input_type_part->type_params[1]->isMixed() |
||
1721 | || $input_type_part->type_params[1]->hasScalar() |
||
1722 | ) { |
||
1723 | if ($atomic_comparison_result) { |
||
1724 | $atomic_comparison_result->type_coerced_from_mixed = true; |
||
1725 | $atomic_comparison_result->type_coerced = true; |
||
1726 | } |
||
1727 | |||
1728 | return false; |
||
1729 | } |
||
1730 | |||
1731 | if (!$input_type_part->type_params[1]->hasString()) { |
||
1732 | return false; |
||
1733 | } |
||
1734 | |||
1735 | if (!$input_type_part instanceof Type\Atomic\TCallableArray) { |
||
1736 | if ($atomic_comparison_result) { |
||
1737 | $atomic_comparison_result->type_coerced_from_mixed = true; |
||
1738 | $atomic_comparison_result->type_coerced = true; |
||
1739 | } |
||
1740 | |||
1741 | return false; |
||
1742 | } |
||
1743 | } elseif ($input_type_part instanceof ObjectLike) { |
||
1744 | $method_id = self::getCallableMethodIdFromObjectLike($input_type_part); |
||
1745 | |||
1746 | if ($method_id === 'not-callable') { |
||
1747 | return false; |
||
1748 | } |
||
1749 | |||
1750 | if (!$method_id) { |
||
1751 | return true; |
||
1752 | } |
||
1753 | |||
1754 | try { |
||
1755 | $method_id = $codebase->methods->getDeclaringMethodId($method_id); |
||
1756 | |||
1757 | if (!$method_id) { |
||
1758 | return false; |
||
1759 | } |
||
1760 | |||
1761 | $codebase->methods->getStorage($method_id); |
||
1762 | } catch (\Exception $e) { |
||
1763 | return false; |
||
1764 | } |
||
1765 | } |
||
1766 | |||
1767 | $input_callable = self::getCallableFromAtomic($codebase, $input_type_part, $container_type_part); |
||
1768 | |||
1769 | if ($input_callable) { |
||
1770 | $all_types_contain = true; |
||
1771 | |||
1772 | if (self::compareCallable( |
||
1773 | $codebase, |
||
1774 | $input_callable, |
||
1775 | $container_type_part, |
||
1776 | $atomic_comparison_result ?: new TypeComparisonResult(), |
||
1777 | $all_types_contain |
||
1778 | ) === false |
||
1779 | ) { |
||
1780 | return false; |
||
1781 | } |
||
1782 | |||
1783 | if (!$all_types_contain) { |
||
1784 | return false; |
||
1785 | } |
||
1786 | } |
||
1787 | |||
1788 | return true; |
||
1789 | } |
||
1790 | |||
1791 | if ($input_type_part instanceof TNumeric) { |
||
1792 | if ($container_type_part->isNumericType()) { |
||
1793 | if ($atomic_comparison_result) { |
||
1794 | $atomic_comparison_result->scalar_type_match_found = true; |
||
1795 | } |
||
1796 | } |
||
1797 | } |
||
1798 | |||
1799 | if ($input_type_part instanceof Scalar) { |
||
1800 | if ($container_type_part instanceof Scalar |
||
1801 | && !$container_type_part instanceof TLiteralInt |
||
1802 | && !$container_type_part instanceof TLiteralString |
||
1803 | && !$container_type_part instanceof TLiteralFloat |
||
1804 | ) { |
||
1805 | if ($atomic_comparison_result) { |
||
1806 | $atomic_comparison_result->scalar_type_match_found = true; |
||
1807 | } |
||
1808 | } |
||
1809 | } elseif ($container_type_part instanceof TObject |
||
1810 | && $input_type_part instanceof TNamedObject |
||
1811 | ) { |
||
1812 | if ($container_type_part instanceof TObjectWithProperties |
||
1813 | && $input_type_part->value !== 'stdClass' |
||
1814 | ) { |
||
1815 | $all_types_contain = true; |
||
1816 | |||
1817 | foreach ($container_type_part->properties as $property_name => $container_property_type) { |
||
1818 | if (!is_string($property_name)) { |
||
1819 | continue; |
||
1820 | } |
||
1821 | |||
1822 | if (!$codebase->properties->propertyExists( |
||
1823 | $input_type_part . '::$' . $property_name, |
||
1824 | true |
||
1825 | )) { |
||
1826 | $all_types_contain = false; |
||
1827 | |||
1828 | continue; |
||
1829 | } |
||
1830 | |||
1831 | $property_declaring_class = (string) $codebase->properties->getDeclaringClassForProperty( |
||
1832 | $input_type_part . '::$' . $property_name, |
||
1833 | true |
||
1834 | ); |
||
1835 | |||
1836 | $class_storage = $codebase->classlike_storage_provider->get($property_declaring_class); |
||
1837 | |||
1838 | $input_property_storage = $class_storage->properties[$property_name]; |
||
1839 | |||
1840 | $input_property_type = $input_property_storage->type ?: Type::getMixed(); |
||
1841 | |||
1842 | $property_type_comparison = new TypeComparisonResult(); |
||
1843 | |||
1844 | if (!$input_property_type->isEmpty() |
||
1845 | && !self::isContainedBy( |
||
1846 | $codebase, |
||
1847 | $input_property_type, |
||
1848 | $container_property_type, |
||
1849 | false, |
||
1850 | false, |
||
1851 | $property_type_comparison, |
||
1852 | $allow_interface_equality |
||
1853 | ) |
||
1854 | && !$property_type_comparison->type_coerced_from_scalar |
||
1855 | ) { |
||
1856 | $inverse_property_type_comparison = new TypeComparisonResult(); |
||
1857 | |||
1858 | if (self::isContainedBy( |
||
1859 | $codebase, |
||
1860 | $container_property_type, |
||
1861 | $input_property_type, |
||
1862 | false, |
||
1863 | false, |
||
1864 | $inverse_property_type_comparison, |
||
1865 | $allow_interface_equality |
||
1866 | ) |
||
1867 | || $inverse_property_type_comparison->type_coerced_from_scalar |
||
1868 | ) { |
||
1869 | if ($atomic_comparison_result) { |
||
1870 | $atomic_comparison_result->type_coerced = true; |
||
1871 | } |
||
1872 | } |
||
1873 | |||
1874 | $all_types_contain = false; |
||
1875 | } |
||
1876 | } |
||
1877 | |||
1878 | if ($all_types_contain === true) { |
||
1879 | return true; |
||
1880 | } |
||
1881 | |||
1882 | return false; |
||
1883 | } |
||
1884 | |||
1885 | return true; |
||
1886 | } elseif ($atomic_comparison_result) { |
||
1887 | if ($input_type_part instanceof TObject && $container_type_part instanceof TNamedObject) { |
||
1888 | $atomic_comparison_result->type_coerced = true; |
||
1889 | } elseif ($container_type_part instanceof TNamedObject |
||
1890 | && $input_type_part instanceof TNamedObject |
||
1891 | ) { |
||
1892 | if ($container_type_part->was_static |
||
1893 | && !$input_type_part->was_static |
||
1894 | ) { |
||
1895 | $atomic_comparison_result->type_coerced = true; |
||
1896 | } elseif ($codebase->classOrInterfaceExists($input_type_part->value) |
||
1897 | && ( |
||
1898 | ( |
||
1899 | $codebase->classExists($container_type_part->value) |
||
1900 | && $codebase->classExtendsOrImplements( |
||
1901 | $container_type_part->value, |
||
1902 | $input_type_part->value |
||
1903 | ) |
||
1904 | ) |
||
1905 | || |
||
1906 | ( |
||
1907 | $codebase->interfaceExists($container_type_part->value) |
||
1908 | && $codebase->interfaceExtends( |
||
1909 | $container_type_part->value, |
||
1910 | $input_type_part->value |
||
1911 | ) |
||
1912 | ) |
||
1913 | ) |
||
1914 | ) { |
||
1915 | $atomic_comparison_result->type_coerced = true; |
||
1916 | } |
||
1917 | } |
||
1918 | } |
||
1919 | |||
1920 | return false; |
||
1921 | } |
||
1922 | |||
2787 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.