定制 roave/you-are-using-it-wrong 二次开发

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

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

roave/you-are-using-it-wrong

最新稳定版本:1.15.0

Composer 安装命令:

composer require roave/you-are-using-it-wrong

包简介

Composer plugin enforcing strict type checks in downstream package dependants

README 文档

README

Archived: no longer necessary

This project has been initially introduced to educate the PHP community about type checkers, their importance, and also to bother those that don't rely on them.

This project won't be updated to support vimeo/psalm:^6, since it is both technically challenging, and no longer necessary, as type-checkers are now unequivocally part of the minimum toolkit of every professional PHP developer.

Since the first release of this package in 2019, the community has grown, and we are proud of the work we've done to advocate for type-checkers.

We've been noisy, annoying, cheeky about it: it has actually helped a lot!

To everyone that now pushes for better coding practices through vimeo/psalm and phpstan/phpstan: thank you ♥️.

Overview

Mutation testing badge Type Coverage Packagist

This package enforces type checks during composer installation in downstream consumers of your package. This only applies to usages of classes, properties, methods and functions declared within packages that directly depend on roave/you-are-using-it-wrong.

Issues that the static analyser finds that do not relate to these namespaces will not be reported.

roave/you-are-using-it-wrong comes with a zero-configuration out-of-the-box setup.

By default, it hooks into composer install and composer update, preventing a successful command execution if there are type errors in usages of protected namespaces.

The usage of this plugin is highly endorsed for authors of new PHP libraries who appreciate the advantages of static types.

This project is built with the hope that libraries with larger user-bases will raise awareness of type safety (or current lack thereof) in the PHP ecosystem.

As annoying as it might sound, it is not uncommon for library maintainers to respond to support questions caused by lack of type checks in downstream projects. In addition to that, relying more on static types over runtime checks, it is possible to reduce code size and maintenance burden by strengthening the API boundaries of a library.

Installation

This package is designed to be installed as a dependency of PHP libraries.

In your library, add it to your composer.json:

composer require roave/you-are-using-it-wrong

No further changes are needed for this tool to start operating as per its design, if your declared types are already reflecting your library requirements.

Please also note that this should not be used in "require-dev", but specifically in "require" in order for the type checks to be applied to downstream consumers of your code.

Examples

You can experiment with the following example by running cd examples && ./run-example.sh.

Given you are the author of my/awesome-library, which has following composer.json:

{
    "name": "my/awesome-library",
    "type": "library",
    "autoload": {
        "psr-4": {
            "My\\AwesomeLibrary\\": "src"
        }
    },
    "require": {
        "roave/you-are-using-it-wrong": "^1.0.0"
    }
}

Given following my/awesome-library/src/MyHelloWorld.php:

<?php declare(strict_types=1);

namespace My\AwesomeLibrary;

final class MyHelloWorld
{
    /** @param array<string> $people */
    public static function sayHello(array $people) : string
    {
        return 'Hello ' . implode(', ', $people) . '!';
    }
}

Given following downstream a/project/composer.json project that depends on your my/awesome-library:

{
    "name": "a/project",
    "type": "project",
    "autoload": {
        "psr-4": {
            "The\\Project\\": "src"
        }
    },
    "require": {
        "my/awesome-library": "^1.0.0"
    }
}

And following a/project/src/MyExample.php:

<?php declare(strict_types=1);

// notice the simple type error
echo \My\AwesomeLibrary\MyHelloWorld::sayHello([123, 456]);

Then composer install in said project will fail:

$ cd a/project
$ composer install

Loading composer repositories with package information
Updating dependencies (including require-dev)
  ... <snip>
  - Installing roave/you-are-using-it-wrong (1.0.0): ...
  - Installing my/awesome-library (1.0.0): ...
  ... <snip>

roave/you-are-using-it-wrong: checking strictly type-checked packages...
Scanning files...
Analyzing files...

ERROR: InvalidScalarArgument - a-project/src/MyExample.php:4:48 
  - Argument 1 of My\AwesomeLibrary\MyHelloWorld::sayhello expects array<array-key, string>,
    array{0:int(123), 1:int(456)} provided
echo \My\AwesomeLibrary\MyHelloWorld::sayHello([123, 456]);

$ echo $?
1

Workarounds

This package is designed to be quite invasive from a type-check perspective, but it will bail out of any checks if a psalm configuration is detected in the root of the installation/project. If that is the case, the tool assumes that the author of the project is already responsible for ensuring type-safety within their own domain, and therefore bails out without performing further checks.

As mentioned above, the design of the tool circles around raising awareness of static type usage in the PHP ecosystem, and therefore it will only give up if it is sure that library consumers are already taking care of the matter on their own.

Professional Support

If you need help with setting up this library in your project, you can contact us at team@roave.com for consulting/support.

roave/you-are-using-it-wrong 适用场景与选型建议

roave/you-are-using-it-wrong 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 62.94k 次下载、GitHub Stars 达 240, 最近一次更新时间为 2019 年 05 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 roave/you-are-using-it-wrong 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 62.94k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 240
  • 点击次数: 11
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 240
  • Watchers: 8
  • Forks: 10
  • 开发语言: PHP

其他信息

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