StaticTranslator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Bundle\UrlBundle\Url\Params\Translator;
8
9
use Zicht\Bundle\UrlBundle\Url\Params\Translator;
10
11
/**
12
 * Static translator which holds mappings for keys and values.
13
 */
14
class StaticTranslator implements Translator
15
{
16
    protected $keyName;
17
    protected $keyTranslation;
18
    protected $valueTranslations;
19
20
    /**
21
     * Constructor.
22
     *
23
     * @param string $keyName
24
     * @param string $keyTranslation
25
     * @param array $valueTranslations
26
     */
27 6
    public function __construct($keyName, $keyTranslation, $valueTranslations = array())
28
    {
29 6
        $this->keyName           = $keyName;
30 6
        $this->keyTranslation    = $keyTranslation;
31 6
        $this->valueTranslations = $valueTranslations;
32 6
    }
33
34
35
    /**
36
     * @{inheritDoc}
37
     */
38 5
    public function translateKeyInput($keyTranslation)
39
    {
40 5
        if ($keyTranslation == $this->keyTranslation) {
41 5
            return $this->keyName;
42
        }
43
44 3
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Zicht\Bundle\UrlBundle\U...or::translateKeyInput() of string.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
45
    }
46
47
48
    /**
49
     * @{inheritDoc}
50
     */
51 3
    public function translateValueInput($keyTranslation, $valueTranslation)
52
    {
53 3
        if ($keyTranslation == $this->keyTranslation) {
54 3
            if (false !== ($value = array_search($valueTranslation, $this->valueTranslations))) {
55 3
                return $value;
56
            }
57
        }
58
59 2
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Zicht\Bundle\UrlBundle\U...::translateValueInput() of string.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
60
    }
61
62
63
    /**
64
     * @{inheritDoc}
65
     */
66 5
    public function translateKeyOutput($keyName)
67
    {
68 5
        if ($keyName == $this->keyName) {
69 5
            return $this->keyTranslation;
70
        }
71
72 3
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Zicht\Bundle\UrlBundle\U...r::translateKeyOutput() of string.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
73
    }
74
75
76
    /**
77
     * @{inheritDoc}
78
     */
79 3
    public function translateValueOutput($keyName, $value)
80
    {
81 3
        if ($keyName == $this->keyName) {
82 3
            if (isset($this->valueTranslations[$value])) {
83 3
                return $this->valueTranslations[$value];
84
            }
85
        }
86
87 2
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Zicht\Bundle\UrlBundle\U...:translateValueOutput() of string.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
88
    }
89
90
91
    /**
92
     * Adds a value translation
93
     *
94
     * @param string $in
95
     * @param string $out
96
     * @return self
97
     */
98 1
    public function addTranslation($in, $out)
99
    {
100 1
        $this->valueTranslations[$in] = $out;
101 1
        return $this;
102
    }
103
}
104