mago 1.48.1

A comprehensive suite of PHP tooling inspired by Rust’s approach, providing parsing, linting, formatting, and more through a unified CLI and library interface.
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
#![allow(clippy::expect_used, clippy::missing_panics_doc)]

use std::borrow::Cow;
use std::num::NonZeroUsize;
use std::path::Path;
use std::sync::Arc;

use mago_analyzer::external::ExternalAnalyzer;
use mago_analyzer::external::ExternalAnalyzerHandle;
use mago_analyzer::plugin::PluginRegistry;
use mago_analyzer::settings::Settings;
use mago_codex::metadata::CodebaseMetadata;
use mago_codex::reference::SymbolReferences;
use mago_database::Database;
use mago_database::file::File;
use mago_database::file::FileType;
use mago_extension::WorkerCommand;
use mago_extension::WorkerPool;
use mago_extension::WorkerPoolOptions;
use mago_orchestrator::service::incremental_analysis::IncrementalAnalysisService;
use mago_php_version::PHPVersion;
use mago_syntax::settings::ParserSettings;

mod common;

const SOURCE: &str = r"<?php

declare(strict_types=1);

/** @template T */
class Builder
{
    /** @return T */
    public function first(): mixed
    {
        return null;
    }
}

/** @property string $magic */
class BaseModel
{
    public function __get(string $property): mixed
    {
        return null;
    }

    public function __set(string $property, mixed $value): void {}

    public static function query(): mixed
    {
        return null;
    }

    public function newQuery(): mixed
    {
        return null;
    }

    public static function echoArgument(string $value): mixed
    {
        return null;
    }

    public static function acceptString(array $value): mixed
    {
        return null;
    }

    public static function late(): mixed
    {
        return null;
    }

    public static function magicValues(): mixed
    {
        return null;
    }

    public static function callLate(): mixed
    {
        return static::late();
    }

    public function passthrough(): object
    {
        return $this;
    }
}

final class User extends BaseModel {}

class Relation
{
    public function __call(string $name, array $arguments): mixed
    {
        return null;
    }
}

class HasMany extends Relation {}

final class CustomRelation extends HasMany {}

interface ExternalInterface {}

final class ExternalResult
{
    public function path(): string
    {
        return '';
    }
}

final class ExternalService {}

final class ExternalStaticService {}

final class ExternalAssertions
{
    public static function isString(mixed $value): bool
    {
        return true;
    }
}

interface ReceiverMarker
{
    public function marked(): string;
}

final class ReceiverWidening
{
    public function willImplement(string $interface): self
    {
        return $this;
    }
}

final class Attribute
{
    public const int TARGET_METHOD = 4;

    public function __construct(public int $flags = 0) {}
}

#[Attribute(Attribute::TARGET_METHOD)]
final class FrameworkInitializer {}

class FrameworkTestCase
{
    protected ExternalResult $framework;

    #[FrameworkInitializer]
    protected function setUp(): void
    {
        $this->framework = new ExternalResult();
    }

    public function getFramework(): ExternalResult
    {
        return $this->framework;
    }
}

final class ManagedTestCase extends FrameworkTestCase
{
    public ExternalResult $application;

    private ExternalResult $native;

    private ExternalResult $lifecycle;

    public function __construct()
    {
        $this->native = new ExternalResult();
    }

    public function getApplication(): ExternalResult
    {
        return $this->application;
    }

    public function getNative(): ExternalResult
    {
        return $this->native;
    }

    #[FrameworkInitializer]
    protected function setUp(): void
    {
        parent::setUp();
        $this->lifecycle = new ExternalResult();
    }

    public function getLifecycle(): ExternalResult
    {
        return $this->lifecycle;
    }
}

interface Marker {}

class DynamicProxy
{
    public function __call(string $name, array $arguments): mixed
    {
        return null;
    }
}

class DynamicFacade
{
    public static function __callStatic(string $name, array $arguments): mixed
    {
        return null;
    }
}

class ClosureCommand
{
    public function comment(string $message): void {}
}

class Artisan
{
    public static function command(string $signature, Closure $callback): void {}
}

function external_function(): mixed
{
    return null;
}

function external_assert_string(mixed $value): void {}

function collect(array $value): mixed
{
    return null;
}

/** @param Builder<User> $_builder */
function take_user_builder(Builder $_builder): void {}

function take_user(User $_user): void {}

function take_string(string $_value): void {}

function take_int(int $_value): void {}

