Skip to content

Commit

Permalink
Merge pull request #703 from MarcusSchwarz/variadic-parameters
Browse files Browse the repository at this point in the history
replacing func_get_args with variadic parameters for better IDE-support
  • Loading branch information
davedevelopment committed Feb 28, 2017
2 parents ebad32e + 7a531d1 commit ea44b82
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 73 deletions.
46 changes: 28 additions & 18 deletions library/Mockery.php
Expand Up @@ -72,47 +72,49 @@ public static function globalHelpers()
/**
* Static shortcut to \Mockery\Container::mock().
*
* @param array $args
*
* @return \Mockery\MockInterface
*/
public static function mock()
public static function mock(...$args)
{
$args = func_get_args();

return call_user_func_array(array(self::getContainer(), 'mock'), $args);
}

/**
* Static and semantic shortcut for getting a mock from the container
* and applying the spy's expected behavior into it.
*
* @param array $args
*
* @return \Mockery\MockInterface
*/
public static function spy()
public static function spy(...$args)
{
$args = func_get_args();
return call_user_func_array(array(self::getContainer(), 'mock'), $args)->shouldIgnoreMissing();
}

/**
* Static and Semantic shortcut to \Mockery\Container::mock().
*
* @param array $args
*
* @return \Mockery\MockInterface
*/
public static function instanceMock()
public static function instanceMock(...$args)
{
$args = func_get_args();

return call_user_func_array(array(self::getContainer(), 'mock'), $args);
}

