Passed
Push — master ( afa2e3...eb32d2 )
by Siad
18:11
created

EchoTask::setLevel()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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
 * Echos a message to the logging system or to a file
22
 *
23
 * @author  Michiel Rook <[email protected]>
24
 * @author  Andreas Aderhold, [email protected]
25
 * @package phing.tasks.system
26
 */
27
class EchoTask extends Task
28
{
29
    use DirSetAware;
30
    use FileSetAware;
31
32
    protected $msg = "";
33
34
    protected $file = "";
35
36
    protected $append = false;
37
38
    protected $level = "info";
39
40 85
    public function main()
41
    {
42 85
        switch ($this->level) {
43 85
            case "error":
44
                $loglevel = Project::MSG_ERR;
45
                break;
46 85
            case "warning":
47
                $loglevel = Project::MSG_WARN;
48
                break;
49 85
            case "verbose":
50
                $loglevel = Project::MSG_VERBOSE;
51
                break;
52 85
            case "debug":
53
                $loglevel = Project::MSG_DEBUG;
54
                break;
55 85
            case "info":
56
            default:
57 85
                $loglevel = Project::MSG_INFO;
58 85
                break;
59
        }
60
61 85
        $this->filesets = array_merge($this->filesets, $this->dirsets);
62
63 85
        if (count($this->filesets)) {
64 4
            if (trim(substr($this->msg, -1)) != '') {
65 1
                $this->msg .= "\n";
66
            }
67 4
            $this->msg .= $this->getFilesetsMsg();
68
        }
69
70 85
        if (empty($this->file)) {
71 72
            $this->log($this->msg, $loglevel);
72
        } else {
73 16
            if ($this->append) {
74
                $handle = fopen($this->file, "a");
75
            } else {
76 16
                $handle = fopen($this->file, "w");
77
            }
78
79 16
            fwrite($handle, $this->msg);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

79
            fwrite(/** @scrutinizer ignore-type */ $handle, $this->msg);
Loading history...
80
81 16
            fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

81
            fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
82
        }
83 85
    }
84
85
    /**
86
     * Merges all filesets into a string to be echoed out
87
     *
88
     * @return string String to echo
89
     */
90 4
    protected function getFilesetsMsg()
91
    {
92 4
        $project = $this->getProject();
93 4
        $msg = '';
94 4
        foreach ($this->filesets as $fs) {
95 4
            $ds = $fs->getDirectoryScanner($project);
96 4
            $fromDir = $fs->getDir($project);
97 4
            $srcDirs = $ds->getIncludedDirectories();
98 4
            $srcFiles = $ds->getIncludedFiles();
99 4
            $msg .= 'Directory: ' . $fromDir . ' => '
100 4
                . realpath($fromDir) . "\n";
101 4
            foreach ($srcDirs as $dir) {
102 4
                $relPath = $fromDir . DIRECTORY_SEPARATOR . $dir;
103 4
                $msg .= $relPath . "\n";
104
            }
105 4
            foreach ($srcFiles as $file) {
106 4
                $relPath = $fromDir . DIRECTORY_SEPARATOR . $file;
107 4
                $msg .= $relPath . "\n";
108
            }
109
        }
110
111 4
        return $msg;
112
    }
113
114
    /**
115
     * setter for file
116
     *
117
     * @param $file
118
     */
119 16
    public function setFile(string $file)
120
    {
121 16
        $this->file = $file;
122 16
    }
123
124
    /**
125
     * setter for level
126
     *
127
     * @param $level
128
     */
129
    public function setLevel(string $level)
130
    {
131
        $this->level = $level;
132
    }
133
134
    /**
135
     * setter for append
136
     *
137
     * @param $append
138
     */
139
    public function setAppend(bool $append)
140
    {
141
        $this->append = $append;
142
    }
143
144
    /**
145
     * setter for message
146
     *
147
     * @param $msg
148
     */
149 10
    public function setMsg(string $msg)
150
    {
151 10
        $this->setMessage($msg);
152 10
    }
153
154
    /**
155
     * alias setter
156
     *
157
     * @param $msg
158
     */
159 39
    public function setMessage(string $msg)
160
    {
161 39
        $this->msg = $msg;
162 39
    }
163
164
    /**
165
     * Supporting the <echo>Message</echo> syntax.
166
     *
167
     * @param $msg
168
     */
169 47
    public function addText(string $msg)
170
    {
171 47
        $this->msg = $msg;
172 47
    }
173
}
174