Is there a reason why you think that Design by Contract (DbC) should be implemented via annotations/attributes?
I personally think that such a system should be implemented directly in
the language, like Eiffel has it. I even think that it would be easy to
add it without any BC. It might be a bit more complicated to implement
thought.
[assert]
assert.active = 1
assert.invariant = 1
assert.require = 1 ; preconditions
assert.ensure = 1 ; postconditions
class PreconditionError extends AssertionError {}
class PostconditionError extends AssertionError {}
function hello(string $who): string {
return "Hello {$who}\n";
}
require {
# argument must have content
$who !== '';
# argument cannot contain unprintable characters
ctype_print($who);
}
hello('');
// PHP Warning: require(): argument must have content: "$who !== ''"
// failed in ...
hello("\0");
// PHP Warning: require(): argument cannot contain unprintable
// characters: "ctype_print($who)" failed in ...
class A {
private DateTimeImmutable $created;
private DateTimeImmutable $changed;
function f() {}
require {}
// Special scope "old" available...
ensure {
# error message
$this->x = old::$x - 42;
}
}
ensure {
# created cannot be less than changed time
$this->created <= $this->changed;
}
--
Richard "Fleshgrinder" Fussenegger
signature.asc
Description: OpenPGP digital signature
