定制 hediet/contracts 二次开发

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

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

hediet/contracts

Composer 安装命令:

composer require hediet/contracts

包简介

A library which helps to specify, check and reflect code contracts.

README 文档

README

PHP Contracts is an assertion library for PHP to validate arguments and invariants. This library aims to replace my library nunzion/php-expect.

Warning

This library is still in development! However, the signature of the method Contract::requires will not change and it is guaranteed that an exception is beeing thrown if the condition evaluates to false.

Installation

You can use Composer to download and install PHP Contracts. To add PHP Contracts to your project, simply add a dependency on hediet/contracts to your project's composer.json file.

Here is a minimal example of a composer.json file that just defines a dependency on PHP Contracts:

{
    "require": {
        "hediet/contracts": "dev-master"
    }
}

Usage

Using this library is as simple as calling Contract::requires with an arbitrary condition:

use Hediet\Contract;

function sum($a, $b)
{
    Contract::requires(is_int($a) && is_int($b));
}

If the specified condition evaluates to false, PHP Contracts analyzes the condition and throws an exception with an appropriate error message.

The following code will throw an \InvalidArgumentException with the message Argument 'b' must be of type 'integer', but is of type 'string'.:

sum(1, "test");

Explicitly Supported Tests

Currently PHP Contracts understands the following conditions:

  • All is_TYPE functions for TYPE being a primitive.
  • Comparison operators like <, <=, >, >=.
  • Disjunction of type constraints, e.g. is_int($a) || is_float($a) || $a === null. Internally, a type constraint is being created for $a which denotes that $a must be of type int|float|null.
  • Conjunction of constraints.

However, currently all expressions must be constants or arguments.

Examples

public function intArgumentsProvider() { return array(array(7, 1)); }

/**
 * @dataProvider intArgumentsProvider
 * @expectedException \InvalidArgumentException
 * @expectedExceptionMessage Argument 'a' must be greater than '1' 
 * and less than or equal to argument 'b', but 'a' is 7 and 'b' is 1.
 */
public function testExampleRange($a, $b)
{
    Contract::requires(1 < $a && $a <= $b);
}

/**
 * @dataProvider intArgumentsProvider
 * @expectedException \InvalidArgumentException
 * @expectedExceptionMessage Argument 'a' must be of type 'null|string', but is of type 'integer'.
 */
public function testExampleUnionType($a)
{
    Contract::requires($a === null || is_string($a));
}

/**
 * @dataProvider intArgumentsProvider
 * @expectedException \InvalidArgumentException
 * @expectedExceptionMessage Argument 'a' must be greater than '10', 
 * but is 7 or argument 'a' must be of type 'null', but is of type 'integer'.
 */
public function testExample4($a)
{
    Contract::requires(($a === null) || ($a > 10));
}

Internals

If the condition evaluates to true, the requires method returns immediately. Thus, if a condition does not fail, there is no significant performance decrease.

If a condition fails, i.e. evaluates to false, the stacktrace is used to determine the location of the requires call. After that, the invocation is parsed with nikic/PHP-Parser and converted to a set of constraints. These constraints may reference expressions to which the constraints are applied to. By using the stacktrace a second time, the arguments and the value of $this can be obtained for the context of the invocation to evaluate these expressions, so that a constraint can add an explanation of why he has failed.

Todos

  • Support deep expressions, so that conditions like Contract::requires(count($a) > 0) can be analyzed. Since $a may be obtained from the stacktrace and count is a pure method, count($a) can be evaluated without side effects.
  • Support various array tests.
  • Throw an invariant exception if no argument is referenced in the condition.
  • Use optionally provided values to evaluate expressions which uses variables that are neither arguments nor $this.
  • Add a reflection API which parses the first Contract::requires calls of a method and returns their corresponding constraints. This enables propagating constraints to the UI.

Author

Henning Dieterichs - henning.dieterichs@hediet.de

License

PHP Expect is licensed under the MIT License.

hediet/contracts 适用场景与选型建议

hediet/contracts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 49 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 03 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「exception」 「code」 「validation」 「assert」 「assertion」 「argument」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 hediet/contracts 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 49
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 24
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-03-08