Skip to content

Commit

Permalink
Clean some code
Browse files Browse the repository at this point in the history
  • Loading branch information
Giuffre committed Jun 28, 2017
1 parent 1a68d82 commit 6d289ae
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
34 changes: 26 additions & 8 deletions src/Moka/Factory/ProxyGeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,44 @@
namespace Moka\Factory;

use Moka\Generator\ProxyGenerator;
use Moka\Strategy\MockingStrategyInterface;

class ProxyGeneratorFactory
{
/**
* @param string $fqcn
* @var array|ProxyGenerator[]
*/
private static $proxyGenerators = [];

/**
* @param MockingStrategyInterface $mockingStrategy
* @return ProxyGenerator
*/
public static function get(MockingStrategyInterface $mockingStrategy): ProxyGenerator
{
$key = self::key($mockingStrategy);
if (!array_key_exists($key, self::$proxyGenerators) || !self::$proxyGenerators[$key] instanceof ProxyGenerator) {
self::$proxyGenerators[$key] = static::build($mockingStrategy);
}

return self::$proxyGenerators[$key];
}

/**
* @param MockingStrategyInterface $mockingStrategy
* @return ProxyInterface
* @return string
*/
public static function get(string $fqcn, MockingStrategyInterface $mockingStrategy)
private static function key(MockingStrategyInterface $mockingStrategy): string
{
return (new ProxyGenerator($mockingStrategy));
return get_class($mockingStrategy);
}

/**
* @param string $fqcn
* @param MockingStrategyInterface $mockingStrategy
* @return ProxyInterface
* @return ProxyGenerator
*/
protected static function build(string $fqcn, MockingStrategyInterface $mockingStrategy)
protected static function build(MockingStrategyInterface $mockingStrategy): ProxyGenerator
{
return new Proxy($fqcn, $mockingStrategy);
return new ProxyGenerator($mockingStrategy);
}
}
7 changes: 5 additions & 2 deletions src/Moka/Generator/ProxyArgumentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

namespace Moka\Generator;

/**
* Class ProxyArgumentGenerator
* @package Moka\Generator
*/
class ProxyArgumentGenerator
{
public static function generateMethodParameter(\ReflectionParameter $parameter)
public static function generateMethodParameter(\ReflectionParameter $parameter): string
{
try {
$allowsNull = $parameter->allowsNull();
Expand Down Expand Up @@ -33,7 +37,6 @@ public static function generateMethodParameter(\ReflectionParameter $parameter)

$name = '$' . $parameter->getName();

// $canBePassByValue = $parameter->canBePassedByValue();
$isPassedByReference = $parameter->isPassedByReference();
$byReference = '';
if ($isPassedByReference) {
Expand Down
15 changes: 10 additions & 5 deletions src/Moka/Generator/ProxyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

trait ProxyTrait
{
private $object;
/**
* @var object
*/
private $mock;

/**
* @var MockingStrategyInterface
Expand All @@ -25,7 +28,7 @@ public function __construct()
public function __call($name, array $arguments)
{
if ($this->mockingStrategy instanceof MockingStrategyInterface) {
return $this->mockingStrategy->call($this->object, $name, $arguments);
return $this->mockingStrategy->call($this->mock, $name, $arguments);
}
}

Expand All @@ -34,7 +37,7 @@ public function __call($name, array $arguments)
*/
public function _moka_setObject($object)
{
$this->object = $object;
$this->mock = $object;
}

public function _moka_setMockingStrategy(MockingStrategyInterface $mockingStrategy)
Expand All @@ -51,7 +54,7 @@ public function _moka_setMockingStrategy(MockingStrategyInterface $mockingStrate
*/
public function stub(array $methodsWithValues): ProxyInterface
{
$this->mockingStrategy->decorate($this->object, $methodsWithValues);
$this->mockingStrategy->decorate($this->mock, $methodsWithValues);

return $this;
}
Expand All @@ -60,9 +63,11 @@ public function stub(array $methodsWithValues): ProxyInterface
* @return object
*
* @throws MockNotServedException
*
* @deprecated since v2.0.0
*/
public function serve()
{
return $this->object;
return $this->mock;
}
}
2 changes: 2 additions & 0 deletions src/Moka/Proxy/ProxyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public function stub(array $methodsWithValues): ProxyInterface;
* @return object
*
* @throws MockNotServedException
*
* @deprecated since v2.0.0
*/
public function serve();
}
3 changes: 3 additions & 0 deletions src/Moka/Strategy/AbstractMockingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ abstract protected function doBuild(string $fqcn);
* @param object $mock
* @param array $stubs
* @return void
*
* @throws InvalidArgumentException
* @throws NotImplementedException
*/
public function decorate($mock, array $stubs)
{
Expand Down

0 comments on commit 6d289ae

Please sign in to comment.