Skip to content

Support for Lazy assertions / assertions chains #169

Open

Description

@Taluu

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.

Activity

  1. BackEndTea commented on Mar 14, 2020

    @BackEndTea
    Collaborator

    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.

  2. arjanfrans commented on Mar 29, 2022

    @arjanfrans

    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.

  3. mindofmicah commented on Oct 4, 2023

    @mindofmicah

    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);
    	}
    }
    
  4. shadowhand commented on Oct 21, 2025

    @shadowhand
    Collaborator

    I think this becomes much easier with PHP 8.5 pipe operator if we implement #280

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions