Completed
Push — min-php71 ( f5a25a )
by James
05:53
created

PropertyFormatter::formatPropertyValue()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 20
ccs 0
cts 12
cp 0
rs 8.2222
c 1
b 0
f 0
cc 7
eloc 14
nc 5
nop 2
crap 56
1
<?php
2
declare(strict_types=1);
3
4
namespace BrowscapPHP\Data;
5
6
final class PropertyFormatter
7
{
8
    /**
9
     * @var PropertyHolder
10
     */
11
    private $propertyHolder;
12
13
    /**
14
     * class constructor
15
     *
16
     * @param PropertyHolder $propertyHolder
17
     */
18
    public function __construct(PropertyHolder $propertyHolder)
19
    {
20
        $this->propertyHolder = $propertyHolder;
21
    }
22
23
    /**
24
     * formats the name of a property
25
     *
26
     * @param string|bool $value
27
     * @param string $property
28
     *
29
     * @return string|bool
30
     * @throws \Exception
31
     */
32
    public function formatPropertyValue($value, string $property)
33
    {
34
        switch ($this->propertyHolder->getPropertyType($property)) {
35
            case PropertyHolder::TYPE_BOOLEAN:
36
                if (true === $value || $value === 'true' || $value === '1') {
37
                    return true;
38
                }
39
                return false;
40
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
41
            case PropertyHolder::TYPE_IN_ARRAY:
42
                try {
43
                    return $this->propertyHolder->checkValueInArray($property, $value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 32 can also be of type boolean; however, BrowscapPHP\Data\Propert...er::checkValueInArray() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
44
                } catch (\InvalidArgumentException $ex) {
45
                    return '';
46
                }
47
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
48
        }
49
50
        return $value;
51
    }
52
}
53