diff --git a/lib/ProxyClassFactory.php b/lib/ProxyClassFactory.php index 598e2ec..52a6a6e 100644 --- a/lib/ProxyClassFactory.php +++ b/lib/ProxyClassFactory.php @@ -63,7 +63,7 @@ private static function buildMethods(array $methods): string { $publicStringMethods = array(); foreach ($methods as $method) { - if (false !== strpos($method->getName(), '__')) { + if (false !== strpos($method->getName(), '__construct')) { continue; } @@ -132,7 +132,14 @@ private static function buildParameterDefaultValue(ReflectionParameter $paramete $defaultValue = '='; if ($parameter->isDefaultValueConstant()) { - $defaultValue .= '\\'.$parameter->getDefaultValueConstantName(); + $defaultValueConstantName = $parameter->getDefaultValueConstantName(); + list($type) = explode('::', $defaultValueConstantName); + + if (!\in_array($type, array('self', 'static'))) { + $defaultValue .= '\\'; + } + + $defaultValue .= $defaultValueConstantName; return $defaultValue; } diff --git a/tests/Dummy.php b/tests/Dummy.php index 1291a92..01f2122 100644 --- a/tests/Dummy.php +++ b/tests/Dummy.php @@ -6,6 +6,8 @@ class Dummy { + const SOME_VALUE = 'dummy-value'; + public function hello(): string { return 'hello'; diff --git a/tests/InstanceSpy.php b/tests/InstanceSpy.php new file mode 100644 index 0000000..756c991 --- /dev/null +++ b/tests/InstanceSpy.php @@ -0,0 +1,40 @@ +method = $method; + $this->input = $input; + } + + /** + * @return mixed + */ + public function getMethod() + { + return $this->method; + } + + /** + * @return mixed + */ + public function getInput() + { + return $this->input; + } +} diff --git a/tests/ProxyClassFactoryTest.php b/tests/ProxyClassFactoryTest.php index fbe343d..9438f9d 100644 --- a/tests/ProxyClassFactoryTest.php +++ b/tests/ProxyClassFactoryTest.php @@ -12,70 +12,79 @@ class ProxyClassFactoryTest extends TestCase public function testCreate() { /** @var TestSpy $proxy */ - $proxy = ProxyClassFactory::create(TestSpy::class, function () { - return new TestSpy(); + $instanceSpy = new InstanceSpy(); + $proxy = ProxyClassFactory::create(TestSpy::class, function () use ($instanceSpy) { + return new TestSpy($instanceSpy); }); $this->assertInstanceOf(TestSpy::class, $proxy); $proxy->noReturnType(); - $this->assertEquals('noReturnType', $proxy::$method); - $this->assertNull($proxy::$input); + $this->assertEquals('noReturnType', $instanceSpy->getMethod()); + $this->assertNull($instanceSpy->getInput()); $proxy->voidReturnType(); - $this->assertEquals('voidReturnType', $proxy::$method); - $this->assertNull($proxy::$input); + $this->assertEquals('voidReturnType', $instanceSpy->getMethod()); + $this->assertNull($instanceSpy->getInput()); $output = $proxy->nullableBuiltInReturnType(); - $this->assertEquals('nullableBuiltInReturnType', $proxy::$method); - $this->assertNull($proxy::$input); + $this->assertEquals('nullableBuiltInReturnType', $instanceSpy->getMethod()); + $this->assertNull($instanceSpy->getInput()); $this->assertEquals($output, 1); $output = $proxy->notNullableBuiltInReturnType(); - $this->assertEquals('notNullableBuiltInReturnType', $proxy::$method); - $this->assertNull($proxy::$input); + $this->assertEquals('notNullableBuiltInReturnType', $instanceSpy->getMethod()); + $this->assertNull($instanceSpy->getInput()); $this->assertEquals($output, true); $output = $proxy->notNullableNotBuiltInReturnType(); - $this->assertEquals('notNullableNotBuiltInReturnType', $proxy::$method); - $this->assertNull($proxy::$input); + $this->assertEquals('notNullableNotBuiltInReturnType', $instanceSpy->getMethod()); + $this->assertNull($instanceSpy->getInput()); $this->assertEquals($output, new Dummy()); $output = $proxy->nullableNotBuiltInReturnType(); - $this->assertEquals('nullableNotBuiltInReturnType', $proxy::$method); - $this->assertNull($proxy::$input); + $this->assertEquals('nullableNotBuiltInReturnType', $instanceSpy->getMethod()); + $this->assertNull($instanceSpy->getInput()); $this->assertEquals($output, new Dummy()); $proxy->noTypeHintingParamType(10); - $this->assertEquals('noTypeHintingParamType', $proxy::$method); - $this->assertEquals(10, $proxy::$input); + $this->assertEquals('noTypeHintingParamType', $instanceSpy->getMethod()); + $this->assertEquals(10, $instanceSpy->getInput()); $proxy->nullableBuiltInParamType(null); - $this->assertEquals('nullableBuiltInParamType', $proxy::$method); - $this->assertNull($proxy::$input); + $this->assertEquals('nullableBuiltInParamType', $instanceSpy->getMethod()); + $this->assertNull($instanceSpy->getInput()); $proxy->notNullableBuiltInParamType(true); - $this->assertEquals('notNullableBuiltInParamType', $proxy::$method); - $this->assertTrue($proxy::$input); + $this->assertEquals('notNullableBuiltInParamType', $instanceSpy->getMethod()); + $this->assertTrue($instanceSpy->getInput()); $proxy->notNullableNotBuiltInParamType(new Dummy()); - $this->assertEquals('notNullableNotBuiltInParamType', $proxy::$method); - $this->assertEquals(new Dummy(), $proxy::$input); + $this->assertEquals('notNullableNotBuiltInParamType', $instanceSpy->getMethod()); + $this->assertEquals(new Dummy(), $instanceSpy->getInput()); $proxy->nullableNotBuiltInParamType(null); - $this->assertEquals('nullableNotBuiltInParamType', $proxy::$method); - $this->assertNull($proxy::$input); + $this->assertEquals('nullableNotBuiltInParamType', $instanceSpy->getMethod()); + $this->assertNull($instanceSpy->getInput()); $proxy->nullableDefaultParamType(); - $this->assertEquals('nullableDefaultParamType', $proxy::$method); - $this->assertNull($proxy::$input); + $this->assertEquals('nullableDefaultParamType', $instanceSpy->getMethod()); + $this->assertNull($instanceSpy->getInput()); $proxy->notNullableDefaultParamType(); - $this->assertEquals('notNullableDefaultParamType', $proxy::$method); - $this->assertEquals(10, $proxy::$input); + $this->assertEquals('notNullableDefaultParamType', $instanceSpy->getMethod()); + $this->assertEquals(10, $instanceSpy->getInput()); + + $proxy->notNullableExternalConstantDefaultParamType(); + $this->assertEquals('notNullableExternalConstantDefaultParamType', $instanceSpy->getMethod()); + $this->assertEquals(Dummy::SOME_VALUE, $instanceSpy->getInput()); + + $proxy->notNullableSelfConstantDefaultParamType(); + $this->assertEquals('notNullableSelfConstantDefaultParamType', $instanceSpy->getMethod()); + $this->assertEquals($proxy::SOME_VALUE, $instanceSpy->getInput()); $proxy->paramList(10, null, true, new Dummy(), null, new Dummy()); - $this->assertEquals('paramList', $proxy::$method); + $this->assertEquals('paramList', $instanceSpy->getMethod()); $this->assertEquals(array( 10, null, @@ -84,13 +93,21 @@ public function testCreate() null, new Dummy(), 10, - ), $proxy::$input); + ), $instanceSpy->getInput()); $callable = function () { return 10; }; $proxy->withCallableParameter($callable); - $this->assertEquals('withCallableParameter', $proxy::$method); - $this->assertEquals($callable(), ($proxy::$input)()); + $this->assertEquals('withCallableParameter', $instanceSpy->getMethod()); + $this->assertEquals($callable(), ($instanceSpy->getInput())()); + + $proxy(); + $this->assertEquals('__invoke', $instanceSpy->getMethod()); + $this->assertNull($instanceSpy->getInput()); + + $clonedProxy = clone $proxy; + $this->assertEquals('__clone', $instanceSpy->getMethod()); + $this->assertNull($instanceSpy->getInput()); } } diff --git a/tests/TestSpy.php b/tests/TestSpy.php index ca42ed6..5e25b9b 100644 --- a/tests/TestSpy.php +++ b/tests/TestSpy.php @@ -6,95 +6,121 @@ class TestSpy { - public static $method; - public static $input; + const SOME_VALUE = 'self-value'; - private static function setVars($method, $input = null) + /** + * @var InstanceSpy + */ + private $instanceSpy; + + /** + * TestSpy constructor. + */ + public function __construct(InstanceSpy $instanceSpy) { - static::$method = $method; - static::$input = $input; + $this->instanceSpy = $instanceSpy; } public function noReturnType() { - static::setVars('noReturnType'); + $this->instanceSpy->setVars('noReturnType'); } public function voidReturnType(): void { - static::setVars('voidReturnType'); + $this->instanceSpy->setVars('voidReturnType'); } public function nullableBuiltInReturnType(): ?int { - static::setVars('nullableBuiltInReturnType'); + $this->instanceSpy->setVars('nullableBuiltInReturnType'); return 1; } public function notNullableBuiltInReturnType(): bool { - static::setVars('notNullableBuiltInReturnType'); + $this->instanceSpy->setVars('notNullableBuiltInReturnType'); return true; } public function notNullableNotBuiltInReturnType(): Dummy { - static::setVars('notNullableNotBuiltInReturnType'); + $this->instanceSpy->setVars('notNullableNotBuiltInReturnType'); return new Dummy(); } public function nullableNotBuiltInReturnType(): ?Dummy { - static::setVars('nullableNotBuiltInReturnType'); + $this->instanceSpy->setVars('nullableNotBuiltInReturnType'); return new Dummy(); } public function noTypeHintingParamType($value) { - static::setVars('noTypeHintingParamType', $value); + $this->instanceSpy->setVars('noTypeHintingParamType', $value); } public function nullableBuiltInParamType(?int $value) { - static::setVars('nullableBuiltInParamType', $value); + $this->instanceSpy->setVars('nullableBuiltInParamType', $value); } public function notNullableBuiltInParamType(bool $value) { - static::setVars('notNullableBuiltInParamType', $value); + $this->instanceSpy->setVars('notNullableBuiltInParamType', $value); } public function notNullableNotBuiltInParamType(Dummy $value) { - static::setVars('notNullableNotBuiltInParamType', $value); + $this->instanceSpy->setVars('notNullableNotBuiltInParamType', $value); } public function nullableNotBuiltInParamType(?Dummy $value) { - static::setVars('nullableNotBuiltInParamType', $value); + $this->instanceSpy->setVars('nullableNotBuiltInParamType', $value); } public function nullableDefaultParamType(?Dummy $juan = null) { - static::setVars('nullableDefaultParamType', $juan); + $this->instanceSpy->setVars('nullableDefaultParamType', $juan); } public function notNullableDefaultParamType(int $value = 10) { - static::setVars('notNullableDefaultParamType', $value); + $this->instanceSpy->setVars('notNullableDefaultParamType', $value); + } + + public function notNullableExternalConstantDefaultParamType(string $value = Dummy::SOME_VALUE) + { + $this->instanceSpy->setVars('notNullableExternalConstantDefaultParamType', $value); + } + + public function notNullableSelfConstantDefaultParamType(string $value = self::SOME_VALUE) + { + $this->instanceSpy->setVars('notNullableSelfConstantDefaultParamType', $value); } public function paramList($value1, ?int $value2, bool $value3, Dummy $value4, ?Dummy $value5, ?Dummy $value6 = null, int $value7 = 10) { - static::setVars('paramList', \func_get_args()); + $this->instanceSpy->setVars('paramList', \func_get_args()); } public function withCallableParameter(callable $param) { - static::setVars('withCallableParameter', $param); + $this->instanceSpy->setVars('withCallableParameter', $param); + } + + public function __invoke() + { + $this->instanceSpy->setVars('__invoke'); + } + + public function __clone() + { + $this->instanceSpy->setVars('__clone'); } }