function take_external_result(ExternalResult $_result): void {}

function take_intersection(BaseModel&Marker $_model): void {}

function take_widened_receiver(ReceiverWidening&ReceiverMarker $_receiver): void {}

/** @param array<int, string> $_values */
function take_string_map(array $_values): void {}

/** @param list<Builder<string>> $_builders */
function take_string_builders(array $_builders): void {}

take_string(external_function());
/** @var mixed $asserted */
$asserted = 'asserted';
external_assert_string($asserted);
take_string($asserted);
/** @var mixed $conditional */
$conditional = 'conditional';
if (ExternalAssertions::isString($conditional)) {
    take_string($conditional);
}
$receiver = new ReceiverWidening();
$receiver->willImplement(ReceiverMarker::class);
take_widened_receiver($receiver);
take_string($receiver->marked());
take_string(collect('function'));
Artisan::command('inspire', function (): void {
    $this->comment('Be inspired!');
});
take_user_builder(User::query());
take_string(User::echoArgument('value'));
take_string(User::acceptString('method'));
take_string_builders(User::magicValues());
take_string((new DynamicProxy())->dynamic('instance'));
take_string((new DynamicProxy())->acceptString('dynamic-signature'));
take_string_map(DynamicFacade::dynamic('static'));
take_string((new CustomRelation())->where());
take_string((new ExternalService())->resolve('instance-provider'));
take_string(ExternalStaticService::resolve('static-provider'));
User::callLate();
(new ManagedTestCase())->getApplication();
(new ManagedTestCase())->getNative();
(new ManagedTestCase())->getFramework();
(new ManagedTestCase())->getLifecycle();

function use_external_interface(ExternalInterface $service): void
{
    take_external_result($service->fetch());
}

function get_external_path(?ExternalInterface $service): ?string
{
    return $service?->fetch()->path();
}

$user = new User();
take_user_builder($user->newQuery());
take_string($user->name);
$user->name = 123;
take_string($user->name);
take_int($user->id);
take_user($user->self);
$user->secret = 'token';
// @mago-expect analysis:invalid-property-write
$user->id = 'forbidden';
// @mago-expect analysis:invalid-property-read
// @mago-expect analysis:no-value
echo $user->secret;

/** @var Builder<User> $builder */
$builder = new Builder();
take_user($builder->first());

/** @param BaseModel&Marker $model */
function inspect_intersection(BaseModel $model): void
{
    take_intersection($model->passthrough());
}
";

const NO_METHOD_PROVIDER_SOURCE: &str = r"<?php

declare(strict_types=1);

class BaseModel
{
    public static function query(): mixed
    {
        return null;
    }
}

final class User extends BaseModel {}

User::query();
";

const UNDOCUMENTED_METHOD_SOURCE: &str = r"<?php

declare(strict_types=1);

final class DynamicProxy
{
    /** @return static */
    public function __call(string $name, array $arguments): mixed
    {
        return $this;
    }
}

final class DynamicFacade
{
    /** @return static */
    public static function __callStatic(string $name, array $arguments): mixed
    {
        return new static();
    }
}

function take_proxy(DynamicProxy $_proxy): void {}

function take_facade(DynamicFacade $_facade): void {}

take_proxy((new DynamicProxy())->dynamic('instance'));
take_facade(DynamicFacade::dynamic('static'));
";

const MISSING_METHOD_SOURCE: &str = r"<?php

declare(strict_types=1);

interface DeclinedContract {}

function test(DeclinedContract $contract): void
{
    $contract->missing();
}
";

const DECLINED_PROPERTY_INITIALIZATION_SOURCE: &str = r"<?php

declare(strict_types=1);

final class ExternalResult {}

class FrameworkTestCase {}

final class ManagedTestCase extends FrameworkTestCase
{
    public ExternalResult $application;

    public function __construct() {}

    public function getApplication(): ExternalResult
    {
        return $this->application;
    }
}

(new ManagedTestCase())->getApplication();
";

const ISSUE_FILTER_SOURCE: &str = r"<?php

declare(strict_types=1);

// issue-filter-batch-proof
function known_function(): void {}

filtered_missing();
retained_missing();
";

const EFFECTIVE_SIGNATURE_DIAGNOSTIC_SOURCE: &str = r"<?php

declare(strict_types=1);

final class DynamicProxy
{
    public function __call(string $name, array $arguments): mixed
    {
        return null;
    }
}

