Skip to content

Commit

Permalink
Merge pull request #46 from yiisoft/45-builtin-interfaces
Browse files Browse the repository at this point in the history
Allow using a class with built-in interface
  • Loading branch information
arogachev committed Jul 7, 2022
2 parents eb54fa4 + 3cde072 commit 8a7c630
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/ClassCache.php
Expand Up @@ -109,6 +109,10 @@ public function getClassPath(string $className, string $baseProxyClassName): str
private function getClassFileNameAndPath(string $className, string $baseProxyClassName): array
{
$classParts = explode('\\', $className);
if (count($classParts) === 1) {
$classParts = ['Builtin', ...$classParts];
}

$parentClassParts = explode('\\', $baseProxyClassName);
$classFileName = array_pop($classParts) . '.' . array_pop($parentClassParts) . '.php';
$classFilePath = $this->cachePath . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $classParts);
Expand Down
13 changes: 7 additions & 6 deletions src/ClassConfigFactory.php
Expand Up @@ -166,16 +166,17 @@ private function getMethodParameterTypeConfig(ReflectionParameter $param): ?Type
private function getMethodReturnTypeConfig(ReflectionMethod $method): ?TypeConfig
{
$returnType = $method->getReturnType();
if (!$returnType && method_exists($method, 'getTentativeReturnType')) {
$returnType = $method->getTentativeReturnType();
}

if (!$returnType) {
return null;
}

if ($returnType instanceof ReflectionUnionType) {
$name = $this->getUnionType($returnType);
} else {
/** @var ReflectionNamedType $returnType */
$name = $returnType->getName();
}
$name = $returnType instanceof ReflectionUnionType
? $this->getUnionType($returnType)
: $returnType->getName();

return new TypeConfig(
name: $name,
Expand Down
2 changes: 0 additions & 2 deletions src/ClassRenderer.php
Expand Up @@ -20,14 +20,12 @@ final class ClassRenderer
* @see renderClassSignature()
*/
private string $classSignatureTemplate = '{{modifiers}} {{classType}} {{name}}{{extends}}{{parent}}{{implements}}';

/**
* @var string A template for rendering proxy method signature.
*
* @see renderMethodSignature()
*/
private string $proxyMethodSignatureTemplate = '{{modifiers}} function {{name}}({{params}}){{returnType}}';

/**
* @var string A template for rendering proxy method body.
*
Expand Down
7 changes: 6 additions & 1 deletion tests/ClassConfigFactoryTest.php
Expand Up @@ -234,7 +234,12 @@ interfaces: [
],
name: 'count',
parameters: [],
returnType: null
returnType: PHP_VERSION_ID >= 80100
? new TypeConfig(
name: 'int',
allowsNull: false
)
: null
),
'parentMethod1' => new MethodConfig(
modifiers: [
Expand Down
3 changes: 3 additions & 0 deletions tests/ClassRendererTest.php
Expand Up @@ -78,6 +78,9 @@ abstract public function grandParentMethod4(): Yiisoft\Proxy\Tests\Stub\Node
}
}
EOD;
if (PHP_VERSION_ID >= 80100) {
$expectedOutput = str_replace('function count()', 'function count(): int', $expectedOutput);
}

$this->assertSame($expectedOutput, $output);
}
Expand Down
17 changes: 17 additions & 0 deletions tests/ProxyTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Proxy\Tests;

use Countable;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Yiisoft\Files\FileHelper;
Expand All @@ -14,6 +15,7 @@
use Yiisoft\Proxy\Tests\Stub\CarInterface;
use Yiisoft\Proxy\Tests\Stub\Graph;
use Yiisoft\Proxy\Tests\Stub\GraphInterface;
use Yiisoft\Proxy\Tests\Stub\Money;
use Yiisoft\Proxy\Tests\Stub\MyProxy;

/**
Expand Down Expand Up @@ -135,4 +137,19 @@ public function testCreateObjectProxyFromClass(): void

$this->assertSame(2, $object->edgesCount());
}

/**
* @link https://github.com/yiisoft/proxy/issues/26
* @link https://github.com/yiisoft/proxy/issues/45
*/
public function testClassWithBuiltInInterface(): void
{
$path = sys_get_temp_dir();
$manager = new ProxyManager($path);
/** @var Money|MyProxy $object */
$object = $manager->createObjectProxy(Countable::class, MyProxy::class, [new Money()]);

$this->assertSame('CountableProxy', get_class($object));
$this->assertSame(1, $object->count());
}
}
15 changes: 15 additions & 0 deletions tests/Stub/Money.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Proxy\Tests\Stub;

use Countable;

class Money implements Countable
{
public function count(): int
{
return 1;
}
}

0 comments on commit 8a7c630

Please sign in to comment.