Passed
Push — master ( d62b89...abb8c8 )
by Michiel
06:34
created

PHPMDFormatterElement::getOutfile()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
use Phing\Exception\BuildException;
21
use Phing\Io\File;
22
use Phing\Util\StringHelper;
23
use PHPMD\AbstractRenderer;
24
use PHPMD\Renderer\HTMLRenderer;
25
use PHPMD\Renderer\TextRenderer;
26
use PHPMD\Renderer\XMLRenderer;
27
use PHPMD\Writer\StreamWriter;
28
29
/**
30
 * A wrapper for the implementations of PHPMDResultFormatter.
31
 *
32
 * @package phing.tasks.ext.phpmd
33
 * @author  Benjamin Schultz <[email protected]>
34
 * @since   2.4.1
35
 */
36
class PHPMDFormatterElement
37
{
38
    /**
39
     * The type of the formatter.
40
     *
41
     * @var string
42
     */
43
    protected $type = "";
44
45
    /**
46
     * @var string
47
     */
48
    protected $className = "";
49
50
    /**
51
     * Whether to use file (or write output to phing log).
52
     *
53
     * @var boolean
54
     */
55
    protected $useFile = true;
56
57
    /**
58
     * Output file for formatter.
59
     *
60
     * @var File
61
     */
62
    protected $outfile = null;
63
64
    /**
65
     * Sets the formatter type.
66
     *
67
     * @param string $type Type of the formatter
68
     *
69
     * @throws BuildException
70
     */
71 3
    public function setType($type)
72
    {
73 3
        $this->type = $type;
74 3
        switch ($this->type) {
75 3
            case 'xml':
76 1
                $this->className = XMLRenderer::class;
77 1
                break;
78
79 2
            case 'html':
80 1
                $this->className = HTMLRenderer::class;
81 1
                break;
82
83 1
            case 'text':
84 1
                $this->className = TextRenderer::class;
85 1
                break;
86
87
            default:
88
                throw new BuildException('Formatter "' . $this->type . '" not implemented');
89
        }
90 3
    }
91
92
    /**
93
     * Get the formatter type
94
     *
95
     * @return string
96
     */
97 3
    public function getType()
98
    {
99 3
        return $this->type;
100
    }
101
102
    /**
103
     * Set whether to write formatter results to file or not.
104
     *
105
     * @param boolean $useFile True or false.
106
     */
107
    public function setUseFile($useFile)
108
    {
109
        $this->useFile = StringHelper::booleanValue($useFile);
110
    }
111
112
    /**
113
     * Return whether to write formatter results to file or not.
114
     *
115
     * @return boolean
116
     */
117 3
    public function getUseFile()
118
    {
119 3
        return $this->useFile;
120
    }
121
122
    /**
123
     * Sets the output file for the formatter results.
124
     *
125
     * @param File $outfile The output file
126
     */
127 3
    public function setOutfile(File $outfile)
128
    {
129 3
        $this->outfile = $outfile;
130 3
    }
131
132
    /**
133
     * Get the output file.
134
     *
135
     * @return File
136
     */
137 3
    public function getOutfile()
138
    {
139 3
        return $this->outfile;
140
    }
141
142
    /**
143
     * Creates a report renderer instance based on the formatter type.
144
     *
145
     * @return AbstractRenderer
146
     * @throws BuildException           When the specified renderer does not exist.
147
     */
148 3
    public function getRenderer()
149
    {
150 3
        $renderer = new $this->className();
151
152
        // Create a report stream
153 3
        if ($this->getUseFile() === false || $this->getOutfile() === null) {
154
            $stream = STDOUT;
155
        } else {
156 3
            $stream = fopen($this->getOutfile()->getAbsoluteFile(), 'wb');
157
        }
158
159 3
        $renderer->setWriter(new StreamWriter($stream));
160
161 3
        return $renderer;
162
    }
163
}
164