Skip to content

Commit

Permalink
Refactor `library/Mockery/CompositeExpectation.php (#1363)
Browse files Browse the repository at this point in the history
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
  • Loading branch information
ghostwriter committed Feb 29, 2024
2 parents 1650707 + 13c780b commit 574a5e0
Showing 1 changed file with 59 additions and 52 deletions.
111 changes: 59 additions & 52 deletions library/Mockery/CompositeExpectation.php
Expand Up @@ -5,24 +5,61 @@
*
* @copyright https://github.com/mockery/mockery/blob/HEAD/COPYRIGHT.md
* @license https://github.com/mockery/mockery/blob/HEAD/LICENSE BSD 3-Clause License
*
* @link https://github.com/mockery/mockery for the canonical source repository
*/

namespace Mockery;

use function array_map;
use function current;
use function implode;
use function reset;

class CompositeExpectation implements ExpectationInterface
{
/**
* Stores an array of all expectations for this composite
*
* @var array
* @var array<ExpectationInterface>
*/
protected $_expectations = [];

/**
* Intercept any expectation calls and direct against all expectations
*
* @param string $method
*
* @return self
*/
public function __call($method, array $args)
{
foreach ($this->_expectations as $expectation) {
$expectation->{$method}(...$args);
}

return $this;
}

/**
* Return the string summary of this composite expectation
*
* @return string
*/
protected $_expectations = array();
public function __toString()
{
$parts = array_map(static function (ExpectationInterface $expectation): string {
return (string) $expectation;

Check failure on line 52 in library/Mockery/CompositeExpectation.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidCast

library/Mockery/CompositeExpectation.php:52:29: InvalidCast: Mockery\ExpectationInterface cannot be cast to string (see https://psalm.dev/103)
}, $this->_expectations);

return '[' . implode(', ', $parts) . ']';
}

/**
* Add an expectation to the composite
*
* @param \Mockery\Expectation|\Mockery\CompositeExpectation $expectation
* @param ExpectationInterface|HigherOrderMessage $expectation
*
* @return void
*/
public function add($expectation)
Expand All @@ -42,26 +79,24 @@ public function andReturn(...$args)
* Set a return value, or sequential queue of return values
*
* @param mixed ...$args
*
* @return self

Check failure on line 83 in library/Mockery/CompositeExpectation.php

View workflow job for this annotation

GitHub Actions / Psalm

MoreSpecificReturnType

library/Mockery/CompositeExpectation.php:83:16: MoreSpecificReturnType: The declared return type 'Mockery\CompositeExpectation' for Mockery\CompositeExpectation::andReturns is more specific than the inferred return type 'Mockery\ExpectationInterface' (see https://psalm.dev/070)
*/
public function andReturns(...$args)
{
return call_user_func_array([$this, 'andReturn'], $args);
return $this->andReturn(...$args);

Check failure on line 87 in library/Mockery/CompositeExpectation.php

View workflow job for this annotation

GitHub Actions / Psalm

LessSpecificReturnStatement

library/Mockery/CompositeExpectation.php:87:16: LessSpecificReturnStatement: The type 'Mockery\ExpectationInterface' is more general than the declared return type 'Mockery\CompositeExpectation' for Mockery\CompositeExpectation::andReturns (see https://psalm.dev/129)
}

/**
* Intercept any expectation calls and direct against all expectations
* Return the parent mock of the first expectation
*
* @param string $method
* @param array $args
* @return self
* @return LegacyMockInterface|MockInterface
*/
public function __call($method, array $args)
public function getMock()
{
foreach ($this->_expectations as $expectation) {
call_user_func_array(array($expectation, $method), $args);
}
return $this;
reset($this->_expectations);
$first = current($this->_expectations);
return $first->getMock();
}

/**
Expand All @@ -76,69 +111,41 @@ public function getOrderNumber()
return $first->getOrderNumber();
}

/**
* Return the parent mock of the first expectation
*
* @return \Mockery\MockInterface|\Mockery\LegacyMockInterface
*/
public function getMock()
{
reset($this->_expectations);
$first = current($this->_expectations);
return $first->getMock();
}

/**
* Mockery API alias to getMock
*
* @return \Mockery\LegacyMockInterface|\Mockery\MockInterface
* @return LegacyMockInterface|MockInterface
*/
public function mock()
{
return $this->getMock();
}

/**
* Starts a new expectation addition on the first mock which is the primary
* target outside of a demeter chain
* Starts a new expectation addition on the first mock which is the primary target outside of a demeter chain
*
* @param mixed ...$args
* @return \Mockery\Expectation
*
* @return Expectation

Check failure on line 129 in library/Mockery/CompositeExpectation.php

View workflow job for this annotation

GitHub Actions / Psalm

MoreSpecificReturnType

library/Mockery/CompositeExpectation.php:129:16: MoreSpecificReturnType: The declared return type 'Mockery\Expectation' for Mockery\CompositeExpectation::shouldNotReceive is more specific than the inferred return type 'Mockery\ExpectationInterface|Mockery\HigherOrderMessage' (see https://psalm.dev/070)
*/
public function shouldReceive(...$args)
public function shouldNotReceive(...$args)
{
reset($this->_expectations);
$first = current($this->_expectations);
return call_user_func_array(array($first->getMock(), 'shouldReceive'), $args);
return $first->getMock()->shouldNotReceive(...$args);

Check failure on line 135 in library/Mockery/CompositeExpectation.php

View workflow job for this annotation

GitHub Actions / Psalm

LessSpecificReturnStatement

library/Mockery/CompositeExpectation.php:135:16: LessSpecificReturnStatement: The type 'Mockery\ExpectationInterface|Mockery\HigherOrderMessage' is more general than the declared return type 'Mockery\Expectation' for Mockery\CompositeExpectation::shouldNotReceive (see https://psalm.dev/129)

Check failure on line 135 in library/Mockery/CompositeExpectation.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedArgument

library/Mockery/CompositeExpectation.php:135:55: MixedArgument: Argument 1 of Mockery\LegacyMockInterface::shouldNotReceive cannot be mixed, expecting array<array-key, mixed>|string (see https://psalm.dev/030)
}

/**
* Starts a new expectation addition on the first mock which is the primary
* target outside of a demeter chain
* Starts a new expectation addition on the first mock which is the primary target, outside of a demeter chain
*
* @param mixed ...$args
* @return \Mockery\Expectation
*
* @return Expectation

Check failure on line 143 in library/Mockery/CompositeExpectation.php

View workflow job for this annotation

GitHub Actions / Psalm

MoreSpecificReturnType

library/Mockery/CompositeExpectation.php:143:16: MoreSpecificReturnType: The declared return type 'Mockery\Expectation' for Mockery\CompositeExpectation::shouldReceive is more specific than the inferred return type 'Mockery\ExpectationInterface|Mockery\HigherOrderMessage' (see https://psalm.dev/070)
*/
public function shouldNotReceive(...$args)
public function shouldReceive(...$args)
{
reset($this->_expectations);
$first = current($this->_expectations);
return call_user_func_array(array($first->getMock(), 'shouldNotReceive'), $args);
}

/**
* Return the string summary of this composite expectation
*
* @return string
*/
public function __toString()
{
$return = '[';
$parts = array();
foreach ($this->_expectations as $exp) {
$parts[] = (string) $exp;
}
$return .= implode(', ', $parts) . ']';
return $return;
return $first->getMock()->shouldReceive(...$args);

Check failure on line 149 in library/Mockery/CompositeExpectation.php

View workflow job for this annotation

GitHub Actions / Psalm

LessSpecificReturnStatement

library/Mockery/CompositeExpectation.php:149:16: LessSpecificReturnStatement: The type 'Mockery\ExpectationInterface|Mockery\HigherOrderMessage' is more general than the declared return type 'Mockery\Expectation' for Mockery\CompositeExpectation::shouldReceive (see https://psalm.dev/129)

Check failure on line 149 in library/Mockery/CompositeExpectation.php

View workflow job for this annotation

GitHub Actions / Psalm

MixedArgument

library/Mockery/CompositeExpectation.php:149:52: MixedArgument: Argument 1 of Mockery\LegacyMockInterface::shouldReceive cannot be mixed, expecting array<array-key, mixed>|string (see https://psalm.dev/030)
}
}

0 comments on commit 574a5e0

Please sign in to comment.