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

Fragment   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 1 Features 0
Metric Value
wmc 13
c 7
b 1
f 0
lcom 1
cbo 1
dl 0
loc 103
ccs 27
cts 27
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A __set_state() 0 7 1
A __debugInfo() 0 4 1
A getComponent() 0 8 3
A getUriComponent() 0 9 2
A modify() 0 12 4
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 625
    public function __construct($data = null)
43
    {
44 625
        if ($data !== null) {
45 382
            $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 382
            $this->init($data);
47 246
        }
48 613
    }
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 2
    public function __debugInfo()
65
    {
66 2
        return ['fragment' => $this->__toString()];
67
    }
68
69
    /**
70
     * Returns the component literal value. The return type can be
71
     * <ul>
72
     * <li> null: If the component is not defined
73
     * <li> string: Otherwise
74
     * </ul>
75
     *
76
     * @return string|int|null
77
     */
78 631
    public function getComponent()
79
    {
80 631
        if (null === $this->data && false === $this->preserveDelimiter) {
81 348
            return null;
82
        }
83
84 370
        return $this->encode($this->data);
85
    }
86
87
    /**
88
     * Returns the instance string representation
89
     * with its optional URI delimiters
90
     *
91
     * @return string
92
     */
93 629
    public function getUriComponent()
94
    {
95 629
        $component = $this->__toString();
96 629
        if ($this->preserveDelimiter) {
97 368
            return FragmentInterface::DELIMITER.$component;
98
        }
99
100 348
        return $component;
101
    }
102
103
    /**
104
     * Returns an instance with the specified string
105
     *
106
     * This method MUST retain the state of the current instance, and return
107
     * an instance that contains the modified data
108
     *
109
     * @param string $value
110
     *
111
     * @return static
112
     */
113 111
    public function modify($value)
114
    {
115 111
        if (null === $value && $value === $this->getComponent()) {
116 93
            return $this;
117
        }
118
119 18
        if ($value === $this->__toString()) {
120 15
            return $this;
121
        }
122
123 3
        return new static($value);
124
    }
125
}
126