(new DynamicProxy())->knownMethod('not-an-int');
(new DynamicProxy())->knownMethod(null);
";

#[test]
fn external_providers_receive_complete_invocation_context() -> Result<(), Box<dyn std::error::Error>> {
    if !php_sdk_is_available() {
        return Ok(());
    }

    let observation = analyze_with_fixture(SOURCE, false, false, false)?;
    assert!(
        observation.issues.is_empty(),
        "complete external invocation context should preserve precise types: {:#?}",
        observation.issues
    );

    let mut invocations = observation.invocations;
    invocations.sort_unstable();
    assert_eq!(
        invocations,
        [
            "argument-handle",
            "class-initializer-frameworktestcase",
            "class-initializer-managedtestcase",
            "closure-this-return",
            "closure-this-signature",
            "complete-composite",
            "dynamic-instance",
            "dynamic-signature",
            "dynamic-signature-return",
            "dynamic-static",
            "function",
            "function-assertion",
            "function-signature",
            "function-signature-return",
            "generic",
            "instance",
            "intersection",
            "late-static",
            "method-assertion",
            "method-signature",
            "method-signature-return",
            "missing-class-return",
            "missing-class-signature",
            "missing-interface-return",
            "missing-interface-return",
            "missing-interface-signature",
            "missing-interface-signature",
            "missing-static-return",
            "missing-static-signature",
            "property-id-read",
            "property-id-write",
            "property-initialization",
            "property-name-read",
            "property-name-write",
            "property-secret-read",
            "property-secret-write",
            "property-self-read",
            "receiver-assertion",
            "relation-subclass",
            "static"
        ]
    );

    Ok(())
}

#[test]
fn analyzer_without_method_providers_sends_no_method_request() -> Result<(), Box<dyn std::error::Error>> {
    if !php_sdk_is_available() {
        return Ok(());
    }

    let observation = analyze_with_fixture(NO_METHOD_PROVIDER_SOURCE, true, false, false)?;
    assert!(observation.issues.is_empty(), "native method analysis should remain unchanged: {:#?}", observation.issues);
    assert!(observation.invocations.is_empty(), "a host without method providers must not receive method requests");

    Ok(())
}

#[test]
fn declined_dynamic_methods_preserve_non_documented_method_diagnostics() -> Result<(), Box<dyn std::error::Error>> {
    if !php_sdk_is_available() {
        return Ok(());
    }

    let observation = analyze_with_fixture(UNDOCUMENTED_METHOD_SOURCE, true, false, false)?;
    assert_eq!(
        observation.issues,
        [
            (
                Some("non-documented-method".to_owned()),
                "Ambiguous method call to `dynamic` on class `DynamicProxy`.".to_owned()
            ),
            (
                Some("non-documented-method".to_owned()),
                "Ambiguous method call to `dynamic` on class `DynamicFacade`.".to_owned()
            ),
        ]
    );
    assert!(observation.invocations.is_empty(), "a host without method providers must receive no dynamic requests");

    Ok(())
}

#[test]
fn declined_missing_methods_preserve_non_existent_method_diagnostics() -> Result<(), Box<dyn std::error::Error>> {
    if !php_sdk_is_available() {
        return Ok(());
    }

    let observation = analyze_with_fixture(MISSING_METHOD_SOURCE, false, false, false)?;
    assert_eq!(
        observation.issues,
        [(
            Some("non-existent-method".to_owned()),
            "Method `missing` does not exist on type `DeclinedContract`.".to_owned(),
        )]
    );
    assert!(observation.invocations.is_empty(), "a declining provider must not establish the missing method");

    Ok(())
}

#[test]
fn declined_property_initialization_preserves_uninitialized_diagnostic() -> Result<(), Box<dyn std::error::Error>> {
    if !php_sdk_is_available() {
        return Ok(());
    }

    let observation = analyze_with_fixture(DECLINED_PROPERTY_INITIALIZATION_SOURCE, false, true, false)?;
    assert_eq!(observation.issues.len(), 1, "a declining provider must preserve the native diagnostic");
    assert_eq!(observation.issues[0].0.as_deref(), Some("uninitialized-property"));
    assert!(observation.invocations.is_empty(), "a declining provider must not claim initialization");

    Ok(())
}

