2015-02-05 15:32 GMT+03:00 Dmitry Stogov <dmi...@zend.com>:

> Hi Alexander,
>
> Defining contracts through doc-comments is also possible, but this way is
> not native.
> On the other hand, if you already have this implemented, we may just reuse
> it.
>


Thanks, Dmitry! This would be a really nice feature on engine-level.
Ideally, for PHP7-8, I would like to see a hook system from compile-level
to userland, to define a language extension. This feature is highly
required for custom DSLs and much more. DbC technique can be also
implemented as parse-time weaver. E.g. we define a parser-extension:

register_parser_extension(ContractExtension::class);

this class will receive an AST of PHP file and check interesting nodes.
This node can be annotation or something else:

[Contract::Invariant => $this->value > 0]
class Test {
    public $value = 100;
    public function foo($newValue) {
        $this->value = $newValue;
    }
}

and parser extension change the compiled source code by inserting this
check into methods:

[Contract::Invariant => $this->value > 0]
class Test {
    public $value = 100;
    public function foo($newValue) {
        $this->value = $newValue;
        assert($this->value > 0); // Inserted by parser extension (via
opcodes)
    }
}

Is it possible to do this?

Reply via email to