ReactorBasedPropertyRenderer::renderProperty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * Spiral Framework, IDE Helper
4
 *
5
 * @author    Dmitry Mironov <[email protected]>
6
 * @licence   MIT
7
 */
8
9
namespace Spiral\IdeHelper\Renderer;
10
11
use Spiral\IdeHelper\Helpers;
12
use Spiral\IdeHelper\Model\ClassProperty;
13
use Spiral\Reactor\ClassDeclaration;
14
use Spiral\Reactor\ClassDeclaration\PropertyDeclaration;
15
use Spiral\Reactor\FileDeclaration;
16
17
18
/**
19
 * Class ReactorBasedPropertyRenderer
20
 *
21
 * WARNING: this class only support rendering multiple classes in the same namespace.
22
 *
23
 * @package Spiral\IdeHelper\Renderer
24
 */
25
class ReactorBasedPropertyRenderer implements RendererInterface
26
{
27
    /**
28
     * @inheritdoc
29
     */
30
    public function render(array $classes): string
31
    {
32
        if (count($classes) === 0) {
33
            $declaration = new FileDeclaration();
34
35
            return $declaration->render();
36
        }
37
38
        $namespace = $classes[0]->getNamespace() ?? '';
39
        $fileDeclaration = new FileDeclaration($namespace);
40
        foreach ($classes as $class) {
41
            $classDeclaration = new ClassDeclaration($class->getShortName());
42
            $properties = $classDeclaration->getProperties();
43
44
            foreach ($class->getMembers() as $member) {
45
                if ($member instanceof ClassProperty) {
46
                    $name = $member->getName();
47
                    $comment = $this->renderProperty($member->getTypes());
48
49
                    $property = new PropertyDeclaration($name, null, $comment);
50
                    $property->setAccess("protected");
51
52
                    $properties->add($property);
53
                }
54
            }
55
56
            $fileDeclaration->addElement($classDeclaration);
57
        }
58
59
        return $fileDeclaration->render();
60
    }
61
62
    /**
63
     * @param array $types
64
     * @return string
65
     */
66
    private function renderProperty(array $types): string
67
    {
68
        $type = implode('|', array_map([Helpers::class, 'fqcn'], $types));
69
70
        return "@var {$type}";
71
    }
72
}