AliasMethod::toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4286
cc 1
eloc 8
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the ClassGeneration package.
5
 *
6
 * (c) Antonio Spinelli <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ClassGeneration\Composition;
13
14
use ClassGeneration\Element\AliasInterface;
15
use ClassGeneration\Visibility;
16
17
/**
18
 * Composition Trait Alias Method ClassGeneration
19
 * @author Antonio Spinelli <[email protected]>
20
 */
21
class AliasMethod extends Method implements AliasMethodInterface
22
{
23
24
    /**
25
     * @var string
26
     */
27
    protected $alias;
28
29
    /**
30
     * @var string
31
     */
32
    protected $visibility;
33
34 11
    public function __construct($traitName, $name, $alias, $visibility = null)
35
    {
36 11
        $this->setName($name);
37 11
        $this->setTraitName($traitName);
38 11
        $this->setAlias($alias);
39
40 11
        if (!empty($visibility)) {
41 3
            $this->setVisibility($visibility);
42 3
        }
43 11
        $this->init();
44 11
    }
45
46
    /**
47
     * @inheritdoc
48
     * @return AliasInterface
49
     */
50 11
    public function setAlias($alias)
51
    {
52 11
        $this->alias = (string)$alias;
53 11
        return $this;
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59 5
    public function getAlias()
60
    {
61 5
        return $this->alias;
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67 1
    public function hasAlias()
68
    {
69 1
        return !(empty($this->alias) || is_null($this->alias));
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75 5
    public function getVisibility()
76
    {
77 5
        return $this->visibility;
78
    }
79
80
    /**
81
     * @inheritdoc
82
     * @return AliasMethod
83
     */
84 3
    public function setVisibility($visibility)
85
    {
86 3
        Visibility::isValid($visibility);
87 3
        $this->visibility = $visibility;
88
89 3
        return $this;
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 4
    public function toString()
96
    {
97 4
        $result = sprintf(
98 4
            '%s::%s as%s%s;',
99 4
            $this->getTraitName(),
100 4
            $this->getName(),
101 4
            $this->prependWhiteSpace($this->getVisibility()),
102 4
            $this->prependWhiteSpace($this->getAlias())
103 4
        );
104 4
        return $result . PHP_EOL;
105
    }
106
107
    /**
108
     * Convert visibility to string.
109
     * @param string $text
110
     * @return string
111
     */
112 4
    protected function prependWhiteSpace($text)
113
    {
114 4
        if (is_null($text) || empty($text)) {
115 3
            return '';
116
        }
117 4
        return ' ' . $text;
118
    }
119
}
120