Passed
Push — master ( 3491f1...e1ceb1 )
by Chris
40:32
created

ModelRepositoryPrinter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 8
dl 0
loc 21
ccs 0
cts 3
cp 0
crap 2
rs 9.9666

How to fix   Many Parameters   

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
3
namespace Leonidas\Console\Library\Printer\Model;
4
5
use Leonidas\Console\Library\Printer\Model\Abstracts\AbstractModelRepositoryPrinter;
6
use Leonidas\Console\Library\Printer\Model\Abstracts\TypedClassPrinterTrait;
7
use Leonidas\Library\System\Model\Abstracts\Attachment\AbstractAttachmentEntityRepository;
8
use Leonidas\Library\System\Model\Abstracts\Comment\AbstractCommentModelRepositoryInterface;
9
use Leonidas\Library\System\Model\Abstracts\Post\AbstractPostEntityRepository;
10
use Leonidas\Library\System\Model\Abstracts\Term\AbstractTermEntityRepository;
11
use Leonidas\Library\System\Model\Abstracts\User\AbstractUserModelRepository;
12
use Nette\PhpGenerator\PhpNamespace;
13
14
class ModelRepositoryPrinter extends AbstractModelRepositoryPrinter
15
{
16
    use TypedClassPrinterTrait;
17
18
    public const ABSTRACTS = [
19
        'post' => AbstractPostEntityRepository::class,
20
        'post:h' => AbstractPostEntityRepository::class,
21
        'attachment' => AbstractAttachmentEntityRepository::class,
22
        'term' => AbstractTermEntityRepository::class,
23
        'term:h' => AbstractTermEntityRepository::class,
24
        'user' => AbstractUserModelRepository::class,
25
        'comment' => AbstractCommentModelRepositoryInterface::class,
26
    ];
27
28
    protected string $template;
29
30
    public function __construct(
31
        string $model,
32
        string $collection,
33
        string $single,
34
        string $plural,
35
        string $namespace,
36
        string $class,
37
        string $type,
38
        string $template = 'post'
39
    ) {
40
        parent::__construct(
41
            $model,
42
            $collection,
43
            $single,
44
            $plural,
45
            $namespace,
46
            $class,
47
            $template
48
        );
49
50
        $this->type = $type;
51
    }
52
53
    protected function setupClass(PhpNamespace $namespace): object
54
    {
55
        return $namespace
56
            ->addUse($this->model)
57
            ->addUse($this->collection)
58
            ->addUse($this->type)
59
            ->addUse($parent = static::ABSTRACTS[$this->template])
60
            ->addClass($this->class)
61
            ->setExtends($parent)
62
            ->addImplement($this->type);
63
    }
64
65
    protected function finishClass($class): void
66
    {
67
        $data = $class->addMethod('extractData')
68
            ->setVisibility('protected')
69
            ->setReturnType('array')
70
            ->setBody('return [];');
71
72
        $data->addParameter($this->single)->setType($this->model);
73
74
        if (in_array($this->template, ['post', 'attachment'])) {
75
            $tax = $class->addMethod('extractTaxInput')
76
                ->setVisibility('protected')
77
                ->setReturnType('array')
78
                ->setBody('return [];');
79
80
            $tax->addParameter($this->single)->setType($this->model);
81
        }
82
83
        $meta = $class->addMethod('extractMetaInput')
84
            ->setVisibility('protected')
85
            ->setReturnType('array')
86
            ->setBody('return [];');
87
88
        $meta->addParameter($this->single)->setType($this->model);
89
    }
90
}
91