mago-analyzer 1.21.1

A PHP static analyzer that can detect type errors in PHP code, and provide suggestions for fixing them.
Documentation
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
use mago_codex::ttype::TType;
use mago_codex::ttype::add_optional_union_type;
use mago_codex::ttype::comparator::ComparisonResult;
use mago_codex::ttype::comparator::union_comparator;
use mago_codex::ttype::expander;
use mago_codex::ttype::expander::TypeExpansionOptions;
use mago_codex::ttype::get_iterable_parameters;
use mago_codex::ttype::get_mixed;
use mago_codex::ttype::get_non_negative_int;
use mago_codex::ttype::get_null;
use mago_codex::ttype::union::TUnion;
use mago_reporting::Annotation;
use mago_reporting::Issue;
use mago_span::HasSpan;
use mago_span::Span;
use mago_syntax::ast::Yield;
use mago_syntax::ast::YieldFrom;
use mago_syntax::ast::YieldPair;
use mago_syntax::ast::YieldValue;

use crate::analyzable::Analyzable;
use crate::artifacts::AnalysisArtifacts;
use crate::code::IssueCode;
use crate::context::Context;
use crate::context::block::BlockContext;
use crate::error::AnalysisError;
use crate::utils::get_type_diff;

impl<'ast, 'arena> Analyzable<'ast, 'arena> for Yield<'arena> {
    fn analyze<'ctx>(
        &'ast self,
        context: &mut Context<'ctx, 'arena>,
        block_context: &mut BlockContext<'ctx>,
        artifacts: &mut AnalysisArtifacts,
    ) -> Result<(), AnalysisError> {
        match self {
            Yield::Value(yield_value) => yield_value.analyze(context, block_context, artifacts),
            Yield::Pair(yield_pair) => yield_pair.analyze(context, block_context, artifacts),
            Yield::From(yield_from) => yield_from.analyze(context, block_context, artifacts),
        }
    }
}

impl<'ast, 'arena> Analyzable<'ast, 'arena> for YieldValue<'arena> {
    fn analyze<'ctx>(
        &'ast self,
        context: &mut Context<'ctx, 'arena>,
        block_context: &mut BlockContext<'ctx>,
        artifacts: &mut AnalysisArtifacts,
    ) -> Result<(), AnalysisError> {
        let key_type = get_non_negative_int();
        let value_type = if let Some(value) = self.value.as_ref() {
            let was_inside_call = block_context.flags.inside_call();
            block_context.flags.set_inside_call(true);
            value.analyze(context, block_context, artifacts)?;
            block_context.flags.set_inside_call(was_inside_call);

            artifacts.get_expression_type(value).cloned().unwrap_or_else(get_mixed)
        } else {
            get_null()
        };

        let Some((k, v, s, _)) = get_current_generator_parameters(context, block_context, self.span()) else {
            artifacts.inferred_yield_key_types.push(key_type);
            artifacts.inferred_yield_value_types.push(value_type);

            return Ok(());
        };

        if !union_comparator::is_contained_by(
            context.codebase,
            &value_type,
            &v,
            false,
            false,
            false,
            &mut ComparisonResult::new(),
        ) {
            let mut issue = Issue::error(format!(
                "Invalid value type yielded; expected `{}`, but found `{}`.",
                v.get_id(),
                value_type.get_id()
            ))
            .with_annotation(
                Annotation::primary(self.value.as_ref().map_or_else(|| self.span(), mago_span::HasSpan::span))
                    .with_message(format!("This expression yields type `{}`", value_type.get_id())),
            )
            .with_note("The type of the value yielded must be assignable to the value type declared in the Generator's return type hint.")
            .with_help("Ensure the yielded value matches the expected type, or adjust the Generator's return type hint.");

            if let Some(type_diff) = get_type_diff(context, &v, &value_type) {
                issue = issue.with_note(type_diff);
            }

            context.collector.report_with_code(IssueCode::InvalidYieldValueType, issue);
        }

        if !union_comparator::is_contained_by(
            context.codebase,
            &key_type,
            &k,
            false,
            false,
            false,
            &mut ComparisonResult::new(),
        ) {
            let mut issue = Issue::error(format!(
                "Invalid key type yielded implicitly; expected `{}`, but implicit key is `{}`.",
                k.get_id(),
                key_type.get_id()
            ))
            .with_annotation(
                Annotation::primary(self.span())
                    .with_message(format!("Implicitly yields key of type `{}`", key_type.get_id())),
            )
            .with_note("When `yield $value` is used, an implicit integer key is generated. This key must be assignable to the key type declared in the Generator's return type hint.")
            .with_help("Use `yield $key => $value;` to specify a key of the correct type, or adjust the Generator's key type hint.");

            if let Some(type_diff) = get_type_diff(context, &k, &key_type) {
                issue = issue.with_note(type_diff);
            }

            context.collector.report_with_code(IssueCode::InvalidYieldKeyType, issue);
        }

        artifacts.inferred_yield_key_types.push(key_type);
        artifacts.inferred_yield_value_types.push(value_type);

        artifacts.set_expression_type(self, s);

        Ok(())
    }
}

