定制 vashkatsi/php-duck-typing 二次开发

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

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

vashkatsi/php-duck-typing

Composer 安装命令:

composer require vashkatsi/php-duck-typing

包简介

A PHP library for asserting duck typing.

README 文档

README

A PHP library for asserting duck typing and structural type compliance.

Introduction

DuckType allows you to check whether an object conforms to a specific structure (methods and properties) defined by an interface or class, without requiring the object to explicitly implement that interface or extend that class. This is useful in scenarios where you want to ensure that an object can be used in a particular context based on its available methods and properties, embracing the concept of duck typing:

"If it looks like a duck and quacks like a duck, it's a duck."

Installation

Install the package via Composer:

composer require vashkatsi/ducktype

Usage

Basic Example

<?php
require 'vendor/autoload.php';

use function DuckType\assertDuckType;
use DuckType\Exceptions\DuckTypeException;

interface Renderable
{
    public function render(): string;
}

class PDFDocument
{
    public function render(): string
    {
        return "PDF content";
    }
}

$doc = new PDFDocument();

try {
    assertDuckType($doc, Renderable::class);
    echo "The object conforms to Renderable.";
} catch (DuckTypeException $e) {
    echo "Duck typing assertion failed:\n";
    foreach ($e->getErrors() as $error) {
        echo "- $error\n";
    }
}

Handling Exceptions

try {
    // Assume $invalidDoc is an object that doesn't conform
    assertDuckType($invalidDoc, Renderable::class);
} catch (DuckTypeException $e) {
    echo "Duck typing assertion failed:\n";
    foreach ($e->getErrors() as $error) {
        echo "- $error\n";
    }
}

Checking Against Classes

class Template
{
    public string $content;

    public function render(): string
    {
        return $this->content;
    }
}

class Page
{
    public string $content;

    public function render(): string
    {
        return $this->content;
    }
}

$page = new Page();

assertDuckType($page, Template::class); // Returns true

Advanced Example with Union Types

interface Processor
{
    public function process(int|string $data): bool;
}

class DataProcessor
{
    public function process(int|string $data): bool
    {
        // Processing logic
        return true;
    }
}

$processor = new DataProcessor();

assertDuckType($processor, Processor::class); // Returns true

Running Tests

To run the test suite, first install development dependencies:

composer install

Then run PHPUnit:

composer test

Or directly:

vendor/bin/phpunit tests

To generate code coverage reports:

vendor/bin/phpunit --coverage-html coverage

Reports will be available in the coverage directory.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

This project is licensed under the BSD-3-Clause License - see the LICENSE file for details.

Acknowledgments

  • Inspired by the concept of duck typing in dynamic languages like Python and Go.
  • Thanks to all contributors and users who have helped improve this library.

Contact

Feel free to reach out if you have any questions or need assistance using the library.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2024-10-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固