GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TypeCast   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 87.8%

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 36
cts 41
cp 0.878
rs 10
c 0
b 0
f 0
wmc 18
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
F cast() 0 49 18
1
<?php
2
3
namespace PhpBoot\Utils;
4
5
class TypeCast
6
{
7
    /**
8
     * @param mixed $val
9
     * @param string $type
10
     * @param bool $validate
11
     * @return mixed
12
     */
13 9
    static public function cast($val, $type, $validate = true)
14
    {
15 9
        TypeHint::isScalarType($type) or \PhpBoot\abort(new \InvalidArgumentException("$type is not scalar type"));
16
17 9
        if(is_bool($val)){
18 2
            $val = intval($val);
19 9
        }else if($val === null){
20
            $map = [
21 1
                'string'=>'',
22 1
                'bool'=>false,
23 1
                'int'=>0,
24 1
                'float'=>0,
25 1
            ];
26 1
            if(isset($map[$type])){
27 1
                $val = $map[$type];
28 1
            }
29 1
        }
30 9
        if(is_object($val)){
31
            try{
32 1
                $val = (string)$val;
33 1
            }catch (\Exception $e){
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) { ...sName} to {$type}")); } 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...
34 1
                $className = get_class($val);
35 1
                \PhpBoot\abort(new \InvalidArgumentException("could not cast value from class $className to {$type}"));
36
            }
37
38
        }
39 9
        if(is_array($val)){
40 1
            $type == 'array' ||  $type =='mixed' || !$type or \PhpBoot\abort(new \InvalidArgumentException("could not cast value from resource to {$type}"));
41
        }
42 9
        if(is_resource($val)) {
43
            \PhpBoot\abort(new \InvalidArgumentException("could not cast value from resource to {$type}"));
44
        }
45 9
        if(!$validate){
46 2
            settype($val, $type) or \PhpBoot\abort(new \InvalidArgumentException("cast value($val) to {$type} failed"));
47 2
        }else{
48 8
            $ori = $val;
49 8
            $oriType = gettype($val);
50 8
            settype($val, $type) or \PhpBoot\abort(new \InvalidArgumentException("cast value($ori) to type {$type} failed"));
51 8
            $newData = $val;
52 8
            if(is_bool($newData)){
53 1
                $newData = intval($newData);
54 1
            }
55 8
            settype($newData, $oriType) or \PhpBoot\abort(new \InvalidArgumentException("cast value($ori) to type {$type} failed"));
56 8
            if($ori != $newData){
57 3
                \PhpBoot\abort(new \InvalidArgumentException("could not cast value($ori) to type {$type}"));
58
            }
59
        }
60 9
        return $val;
61
    }
62
}