File tree 4 files changed +39
-18
lines changed
src/Psalm/Internal/Analyzer
4 files changed +39
-18
lines changed Original file line number Diff line number Diff line change @@ -193,8 +193,7 @@ public static function getReturnTypeFromCallMapWithArgs(
193
193
return clone $ array_type ->type_param ;
194
194
}
195
195
} elseif ($ first_arg_type ->hasScalarType ()
196
- && isset ($ call_args [1 ])
197
- && ($ second_arg = $ call_args [1 ]->value )
196
+ && ($ second_arg = ($ call_args [1 ]->value ?? null ))
198
197
&& ($ second_arg_type = $ statements_analyzer ->node_data ->getType ($ second_arg ))
199
198
&& $ second_arg_type ->hasScalarType ()
200
199
) {
Original file line number Diff line number Diff line change @@ -123,6 +123,29 @@ public static function scrapeAssertions(
123
123
return $ if_types ;
124
124
}
125
125
126
+ if ($ conditional instanceof PhpParser \Node \Expr \Assign) {
127
+ $ var_name = ExpressionIdentifier::getArrayVarId (
128
+ $ conditional ->var ,
129
+ $ this_class_name ,
130
+ $ source
131
+ );
132
+
133
+ $ if_types = self ::scrapeAssertions (
134
+ $ conditional ->expr ,
135
+ $ this_class_name ,
136
+ $ source ,
137
+ $ codebase ,
138
+ $ inside_negation ,
139
+ $ cache
140
+ );
141
+
142
+ if ($ var_name ) {
143
+ $ if_types [$ var_name ] = [['!falsy ' ]];
144
+ }
145
+
146
+ return $ if_types ;
147
+ }
148
+
126
149
$ var_name = ExpressionIdentifier::getArrayVarId (
127
150
$ conditional ,
128
151
$ this_class_name ,
@@ -139,20 +162,6 @@ public static function scrapeAssertions(
139
162
}
140
163
}
141
164
142
- if ($ conditional instanceof PhpParser \Node \Expr \Assign) {
143
- $ var_name = ExpressionIdentifier::getArrayVarId (
144
- $ conditional ->var ,
145
- $ this_class_name ,
146
- $ source
147
- );
148
-
149
- if ($ var_name ) {
150
- $ if_types [$ var_name ] = [['!falsy ' ]];
151
- }
152
-
153
- return $ if_types ;
154
- }
155
-
156
165
if ($ conditional instanceof PhpParser \Node \Expr \BooleanNot) {
157
166
$ expr_assertions = null ;
158
167
Original file line number Diff line number Diff line change @@ -85,8 +85,7 @@ public static function fetch(
85
85
86
86
if (InternalCallMapHandler::inCallMap ((string ) $ call_map_id )) {
87
87
if (($ template_result ->upper_bounds || $ class_storage ->stubbed )
88
- && isset ($ class_storage ->methods [$ method_id ->method_name ])
89
- && ($ method_storage = $ class_storage ->methods [$ method_id ->method_name ])
88
+ && ($ method_storage = ($ class_storage ->methods [$ method_id ->method_name ] ?? null ))
90
89
&& $ method_storage ->return_type
91
90
) {
92
91
$ return_type_candidate = clone $ method_storage ->return_type ;
Original file line number Diff line number Diff line change @@ -2781,6 +2781,20 @@ function sayHi(string $greeting): void {
2781
2781
$a->format("d-m-Y");
2782
2782
} ' ,
2783
2783
],
2784
+ 'applyTruthyAssertionsToRightHandSideOfAssignment ' => [
2785
+ '<?php
2786
+ function takesAString(string $name): void {}
2787
+
2788
+ function randomReturn(): ?string {
2789
+ return rand(1,2) === 1 ? "foo" : null;
2790
+ }
2791
+
2792
+ $name = randomReturn();
2793
+
2794
+ if ($foo = ($name !== null)) {
2795
+ takesAString($name);
2796
+ } '
2797
+ ],
2784
2798
];
2785
2799
}
2786
2800
You can’t perform that action at this time.
0 commit comments