Passed
Push — master ( 42cd76...fd00d5 )
by Bruno
04:07
created

Framework::generateRenderable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 32
ccs 0
cts 0
cp 0
rs 9.568
cc 1
nc 1
nop 2
crap 2
1
<?php declare(strict_types=1);
2
3
namespace Formularium;
4
5
use Formularium\Exception\ClassNotFoundException;
6
use Formularium\Exception\Exception;
7
use Formularium\Factory\ElementFactory;
8
use Formularium\Factory\RenderableFactory;
9
use Formularium\HTMLNode;
10
use Nette\PhpGenerator\PhpNamespace;
11
12
/**
13
 * Abstract base class for frameworks. Each framework should have a class inheriting
14
 * from this class.
15
 */
16
abstract class Framework
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $name;
22
23
    /**
24
     * Options for rendering
25
     * @var array
26
     */
27
    protected $options = [];
28
29 10
    public function __construct(string $name)
30
    {
31 10
        $this->name = $name;
32 10
    }
33
34 2
    public function getRenderable(Datatype $datatype, FrameworkComposer $composer = null): Renderable
35
    {
36 2
        return RenderableFactory::factory($datatype, $this, $composer);
37
    }
38
39 1
    public function getElement(string $name, FrameworkComposer $composer = null): Element
40
    {
41 1
        return ElementFactory::factory($name, $this, $composer);
42
    }
43
44 7
    public function getName(): string
45
    {
46 7
        return $this->name;
47
    }
48
49
    /**
50
     * @param string $name
51
     * @param mixed $value
52
     * @return mixed
53
     */
54 2
    public function getOption(string $name, $value = null)
55
    {
56 2
        return $this->options[$name] ?? $value;
57
    }
58
59
    /**
60
     *
61
     * @param string $name
62
     * @param mixed $value
63
     * @return self
64
     */
65
    public function setOption(string $name, $value): self
66
    {
67
        $this->options[$name] = $value;
68
        return $this;
69
    }
70
71
    /**
72
     * Set a new option
73
     *
74
     * @param array $options
75
     * @return self
76
     */
77 1
    public function setOptions(array $options): self
78
    {
79 1
        $this->options = $options;
80 1
        return $this;
81
    }
82
83
    /**
84
     * Returns a string with the <head> HTML to generate standalone files.
85
     * This is used by the kitchensink generator.
86
     *
87
     * @param HTMLNode $head
88
     * @return void
89
     * @codeCoverageIgnore
90
     */
91
    public function htmlHead(HTMLNode &$head)
0 ignored issues
show
Unused Code introduced by
The parameter $head is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

91
    public function htmlHead(/** @scrutinizer ignore-unused */ HTMLNode &$head)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
    {
93
    }
94
95
    /**
96
     * Returns a string with things to add to the footer of the page (such as scripts)
97
     * This is used by the kitchensink generator.
98
     *
99
     * @param HTMLNode $head
100
     * @return void
101
     * @codeCoverageIgnore
102
     */
103
    public function htmlFooter(HTMLNode &$head)
0 ignored issues
show
Unused Code introduced by
The parameter $head is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

103
    public function htmlFooter(/** @scrutinizer ignore-unused */ HTMLNode &$head)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
    {
105
    }
106
107
    /**
108
     * @param Model $m
109
     * @param HTMLNode[] $elements
110
     * @param string $previousCompose
111
     * @return string
112
     */
113
    public function editableCompose(Model $m, array $elements, string $previousCompose): string
0 ignored issues
show
Unused Code introduced by
The parameter $elements is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

113
    public function editableCompose(Model $m, /** @scrutinizer ignore-unused */ array $elements, string $previousCompose): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $m is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

113
    public function editableCompose(/** @scrutinizer ignore-unused */ Model $m, array $elements, string $previousCompose): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
114
    {
115
        return $previousCompose;
116
    }
117
118
    /**
119
     * @param Model $m
120
     * @param HTMLNode[] $elements
121
     * @param string $previousCompose
122
     * @return string
123
     */
124
    public function viewableCompose(Model $m, array $elements, string $previousCompose): string
0 ignored issues
show
Unused Code introduced by
The parameter $elements is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

124
    public function viewableCompose(Model $m, /** @scrutinizer ignore-unused */ array $elements, string $previousCompose): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $m is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

124
    public function viewableCompose(/** @scrutinizer ignore-unused */ Model $m, array $elements, string $previousCompose): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
125
    {
126
        return $previousCompose;
127
    }
128
129
    /**
130
     * Generates scaffolding for a new renderable. This can be overriden so frameworks change
131
     * the default template.
132
     *
133
     * @codeCoverageIgnore
134
     * @param Datatype $datatype The new datatype name.
135
     * @param string $baseNamespace The namespace for the new file.
136
     * @return PhpNamespace
137
     */
138
    public function generateRenderable(
139
        Datatype $datatype,
140
        string $baseNamespace
141
    ): PhpNamespace {
142
        $datatypeLower = mb_strtolower($datatype->getName());
143
        $frameworkName = $this->getName();
144
145
        $baseClass = '\\Formularium\\Renderable';
146
        $ns = "{$baseNamespace}\\{$frameworkName}\\Renderable\Datatype_{$datatypeLower}";
147
148
        $namespace = new PhpNamespace($ns);
149
        $namespace->addUse('\\Formularium\\Field');
150
        $namespace->addUse('\\Formularium\\HTMLNode');
151
152
        $class = $namespace->addClass("Renderable_${datatypeLower}")
153
            ->setExtends($baseClass);
154
155
        $viewable = $class->addMethod('viewable')
156
            ->setBody("return \$previous;\n")
157
            ->setReturnType('\\Formularium\\HTMLNode');
158
        $viewable->addParameter('value');
159
        $viewable->addParameter('field')->setType('\\Formularium\\Field');
160
        $viewable->addParameter('previous')->setType('\\Formularium\\HTMLNode');
161
162
        $editable = $class->addMethod('editable')
163
            ->setBody("return \$previous;\n")
164
            ->setReturnType('\\Formularium\\HTMLNode');
165
        $editable->addParameter('value');
166
        $editable->addParameter('field')->setType('\\Formularium\\Field');
167
        $editable->addParameter('previous')->setType('\\Formularium\\HTMLNode');
168
        
169
        return $namespace;
170
    }
171
}
172