Passed
Push — main ( 28d658...6dddcc )
by Michiel
06:12
created

PHPMDFormatterElement::getRenderer()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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