PhpMethod::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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

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
declare(strict_types=1);
4
5
namespace WsdlToPhp\PhpGenerator\Element;
6
7
class PhpMethod extends PhpFunction implements AccessRestrictedElementInterface
8
{
9
    use AccessRestrictedElementTrait;
10
11
    protected bool $final;
12
13
    protected bool $static;
14
15
    protected bool $abstract;
16
17
    protected bool $hasBody;
18
19
    /**
20
     * @param PhpFunctionParameter[]|string[] $parameters
21
     */
22 72
    public function __construct(string $name, array $parameters = [], ?string $returnType = null, string $access = self::ACCESS_PUBLIC, bool $abstract = false, bool $static = false, bool $final = false, bool $hasBody = true)
23
    {
24 72
        parent::__construct($name, $parameters, $returnType);
25
        $this
26 72
            ->setAbstract($abstract)
27 72
            ->setStatic($static)
28 72
            ->setFinal($final)
29 72
            ->setHasBody($hasBody)
30 72
            ->setAccess($access)
31
        ;
32 72
    }
33
34 72
    public function setAbstract(bool $abstract): self
35
    {
36 72
        $this->abstract = $abstract;
37
38 72
        return $this;
39
    }
40
41 54
    public function getAbstract(): bool
42
    {
43 54
        return $this->abstract;
44
    }
45
46 72
    public function setFinal(bool $final): self
47
    {
48 72
        $this->final = $final;
49
50 72
        return $this;
51
    }
52
53 54
    public function getFinal(): bool
54
    {
55 54
        return $this->final;
56
    }
57
58 72
    public function setStatic(bool $static): self
59
    {
60 72
        $this->static = $static;
61
62 72
        return $this;
63
    }
64
65 54
    public function getStatic(): bool
66
    {
67 54
        return $this->static;
68
    }
69
70 72
    public function setHasBody(bool $hasBody): self
71
    {
72 72
        $this->hasBody = $hasBody;
73
74 72
        return $this;
75
    }
76
77 62
    public function getHasBody(): bool
78
    {
79 62
        return $this->hasBody;
80
    }
81
82 54
    public function getPhpDeclaration(): string
83
    {
84 54
        return sprintf(
85 54
            '%s%s%s%sfunction %s(%s)%s%s',
86 54
            $this->getPhpFinal(),
87 54
            $this->getPhpAbstract(),
88 54
            $this->getPhpAccess(),
89 54
            $this->getPhpStatic(),
90 54
            $this->getPhpName(),
91 54
            $this->getPhpParameters(),
92 54
            $this->getPhpReturnType(),
93 54
            $this->getPhpDeclarationEnd()
94
        );
95
    }
96
97
    /**
98
     * Allows to generate content before children content is generated.
99
     */
100 10
    public function getLineBeforeChildren(?int $indentation = null): string
101
    {
102 10
        if ($this->getHasBody()) {
103 2
            return parent::getLineBeforeChildren($indentation);
104
        }
105
106 8
        return '';
107
    }
108
109
    /**
110
     * Allows to generate content after children content is generated.
111
     */
112 10
    public function getLineAfterChildren(int $indentation = null): string
113
    {
114 10
        if ($this->getHasBody()) {
115 2
            return parent::getLineAfterChildren($indentation);
116
        }
117
118 8
        return '';
119
    }
120
121 40
    public function getChildren(): array
122
    {
123 40
        if ($this->getHasBody()) {
124 32
            return parent::getChildren();
125
        }
126
127 8
        return [];
128
    }
129
130
    /**
131
     * Allows to indicate that children are contained by brackets,
132
     * in the case the method returns true, getBracketBeforeChildren
133
     * is called instead of getLineBeforeChildren and getBracketAfterChildren
134
     * is called instead of getLineAfterChildren, but be aware that these methods
135
     * call the two others.
136
     */
137 36
    public function useBracketsForChildren(): bool
138
    {
139 36
        return $this->getHasBody();
140
    }
141
142 54
    protected function getPhpAbstract(): string
143
    {
144 54
        return $this->getAbstract() ? 'abstract ' : '';
145
    }
146
147 54
    protected function getPhpFinal(): string
148
    {
149 54
        return $this->getFinal() ? 'final ' : '';
150
    }
151
152 54
    protected function getPhpStatic(): string
153
    {
154 54
        return $this->getStatic() ? 'static ' : '';
155
    }
156
157 54
    protected function getPhpReturnType(): string
158
    {
159 54
        return $this->getReturnType() ? sprintf(': %s', $this->getReturnType()) : '';
160
    }
161
162 54
    protected function getPhpDeclarationEnd(): string
163
    {
164 54
        return (!$this->getHasBody() || $this->getAbstract()) ? ';' : '';
165
    }
166
}
167