Skip to content

Instantly share code, notes, and snippets.

@Maras0830
Last active January 23, 2017 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Maras0830/69e05bfceba3487897681519bb063c28 to your computer and use it in GitHub Desktop.
Save Maras0830/69e05bfceba3487897681519bb063c28 to your computer and use it in GitHub Desktop.
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