RouteParameter::getValueOrDefault()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * Fwk
4
 *
5
 * Copyright (c) 2011-2014, Julien Ballestracci <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
12
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
13
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
15
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
16
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
17
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
21
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
 * POSSIBILITY OF SUCH DAMAGE.
23
 *
24
 * PHP Version 5.3
25
 * 
26
 * @category   Core
27
 * @package    Fwk\Core
28
 * @subpackage Components
29
 * @author     Julien Ballestracci <[email protected]>
30
 * @copyright  2011-2014 Julien Ballestracci <[email protected]>
31
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
32
 * @link       http://www.fwk.pw
33
 */
34
namespace Fwk\Core\Components\UrlRewriter;
35
36
/**
37
 * Route Parameter
38
 * 
39
 * @category   Utilities
40
 * @package    Fwk\Core
41
 * @subpackage Components
42
 * @author     Julien Ballestracci <[email protected]>
43
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
44
 * @link       http://www.phpfwk.com
45
 */
46
class RouteParameter
47
{
48
    const DEFAULT_REGEX = '.[^/\.]{0,}';
49
    
50
    /**
51
     *
52
     * @var string 
53
     */
54
    protected $name;
55
56
    /**
57
     *
58
     * @var boolean 
59
     */
60
    protected $required;
61
62
    /**
63
     *
64
     * @var string 
65
     */
66
    protected $regex;
67
68
    /**
69
     *
70
     * @var mixed 
71
     */
72
    protected $default;
73
74
    /**
75
     *
76
     * @var string 
77
     */
78
    protected $value;
79
80
    /**
81
     * 
82
     * @param type $name
83
     * @param type $default
0 ignored issues
show
Documentation introduced by
Should the type for parameter $default not be type|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
84
     * @param type $regex
0 ignored issues
show
Documentation introduced by
Should the type for parameter $regex not be type|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
85
     * @param type $required
0 ignored issues
show
Documentation introduced by
Should the type for parameter $required not be boolean|type?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
86
     * @param type $value
0 ignored issues
show
Documentation introduced by
Should the type for parameter $value not be type|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
87
     * 
88
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
89
     */
90 13
    public function __construct($name, $default = null, 
91
        $regex = null, $required = true, $value = null
92
    ) {
93 13
        $this->name     = (string)$name;
94 13
        $this->default  = $default;
95 13
        $this->required = (bool)$required;
96 13
        $this->regex    = (empty($regex) ? self::DEFAULT_REGEX : $regex);
0 ignored issues
show
Documentation Bug introduced by
It seems like empty($regex) ? self::DEFAULT_REGEX : $regex can also be of type object<Fwk\Core\Components\UrlRewriter\type>. However, the property $regex is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
97 13
        $this->value    = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value can also be of type object<Fwk\Core\Components\UrlRewriter\type>. However, the property $value is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
98 13
    }
99
100 13
    public function getName()
101
    {
102 13
        return $this->name;
103
    }
104
105 7
    public function isRequired()
106
    {
107 7
        return $this->required;
108
    }
109
110 7
    public function getRegex()
111
    {
112 7
        return $this->regex;
113
    }
114
115
    public function getDefault()
116
    {
117
        return $this->default;
118
    }
119
120 4
    public function getValueOrDefault()
121
    {
122 4
        return (!empty($this->value) ? $this->value : $this->default);
123
    }
124
    
125 2
    public function setValue($value)
126
    {
127 2
        $this->value = $value;
128 2
    }
129
    
130 3
    public function getValue()
131
    {
132 3
        return $this->value;
133
    }
134
}
135