Passed
Push — master ( 192b45...fd1497 )
by Bruno
06:54
created

elements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 26
nc 1
nop 1
dl 0
loc 44
rs 9.504
c 2
b 1
f 0
1
<?php declare(strict_types=1);
2
3
require(__DIR__ . '/../vendor/autoload.php');
4
5
use Formularium\Factory\DatatypeFactory;
6
use Formularium\Element;
7
use Formularium\Exception\ClassNotFoundException;
8
use Formularium\Formularium;
9
use Formularium\FrameworkComposer;
10
use Formularium\Frontend\HTML\Element\Button;
11
use Formularium\Frontend\HTML\Element\Card;
12
use Formularium\Frontend\HTML\Element\Pagination;
13
use Formularium\Frontend\HTML\Element\Table;
14
use Formularium\Frontend\HTML\Renderable\Renderable_choice;
15
use Formularium\Frontend\HTML\Renderable\Renderable_number;
16
use Formularium\Model;
17
use Formularium\Renderable;
18
use Formularium\Validator\MaxLength;
19
use Formularium\Validator\Min;
20
use Formularium\Validator\MinLength;
21
22
function elements(FrameworkComposer $framework): string
23
{
24
    $upload = $framework->element(
25
        'Upload',
26
        [
27
        ]
28
    );
29
    $submitButton = $framework->element(
30
        'Button',
31
        [
32
            Element::LABEL => 'Save',
33
            Button::COLOR => Button::COLOR_PRIMARY
34
        ]
35
    );
36
    $table = $framework->element(
37
        'Table',
38
        [
39
            Table::ROW_NAMES => ['First', 'Second', 'Third'],
40
            Table::ROW_DATA => [ ['a', 'b', 'c'], [ 'd', 'e', 'f'] ],
41
            Table::STRIPED => true
42
        ]
43
    );
44
    $pagination = $framework->element(
45
        'Pagination',
46
        [
47
            Pagination::CURRENT => 21,
48
            Pagination::CURRENT_PAGE => 2, // should have only CURRENT or CURRENT_PAGE, but depends on framework
49
            Pagination::TOTAL_ITEMS => 253,
50
        ]
51
    );
52
    $card = $framework->element(
53
        'Card',
54
        [
55
            Card::TITLE => 'Card title',
56
            Card::IMAGE => 'https://via.placeholder.com/150',
57
            Card::CONTENT => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '
58
        ]
59
    );
60
61
    return '<h3 class="kitchen">Upload</h3>' . $upload . "\n" .
62
        '<h3 class="kitchen">Button</h3>' . $submitButton . "\n" .
63
        '<h3 class="kitchen">Table</h3>' .  $table . "\n" .
64
        '<h3 class="kitchen">Pagination</h3>' .  $pagination . "\n" .
65
        '<h3 class="kitchen">Card</h3><div style="width: 180px">' .  $card . "</div>\n";
66
}
67
68
function kitchenSink($frameworkName, string $templateName)
69
{
70
    $framework = FrameworkComposer::create($frameworkName);
71
    $head = $framework->htmlHead();
72
    $footer = $framework->htmlFooter();
73
74
    /*
75
     * kitchen sink fields
76
     */
77
    $fields = [];
78
    $datatypes = Formularium::getDatatypeNames();
79
80
    // make a default for all types
81
    foreach ($datatypes as $d) {
82
        try {
83
            DatatypeFactory::factory($d);
84
            $fields[$d] = [
85
                'datatype' => $d,
86
                'renderable' => [
87
                    Renderable::LABEL => 'Type ' . $d
88
                ],
89
            ];
90
        } catch (ClassNotFoundException $e) {
91
            // Abstract class
92
        }
93
    }
94
95
    /*
96
     * basic demo fiels
97
     */
98
    $basicFields = [
99
        'myString' => [
100
            'datatype' => 'string',
101
            'validators' => [
102
                MinLength::class => [ 'value' => 3],
103
                MaxLength::class => [ 'value' => 30],
104
            ],
105
            'renderable' => [
106
                Renderable::LABEL => 'Type string',
107
                Renderable::COMMENT => 'Some text explaining this field',
108
                Renderable::PLACEHOLDER => "Type here",
109
                Renderable::SIZE => Renderable::SIZE_LARGE,
110
                Renderable::ICON_PACK => 'fas',
111
                Renderable::ICON => 'fa-check'
112
            ],
113
        ],
114
        'myInteger' => [
115
            'datatype' => 'integer',
116
            'validators' => [
117
                Min::class => [ 'value' => 4],
118
                Max::class => [ 'value' => 40],
119
            ],
120
            'renderable' => [
121
                Renderable_number::STEP => 2,
122
                Renderable::LABEL => 'Type integer',
123
                Renderable::PLACEHOLDER => "Type here"
124
            ],
125
        ],
126
        'countrycodeselect' => [
127
            'datatype' => 'countrycodeISO3',
128
            'renderable' => [
129
                Renderable_choice::FORMAT_CHOOSER => Renderable_choice::FORMAT_CHOOSER_SELECT,
130
                Renderable::LABEL => 'Country code - select choice'
131
            ],
132
        ],
133
    ];
134
135
    // generate basic model
136
    $basicModel = Model::fromStruct(
137
        [
138
            'name' => 'BasicModel',
139
            'fields' => $basicFields
140
        ]
141
    );
142
    $basicDemoEditable = $basicModel->editable($framework, []);
143
144
    // generate kitchen sink model
145
    $model = Model::fromStruct(
146
        [
147
            'name' => 'TestModel',
148
            'fields' => $fields
149
        ]
150
    );
151
    $randomData = [];
152
    foreach ($model->getFields() as $f) {
153
        if ($f->getDatatype()->getBasetype() !== 'constant') {
154
            $randomData[$f->getName()] = $f->getDatatype()->getRandom();
155
        }
156
    }
157
    $modelViewable = $model->viewable($framework, $randomData);
158
    $modelEditable = $model->editable($framework);
159
160
    $title = join('/', $frameworkName);
161
    $template = file_get_contents(__DIR__ . '/kitchentemplates/' . $templateName);
162
163
    $template = strtr(
164
        $template,
165
        [
166
            '{{title}}' => $title,
167
            '{{head}}' => $head,
168
            '{{basicDemoEditable}}' => $basicDemoEditable,
169
            '{{elements}}' => elements($framework),
0 ignored issues
show
Unused Code introduced by
The call to elements() has too many arguments starting with $framework. ( Ignorable by Annotation )

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

169
            '{{elements}}' => /** @scrutinizer ignore-call */ elements($framework),

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
Are you sure the usage of elements($framework) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
170
            '{{modelViewable}}' => $modelViewable,
171
            '{{modelEditable}}' => $modelEditable,
172
            '{{footer}}' => $footer,
173
        ]
174
    );
175
    return $template;
176
}
177
178
@mkdir(__DIR__ . '/../docs/');
179
@mkdir(__DIR__ . '/../docs/kitchensink/');
180
$path = __DIR__ . '/../Formularium/Frontend/';
181
$dir = scandir($path);
182
if ($dir === false) {
183
    echo 'Cannot find frontend';
184
    return 1;
185
}
186
$frameworks = array_diff($dir, array('.', '..'));
187
$index = <<<EOF
188
<!DOCTYPE html>
189
<html>
190
<head>
191
<head>
192
    <meta charset="UTF-8">
