Passed
Push — main ( 2c4e25...c05734 )
by Michiel
08:01
created

EchoTask::main()   B

Complexity

Conditions 11
Paths 90

Size

Total Lines 46
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 15.0623

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 46
ccs 21
cts 31
cp 0.6774
rs 7.3166
cc 11
nc 90
nop 0
crap 15.0623

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
namespace Phing\Task\System;
21
22
use Phing\Exception\BuildException;
23
use Phing\Project;
24
use Phing\Task;
25
use Phing\Type\Element\DirSetAware;
26
use Phing\Type\Element\FileSetAware;
27
28
/**
29
 * Echos a message to the logging system or to a file
30
 *
31
 * @author  Michiel Rook <[email protected]>
32
 * @author  Andreas Aderhold, [email protected]
33
 */
34
class EchoTask extends Task
35
{
36
    use DirSetAware;
37
    use FileSetAware;
38
39
    protected $msg = "";
40
41
    protected $file = "";
42
43
    protected $append = false;
44
45
    protected $level = "info";
46
47 88
    public function main()
48
    {
49 88
        switch ($this->level) {
50 88
            case "error":
51
                $loglevel = Project::MSG_ERR;
52
                break;
53 88
            case "warning":
54
                $loglevel = Project::MSG_WARN;
55
                break;
56 88
            case "verbose":
57
                $loglevel = Project::MSG_VERBOSE;
58
                break;
59 88
            case "debug":
60
                $loglevel = Project::MSG_DEBUG;
61
                break;
62 88
            case "info":
63
            default:
64 88
                $loglevel = Project::MSG_INFO;
65 88
                break;
66
        }
67
68 88
        $this->filesets = array_merge($this->filesets, $this->dirsets);
69
70 88
        if (count($this->filesets)) {
71 4
            if (trim(substr($this->msg, -1)) != '') {
72 1
                $this->msg .= "\n";
73
            }
74 4
            $this->msg .= $this->getFilesetsMsg();
75
        }
76
77 88
        if (empty($this->file)) {
78 74
            $this->log($this->msg, $loglevel);
79
        } else {
80 17
            if ($this->append) {
81
                $handle = @fopen($this->file, "a");
82
            } else {
83 17
                $handle = @fopen($this->file, "w");
84
            }
85
86 17
            if ($handle === false) {
87
                throw new BuildException("Unable to open file {$this->file}");
88
            }
89
90 17
            fwrite($handle, $this->msg);
91
92 17
            fclose($handle);
93
        }
94 88
    }
95
96
    /**
97
     * Merges all filesets into a string to be echoed out
98
     *
99
     * @return string String to echo
100
     */
101 4
    protected function getFilesetsMsg()
102
    {
103 4
        $project = $this->getProject();
104 4
        $msg = '';
105 4
        foreach ($this->filesets as $fs) {
106 4
            $ds = $fs->getDirectoryScanner($project);
107 4
            $fromDir = $fs->getDir($project);
108 4
            $srcDirs = $ds->getIncludedDirectories();
109 4
            $srcFiles = $ds->getIncludedFiles();
110 4
            $msg .= 'Directory: ' . $fromDir . ' => '
111 4
                . realpath($fromDir) . "\n";
112 4
            foreach ($srcDirs as $dir) {
113 4
                $relPath = $fromDir . DIRECTORY_SEPARATOR . $dir;
114 4
                $msg .= $relPath . "\n";
115
            }
116 4
            foreach ($srcFiles as $file) {
117 4
                $relPath = $fromDir . DIRECTORY_SEPARATOR . $file;
118 4
                $msg .= $relPath . "\n";
119
            }
120
        }
121
122 4
        return $msg;
123
    }
124
125 17
    public function setFile(string $file)
126
    {
127 17
        $this->file = $file;
128 17
    }
129
130
    public function setLevel(string $level)
131
    {
132
        $this->level = $level;
133
    }
134
135
    public function setAppend(bool $append)
136
    {
137
        $this->append = $append;
138
    }
139
140 11
    public function setMsg(string $msg)
141
    {
142 11
        $this->setMessage($msg);
143 11
    }
144
145 40
    public function setMessage(string $msg)
146
    {
147 40
        $this->msg = $msg;
148 40
    }
149
150
    /**
151
     * Supporting the <echo>Message</echo> syntax.
152
     */
153 49
    public function addText(string $msg)
154
    {
155 49
        $this->msg = $msg;
156 49
    }
157
}
158