This is a proposal for providing an alternative to PHP's ''assert()'' that allows for an simple and reliable way to check preconditions and postconditions in MediaWiki code.
The background of this proposal is the reoccurring discussions about whether PHP's ''assert()'' can and should be used in MediaWiki code. Two relevant threads:
The outcome appears to be that
- assertions are generally a good way to improve code quality
- but PHP's ''assert()'' is broken by design
Following a suggestion by Tim Starling, I propose to create our own functions for assertions. A first implementation is available at https://github.com/wmde/Assert and can be installed using composer.
Usage
function frob( $foo ) { Assert::parameterType( 'string', $foo, 'foo' ); Assert::parameter( $foo !== '', 'foo', 'must not be empty' ); //... Assert::postcondition( is_string( $result ), 'string expected' ); return $result; }
This originated as https://www.mediawiki.org/wiki/Requests_for_comment/Assert. Code is at https://github.com/wmde/Assert