定制 amiut/phpstan-type-utilities 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

amiut/phpstan-type-utilities

Composer 安装命令:

composer require amiut/phpstan-type-utilities

包简介

PHPStan type utilities for inferring and reusing static array shapes from PHPDoc.

README 文档

README

Warning

This project is experimental and not stable. APIs may change without notice.

package version php version Packagist

PHPStan Type Utilities is a set of opt-in PHPStan/PHPDoc type utilities for PHP projects.

The package currently includes utilities for detecting callable return types and inferring static PHP array shapes. The goal is to bring a small, explicit, TypeScript-utility-types style workflow to PHPStan without turning the extension into a general type solver.

The implementation is intentionally conservative. It infers PHP array literals as PHP array shapes only; it does not interpret arrays semantically or treat domain-specific array structures differently.

Installation

Install the package as a development dependency:

composer require --dev amiut/phpstan-type-utilities

The extension is registered automatically through Composer's PHPStan extension discovery. If your project does not use extension discovery, include it manually:

includes:
    - %currentWorkingDirectory%/vendor/amiut/phpstan-type-utilities/extension.neon

Use %currentWorkingDirectory% for manual includes. A plain relative include can break when another tool wraps your PHPStan config from a generated temporary config file, which can make PHPStan behave as if the extension is not installed.

Features

@phpstan-infer-return

Infer the exact static array shape of a function or method from its return expression.

/** @phpstan-return array @phpstan-infer-return */
public function options(): array
{
    return [
        'enabled' => true,
        'limit' => 100,
        'label' => 'default',
    ];
}

PHPStan receives the inferred return type:

array{enabled: bool, limit: int, label: string}

This suppresses PHPStan's generic missing iterable value type error when the shape can be inferred. If inference fails, the extension reports a focused arrayTypeInference.missingType diagnostic.

Read the @phpstan-infer-return docs

ReturnType<callable>

Detect and reuse the return type of a function or method as a PHPDoc type. ReturnType<callable> works with any callable return type PHPStan can resolve — from a native declaration, PHPDoc, another PHPStan extension, or @phpstan-infer-return.

The main benefit is avoiding duplicated array shape annotations. Instead of copying an array{...} PHPDoc in every place that consumes it, point ReturnType at the single method that defines it:

final class PluginConfig
{
    /** @return array{enabled: bool, limit: int, label: string} */
    public function defaults(): array
    {
        return ['enabled' => true, 'limit' => 100, 'label' => 'default'];
    }
}

/**
 * @phpstan-type PluginDefaults \ReturnType<PluginConfig, 'defaults'>
 */
final class PluginService
{
    /**
     * @param array $config
     * @phpstan-param PluginDefaults $config
     */
    public function boot(array $config): void
    {
        // PHPStan knows $config['enabled'] is bool, $config['limit'] is int, etc.
    }
}

PluginDefaults resolves to:

array{enabled: bool, limit: int, label: string}

When the defaults() signature changes, all consumers pick up the updated type automatically — without touching their own annotations.

Combined with @phpstan-infer-return

When a method uses @phpstan-infer-return, its return type becomes a precise static array shape, and ReturnType<callable> can reference it like any other documented type. This is the most common pattern: define the shape once via inference, reuse it everywhere:

/**
 * @phpstan-type Options \ReturnType<self, 'defaults'>
 */
final class Config
{
    /** @phpstan-return array @phpstan-infer-return */
    public function defaults(): array
    {
        return [
            'enabled' => true,
            'limit'   => 100,
        ];
    }

    /**
     * @param array $options
     * @phpstan-param Options $options
     */
    public function apply(array $options): void
    {
        // PHPStan catches type errors on $options['enabled'] and $options['limit']
    }
}

Options resolves to the same inferred type as defaults():

array{enabled: bool, limit: int}

Referencing a function

/**
 * @phpstan-return array @phpstan-infer-return
 */
function defaultHeaders(): array
{
    return [
        'Content-Type' => 'application/json',
        'Accept'       => 'application/json',
    ];
}

/**
 * @phpstan-type Headers \ReturnType<App\Http\defaultHeaders>
 */
final class HttpClient
{
    /**
     * @param array $headers
     * @phpstan-param Headers $headers
     */
    public function withHeaders(array $headers): void {}
}

Supported syntax

  • \ReturnType<functionName>
  • \ReturnType<Fully\Qualified\functionName>
  • \ReturnType<self, 'methodName'>
  • \ReturnType<Fully\Qualified\ClassName, 'methodName'>

Nested inferred calls are supported when the target is statically resolvable.

ReturnType always reuses the callable's literal PHP return type. If a method returns a JSON-schema definition array, \ReturnType<self, 'schema'> resolves to the PHP shape of that schema-definition array. It does not convert the JSON schema into a separate data shape.

PHPStan-powered IDE hovers should show the same resolved types PHPStan sees on the command line when the extension is active. The optional IDE stubs only reduce editor noise in tools like Intelephense or PHPStorm; those tools do not execute PHPStan's custom type resolver by themselves.

Read the ReturnType<callable> docs

License

MIT

amiut/phpstan-type-utilities 适用场景与选型建议

amiut/phpstan-type-utilities 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 amiut/phpstan-type-utilities 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 amiut/phpstan-type-utilities 我们能提供哪些服务?
定制开发 / 二次开发

基于 amiut/phpstan-type-utilities 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 47
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-02