Giter Club home page Giter Club logo

koriym.nullobject's Introduction

PHP NullObject

codecov Type Coverage Continuous Integration

Generates a NullObject from an interface. It was created for testing and AOP.

Installation

composer require --dev koriym/null-object

Getting Started

Instantiate a Null Object from an Interface.

interface FooInterface
{
   public function do(): void;
}
$nullObject = $this->nullObject->newInstance(FooInterface::class);
assert($nullObject instanceof FooInterface);
$nullObject->do(); // nothing's going to happen

newInstance() defines a class with eval, but use save() to save the generated code to a file.

$class = $this->nullObject->save(FooInterface::class, '/path/to/saveDirectory');
assert(new $class instanceof FooInterface);

On the fly

It is also possible to create a null object by simply adding a Null suffix to the interface by registering autoloader, If this doesn't sound too wild to you.

$loader = require __DIR__ . '/vendor/koriym/null-object/autoload.php';
spl_autoload_register($loader);

or add it to autoload-dev in composer.json.

    "autoload-dev": {
        "files": ["./vendor/koriym/null-object/autoload.php"]
    }

You can create Null Object as follows.

$nullClass = FooInterface::class . 'Null'; // add Null suffix to the interface
$foo = new $nullClass;  // instantiate a NullObject
assert($foo instanceof FooInterface);

Note

Inherited interfaces are not yet supported.

koriym.nullobject's People

Contributors

koriym avatar naokitsuchiya avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

naokitsuchiya

koriym.nullobject's Issues

Attribute named paramter's name is not respected

How to reproduce

original

#[DbPager(type:'type1', id:'id1' )]

compiled null class

#[DbPager('type1', 'id1')]

Name(key) are gone.
This cause unexpected result with attribute with constructor as follows.

class DbPager
{
    /** @var string */
    public $id;
    /** @var string */
    public $type;

    public function __construct(string $id, string $type)
    {
        $this->id = $id;
        $this->type = $type;
    }
}

コンパイルすると名前付きパラメーターの名前が失われています。

Call to undefined method ReflectionMethod::getAttributes() Koriym.NullObject/src/NullObject.php:88

ReflectionMethod::getAttributes() is available starting with 8.0 PHP version.

Please run demo/run.php of the following repo with 7.4, 7.3.
https://github.com/NaokiTsuchiya/Koriym.NullObject/tree/getAttributes

Message is here.

PHP Fatal error:  Uncaught Error: Call to undefined method ReflectionMethod::getAttributes() in /Users/tsuchiya/work/tsuchiya/Koriym.NullObject/src/NullObject.php:88
Stack trace:
#0 /Users/tsuchiya/work/tsuchiya/Koriym.NullObject/src/NullObject.php(75): Koriym\NullObject\NullObject->getMethodMeta(Object(ReflectionMethod))
#1 /Users/tsuchiya/work/tsuchiya/Koriym.NullObject/src/NullObject.php(57): Koriym\NullObject\NullObject->getMethods('Demo\\FooInterfa...')
#2 /Users/tsuchiya/work/tsuchiya/Koriym.NullObject/autoloader.php(28): Koriym\NullObject\NullObject->__invoke('Demo\\FooInterfa...')
#3 [internal function]: {closure}('Demo\\FooInterfa...')
#4 /Users/tsuchiya/work/tsuchiya/Koriym.NullObject/demo/run.php(15): spl_autoload_call('Demo\\FooInterfa...')
#5 {main}
  thrown in /Users/tsuchiya/work/tsuchiya/Koriym.NullObject/src/NullObject.php on line 88

Fatal error: Uncaught Error: Call to undefined method ReflectionMethod::getAttributes() in /Users/tsuchiya/work/tsuchiya/Koriym.NullObject/src/NullObject.php:88
Stack trace:
#0 /Users/tsuchiya/work/tsuchiya/Koriym.NullObject/src/NullObject.php(75): Koriym\NullObject\NullObject->getMethodMeta(Object(ReflectionMethod))
#1 /Users/tsuchiya/work/tsuchiya/Koriym.NullObject/src/NullObject.php(57): Koriym\NullObject\NullObject->getMethods('Demo\\FooInterfa...')
#2 /Users/tsuchiya/work/tsuchiya/Koriym.NullObject/autoloader.php(28): Koriym\NullObject\NullObject->__invoke('Demo\\FooInterfa...')
#3 [internal function]: {closure}('Demo\\FooInterfa...')
#4 /Users/tsuchiya/work/tsuchiya/Koriym.NullObject/demo/run.php(15): spl_autoload_call('Demo\\FooInterfa...')
#5 {main}
  thrown in /Users/tsuchiya/work/tsuchiya/Koriym.NullObject/src/NullObject.php on line 88

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.