Passed
Push — master ( e1f86a...4e1a3a )
by Siad
05:23
created

ReflexiveTask::main()   D

Complexity

Conditions 14
Paths 257

Size

Total Lines 69
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 0
Metric Value
cc 14
eloc 41
nc 257
nop 0
dl 0
loc 69
ccs 0
cts 39
cp 0
crap 210
rs 4.7208
c 0
b 0
f 0

How to fix   Long Method    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\Tasks\System;
21
22
use Phing\Exception\BuildException;
23
use Phing\Io\FileReader;
24
use Phing\Io\FileUtils;
25
use Phing\Io\FileWriter;
26
use Phing\Io\File;
27
use Phing\Project;
28
use Phing\Task;
29
use Phing\Type\Element\FileSetAware;
30
use Phing\Type\Element\FilterChainAware;
31
32
/**
33
 * This task is for using filter chains to make changes to files and overwrite the original files.
34
 *
35
 * This task was created to serve the need for "cleanup" tasks -- e.g. a ReplaceRegexp task or strip task
36
 * being used to modify files and then overwrite the modified files.  In many (most?) cases you probably
37
 * should just use a copy task  to preserve the original source files, but this task supports situations
38
 * where there is no src vs. build directory, and modifying source files is actually desired.
39
 *
40
 * <code>
41
 *    <reflexive>
42
 *        <fileset dir=".">
43
 *            <include pattern="*.html">
44
 *        </fileset>
45
 *        <filterchain>
46
 *            <replaceregexp>
47
 *                <regexp pattern="\n\r" replace="\n"/>
48
 *            </replaceregexp>
49
 *        </filterchain>
50
 *    </reflexive>
51
 * </code>
52
 *
53
 * @author Hans Lellelid <[email protected]>
54
 *
55
 * @package phing.tasks.system
56
 */
57
class ReflexiveTask extends Task
58
{
59
    use FileSetAware;
60
    use FilterChainAware;
61
62
    /**
63
     * Single file to process.
64
     */
65
    private $file;
66
67
    /**
68
     * Alias for setFrom()
69
     *
70
     * @param File $f
71
     */
72
    public function setFile(File $f)
73
    {
74
        $this->file = $f;
75
    }
76
77
    /**
78
     * Append the file(s).
79
     */
80
    public function main()
81
    {
82
        if ($this->file === null && empty($this->filesets)) {
83
            throw new BuildException("You must specify a file or fileset(s) for the <reflexive> task.");
84
        }
85
86
        // compile a list of all files to modify, both file attrib and fileset elements
87
        // can be used.
88
89
        $files = [];
90
91
        if ($this->file !== null) {
92
            $files[] = $this->file;
93
        }
94
95
        if (!empty($this->filesets)) {
96
            foreach ($this->filesets as $fs) {
97
                try {
98
                    $ds = $fs->getDirectoryScanner($this->project);
99
                    $filenames = $ds->getIncludedFiles(); // get included filenames
100
                    $dir = $fs->getDir($this->project);
101
                    foreach ($filenames as $fname) {
102
                        $files[] = new File($dir, $fname);
103
                    }
104
                } catch (BuildException $be) {
105
                    $this->log($be->getMessage(), Project::MSG_WARN);
106
                }
107
            }
108
        }
109
110
        $this->log("Applying reflexive processing to " . count($files) . " files.");
111
112
        // These "slots" allow filters to retrieve information about the currently-being-process files
113
        $slot = $this->getRegisterSlot("currentFile");
114
        $basenameSlot = $this->getRegisterSlot("currentFile.basename");
115
116
        foreach ($files as $file) {
117
            // set the register slots
118
119
            $slot->setValue($file->getPath());
120
            $basenameSlot->setValue($file->getName());
121
122
            // 1) read contents of file, pulling through any filters
123
            $in = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $in is dead and can be removed.
Loading history...
124
            try {
125
                $contents = "";
126
                $in = FileUtils::getChainedReader(new FileReader($file), $this->filterChains, $this->project);
127
                while (-1 !== ($buffer = $in->read())) {
128
                    $contents .= $buffer;
129
                }
130
                $in->close();
131
            } catch (\Exception $e) {
132
                if ($in) {
133
                    $in->close();
134
                }
135
                $this->log("Erorr reading file: " . $e->getMessage(), Project::MSG_WARN);
136
            }
137
138
            try {
139
                // now create a FileWriter w/ the same file, and write to the file
140
                $out = new FileWriter($file);
141
                $out->write($contents);
142
                $out->close();
143
                $this->log("Applying reflexive processing to " . $file->getPath(), Project::MSG_VERBOSE);
144
            } catch (\Exception $e) {
145
                if ($out) {
146
                    $out->close();
147
                }
148
                $this->log("Error writing file back: " . $e->getMessage(), Project::MSG_WARN);
149
            }
150
        }
151
    }
152
}
153