193
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
194
    <meta name="viewport" content="width=device-width, initial-scale=1">
195
196
    <title>Formularium Kitchen Sink</title>
197
    <meta property="og:title" content="Formularium">
198
    <meta property="og:locale" content="en_US">
199
    <meta name="description" content="Form validation and generation for PHP with custom frontend generators">
200
    <meta property="og:description" content="Form validation and generation for PHP with custom frontend generators">
201
    <link rel="canonical" href="https://corollarium.github.io/Formularium/">
202
    <meta property="og:url" content="https://corollarium.github.io/Formularium/">
203
    <meta property="og:site_name" content="Formularium">
204
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
205
</head>
206
<body>
207
<div class="container">
208
<h1>Formularium Kitchen Sink</h1>
209
<ul>
210
EOF;
211
$frameworks = [
212
    ['framework' => ['HTML', 'Quill'], 'template' => 'base.html'],
213
    ['framework' => ['HTML', 'Bulma', 'Quill'], 'template' => 'bulma.html'],
214
    ['framework' => ['HTML', 'Bootstrap', 'Quill'], 'template' => 'base.html'],
215
    ['framework' => ['HTML', 'Bootstrap', 'Quill', 'Parsley'], 'template' => 'base.html'],
216
    ['framework' => ['HTML', 'Bootstrapvue', 'Vue'], 'template' => 'base.html'],
217
    ['framework' => ['HTML', 'Materialize'], 'template' => 'base.html'],
218
    ['framework' => ['HTML', 'Bulma', 'Quill', 'Vue'], 'template' => 'bulma.html'],
219
    ['framework' => ['HTML', 'Bootstrap', 'Vue'], 'template' => 'base.html'],
220
    ['framework' => ['HTML', 'Buefy', 'Vue'], 'template' => 'bulma.html'],
221
    ['framework' => ['HTML', 'React'], 'template' => 'base.html'],
222
    ['framework' => ['HTML', 'Bootstrap', 'React'], 'template' => 'base.html'],
223
];
224
foreach ($frameworks as $f) {
225
    $name = join('', $f['framework']);
226
    echo "Building $name...\n";
227
    $html = kitchenSink($f['framework'], $f['template']);
228
    file_put_contents(__DIR__ . '/../docs/kitchensink/' . $name . '.html', $html);
229
    $index .= "<li><a href='{$name}.html'>" . join('+', $f['framework']) . '</a></li>';
230
}
231
$index .= "</ul>
232
<footer>
233
    <a href='https://github.com/Corollarium/Formularium/'>Source code</a> and <a href='https://corollarium.github.io/Formularium/'>Documentation</a>
234
</footer>
235
</div>
236
</body></html>";
237
file_put_contents(__DIR__ . '/../docs/kitchensink/index.html', $index);
238