Skip to content

Commit

Permalink
Fix #58: Fix rendering of class modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Jul 18, 2022
1 parent 095abc1 commit 7a24053
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -2,7 +2,7 @@

## 1.0.2 under development

- no changes in this release.
- Bug #58: Fix rendering of class modifiers (@arogachev)

## 1.0.1 July 11, 2022

Expand Down
13 changes: 9 additions & 4 deletions src/ClassRenderer.php
Expand Up @@ -28,7 +28,7 @@ final class ClassRenderer
*
* @see renderMethodSignature()
*/
private string $proxyMethodSignatureTemplate = '{{modifiers}} function {{name}}({{params}}){{returnType}}';
private string $proxyMethodSignatureTemplate = '{{modifiers}}function {{name}}({{params}}){{returnType}}';
/**
* @var string A template for rendering proxy method body.
*
Expand Down Expand Up @@ -96,17 +96,22 @@ private function renderImplements(array $interfaces): string
}

/**
* Renders modifiers section.
* Renders modifiers section. Can be used for both class and method signature.
*
* @param string[] $modifiers A list of modifiers
* @param string[] $modifiers A list of modifiers.
*
* @return string Modifiers section as a string.
*
* @see ClassConfig::$modifiers
*/
private function renderModifiers(array $modifiers): string
{
return implode(' ', $modifiers);
$output = implode(' ', $modifiers);
if ($output !== '') {
$output .= ' ';
}

return $output;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/ClassRendererTest.php
Expand Up @@ -13,6 +13,7 @@
use Yiisoft\Proxy\Tests\Stub\MyProxy;
use Yiisoft\Proxy\Tests\Stub\Node;
use Yiisoft\Proxy\Tests\Stub\NodeInterface;
use Yiisoft\Proxy\Tests\Stub\Race;

class ClassRendererTest extends TestCase
{
Expand Down Expand Up @@ -75,6 +76,27 @@ public function someMethod(): void
$this->call('someMethod', []);
}
}
EOD;

$this->assertSame($expectedOutput, $output);
}

public function testRenderModifiers(): void
{
$factory = new ClassConfigFactory();
$config = $factory->getClassConfig(Race::class);
$config->parent = MyProxy::class;

$renderer = new ClassRenderer();
$output = $renderer->render($config);
$expectedOutput = <<<'EOD'
final class Race extends Yiisoft\Proxy\Tests\Stub\MyProxy
{
public function time(): int
{
return $this->call('time', []);
}
}
EOD;

$this->assertSame($expectedOutput, $output);
Expand Down
13 changes: 13 additions & 0 deletions tests/Stub/Race.php
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Proxy\Tests\Stub;

final class Race
{
public function time(): int
{
return 10;
}
}

0 comments on commit 7a24053

Please sign in to comment.