Skip to content

Commit

Permalink
Refactor Expectation::withArgs() method
Browse files Browse the repository at this point in the history
This commit refactor the Expectation::withArgs() method to make the code more readable, understandable and easier to maintain.
To do that, it applies the "extract method" refactoring technique.
  • Loading branch information
beni0888 committed Mar 1, 2016
1 parent 25b4b0c commit b4b978c
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions library/Mockery/Expectation.php
Expand Up @@ -20,6 +20,9 @@

namespace Mockery;

use Closure;
use Mockery\Matcher\MultiArgumentClosure;

class Expectation implements ExpectationInterface
{
/**
Expand Down Expand Up @@ -364,23 +367,49 @@ public function with()
/**
* Expected arguments for the expectation passed as an array
*
* @param array|\Closure $argsOrClosure
* @param array $arguments
* @return self
*/
private function withArgsInArray(array $arguments)
{
if (empty($arguments)) {
return $this->withNoArgs();
}
$this->_expectedArgs = $arguments;
$this->_noArgsExpectation = false;
return $this;
}

/**
* Expected arguments have to be matched by the given closure.
*
* @param Closure $closure
* @return self
*/
private function withArgsMatchedByClosure(Closure $closure)
{
$this->_expectedArgs = [new MultiArgumentClosure($closure)];
$this->_noArgsExpectation = false;
return $this;
}

/**
* Expected arguments for the expectation passed as an array or a closure that matches each passed argument on
* each function call.
*
* @param array|Closure $argsOrClosure
* @return self
*/
public function withArgs($argsOrClosure)
{
if (is_array($argsOrClosure)) {
if (empty($argsOrClosure)) {
return $this->withNoArgs();
}
$this->_expectedArgs = $argsOrClosure;
} elseif ($argsOrClosure instanceof \Closure) {
$this->_expectedArgs = [new \Mockery\Matcher\MultiArgumentClosure($argsOrClosure)];
$this->withArgsInArray($argsOrClosure);
} elseif ($argsOrClosure instanceof Closure) {
$this->withArgsMatchedByClosure($argsOrClosure);
} else {
throw new \InvalidArgumentException(sprintf('Call to %s with an invalid argument (%s), only array and '.
'closure are allowed', __METHOD__, $argsOrClosure));
}
$this->_noArgsExpectation = false;
return $this;
}

Expand Down

0 comments on commit b4b978c

Please sign in to comment.