承接 jasny/reflection-factory 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

jasny/reflection-factory

Composer 安装命令:

composer require jasny/reflection-factory

包简介

Abstract factory for PHP Reflection

README 文档

README

PHP Scrutinizer Code Quality Code Coverage Packagist Stable Version Packagist License

Factory to use in dependency injection when using PHP Reflection.

Why use a factory instead or just doing new ReflectionClass()?

Dependency injection might seem like making things needlessly complex. However it's a key component in building maintainable (and testable) code.

Using new within your classes creates a strong coupling between classes. This makes it more difficult when writing unit tests because there is no opertunity to mock the reflection. In practice this means that a class using ReflectionClass can only be tested with real, existing classes.

Using ReflectionFactory with dependency injection, allows you to inject a mock of the factory instead. This in term allows you to create mock Reflection objects, for non-existing classes, functions, properties, etc.

Installation

composer require jasny/reflection-factory

Usage

use Jasny\ReflectionFactory\ReflectionFactory;

$factory = new ReflectionFactory();
$reflection = $factory->reflectClass(\DateTime::class);

Example use case

Without dependency injection

use Jasny\ReflectionFactory\ReflectionFactory;

class SomeTool
{
    public function foo(string $class)
    {
        $reflection = new ReflectionClass($class);
        
        return $reflection->getConstant('FOO');
    }
}

But writing the test is hard, as it doesn't allow mocking. Instead we need to create a SomeToolTestFooSupport class just to test this feature.

class SomeToolTestFooSupport
{
    const FOO = 10;
}

In the unit test we do

use PHPUnit\Framework\TestCase;

class SomeToolTest extends TestCase
{
    public function testFoo()
    {
        $tool = new SomeTool();
        
        $this->assertEquals(10, $tool->foo("SomeToolTestFooSupport"));
    }
}

Adding one test class isn't so bad. But consider we need to add one per test, it quickly becomes a mess.

With dependency injection

Dependency injection adds a little overhead to the class as we need to pass the reflection factory to SomeTool.

use Jasny\ReflectionFactory\ReflectionFactoryInterface;

class SomeTool
{
    protected $reflectionFactory;
    
    public function __construct(ReflectionFactoryInterface $reflectionFactory)
    {
        $this->reflectionFactory = $reflectionFactory;    
    }
    
    public function foo(string $class)
    {
        return $this->reflectionFactory->reflectClass($class)->getConstant('FOO');
    }
}

In the unit test, we mock the ReflectionClass and ReflectionFactory. The tests class FakeClass doesn't need to exist.

use PHPUnit\Framework\TestCase;
use Jasny\ReflectionFactory\ReflectionFactoryInterface;

class SomeToolTest extends TestCase
{
    public function testFoo()
    {
        $mockReflection = $this->createMock(\ReflectionClass::class);
        $mockReflection->expects($this->once())->method('getConstant')
            ->with('FOO')->willReturn(10);
            
        $mockFactory = $this->createMock(ReflectionFactoryInterface::class);
        $mockFactory->expects($this->once())->method('reflectClass')
            ->with('FakeClass')->willReturn($mockReflection);
            
        $tool = new SomeTool($mockFactory);
        
        $this->assertEquals(10, $tool->foo("FakeClass"));
    }
}

Methods

Method Reflection class
reflectClass ReflectionClass
reflectClassConstant ReflectionClassConstant
reflectZendExtension ReflectionZendExtension
reflectExtension ReflectionExtension
reflectFunction ReflectionFunction
reflectMethod ReflectionMethod
reflectObject ReflectionObject
reflectParameter ReflectionParameter
reflectProperty ReflectionProperty
reflectGenerator ReflectionGenerator

Some PHP functions have been wrapped, so they can be mocked

Method Function
functionExists function_exists
classExists class_exists
methodExists method_exists
propertyExists property_exists
extensionLoaded extension_loaded
isA is_a
isCallable is_callable

jasny/reflection-factory 适用场景与选型建议

jasny/reflection-factory 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.53k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 08 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 jasny/reflection-factory 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 5.53k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 7
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-08-16