#[test]
fn external_issue_filters_batch_and_remove_selected_native_diagnostics() -> Result<(), Box<dyn std::error::Error>> {
    if !php_sdk_is_available() {
        return Ok(());
    }

    let observation = analyze_with_fixture(ISSUE_FILTER_SOURCE, true, false, true)?;
    assert_eq!(observation.issues.len(), 1, "only the explicitly filtered native issue should be suppressed");
    assert_eq!(observation.issues[0].0.as_deref(), Some("non-existent-function"));
    assert!(observation.issues[0].1.contains("`retained_missing`"));
    assert_eq!(observation.invocations.len(), 2, "both issues from the file should reach the worker batch");
    assert!(observation.invocations.iter().all(|entry| entry.starts_with("issue-filter-")));

    Ok(())
}

#[test]
fn effective_signatures_name_the_logical_callable_and_parameter_in_diagnostics()
-> Result<(), Box<dyn std::error::Error>> {
    if !php_sdk_is_available() {
        return Ok(());
    }

    let observation = analyze_with_fixture(EFFECTIVE_SIGNATURE_DIAGNOSTIC_SOURCE, false, false, false)?;
    assert_eq!(
        observation.issues,
        [(
            Some("invalid-argument".to_owned()),
            "Invalid argument type for `$a` of `Subject::knownMethod`: expected `int`, but found `string('not-an-int')`."
                .to_owned(),
        ), (
            Some("null-argument".to_owned()),
            "Argument `$a` of method `Subject::knownMethod` is `null`, but parameter type `int` does not accept it."
                .to_owned(),
        )]
    );
    assert_eq!(
        observation.invocations,
        ["diagnostic-signature", "diagnostic-return", "diagnostic-signature", "diagnostic-return"]
    );

    Ok(())
}

fn analyze_with_fixture(
    source: &str,
    function_only: bool,
    decline_property_initialization: bool,
    register_issue_filter: bool,
) -> Result<AnalysisObservation, Box<dyn std::error::Error>> {
    let repository = Path::new(env!("CARGO_MANIFEST_DIR"));
    let temporary = tempfile::tempdir()?;
    let audit = temporary.path().join("invocations.txt");
    std::fs::write(&audit, [])?;
    let mut command = WorkerCommand::new("php")
        .with_argument(repository.join("composer/tests/Sdk/Fixtures/analyzer-invocation-worker.php"))
        .with_current_directory(repository)
        .with_environment("MAGO_INVOCATION_AUDIT_LOG", &audit);
    if function_only {
        command = command.with_environment("MAGO_INVOCATION_FUNCTION_ONLY", "1");
    }
    if decline_property_initialization {
        command = command.with_environment("MAGO_INVOCATION_DECLINE_PROPERTY_INITIALIZATION", "1");
    }
    if register_issue_filter {
        command = command.with_environment("MAGO_INVOCATION_ISSUE_FILTER", "1");
    }
    let pool = WorkerPool::spawn(command, NonZeroUsize::MIN, WorkerPoolOptions::default())?;
    let external = ExternalAnalyzer::initialize([Arc::new(pool)], PHPVersion::PHP85, &[], false)?;
    let mut registry = PluginRegistry::with_library_providers();
    registry.set_external_analyzer(Arc::new(ExternalAnalyzerHandle::ready(external)));

    let configuration = common::database_configuration("/invocation-proof", vec![Cow::Borrowed(b"src")]);
    let mut database = Database::new(configuration);
    database.add(File::new(
        Cow::Borrowed(b"src/invocation.php"),
        FileType::Host,
        None,
        Cow::Owned(source.as_bytes().to_vec()),
    ));

    let mut settings = Settings::new(PHPVersion::PHP85);
    settings.find_unused_parameters = false;
    settings.check_property_initialization = true;
    let mut service = IncrementalAnalysisService::new(
        database.read_only(),
        CodebaseMetadata::new(),
        SymbolReferences::new(),
        settings,
        ParserSettings::default(),
        Arc::new(registry),
    );
    let result = service.analyze()?;
    let issues = result.issues.iter().map(|issue| (issue.code.clone(), issue.message.clone())).collect::<Vec<_>>();
    let invocations = std::fs::read_to_string(audit)?.lines().map(str::to_owned).collect::<Vec<_>>();

    Ok(AnalysisObservation { issues, invocations })
}

fn php_sdk_is_available() -> bool {
    common::php_sdk_is_available(Path::new(env!("CARGO_MANIFEST_DIR")), "the external analyzer invocation tests")
}

struct AnalysisObservation {
    issues: Vec<(Option<String>, String)>,
    invocations: Vec<String>,
}