Completed
Push — fetcher_factories ( 10a56f...fd93ea )
by David
13:44
created

SplashCreateControllerService::generate()   F

Complexity

Conditions 81
Paths > 20000

Size

Total Lines 424
Code Lines 257

Duplication

Lines 30
Ratio 7.08 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 30
loc 424
rs 2
cc 81
eloc 257
nc 4294967295
nop 8

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
namespace Mouf\Mvc\Splash\Services;
3
4
use Mouf\Composer\ClassNameMapper;
5
use Mouf\Mvc\Splash\Controllers\Controller;
6
use Mouf\Html\Template\TemplateInterface;
7
use Mouf\Html\HtmlElement\HtmlBlock;
8
use Mouf\Mvc\Splash\Utils\SplashException;
9
use Psr\Log\LoggerInterface;
10
use Mouf\MoufManager;
11
use Mouf\MoufCache;
12
13
/**
14
 * The service used to create controllers in Splash.
15
 */
16
class SplashCreateControllerService
17
{
18
    /**
19
     * Generates a controller, view, and sets the instance up.
20
     *
21
     * @param string $controllerName
22
     * @param string $instanceName
23
     * @param string $namespace
24
     * @param string $injectLogger
25
     * @param string $injectTemplate
26
     * @param string $injectDaoFactory
27
     * @param array  $actions
28
     */
29
    public function generate(MoufManager $moufManager, $controllerName, $instanceName, $namespace, $injectLogger = false,
30
            $injectTemplate = false, $injectDaoFactory = false, $actions = array())
31
    {
32
        $namespace = rtrim($namespace, '\\').'\\';
33
34
        $classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json');
35
        $possibleFileNames = $classNameMapper->getPossibleFileNames($namespace.$controllerName);
36
        if (!isset($possibleFileNames[0])) {
37
            throw new SplashException("The class '".$namespace.$controllerName."' cannot be loaded using rules defined in composer autoload section");
38
        }
39
        $fileName = $possibleFileNames[0];
40
        $controllerPhpDirectory = dirname($fileName);
41
        $errors = array();
42
        if (!preg_match('/^[a-z_]\w*$/i', $controllerName)) {
43
            $errors['controllerNameError'] = 'This is not a valid PHP class name.';
44
        }
45
        if (!preg_match('/^[a-z_][\w\\\\]*$/i', $namespace)) {
46
            $errors['namespaceError'] = 'This is not a valid PHP namespace.';
47
        }
48
49
        $namespace = trim($namespace, '\\');
50
51
        if (!file_exists(ROOT_PATH.'../database.tdbm') && $injectDaoFactory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectDaoFactory of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
52
            $injectDaoFactory = false;
53
        }
54
55
        // Check that instance does not already exists
56
        if ($moufManager->has($instanceName)) {
57
            $errors['instanceError'] = 'This instance already exists.';
58
        }
59
60
        $injectTwig = false;
61
        $importJsonResponse = false;
62
        $importHtmlResponse = false;
63
        $importRedirectResponse = false;
64
65
        foreach ($actions as $key => $action) {
66
            // Check if the view file exists
67 View Code Duplication
            if ($injectTemplate && $action['view'] == 'twig') {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
                $injectTwig = true;
69
                $importHtmlResponse = true;
70
                $twigFile = ltrim($action['twigFile'], '/\\');
71
72
                $viewDirName = ROOT_PATH.'../../../'.dirname($twigFile);
73
                $result = $this->createDirectory($viewDirName);
74
                if (!$result) {
75
                    $errors['actions'][$key]['twigTemplateFileError'] = 'Unable to create directory "'.$viewDirName.'"';
76
                }
77
78
                if (file_exists(ROOT_PATH.'../../../'.$twigFile)) {
79
                    $errors['actions'][$key]['twigTemplateFileError'] = 'This file already exists.';
80
                }
81
            }
82 View Code Duplication
            if ($injectTemplate && $action['view'] == 'php') {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
                $importHtmlResponse = true;
84
85
                $phpFile = ltrim($action['phpFile'], '/\\');
86
87
                $viewDirName = ROOT_PATH.'../../../'.dirname($phpFile);
88
                $result = $this->createDirectory($viewDirName);
89
                if (!$result) {
90
                    $errors['actions'][$key]['phpTemplateFileError'] = 'Unable to create directory "'.$viewDirName.'"';
91
                }
92
93
                if (file_exists(ROOT_PATH.'../../../'.$phpFile)) {
94
                    $errors['actions'][$key]['phpTemplateFileError'] = 'This file already exists.';
95
                }
96
            }
97
            if ($action['view'] == 'redirect') {
98
                if (!isset($action['redirect']) || empty($action['redirect'])) {
99
                    $errors['actions'][$key]['redirectError'] = 'Redirection URL cannot be empty.';
100
                }
101
                $importRedirectResponse = true;
102
            }
103
            if ($action['view'] == 'json') {
104
                $importJsonResponse = true;
105
            }
106
        }
107
108
        // TODO: check that URLs are not in error.
109
110
111
        if (!$errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
112
            $result = $this->createDirectory(ROOT_PATH.'../../../'.$controllerPhpDirectory);
113
            if (!$result) {
114
                $errors['namespaceError'] = 'Unable to create directory: "'.$controllerPhpDirectory.'"';
115
            } elseif (file_exists(ROOT_PATH.'../../../'.$controllerPhpDirectory.$controllerName.'.php')) {
116
                $errors['namespaceError'] = 'The file "'.$controllerPhpDirectory.$controllerName.'.php already exists."';
117
            } elseif (!is_writable(ROOT_PATH.'../../../'.$controllerPhpDirectory)) {
118
                $errors['namespaceError'] = 'Unable to write file in directory: "'.$controllerPhpDirectory.'"';
119
            }
120
121
            if (!$errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
122
                ob_start();
123
                echo '<?php
124
';
125
                ?>
126
namespace <?= $namespace ?>;
127
128
use Mouf\Mvc\Splash\Controllers\Controller;
129
<?php if ($injectTemplate) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
130
    ?>
131
use Mouf\Html\Template\TemplateInterface;
132
use Mouf\Html\HtmlElement\HtmlBlock;
133
<?php
134
135
}
136
                ?>
137
<?php if ($injectLogger) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectLogger of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
138
    ?>
139
use Psr\Log\LoggerInterface;
140
<?php
141
142
}
143
                ?>
144
<?php if ($injectDaoFactory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectDaoFactory of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
145
    ?>
146
use <?= $moufManager->getVariable('tdbmDefaultDaoNamespace').'\\'.$moufManager->getVariable('tdbmDefaultDaoFactoryName') ?>;
147
<?php
148
149
}
150
                ?>
151
<?php if ($injectTwig) {
152
    ?>
153
use \Twig_Environment;
154
use Mouf\Html\Renderer\Twig\TwigTemplate;
155
<?php
156
157
}
158
                ?>
159
<?php if ($importJsonResponse) {
160
    ?>
161
use Zend\Diactoros\Response\JsonResponse;
162
<?php
163
164
}
165
                ?>
166
<?php if ($importRedirectResponse) {
167
    ?>
168
use Zend\Diactoros\Response\RedirectResponse;
169
<?php
170
171
}
172
                ?>
173
<?php if ($importHtmlResponse) {
174
    ?>
175
use Mouf\Mvc\Splash\HtmlResponse;
176
<?php
177
178
}
179
                ?>
180
181
/**
182
 * TODO: write controller comment
183
 */
184
class <?= $controllerName ?> extends Controller {
185
186
<?php if ($injectLogger) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectLogger of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
187
    ?>
188
    /**
189
     * The logger used by this controller.
190
     * @var LoggerInterface
191
     */
192
    private $logger;
193
194
<?php
195
196
}
197
                ?>
198
<?php if ($injectTemplate) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
199
    ?>
200
    /**
201
     * The template used by this controller.
202
     * @var TemplateInterface
203
     */
204
    private $template;
205
206
    /**
207
     * The main content block of the page.
208
     * @var HtmlBlock
209
     */
210
    private $content;
211
212
<?php
213
214
}
215
                ?>
216
<?php if ($injectDaoFactory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectDaoFactory of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
217
    ?>
218
    /**
219
     * The DAO factory object.
220
     * @var DaoFactory
221
     */
222
    private $daoFactory;
223
224
<?php
225
226
}
227
                ?>
228
<?php if ($injectTwig) {
229
    ?>
230
    /**
231
     * The Twig environment (used to render Twig templates).
232
     * @var Twig_Environment
233
     */
234
    private $twig;
235
236
<?php
237
238
}
239
                ?>
240
241
    /**
242
     * Controller's constructor.
243
<?php
244
if ($injectLogger) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectLogger of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
245
    echo "     * @param LoggerInterface \$logger The logger\n";
246
}
247
                if ($injectTemplate) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
248
                    echo "     * @param TemplateInterface \$template The template used by this controller\n";
249
                    echo "     * @param HtmlBlock \$content The main content block of the page\n";
250
                }
251
                if ($injectDaoFactory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectDaoFactory of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
252
                    echo "     * @param DaoFactory \$daoFactory The object in charge of retrieving DAOs\n";
253
                }
254
                if ($injectTwig) {
255
                    echo "     * @param Twig_Environment \$twig The Twig environment (used to render Twig templates)\n";
256
                }
257
                ?>
258
     */
259
    public function __construct(<?php
260
$parameters = array();
261
                if ($injectLogger) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectLogger of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
262
                    $parameters[] = 'LoggerInterface $logger';
263
                }
264
                if ($injectTemplate) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
265
                    $parameters[] = 'TemplateInterface $template';
266
                    $parameters[] = 'HtmlBlock $content';
267
                }
268
                if ($injectDaoFactory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectDaoFactory of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
269
                    $parameters[] = 'DaoFactory $daoFactory';
270
                }
271
                if ($injectTwig) {
272
                    $parameters[] = 'Twig_Environment $twig';
273
                }
274
                echo implode(', ', $parameters);
275
                ?>) {
276
<?php if ($injectLogger) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectLogger of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
277
    ?>
278
        $this->logger = $logger;
279
<?php
280
281
}
282
                if ($injectTemplate) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
283
                    ?>
284
        $this->template = $template;
285
        $this->content = $content;
286
<?php
287
288
                }
289
                if ($injectDaoFactory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectDaoFactory of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
290
                    ?>
291
        $this->daoFactory = $daoFactory;
292
<?php
293
294
                }
295
                if ($injectTwig) {
296
                    ?>
297
        $this->twig = $twig;
298
<?php
299
300
                }
301
                ?>
302
    }
303
304
<?php foreach ($actions as $action):
305
    // First step, let's detect the {parameters} in the URL and add them if necessarry
306
    // TODO
307
    // TODO
308
    // TODO
309
    // TODO
310
311
?>
312
    /**
313
     * @URL <?= $action['url'] ?>
314
315
<?php if ($action['anyMethod'] == 'false') {
316
    if ($action['getMethod'] == 'true') {
317
        echo "     * @Get\n";
318
    }
319
    if ($action['postMethod'] == 'true') {
320
        echo "     * @Post\n";
321
    }
322
    if ($action['putMethod'] == 'true') {
323
        echo "     * @Put\n";
324
    }
325
    if ($action['deleteMethod'] == 'true') {
326
        echo "     * @Delete\n";
327
    }
328
}
329
                if (isset($action['parameters'])) {
330
                    $parameters = $action['parameters'];
331
                    foreach ($parameters as $parameter) {
332
                        echo '     * @param '.$parameter['type'].' $'.$parameter['name']."\n";
333
                    }
334
                } else {
335
                    $parameters = array();
336
                }
337
                ?>
338
     */
339
    public function <?= $action['method'] ?>(<?php
340
$parametersCode = array();
341
                foreach ($parameters as $parameter) {
342
                    $parameterCode = '$'.$parameter['name'];
343
                    if ($parameter['optionnal'] == 'true') {
344
                        if ($parameter['type'] == 'int') {
345
                            $defaultValue = (int) $parameter['defaultValue'];
346
                        } elseif ($parameter['type'] == 'number') {
347
                            $defaultValue = (float) $parameter['defaultValue'];
348
                        } else {
349
                            $defaultValue = $parameter['defaultValue'];
350
                        }
351
                        $parameterCode .= ' = '.var_export($defaultValue, true);
352
                    }
353
                    $parametersCode[] = $parameterCode;
354
                }
355
                echo implode(', ', $parametersCode);
356
                ?>) {
357
        // TODO: write content of action here
358
359
<?php if ($injectTemplate && $action['view'] == 'twig'): ?>
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
360
        // Let's add the twig file to the template.
361
        $this->content->addHtmlElement(new TwigTemplate($this->twig, <?php var_export($action['twigFile']);
362
                ?>, array("message"=>"world")));
363
364
        return new HtmlResponse($this->template);
365
<?php elseif ($injectTemplate && $action['view'] == 'php'): ?>
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
366
        // Let's add the view to the content.
367
        // Note: $this is passed as the scope, so in the view file, you can refer to protected
368
        // and public variables and methods of this constructor using "$this".
369
        $this->content->addFile(ROOT_PATH.<?php var_export($action['phpFile']) ?>, $this);
370
371
        return new HtmlResponse($this->template);
372
<?php elseif ($action['view'] == 'json'): ?>
373
374
        return new JsonResponse([ "status"=>"ok" ]);
375
<?php elseif ($action['view'] == 'redirect'): ?>
376
377
        return new RedirectResponse(<?php var_export($action['redirect']);
378
                ?>);
379
<?php endif;
380
                ?>
381
    }