/**
* Static shortcut to \Mockery\Container::mock(), first argument names the mock.
*
* @param array $args
*
* @return \Mockery\MockInterface
*/
public static function namedMock()
public static function namedMock(...$args)
{
$args = func_get_args();
$name = array_shift($args);

$builder = new MockConfigurationBuilder();
Expand Down Expand Up @@ -306,11 +308,13 @@ public static function type($expected)
/**
* Return instance of DUCKTYPE matcher.
*
* @param array $args
*
* @return \Mockery\Matcher\Ducktype
*/
public static function ducktype()
public static function ducktype(...$args)
{
return new \Mockery\Matcher\Ducktype(func_get_args());
return new \Mockery\Matcher\Ducktype($args);
}

/**
Expand All @@ -329,11 +333,13 @@ public static function subset(array $part, $strict = true)
/**
* Return instance of CONTAINS matcher.
*
* @param array $args
*
* @return \Mockery\Matcher\Contains
*/
public static function contains()
public static function contains(...$args)
{
return new \Mockery\Matcher\Contains(func_get_args());
return new \Mockery\Matcher\Contains($args);
}

/**
Expand Down Expand Up @@ -399,21 +405,25 @@ public static function not($expected)
/**
* Return instance of ANYOF matcher.
*
* @param array $args
*
* @return \Mockery\Matcher\AnyOf
*/
public static function anyOf()
public static function anyOf(...$args)
{
return new \Mockery\Matcher\AnyOf(func_get_args());
return new \Mockery\Matcher\AnyOf($args);
}

/**
* Return instance of NOTANYOF matcher.
*
* @param array $args
*
* @return \Mockery\Matcher\NotAnyOf
*/
public static function notAnyOf()
public static function notAnyOf(...$args)
{
return new \Mockery\Matcher\NotAnyOf(func_get_args());
return new \Mockery\Matcher\NotAnyOf($args);
}

/**
Expand Down
11 changes: 5 additions & 6 deletions library/Mockery/CompositeExpectation.php
Expand Up @@ -43,9 +43,9 @@ public function add($expectation)
/**
* @param mixed ...
*/
public function andReturn()
public function andReturn(...$args)
{
return $this->__call(__FUNCTION__, func_get_args());
return $this->__call(__FUNCTION__, $args);
}

/**
Expand All @@ -54,9 +54,9 @@ public function andReturn()
* @param mixed ...
* @return self
*/
public function andReturns()
public function andReturns(...$args)
{
return call_user_func_array([$this, 'andReturn'], func_get_args());
return call_user_func_array([$this, 'andReturn'], $args);
}

/**
Expand Down Expand Up @@ -115,9 +115,8 @@ public function mock()
* @param mixed ...
* @return \Mockery\Expectation
*/
public function shouldReceive()
public function shouldReceive(...$args)
{
$args = func_get_args();
reset($this->_expectations);
$first = current($this->_expectations);
return call_user_func_array(array($first->getMock(), 'shouldReceive'), $args);
Expand Down
8 changes: 4 additions & 4 deletions library/Mockery/Container.php
Expand Up @@ -85,17 +85,17 @@ public function __construct(Generator $generator = null, LoaderInterface $loader
* names or partials - just so long as it's something that can be mocked.
* I'll refactor it one day so it's easier to follow.
*
* @param array $args
*
* @return Mock
* @throws Exception\RuntimeException
* @throws Exception
* @return \Mockery\Mock
*/
public function mock()
public function mock(...$args)
{
$expectationClosure = null;
$quickdefs = array();
$constructorArgs = null;
$blocks = array();
$args = func_get_args();

if (count($args) > 1) {
$finalArg = end($args);
Expand Down
32 changes: 15 additions & 17 deletions library/Mockery/Expectation.php
Expand Up @@ -374,12 +374,12 @@ protected function _matchArg($expected, &$actual)
/**
* Expected argument setter for the expectation
*
* @param mixed ...
* @param mixed[] ...
* @return self
*/
public function with()
public function with(...$args)
{
return $this->withArgs(func_get_args());
return $this->withArgs($args);
}

/**
Expand Down Expand Up @@ -454,24 +454,24 @@ public function withAnyArgs()
/**
* Set a return value, or sequential queue of return values
*
* @param mixed ...
* @param mixed[] ...
* @return self
*/
public function andReturn()
public function andReturn(...$args)
{
$this->_returnQueue = func_get_args();
$this->_returnQueue = $args;
return $this;
}

/**
* Set a return value, or sequential queue of return values
*
* @param mixed ...
* @param mixed[] ...
* @return self
*/
public function andReturns()
public function andReturns(...$args)
{
return call_user_func_array([$this, 'andReturn'], func_get_args());
return call_user_func_array([$this, 'andReturn'], $args);
}

/**
Expand Down Expand Up @@ -501,12 +501,12 @@ public function andReturnValues(array $values)
* values. The arguments passed to the expected method are passed to the
* closures as parameters.
*
* @param callable ...
* @param callable[] $args
* @return self
*/
public function andReturnUsing()
public function andReturnUsing(...$args)
{
$this->_closureQueue = func_get_args();
$this->_closureQueue = $args;
return $this;
}

Expand Down Expand Up @@ -582,13 +582,11 @@ public function andThrowExceptions(array $exceptions)
* Register values to be set to a public property each time this expectation occurs
*
* @param string $name
* @param mixed $value
* @param array $values
* @return self
*/
public function andSet($name, $value)
public function andSet($name, ...$values)
{
$values = func_get_args();
array_shift($values);
$this->_setQueue[$name] = $values;
return $this;
}
Expand Down Expand Up @@ -620,7 +618,7 @@ public function zeroOrMoreTimes()
* Indicates the number of times this expectation should occur
*
* @param int $limit
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
* @return self
*/
public function times($limit = null)
Expand Down
24 changes: 12 additions & 12 deletions library/Mockery/Mock.php
Expand Up @@ -133,7 +133,7 @@ class Mock implements MockInterface
/**
* Just a local cache for this mock's target's methods
*
* @var ReflectionMethod[]
* @var \ReflectionMethod[]
*/
protected static $_mockery_methods;

Expand Down Expand Up @@ -177,16 +177,17 @@ public function mockery_init(\Mockery\Container $container = null, $partialObjec
/**
* Set expected method calls
*
* @param null|string $methodName,... one or many methods that are expected to be called in this mock
* @param array $methodNames,... one or many methods that are expected to be called in this mock
*
* @return \Mockery\ExpectationInterface|\Mockery\HigherOrderMessage
*/
public function shouldReceive($methodName = null)
public function shouldReceive(...$methodNames)
{
if ($methodName === null) {
if (count($methodNames) === 0) {
return new HigherOrderMessage($this, "shouldReceive");
}

foreach (func_get_args() as $method) {
foreach ($methodNames as $method) {
if ("" == $method) {
throw new \InvalidArgumentException("Received empty method name");
}
Expand All @@ -199,7 +200,7 @@ public function shouldReceive($methodName = null)
$allowMockingProtectedMethods = $this->_mockery_allowMockingProtectedMethods;

$lastExpectation = \Mockery::parseShouldReturnArgs(
$this, func_get_args(), function ($method) use ($self, $nonPublicMethods, $allowMockingProtectedMethods) {
$this, $methodNames, function ($method) use ($self, $nonPublicMethods, $allowMockingProtectedMethods) {
$rm = $self->mockery_getMethod($method);
if ($rm) {
if ($rm->isPrivate()) {
Expand Down Expand Up @@ -250,21 +251,20 @@ public function expects()
return new ExpectsHigherOrderMessage($this);
}
// end method expects



/**
* Shortcut method for setting an expectation that a method should not be called.
*
* @param null|string $methodName,... one or many methods that are expected not to be called in this mock
* @param array $methodNames one or many methods that are expected not to be called in this mock
* @return \Mockery\Expectation|\Mockery\HigherOrderMessage
*/
public function shouldNotReceive($methodName = null)
public function shouldNotReceive(...$methodNames)
{
if ($methodName === null) {
if (count($methodNames) === 0) {
return new HigherOrderMessage($this, "shouldNotReceive");
}

$expectation = call_user_func_array(array($this, 'shouldReceive'), func_get_args());
$expectation = call_user_func_array(array($this, 'shouldReceive'), $methodNames);
$expectation->never();
return $expectation;
}
Expand Down
13 changes: 7 additions & 6 deletions library/Mockery/MockInterface.php
Expand Up @@ -37,18 +37,19 @@ public function mockery_init(\Mockery\Container $container = null, $partialObjec
/**
* Set expected method calls
*
* @param null|string $methodName,... one or many methods that are expected to be called in this mock
* @return mixed
* @param array ...$methodNames one or many methods that are expected to be called in this mock
*
* @return \Mockery\ExpectationInterface|\Mockery\HigherOrderMessage
*/
public function shouldReceive($methodName = null);
public function shouldReceive(...$methodNames);

/**
* Shortcut method for setting an expectation that a method should not be called.
*
* @param null|string $methodName,... one or many methods that are expected not to be called in this mock
* @return mixed
* @param array $methodNames one or many methods that are expected not to be called in this mock
* @return \Mockery\Expectation|\Mockery\HigherOrderMessage
*/
public function shouldNotReceive($methodName);
public function shouldNotReceive(...$methodNames);

/**
* Allows additional methods to be mocked that do not explicitly exist on mocked class
Expand Down
4 changes: 2 additions & 2 deletions library/Mockery/VerificationDirector.php
Expand Up @@ -36,9 +36,9 @@ public function verify()
return $this->receivedMethodCalls->verify($this->expectation);
}

public function with()
public function with(...$args)
{
return $this->cloneApplyAndVerify("with", func_get_args());
return $this->cloneApplyAndVerify("with", $args);
}

public function withArgs($args)
Expand Down
12 changes: 6 additions & 6 deletions library/helpers.php
Expand Up @@ -22,20 +22,20 @@
*/

if (!function_exists("mock")) {
function mock() {
return call_user_func_array([Mockery::class, "mock"], func_get_args());
function mock(...$args) {
return call_user_func_array([Mockery::class, "mock"], $args);
}
}

if (!function_exists("spy")) {
function spy() {
return call_user_func_array([Mockery::class, "spy"], func_get_args());
function spy(...$args) {
return call_user_func_array([Mockery::class, "spy"], $args);
}
}

if (!function_exists("namedMock")) {
function namedMock() {
return call_user_func_array([Mockery::class, "namedMock"], func_get_args());
function namedMock(...$args) {
return call_user_func_array([Mockery::class, "namedMock"], $args);
}
}

Expand Down

0 comments on commit ea44b82

Please sign in to comment.