Completed
Pull Request — master (#58)
by ignace nyamagana
03:41
created

Fragment::__set_state()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * League.Uri (http://uri.thephpleague.com)
4
 *
5
 * @package   League.uri
6
 * @author    Ignace Nyamagana Butera <[email protected]>
7
 * @copyright 2013-2015 Ignace Nyamagana Butera
8
 * @license   https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License)
9
 * @version   4.2.0
10
 * @link      https://github.com/thephpleague/uri/
11
 */
12
namespace League\Uri\Components;
13
14
use League\Uri\Interfaces\Fragment as FragmentInterface;
15
16
/**
17
 * Value object representing a URI fragment component.
18
 *
19
 * @package League.uri
20
 * @author  Ignace Nyamagana Butera <[email protected]>
21
 * @since   1.0.0
22
 */
23
class Fragment extends AbstractComponent implements FragmentInterface
24
{
25
    /**
26
     * @inheritdoc
27
     */
28
    protected static $reservedCharactersRegex = "\!\$&'\(\)\*\+,;\=\:\/@\?";
29
30
    /**
31
     * Preserve the delimiter
32
     *
33
     * @var string
34
     */
35
    protected $preserveDelimiter = false;
36
37
    /**
38
     * new instance
39
     *
40
     * @param string|null $data the component value
41
     */
42 623
    public function __construct($data = null)
43
    {
44 623
        if ($data !== null) {
45 380
            $this->preserveDelimiter = true;
0 ignored issues
show
Documentation Bug introduced by
The property $preserveDelimiter was declared of type string, but true is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
46 380
            $this->init($data);
47 368
        }
48 611
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 12
    public static function __set_state(array $properties)
54
    {
55 12
        $component = new static($properties['data']);
56 12
        $component->preserveDelimiter = $properties['preserveDelimiter'];
57
58 12
        return $component;
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64 1
    public function __debugInfo()
65
    {
66 1
        return ['fragment' => $this->__toString()];
67
    }
68
69
    /**
70
     * Returns the instance string representation
71
     * with its optional URI delimiters
72
     *
73
     * @return string
74
     */
75 628
    public function getUriComponent()
76
    {
77 628
        $component = $this->__toString();
78 628
        if ($this->preserveDelimiter) {
79 367
            return FragmentInterface::DELIMITER.$component;
80
        }
81
82 345
        return $component;
83
    }
84
85
    /**
86
     * Returns an instance with the specified string
87
     *
88
     * This method MUST retain the state of the current instance, and return
89
     * an instance that contains the modified data
90
     *
91
     * @param string $value
92
     *
93
     * @return static
94
     */
95 111
    public function modify($value)
96
    {
97 111
        if (null === $value && '' == $this->getUriComponent()) {
98 93
            return $this;
99
        }
100
101 18
        if ($value == $this->__toString()) {
102 15
            return $this;
103
        }
104
105 3
        return new static($value);
106
    }
107
}
108