PhpClass::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Bigwhoop\Trumpet\CodeParsing\Php;
4
5
use Bigwhoop\Trumpet\Exceptions\OutOfBoundsException;
6
7
final class PhpClass
8
{
9
    use FullyQualifiedNameTrait;
10
    use SourceTrait;
11
12
    /** @var PhpMethod[] */
13
    private $methods = [];
14
15
    /**
16
     * @param string      $name
17
     * @param PhpMethod[] $methods
18
     * @param string      $source
19
     */
20 10
    public function __construct(string $name, array $methods = [], string $source = '')
21
    {
22 10
        $this->name = $name;
23 10
        $this->addMethods($methods);
24 10
        $this->source = $source;
25 10
    }
26
    
27 10
    public function addMethods(array $methods)
28
    {
29 10
        foreach ($methods as $method) {
30
            $this->addMethod($method);
31
        }
32 10
    }
33
    
34 8
    public function addMethod(PhpMethod $method)
35
    {
36 8
        $this->methods[$method->getName()] = $method;
37 8
    }
38
39
    /**
40
     * @param string $name
41
     *
42
     * @return bool
43
     */
44 4
    public function hasMethod($name)
45
    {
46 4
        return array_key_exists($name, $this->methods);
47
    }
48
49
    /**
50
     * @return PhpMethod[]
51
     */
52 1
    public function getMethods()
53
    {
54 1
        return $this->methods;
55
    }
56
57
    /**
58
     * @param string $name
59
     *
60
     * @return PhpMethod
61
     *
62
     * @throws OutOfBoundsException
63
     */
64 3
    public function getMethod($name)
65
    {
66 3
        if (!array_key_exists($name, $this->methods)) {
67
            throw new OutOfBoundsException("Method '$name' is not available.");
68
        }
69
70 3
        return $this->methods[$name];
71
    }
72
}
73