Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extract Superclass and Extract Closure

Closure function

Test.php

Class Test extends AbstractTest {

	public function calculateFee(array $weightArray, int $amount) : int
	{
        	$weights = $this->arrayToCollection($weightArray);

        
        	$amount = $this->loopWeights(function (int $weight) {
            		return (100 + $weight * 10);
        	});

        	return $amount;
	}
}

AbstractTest.php

abstract Class AbstractTest {
    	protected function loopWeights(callable $closure): int
    	{
        	foreach ($weights as $weight) {
            		$amount = $amount + $closure($weight);
        	}	

        	return $amount;
    	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment