Passed
Push — master ( 41e1cb...c64b66 )
by Bruno
09:40
created

FrontendGenerator::makeVue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 20
rs 9.7333
cc 2
nc 2
nop 3
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Frontend;
4
5
use Formularium\Model;
6
use Formularium\FrameworkComposer;
7
use Formularium\Frontend\Blade\Framework as FrameworkBlade;
8
use Formularium\Frontend\Vue\Framework as FrameworkVue;
9
use Modelarium\GeneratedCollection;
10
use Modelarium\GeneratedItem;
11
use Modelarium\GeneratorInterface;
12
13
use function Safe\file_get_contents;
14
15
class FrontendGenerator implements GeneratorInterface
16
{
17
    /**
18
     * @var FrameworkComposer
19
     */
20
    protected $composer = null;
21
22
    /**
23
     * @var Model
24
     */
25
    protected $model = null;
26
27
    /**
28
     * @var GeneratedCollection
29
     */
30
    protected $collection;
31
32
    /**
33
     *
34
     * @var string
35
     */
36
    protected $stubDir = __DIR__ . '/stubs';
37
38
    public function __construct(FrameworkComposer $composer, Model $model)
39
    {
40
        $this->composer = $composer;
41
        $this->model = $model;
42
    }
43
44
    public function generate(): GeneratedCollection
45
    {
46
        $this->collection = new GeneratedCollection();
47
48
        /**
49
         * @var FrameworkVue $vue
50
         */
51
        $vue = $this->composer->getByName('Vue');
52
        // $blade = FrameworkComposer::getByName('Blade');
53
54
        if ($vue !== null) {
55
            $this->makeVue($vue, 'Base', 'viewable');
56
            $this->makeVue($vue, 'Card', 'viewable');
57
            $this->makeVue($vue, 'List', 'viewable');
58
            $this->makeVue($vue, 'View', 'viewable');
59
            $this->makeVue($vue, 'Form', 'editable');
60
        }
61
62
        return $this->collection;
63
    }
64
65
    protected function makeVue(FrameworkVue $vue, string $component, string $mode): void
66
    {
67
        $path = 'resources/js/components/' . $this->model->getName() . '/' . $component . '.vue';
68
        $stub = file_get_contents($this->stubDir . "/Vue{$component}.stub.vue");
69
        if ($mode == 'editable') {
70
            $vue->setEditableTemplate($stub);
71
            $this->collection->push(
72
                new GeneratedItem(
73
                    GeneratedItem::TYPE_FRONTEND,
74
                    $this->model->editable($this->composer),
0 ignored issues
show
Bug introduced by
$this->composer of type Formularium\FrameworkComposer is incompatible with the type array expected by parameter $modelData of Formularium\Model::editable(). ( Ignorable by Annotation )

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

74
                    $this->model->editable(/** @scrutinizer ignore-type */ $this->composer),
Loading history...
75
                    $path
76
                )
77
            );
78
        } else {
79
            $vue->setViewableTemplate($stub);
80
            $this->collection->push(
81
                new GeneratedItem(
82
                    GeneratedItem::TYPE_FRONTEND,
83
                    $this->model->viewable($this->composer, []),
0 ignored issues
show
Unused Code introduced by
The call to Formularium\Model::viewable() has too many arguments starting with array(). ( Ignorable by Annotation )

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

83
                    $this->model->/** @scrutinizer ignore-call */ 
84
                                  viewable($this->composer, []),

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
$this->composer of type Formularium\FrameworkComposer is incompatible with the type array expected by parameter $modelData of Formularium\Model::viewable(). ( Ignorable by Annotation )

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

83
                    $this->model->viewable(/** @scrutinizer ignore-type */ $this->composer, []),
Loading history...
84
                    $path
85
                )
86
            );
87
        }
88
    }
89
}
90