impl<'ast, 'arena> Analyzable<'ast, 'arena> for YieldPair<'arena> {
    fn analyze<'ctx>(
        &'ast self,
        context: &mut Context<'ctx, 'arena>,
        block_context: &mut BlockContext<'ctx>,
        artifacts: &mut AnalysisArtifacts,
    ) -> Result<(), AnalysisError> {
        let key_type = {
            let was_inside_call = block_context.flags.inside_call();
            block_context.flags.set_inside_call(true);
            self.key.analyze(context, block_context, artifacts)?;
            block_context.flags.set_inside_call(was_inside_call);

            artifacts.get_expression_type(&self.key).cloned().unwrap_or_else(get_mixed)
        };

        let value_type = {
            let was_inside_call = block_context.flags.inside_call();
            block_context.flags.set_inside_call(true);
            self.value.analyze(context, block_context, artifacts)?;
            block_context.flags.set_inside_call(was_inside_call);

            artifacts.get_expression_type(&self.value).cloned().unwrap_or_else(get_mixed)
        };

        let Some((k, v, s, _)) = get_current_generator_parameters(context, block_context, self.span()) else {
            artifacts.inferred_yield_key_types.push(key_type);
            artifacts.inferred_yield_value_types.push(value_type);

            return Ok(());
        };

        if !union_comparator::is_contained_by(
            context.codebase,
            &value_type,
            &v,
            false,
            false,
            false,
            &mut ComparisonResult::new(),
        ) {
            let mut issue = Issue::error(format!(
                "Invalid value type yielded; expected `{}`, but found `{}`.",
                v.get_id(),
                value_type.get_id()
            ))
            .with_annotation(
                Annotation::primary(self.value.span())
                    .with_message(format!("This expression yields type `{}`", value_type.get_id())),
            )
            .with_note("The type of the value yielded must be assignable to the value type declared in the Generator's return type hint.")
            .with_help("Ensure the yielded value matches the expected type, or adjust the Generator's return type hint.");

            if let Some(type_diff) = get_type_diff(context, &v, &value_type) {
                issue = issue.with_note(type_diff);
            }

            context.collector.report_with_code(IssueCode::InvalidYieldValueType, issue);
        }

        if !union_comparator::is_contained_by(
            context.codebase,
            &key_type,
            &k,
            false,
            false,
            false,
            &mut ComparisonResult::new(),
        ) {
            let mut issue = Issue::error(format!(
                "Invalid key type yielded; expected `{}`, but found `{}`.",
                k.get_id(),
                key_type.get_id()
            ))
            .with_annotation(
                Annotation::primary(self.key.span())
                    .with_message(format!("This key has type `{}`", key_type.get_id())),
            )
            .with_note("The type of the key yielded must be assignable to the key type declared in the Generator's return type hint.")
            .with_help("Ensure the yielded key matches the expected type, or adjust the Generator's key type hint.");

            if let Some(type_diff) = get_type_diff(context, &k, &key_type) {
                issue = issue.with_note(type_diff);
            }

            context.collector.report_with_code(IssueCode::InvalidYieldKeyType, issue);
        }

        artifacts.inferred_yield_key_types.push(key_type);
        artifacts.inferred_yield_value_types.push(value_type);

        artifacts.set_expression_type(self, s);

        Ok(())
    }
}

