Completed
Push — master ( faecaf...e43139 )
by Siad
15:28
created

Reference::getProject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
/**
21
 * Class to hold a reference to another object in the project.
22
 *
23
 * @package phing.types
24
 */
25
class Reference
26
{
27
    /**
28
     * @var string $refid
29
     */
30
    protected $refid;
31
32
    /**
33
     * @var Project $project
34
     */
35
    private $project;
36
37
    /**
38
     * @param string $id
39
     */
40 34
    public function __construct(Project $project, $id = null)
41
    {
42 34
        $this->setRefId($id);
43 34
        $this->setProject($project);
44 34
    }
45
46
    /**
47
     * @param $id
48
     */
49 34
    public function setRefId($id)
50
    {
51 34
        $this->refid = (string) $id;
52 34
    }
53
54
    /**
55
     * @return string
56
     */
57 2
    public function getRefId()
58
    {
59 2
        return $this->refid;
60
    }
61
62 34
    public function setProject(Project $project)
63
    {
64 34
        $this->project = $project;
65 34
    }
66
67
    /**
68
     * Get the associated project, if any; may be null.
69
     *
70
     * @return Project the associated project
71
     */
72 1
    public function getProject(): \Project
73
    {
74 1
        return $this->project;
75
    }
76
77
    /**
78
     * returns reference to object in references container of project
79
     *
80
     * @param Project|null $fallback
81
     *
82
     * @return object
83
     */
84 8
    public function getReferencedObject(Project $fallback = null)
85
    {
86 8
        $project = $fallback ?? $this->project;
87
88
        // setRefId casts its argument to a string, so compare strictly against ''
89 8
        if ($this->refid === '') {
90 1
            throw new BuildException("No reference specified");
91
        }
92 7
        $o = $project->getReference($this->refid);
93 7
        if ($o === null) {
94 1
            throw new BuildException("Reference {$this->refid} not found.");
95
        }
96
97 6
        return $o;
98
    }
99
100 1
    public function __toString()
101
    {
102 1
        return $this->refid;
103
    }
104
}
105