Passed
Push — main ( 95b369...5c384e )
by Michiel
17:35 queued 12s
created

SvnInfoTask::getElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing\Tasks\Ext;
22
23
use Phing\Exception\BuildException;
24
25
/**
26
 * Parses the output of 'svn info --xml' and
27
 *
28
 * @author Michiel Rook <[email protected]>
29
 *
30
 * @package phing.tasks.ext.svn
31
 *
32
 * @see   VersionControl_SVN
33
 * @since 2.4.9
34
 */
35
class SvnInfoTask extends SvnBaseTask
36
{
37
    private $propertyName = "svn.info";
38
39
    private $element = 'url';
40
    private $subElement = null;
41
42
    /**
43
     * Sets the name of the property to use
44
     *
45
     * @param $propertyName
46
     */
47
    public function setPropertyName($propertyName)
48
    {
49
        $this->propertyName = $propertyName;
50
    }
51
52
    /**
53
     * Returns the name of the property to use
54
     */
55
    public function getPropertyName()
56
    {
57
        return $this->propertyName;
58
    }
59
60
    /**
61
     * Sets the name of the xml element to use.
62
     *
63
     * @param string $element
64
     *
65
     * @return void
66
     */
67
    public function setElement($element)
68
    {
69
        $this->element = $element;
70
    }
71
72
    /**
73
     * Returns the name of the xml element to use.
74
     *
75
     * @return string
76
     */
77
    public function getElement()
78
    {
79
        return $this->element;
80
    }
81
82
    /**
83
     * Sets the name of the xml sub element to use.
84
     *
85
     * @param $subElement
86
     *
87
     * @return void
88
     */
89
    public function setSubElement($subElement)
90
    {
91
        $this->subElement = $subElement;
92
    }
93
94
    /**
95
     * Returns the name of the xml sub element to use.
96
     *
97
     * @return string
98
     */
99
    public function getSubElement()
100
    {
101
        return $this->subElement;
102
    }
103
104
    /**
105
     * The main entry point.
106
     *
107
     * @return void
108
     *
109
     * @throws BuildException
110
     */
111
    public function main()
112
    {
113
        $this->setup('info');
114
115
        if ($this->oldVersion) {
116
            $output = $this->run(['--xml', '--incremental']);
117
118
            if (!($xmlObj = @simplexml_load_string($output))) {
0 ignored issues
show
Bug introduced by
$output of type array is incompatible with the type string expected by parameter $data of simplexml_load_string(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
            if (!($xmlObj = @simplexml_load_string(/** @scrutinizer ignore-type */ $output))) {
Loading history...
119
                throw new BuildException("Failed to parse the output of 'svn info --xml'.");
120
            }
121
122
            $object = $xmlObj->{$this->element};
123
124
            if (!empty($this->subElement)) {
125
                $object = $object->{$this->subElement};
126
            }
127
        } else {
128
            $output = $this->run();
129
130
            if (empty($output) || !isset($output['entry'][0])) {
131
                throw new BuildException("Failed to parse the output of 'svn info'.");
132
            }
133
134
            $object = $output['entry'][0][$this->element];
135
136
            if (!empty($this->subElement)) {
137
                $object = $object[$this->subElement];
138
            }
139
        }
140
141
        $this->project->setProperty($this->getPropertyName(), (string) $object);
142
    }
143
}
144