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::cast()   F
last analyzed

Complexity

Conditions 18
Paths 2880

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 18.5883

Importance

Changes 0
Metric Value
cc 18
nc 2880
nop 3
dl 0
loc 49
ccs 36
cts 41
cp 0.878
crap 18.5883
rs 0.7
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
}