impl<'ast, 'arena> Analyzable<'ast, 'arena> for YieldFrom<'arena> {
    fn analyze<'ctx>(
        &'ast self,
        context: &mut Context<'ctx, 'arena>,
        block_context: &mut BlockContext<'ctx>,
        artifacts: &mut AnalysisArtifacts,
    ) -> Result<(), AnalysisError> {
        let was_inside_call = block_context.flags.inside_call();
        block_context.flags.set_inside_call(true);
        self.iterator.analyze(context, block_context, artifacts)?;
        block_context.flags.set_inside_call(was_inside_call);

        let Some((k, v, s, _)) = get_current_generator_parameters(context, block_context, self.span()) else {
            return Ok(());
        };

        let Some(iterator_type) = artifacts.get_rc_expression_type(&self.iterator).cloned() else {
            context.collector.report_with_code(
                IssueCode::UnknownYieldFromIteratorType,
                Issue::error("Cannot determine the type of the expression in `yield from`.")
                    .with_annotation(
                        Annotation::primary(self.iterator.span())
                            .with_message("The type of this iterator is unknown"),
                    )
                    .with_note(
                        "`yield from` requires an iterable (array or `Traversable`). Its key, value, send, and return types must be compatible with the current generator."
                    )
                    .with_help(
                        "Ensure the expression has a known iterable type. Check for undefined variables or unresolvable function calls.",
                    ),
            );

            artifacts.set_expression_type(self, get_null());

            return Ok(());
        };

        for atomic in iterator_type.types.iter() {
            let (key, value) = if let Some(generator) = atomic.get_generator_parameters() {
                // the iterator is a generator! not only does it have to match key and value,
                // but also `send` type must be compatible with the current generator's `send` type
                if !union_comparator::is_contained_by(
                    context.codebase,
                    &s,
                    &generator.2,
                    false,
                    false,
                    false,
                    &mut ComparisonResult::new(),
                ) {
                    context.collector.report_with_code(
                        IssueCode::YieldFromInvalidSendType,
                        Issue::error(format!(
                            "Incompatible `send` type for `yield from`: current generator expects to be sent `{}`, but yielded generator expects `{}`.",
                            s.get_id(),
                            generator.2.get_id()
                        ))
                        .with_annotation(
                            Annotation::primary(self.iterator.span())
                                .with_message(format!("This generator expects to be sent `{}`", generator.2.get_id())),
                        )
                        .with_note("When using `yield from` with another Generator, the `send` type of the inner generator (Ts') must be a supertype of (or equal to) the `send` type of the outer generator (Ts). This means `Ts <: Ts'`.")
                        .with_help("Ensure the send types are compatible, or adjust the Generator type hints."),
                    );
                }

                (generator.0, generator.1)
            } else if let Some(parameters) = get_iterable_parameters(atomic, context.codebase) {
                parameters
            } else {
                context.collector.report_with_code(
                    IssueCode::YieldFromNonIterable,
                    Issue::error(format!(
                        "Cannot `yield from` non-iterable type `{}`.",
                        atomic.get_id()
                    ))
                    .with_annotation(Annotation::primary(self.iterator.span()).with_message(format!(
                        "Expression cannot be yielded from; it is of type `{}`",
                        atomic.get_id()
                    )))
                    .with_note(
                        "`yield from` requires an `iterable` (e.g., `array` or an object implementing `Traversable`).",
                    )
                    .with_help("Ensure the expression used with `yield from` always evaluates to an iterable type."),
                );

                continue;
            };

            if !union_comparator::is_contained_by(
                context.codebase,
                &value,
                &v,
                false,
                false,
                false,
                &mut ComparisonResult::new(),
            ) {
                let mut issue = Issue::error(format!(
                    "Invalid value type from `yield from`: current generator expects to yield `{}`, but the inner iterable yields `{}`.",
                    v.get_id(),
                    value.get_id()
                ))
                .with_annotation(
                    Annotation::primary(self.iterator.span())
                        .with_message(format!("This iterable yields values of type `{}`", value.get_id())),
                )
                .with_note("The value type yielded by the inner iterable (Tv') must be assignable to the value type of the current generator (Tv). This means `Tv' <: Tv`.")
                .with_help("Ensure the inner iterable yields compatible value types, or adjust the current Generator's type hint.");

                if let Some(type_diff) = get_type_diff(context, &v, &value) {
                    issue = issue.with_note(type_diff);
                }

                context.collector.report_with_code(IssueCode::YieldFromInvalidValueType, issue);
            }

            if !union_comparator::is_contained_by(
                context.codebase,
                &key,
                &k,
                false,
                false,
                false,
                &mut ComparisonResult::new(),
            ) {
                let mut issue = Issue::error(format!(
                    "Invalid key type from `yield from`: current generator expects to yield keys of type `{}`, but the inner iterable yields keys of type `{}`.",
                    k.get_id(),
                    key.get_id()
                ))
                .with_annotation(
                    Annotation::primary(self.iterator.span())
                        .with_message(format!("This iterable yields keys of type `{}`", key.get_id())),
                )
                .with_note("The key type yielded by the inner iterable (Tk') must be assignable to the key type of the current generator (Tk). This means `Tk' <: Tk`.")
                .with_help("Ensure the inner iterable yields compatible key types, or adjust the current Generator's type hint.");

                if let Some(type_diff) = get_type_diff(context, &k, &key) {
                    issue = issue.with_note(type_diff);
                }

                context.collector.report_with_code(IssueCode::YieldFromInvalidKeyType, issue);
            }

            artifacts.inferred_yield_key_types.push(key);
            artifacts.inferred_yield_value_types.push(value);
        }

        artifacts.set_expression_type(self, get_null());

        Ok(())
    }
}

