Passed
Push — main ( 49e508...46fa35 )
by Michiel
06:19
created

TextElement::getValue()   B

Complexity

Conditions 11
Paths 16

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 51.7428

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 32
ccs 7
cts 23
cp 0.3043
rs 7.3166
c 0
b 0
f 0
cc 11
nc 16
nop 0
crap 51.7428

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\Append;
21
22
use Phing\Exception\BuildException;
23
use Phing\Io\BufferedReader;
24
use Phing\Io\File;
25
use Phing\Io\FileInputStream;
26
use Phing\Io\FileReader;
27
use Phing\Io\InputStreamReader;
28
use Phing\Io\IOException;
29
use Phing\ProjectComponent;
30
31
/**
32
 * Text element points to a file or contains text.
33
 *
34
 */
35
class TextElement extends ProjectComponent
36
{
37
    public $value = "";
38
    public $trimLeading = false;
39
    public $trim = false;
40
    public $filtering = true;
41
    public $encoding = null;
42
43
    /**
44
     * whether to filter the text in this element
45
     * or not.
46
     *
47
     * @param filtering true if the text should be filtered.
48
     *                  the default value is true.
49
     */
50 1
    public function setFiltering($filtering)
51
    {
52 1
        $this->filtering = $filtering;
53 1
    }
54
55
    /**
56
     * The encoding of the text element
57
     *
58
     * @param string $encoding the name of the charset used to encode
59
     */
60
    public function setEncoding($encoding)
61
    {
62
        $this->encoding = $encoding;
63
    }
64
65
    /**
66
     * set the text using a file
67
     *
68
     * @param  File $file the file to use
69
     * @throws BuildException if the file does not exist, or cannot be
70
     *                        read
71
     */
72 1
    public function setFile(File $file)
73
    {
74
        // non-existing files are not allowed
75 1
        if (!$file->exists()) {
76
            throw new BuildException("File " . $file . " does not exist.");
77
        }
78
79 1
        $reader = null;
80
        try {
81 1
            if ($this->encoding == null) {
82 1
                $reader = new BufferedReader(new FileReader($file));
83
            } else {
84
                $reader = new BufferedReader(
85
                    new InputStreamReader(new FileInputStream($file))
86
                );
87
            }
88 1
            $this->value = $reader->read();
89
        } catch (IOException $ex) {
90
            $reader->close();
91
            throw new BuildException($ex);
92
        }
93 1
        $reader->close();
94 1
    }
95
96
    /**
97
     * set the text using inline
98
     *
99
     * @param string $value the text to place inline
100
     */
101 1
    public function addText($value)
102
    {
103 1
        $this->value .= $this->getProject()->replaceProperties($value);
104 1
    }
105
106
    /**
107
     * s:^\s*:: on each line of input
108
     *
109
     * @param bool $trimLeading if true do the trim
110
     */
111
    public function setTrimLeading($trimLeading)
112
    {
113
        $this->trimLeading = $trimLeading;
114
    }
115
116
    /**
117
     * whether to call text.trim()
118
     *
119
     * @param bool $trim if true trim the text
120
     */
121 1
    public function setTrim($trim)
122
    {
123 1
        $this->trim = $trim;
124 1
    }
125
126
    /**
127
     * @return string the text, after possible trimming
128
     */
129 2
    public function getValue()
130
    {
131 2
        if ($this->value == null) {
132
            $this->value = "";
133
        }
134 2
        if (trim($this->value) === '') {
135
            $this->value = "";
136
        }
137 2
        if ($this->trimLeading) {
138
            $current = str_split($this->value);
139
            $b = '';
140
            $startOfLine = true;
141
            $pos = 0;
142
            while ($pos < count($current)) {
0 ignored issues
show
Bug introduced by
It seems like $current can also be of type true; however, parameter $value of count() does only seem to accept Countable|array, 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

142
            while ($pos < count(/** @scrutinizer ignore-type */ $current)) {
Loading history...
143
                $ch = $current[$pos++];
144
                if ($startOfLine) {
145
                    if ($ch == ' ' || $ch == "\t") {
146
                        continue;
147
                    }
148
                    $startOfLine = false;
149
                }
150
                $b .= $ch;
151
                if ($ch == "\n" || $ch == "\r") {
152
                    $startOfLine = true;
153
                }
154
            }
155
            $this->value = $b;
156
        }
157 2
        if ($this->trim) {
158 1
            $this->value = trim($this->value);
159
        }
160 2
        return $this->value;
161
    }
162
}
163