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

ModelQueryAsChildPrinter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

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\AbstractClassPrinter;
6
use Leonidas\Contracts\System\Schema\Comment\CommentConverterInterface;
7
use Leonidas\Contracts\System\Schema\Post\PostConverterInterface;
8
use Leonidas\Contracts\System\Schema\Term\TermConverterInterface;
9
use Leonidas\Contracts\System\Schema\User\UserConverterInterface;
10
use Leonidas\Library\System\Model\Abstracts\Post\PoweredByModelQueryKernelTrait;
11
use Leonidas\Library\System\Model\Abstracts\Post\ValidatesPostTypeTrait;
12
use Leonidas\Library\System\Model\Abstracts\Term\ValidatesTaxonomyTrait;
13
use Leonidas\Library\System\Model\Abstracts\User\ValidatesRoleTrait;
14
use Nette\PhpGenerator\PhpNamespace;
15
use WP_Comment_Query;
0 ignored issues
show
Bug introduced by
The type WP_Comment_Query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use WP_Query;
0 ignored issues
show
Bug introduced by
The type WP_Query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use WP_Term_Query;
0 ignored issues
show
Bug introduced by
The type WP_Term_Query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use WP_User_Query;
0 ignored issues
show
Bug introduced by
The type WP_User_Query was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
class ModelQueryAsChildPrinter extends AbstractClassPrinter
21
{
22
    public const CORE = 'kernel';
23
24
    public const QUERIES = [
25
        'post' => WP_Query::class,
26
        'post:h' => WP_Query::class,
27
        'attachment' => WP_Query::class,
28
        // 'term' => WP_Term_Query::class,
29
        // 'term:h' => WP_Term_Query::class,
30
        // 'user' => WP_User_Query::class,
31
        // 'comment' => WP_Comment_Query::class,
32
    ];
33
34
    public const ENGINES = [
35
        'post' => PoweredByModelQueryKernelTrait::class,
36
        'post:h' => PoweredByModelQueryKernelTrait::class,
37
        'attachment' => PoweredByModelQueryKernelTrait::class,
38
    ];
39
40
    public const CONVERTERS = [
41
        'post' => PostConverterInterface::class,
42
        'post:h' => PostConverterInterface::class,
43
        'attachment' => PostConverterInterface::class,
44
        // 'term' => TermConverterInterface::class,
45
        // 'term:h' => TermConverterInterface::class,
46
        // 'user' => UserConverterInterface::class,
47
        // 'comment' => CommentConverterInterface::class,
48
    ];
49
50
    public const VALIDATORS = [
51
        'post' => ValidatesPostTypeTrait::class,
52
        'post:h' => ValidatesPostTypeTrait::class,
53
        'attachment' => ValidatesPostTypeTrait::class,
54
        // 'term' => ValidatesTaxonomyTrait::class,
55
        // 'term:h' => ValidatesTaxonomyTrait::class,
56
        // 'user' => ValidatesRoleTrait::class,
57
    ];
58
59
    public const ASSERTIONS = [
60
        'post' => '$this->assertPostTypeOnQuery($query, ?);',
61
        'post:h' => '$this->assertPostTypeOnQuery($query, ?);',
62
        'attachment' => '$this->assertPostTypeOnQuery($query, ?);',
63
        // 'term' => '$this->assertTaxonomyOnQuery($query, ?);',
64
        // 'term:h' => '$this->assertTaxonomyOnQuery($query, ?);',
65
        // 'user' => '$this->assertRoleOnQuery($query, ?);',
66
        // 'comment' => '$this->assertCommentTypeOnQuery($query, ?);',
67
    ];
68
69
    protected string $model;
70
71
    protected string $single;
72
73
    protected string $plural;
74
75
    protected string $type;
76
77
    protected string $parent;
78
79
    protected string $entity;
80
81
    protected string $template;
82
83
    public function __construct(
84
        string $parent,
85
        string $model,
86
        string $single,
87
        string $plural,
88
        string $namespace,
89
        string $class,
90
        string $type,
91
        string $entity,
92
        string $template = 'post'
93
    ) {
94
        parent::__construct($namespace, $class);
95
96
        $this->parent = $parent;
97
        $this->model = $model;
98
        $this->single = $single;
99
        $this->plural = $plural;
100
        $this->type = $type;
101
        $this->entity = $entity;
102
        $this->template = $template;
103
    }
104
105
    protected function setupClass(PhpNamespace $namespace): object
106
    {
107
        $engine = static::ENGINES[$this->template];
108
        $converter = static::CONVERTERS[$this->template];
109
        $query = static::QUERIES[$this->template];
110
        $validator = static::VALIDATORS[$this->template];
111
        $assertion = static::ASSERTIONS[$this->template];
112
113
        $namespace
114
            ->addUse($this->type)
115
            ->addUse($this->parent)
116
            ->addUse($validator)
117
            ->addUse($engine)
118
            ->addUse($converter)
119
            ->addUse($query);
120
121
        $class = $namespace->addClass($this->class)
122
            ->setExtends($this->parent)
123
            ->addImplement($this->type);
124
125
        $class->addTrait($validator);
126
        $class->addTrait($engine);
127
128
        $constructor = $class->addMethod('__construct');
129
        $constraint = $this->template === 'attachment' ? 'attachment' : $this->entity;
130
131
        $constructor->addParameter('query')->setType($query);
132
        $constructor->addParameter('converter')->setType($converter);
133
        $constructor->addBody($assertion, [$constraint]);
134
        $constructor->addBody('$this->initKernel($query, $converter);');
135
136
        return $class;
137
    }
138
}
139