fn get_current_generator_parameters<'ctx>(
    context: &mut Context<'ctx, '_>,
    block_context: &mut BlockContext<'ctx>,
    yield_span: Span,
) -> Option<(TUnion, TUnion, TUnion, TUnion)> {
    let Some(function) = block_context.scope.get_function_like() else {
        context.collector.report_with_code(
            IssueCode::YieldOutsideFunction,
            Issue::error("`yield` can only be used inside a function or method.")
                .with_annotation(
                    Annotation::primary(yield_span).with_message("`yield` used in an invalid context"),
                )
                .with_note("The `yield` keyword is used to create Generators and can only appear within the body of a function or method.")
                .with_help("Move the `yield` expression into a function or method body. If you are in the global scope, you cannot use `yield` directly."),
        );

        return None;
    };

    let Some(return_type_metadata) = &function.return_type_metadata else {
        return Some((get_mixed(), get_mixed(), get_mixed(), get_mixed()));
    };

    let iterable_type = &return_type_metadata.type_union;
    let mut key = None;
    let mut value = None;
    let mut sent = None;
    let mut r#return = None;
    for atomic_iterable in iterable_type.types.as_ref() {
        match atomic_iterable.get_generator_parameters() {
            Some((mut k, mut v, mut s, mut r)) => {
                expander::expand_union(context.codebase, &mut k, &TypeExpansionOptions::default());
                expander::expand_union(context.codebase, &mut v, &TypeExpansionOptions::default());
                expander::expand_union(context.codebase, &mut s, &TypeExpansionOptions::default());
                expander::expand_union(context.codebase, &mut r, &TypeExpansionOptions::default());

                key = Some(add_optional_union_type(k, key.as_ref(), context.codebase));
                value = Some(add_optional_union_type(v, value.as_ref(), context.codebase));
                sent = Some(add_optional_union_type(s, sent.as_ref(), context.codebase));
                r#return = Some(add_optional_union_type(r, r#return.as_ref(), context.codebase));
            }
            None => {
                if let Some((mut k, mut v)) = get_iterable_parameters(atomic_iterable, context.codebase) {
                    // Expand the key and value types to resolve any references like Color::*
                    expander::expand_union(context.codebase, &mut k, &TypeExpansionOptions::default());
                    expander::expand_union(context.codebase, &mut v, &TypeExpansionOptions::default());

                    key = Some(add_optional_union_type(k, key.as_ref(), context.codebase));
                    value = Some(add_optional_union_type(v, value.as_ref(), context.codebase));
                    sent = Some(get_mixed());
                    r#return = Some(get_mixed());
                } else {
                    context.collector.report_with_code(
                    IssueCode::InvalidGeneratorReturnType,
                    Issue::error(format!(
                        "Declared return type `{}` for generator function `{}` is not a valid Generator or iterable type.",
                        iterable_type.get_id(),
                        function.name.map_or_else(|| "current", |id| id.as_str())
                    ))
                    .with_annotation(
                        Annotation::primary(return_type_metadata.span)
                            .with_message(format!("Declared return type is `{}`", iterable_type.get_id())),
                    )
                    .with_annotation(
                        Annotation::secondary(yield_span)
                            .with_message("`yield` used in a generator function with an invalid return type")
                    )
                    .with_note(
                        "Functions containing `yield` are generators. Their return type hint must be `Generator`, `Iterator`, `Traversable`, or `iterable`."
                    )
                    .with_help(
                        "Adjust the return type hint to a valid Generator signature (e.g., `Generator<K, V, S, R>`) or a compatible iterable type.",
                    ),
                );

                    return None;
                }
            }
        }
    }

    Some((
        key.unwrap_or_else(get_mixed),
        value.unwrap_or_else(get_mixed),
        sent.unwrap_or_else(get_mixed),
        r#return.unwrap_or_else(get_mixed),
    ))
}