Passed
Pull Request — master (#7)
by Luis
20:14 queued 05:16
created

Method::public()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 2
b 0
f 0
nc 1
nop 3
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 7.2
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
20
/**
21
 * It represents a class or interface method
22
 */
23
final class Method implements HasVisibility, CanBeAbstract, CanBeStatic
24
{
25
    use WithVisibility;
26
    use WithAbstractModifier;
27
    use WithStaticModifier;
28
29
    /** @var string */
30
    private $name;
31
32
    /** @var Parameter[] */
33
    private $parameters;
34
35
    /** @var TypeDeclaration */
36 156
    private $returnType;
37
38
    /**
39
     * It is used by the `ClassDefinition` to extract the parameters of a constructor
40
     *
41 156
     * @see \PhUml\Code\ClassDefinition::hasConstructor()
42
     * @see \PhUml\Code\ClassDefinition::constructorParameters()
43
     */
44
    public function isConstructor(): bool
45 24
    {
46
        return $this->name === '__construct';
47
    }
48
49
    /** @return Parameter[] */
50 24
    public function parameters(): array
51
    {
52
        return $this->parameters;
53
    }
54 66
55
    public function __toString()
56
    {
57
        return sprintf(
58
            '%s%s%s%s',
59 66
            $this->modifier,
60
            $this->name,
61
            count($this->parameters) === 0 ? '()' : '(' . implode(', ', $this->parameters) . ')',
62
            $this->returnType->isPresent() ? ": {$this->returnType}" : ''
63
        );
64
    }
65
66
    /** @param Parameter[] $parameters */
67
    public function __construct(
68 60
        string $name,
69
        Visibility $modifier,
70 60
        TypeDeclaration $returnType,
71
        array $parameters = [],
72
        bool $isAbstract = false,
73
        bool $isStatic = false
74 60
    ) {
75
        $this->name = $name;
76 60
        $this->modifier = $modifier;
77
        $this->parameters = $parameters;
78
        $this->isAbstract = $isAbstract;
79 60
        $this->isStatic = $isStatic;
80
        $this->returnType = $returnType;
81 60
    }
82
}
83