Passed
Pull Request — master (#22)
by Luis
24:40 queued 21:48
created

Method   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 49
ccs 17
cts 17
cp 1
rs 10
c 2
b 0
f 0
wmc 6
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 8.1
4
 *
5
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
6
 */
7
8
namespace PhUml\Code\Methods;
9
10
use PhUml\Code\Modifiers\CanBeAbstract;
11
use PhUml\Code\Modifiers\CanBeStatic;
12
use PhUml\Code\Modifiers\HasVisibility;
13
use PhUml\Code\Modifiers\Visibility;
14
use PhUml\Code\Modifiers\WithAbstractModifier;
15
use PhUml\Code\Modifiers\WithStaticModifier;
16
use PhUml\Code\Modifiers\WithVisibility;
17
use PhUml\Code\Parameters\Parameter;
18
use PhUml\Code\Variables\TypeDeclaration;
19
use Stringable;
20
21
/**
22
 * It represents a class or interface method
23
 */
24
final class Method implements HasVisibility, CanBeAbstract, CanBeStatic, Stringable
25
{
26
    use WithVisibility;
27
    use WithAbstractModifier;
28
    use WithStaticModifier;
29
30
    /** @param Parameter[] $parameters */
31 50
    public function __construct(
32
        private readonly string $name,
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 32 at column 25
Loading history...
33
        Visibility $modifier,
34
        private readonly TypeDeclaration $returnType,
35
        private readonly array $parameters = [],
36
        bool $isAbstract = false,
37
        bool $isStatic = false
38
    ) {
39 50
        $this->modifier = $modifier;
40 50
        $this->isAbstract = $isAbstract;
41 50
        $this->isStatic = $isStatic;
42
    }
43
44
    /**
45
     * It is used by the `ClassDefinition` to extract the parameters of a constructor
46
     *
47
     * @see \PhUml\Code\ClassDefinition::hasConstructor()
48
     * @see \PhUml\Code\ClassDefinition::constructorParameters()
49
     */
50 15
    public function isConstructor(): bool
51
    {
52 15
        return $this->name === '__construct';
53
    }
54
55
    /** @return Parameter[] */
56 16
    public function parameters(): array
57
    {
58 16
        return $this->parameters;
59
    }
60
61 15
    public function __toString(): string
62
    {
63 15
        return sprintf(
64
            '%s%s%s%s',
65 15
            $this->modifier,
66 15
            $this->name,
67 15
            $this->parameters === [] ? '()' : '(' . implode(', ', $this->parameters) . ')',
68 15
            $this->returnType->isPresent() ? ": {$this->returnType}" : ''
69
        );
70
    }
71
}
72