382
<?php endforeach;
383
                ?>
384
}
385
<?php
386
                $file = ob_get_clean();
387
388
                file_put_contents(ROOT_PATH.'../../../'.$fileName, $file);
389
                chmod(ROOT_PATH.'../../../'.$fileName, 0664);
390
391
                // Now, let's create the views files
392
                foreach ($actions as $action) {
393
                    if ($injectTemplate && $action['view'] == 'twig') {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
394
                        $twigTemplateFile = $this->generateTwigView();
395
396
                        $twigFile = ltrim($action['twigFile'], '/\\');
397
398
                        file_put_contents(ROOT_PATH.'../../../'.$twigFile, $twigTemplateFile);
399
                        chmod(ROOT_PATH.'../../../'.$twigFile, 0664);
400
                    } elseif ($injectTemplate && $action['view'] == 'php') {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
401
                        $phpTemplateFile = $this->generatePhpView($namespace.'\\'.$controllerName);
402
403
                        $phpFile = ltrim($action['phpFile'], '/\\');
404
405
                        file_put_contents(ROOT_PATH.'../../../'.$phpFile, $phpTemplateFile);
406
                        chmod(ROOT_PATH.'../../../'.$phpFile, 0664);
407
                    }
408
                }
409
410
                // Now, let's create the instance
411
                $controllerInstance = $moufManager->createInstance($namespace.'\\'.$controllerName);
412
                $controllerInstance->setName($instanceName);
413
                if ($injectLogger) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectLogger of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
414
                    if ($moufManager->has('psr.errorLogLogger')) {
415
                        $controllerInstance->getProperty('logger')->setValue($moufManager->getInstanceDescriptor('psr.errorLogLogger'));
416
                    }
417
                }
418
                if ($injectTemplate) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectTemplate of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
419
                    if ($moufManager->has('bootstrapTemplate')) {
420
                        $controllerInstance->getProperty('template')->setValue($moufManager->getInstanceDescriptor('bootstrapTemplate'));
421
                    }
422
                    if ($moufManager->has('block.content')) {
423
                        $controllerInstance->getProperty('content')->setValue($moufManager->getInstanceDescriptor('block.content'));
424
                    }
425
                }
426
                if ($injectDaoFactory) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $injectDaoFactory of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
427
                    if ($moufManager->has('daoFactory')) {
428
                        $controllerInstance->getProperty('daoFactory')->setValue($moufManager->getInstanceDescriptor('daoFactory'));
429
                    }
430
                }
431
                if ($injectTwig) {
432
                    if ($moufManager->has('twigEnvironment')) {
433
                        $controllerInstance->getProperty('twig')->setValue($moufManager->getInstanceDescriptor('twigEnvironment'));
434
                    }
435
                }
436
437
                $moufManager->rewriteMouf();
438
439
                // There is a new class, let's purge the cache
440
                $moufCache = new MoufCache();
441
                $moufCache->purgeAll();
442
443
                // TODO: purge cache
444
            }
445
        }
446
447
        if ($errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
448
            $exception = new SplashCreateControllerServiceException('Errors detected');
449
            $exception->setErrors($errors);
450
            throw $exception;
451
        }
452
    }
453
454
    private function generateTwigView()
455
    {
456
        return '<p>Hello {{message}}</p>';
457
    }
458
459
    private function generatePhpView($controllerFQCN)
460
    {
461
        return '<?php /* @var $this '.$controllerFQCN.' */ ?>
462
This is your PHP view. You can access the controller protected and public variables / functions using the $this object';
463
    }
464
465
    /**
466
     * @param string $directory
467
     *
468
     * @return bool
469
     */
470
    private function createDirectory($directory)
471
    {
472
        if (!file_exists($directory)) {
473
            // Let's create the directory:
474
            $old = umask(0);
475
            $result = @mkdir($directory, 0775, true);
476
            umask($old);
477
478
            return $result;
479
        }
480
481
        return true;
482
    }
483
}
484