webmozarts / assert

Assertions to validate method input/output with nice error messages.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for Lazy assertions / assertions chains

Taluu opened this issue · comments

As the assert from beberlei's package propose them, so we can build assertions :

<?php

$assert = Assert::that($value);
$assert->not();
$assert->same('foo');

This is the global idea.

I'd love to see this for this package, but i'm not sure how to go about that without a BC break. If you can create a PR that would allow this functionality, or allow for an easy upgrade path then id like it.

I'd like to see this in this package too. I actually have an implementation for this. I'll see if it can be easily integrated and make a merge request.

Chainability could be introduced without breaking any existing code with a new class that serves as a proxy to Assert's static functions. The value under assertion would be passed through a constructor either directly or through a helper on Assert (Assert::that($variable), Assert::chain($variable)). It would then leverage the magic __call method to pass the arguments onto Assert.

generate.php could call a new script to add @method declarations to this new AssertionChain classes to make sure that intellisense works. This would keep the documentation and the AssertionChain dependent on Assert.php and would not require upkeep since it is generated off of the main class

/**
 * @method void integer(string $message = '')  // Generated by script in bin/
 */
class AssertionChain
{
	public function __construct($value)
	{
		$this->value;
	}
	
	public function getValue() 
	{
		return $this->value;
	}
	
	public function __call($method, array $arguments)
	{
		array_unshift($this->value, $arguments);
		
		Assert::$method($arguments);

                return $this;
	}
}

class Assert 
{
	public static function chain($value)
	{
		return new AssertionChain($value);
	}
}