← All results

Importing a local type alias under a new name (as)

Group: Type aliases · Category: ecosystem · File: aliases_import_type_as.php

@phpstan-import-type X from C as Y / @psalm-import-type X from C as Y binds the imported shape to Y in the importing class. Tools that import but drop as leave Y unresolved (often as a class name) and reject the valid array; tools that ignore import entirely do the same. References: - PHPStan phpdoc-types: Local type aliases (optionally rename) - Psalm supported_annotations.md: @psalm-import-type … as

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.11Phan 6.0.11 php-ast version 1.1.3 PHP version used to run Phan: 8.5.12 Unrecognized
aliases_import_type_as.php:37: Parameter $point has undeclared type \Conformance\Tests\AliasesImportTypeAs\Point (Did you mean class \Conformance\Tests\ObjectsCloneWithProperties\Point or class \Conformance\Tests\PhpdocAdvancedPhpstanImmutable\Point or class \Conformance\Tests\PhpdocAdvancedPsalmImmutable\Point) [PhanUndeclaredTypeParameter]
aliases_import_type_as.php:45: Argument 1 ($point) is ['lat'=>35.0,'lng'=>139.0] of type array{lat:35.0,lng:139.0} but \Conformance\Tests\AliasesImportTypeAs\Map::pin() takes \Conformance\Tests\AliasesImportTypeAs\Point (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/aliases_import_type_as.php:37 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
aliases_import_type_as.php:48: Argument 1 ($point) is ['lat'=>'north','lng'=>139.0] of type array{lat:'north',lng:139.0} but \Conformance\Tests\AliasesImportTypeAs\Map::pin() takes \Conformance\Tests\AliasesImportTypeAs\Point (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/aliases_import_type_as.php:37 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]
  • Recognition: spelling not resolved — reported on declaration line(s) 37
  • Enforcement: 1/1 of the expected violations reported — incidental, since the spelling was not resolved
Expectation diff
Line 45: Expected valid value to be accepted, got ["Argument 1 ($point) is ['lat'=>35.0,'lng'=>139.0] of type array{lat:35.0,lng:139.0} but \\Conformance\\Tests\\AliasesImportTypeAs\\Map::pin() takes \\Conformance\\Tests\\AliasesImportTypeAs\\Point (no real type) defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/aliases_import_type_as.php:37 (the inferred real argument type has nothing in common with the parameter's phpdoc type) [PhanTypeMismatchArgumentProbablyReal]"]
phpstan 2.3.7PHPStan - PHP Static Analysis Tool 2.3.7 Enforced reported Lv.5+
aliases_import_type_as.php:48: Parameter #1 $point of method Conformance\Tests\AliasesImportTypeAs\Map::pin() expects array{lat: float, lng: float}, array{lat: 'north', lng: 139.0} given. [reported-from-level=5]
With strict-rules
aliases_import_type_as.php:48: Parameter #1 $point of method Conformance\Tests\AliasesImportTypeAs\Map::pin() expects array{lat: float, lng: float}, array{lat: 'north', lng: 139.0} given.
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
phpstan-strict 2.3.7PHPStan - PHP Static Analysis Tool 2.3.7 Enforced
aliases_import_type_as.php:48: Parameter #1 $point of method Conformance\Tests\AliasesImportTypeAs\Map::pin() expects array{lat: float, lng: float}, array{lat: 'north', lng: 139.0} given.
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
psalm 6.21.4Psalm 6.21.4@72023213487628df0bc5b2e02d68cbac614b7ef5 Enforced
aliases_import_type_as.php:48: Argument 1 of Conformance\Tests\AliasesImportTypeAs\Map::pin expects array{lat: float, lng: float}, but array{lat: 'north', lng: float(139)} provided [InvalidArgument]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
psalm 7 7.21.0Psalm 7.21.0@1a665f0114bd40cf12ad57a1a82066549d92f1b6 Enforced
aliases_import_type_as.php:48: Argument 1 of Conformance\Tests\AliasesImportTypeAs\Map::pin expects array{lat: float, lng: float}, but array{lat: 'north', lng: float(139)} provided [InvalidArgument]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
mago 1.61.0mago 1.61.0 Not enforced ⚠ 1 false positive
aliases_import_type_as.php:49: Possible argument type mismatch for argument #1 of `Conformance\Tests\AliasesImportTypeAs\Map::pin`: expected `array{'lat': float, 'lng': float}`, but possibly received `array{'lat': string('north'), 'lng': float(139.0)}`. [possibly-invalid-argument]
  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
  • False positives: line(s) 49
Expectation diff
Line 49: Unexpected errors ["Possible argument type mismatch for argument #1 of `Conformance\\Tests\\AliasesImportTypeAs\\Map::pin`: expected `array{'lat': float, 'lng': float}`, but possibly received `array{'lat': string('north'), 'lng': float(139.0)}`. [possibly-invalid-argument]"]
mir 0.101.0mir 0.101.0 Unrecognized
aliases_import_type_as.php:37: UndefinedDocblockClass: Docblock type 'Conformance\Tests\AliasesImportTypeAs\Point' does not exist [MIR1505]
aliases_import_type_as.php:45: InvalidArgument: Argument $point of pin() expects 'Conformance\Tests\AliasesImportTypeAs\Point', got 'array{'lat': 35, 'lng': 139}' [MIR0201]
aliases_import_type_as.php:48: InvalidArgument: Argument $point of pin() expects 'Conformance\Tests\AliasesImportTypeAs\Point', got 'array{'lat': "north", 'lng': 139}' [MIR0201]
  • Recognition: spelling not resolved — reported on declaration line(s) 37
  • Enforcement: 1/1 of the expected violations reported — incidental, since the spelling was not resolved
Expectation diff
Line 45: Expected valid value to be accepted, got ["InvalidArgument: Argument $point of pin() expects 'Conformance\\Tests\\AliasesImportTypeAs\\Point', got 'array{'lat': 35, 'lng': 139}' [MIR0201]"]
phpantom 1.1.0phpantom_lsp 1.1.0 Enforced
aliases_import_type_as.php:48: Argument 1 ($point) expects array{lat: float, lng: float}, got array{lat: 'north', lng: 139.0} [type_mismatch_argument]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
pun 0.4.3pun 0.4.3 Enforced
aliases_import_type_as.php:48: Parameter #1 $point of method Conformance\Tests\AliasesImportTypeAs\Map::pin() expects array{lat: float, lng: float}, array{lat: 'north', lng: 139.0} given. [argument.type]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
intelephense 1.18.5intelephense 1.18.5 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
phpactor 2026.11.22.0Phpactor 2026.11.22.0 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
phpy 1.0.19566phpy 1.0.19566 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/1 of the expected violations reported
qodana 263.7307.215PhpStorm 263.7307.215 Enforced ⚠ 1 false positive
aliases_import_type_as.php:10: Undefined class 'C' [PhpUndefinedClassInspection]
aliases_import_type_as.php:48: Expected parameter of type 'float', 'string' provided [PhpParamsInspection]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported
  • False positives: line(s) 10
Expectation diff
Line 10: Unexpected errors ["Undefined class 'C' [PhpUndefinedClassInspection]"]
noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Unrecognized
aliases_import_type_as.php:35: Class or interface named \Conformance\Tests\AliasesImportTypeAs\Point does not exist [undefinedClass]
  • Recognition: spelling not resolved — reported on declaration line(s) 35
  • Enforcement: 0/1 of the expected violations reported — incidental, since the spelling was not resolved
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Enforced
aliases_import_type_as.php:48: argument ['lat' => 'north', 'lng' => 139.0] to Map::pin() violates declared @param array{lat: float, lng: float} $point — declared contract violation [phpdoc.param-mismatch]
  • Recognition: spelling resolved
  • Enforcement: 1/1 of the expected violations reported

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\AliasesImportTypeAs;

/**
 * Importing a local type alias under a new name (`as`).
 *
 * `@phpstan-import-type X from C as Y` / `@psalm-import-type X from C as Y`
 * binds the imported shape to `Y` in the importing class. Tools that import
 * but drop `as` leave `Y` unresolved (often as a class name) and reject the
 * valid array; tools that ignore import entirely do the same.
 *
 * References:
 * - PHPStan phpdoc-types: Local type aliases (optionally rename)
 * - Psalm supported_annotations.md: `@psalm-import-type … as`
 */

/**
 * @phpstan-type Coordinates array{lat: float, lng: float}
 * @psalm-type Coordinates = array{lat: float, lng: float}
 */
final class Geo // T: @phpstan-type / @psalm-type
{
}

/**
 * @phpstan-import-type Coordinates from Geo as Point
 * @psalm-import-type Coordinates from Geo as Point
 */
final class Map // T: @phpstan-import-type / @psalm-import-type
{
    /**
     * @param Point $point
     */
    public function pin($point): void // T: Point
    {
    }
}

$map = new Map();

// Valid complete shape under the renamed alias.
$map->pin(['lat' => 35.0, 'lng' => 139.0]); // V

// Wrong value type for `lat` — the renamed alias should still enforce float.
$map->pin(['lat' => 'north', 'lng' => 139.0]); // E?: imported Point should reject string lat