WhiteSpaceTrait::fixWhitespace()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
namespace AlgoWeb\xsdTypes\Facets;
3
4
trait WhiteSpaceTrait
5
{
6
    /**
7
     * @Exclude
8
     * @var string specifies how whitespace (line feeds, tabs, spaces, and carriage returns) is handled
9
     */
10
    private $whiteSpace = 'preserve';
11
12
    /**
13
     * Changes the input value in accordance with the defined white space handler.
14
     * @param  string $val
15
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
16
     */
17
    protected function fixWhitespace($val)
18
    {
19
        switch ($this->whiteSpace) {
20
            case 'preserve':
21
                return $val;
22
            case 'replace':
23
                $val = str_replace("\r\n", "\n", $val);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $val. This often makes code more readable.
Loading history...
24
                return preg_replace('/\s/', ' ', $val);
25
            case 'collapse':
26
                return trim(preg_replace('/\s+/', ' ', $val));
27
        }
28
    }
29
30
    /**
31
     * @param string $value
32
     */
33
    protected function setWhiteSpaceFacet($value)
34
    {
35
        if (!in_array($value, ['preserve', 'replace', 'collapse'])) {
36
            throw new \InvalidArgumentException('Invalid whitespace handling method ' . get_class($this));
37
        }
38
        $this->whiteSpace = $value;
39
    }
40
}
41