Passed
Push — master ( ae009e...1bc377 )
by 世昌
02:06
created

Content::send()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 4
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\application\response\provider;
3
4
use SplFileInfo;
5
use JsonSerializable;
6
use nebula\application\response\type\FileContent;
0 ignored issues
show
Bug introduced by
The type nebula\application\response\type\FileContent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use nebula\application\response\type\JsonContent;
0 ignored issues
show
Bug introduced by
The type nebula\application\response\type\JsonContent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
/**
10
 * 响应类型处理器
11
 */
12
class Content
13
{
14
    protected $types = [
15
        JsonContent::class => ['boolean','bool','integer','int','float','string','array','null', JsonSerializable::class ],
16
        FileContent::class => [SplFileInfo::class],
17
    ];
18
19
    protected function register(string $provider, array $types)
20
    {
21
        $this->type[$provider] = $types;
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
22
    }
23
24
    public function send($content)
25
    {
26
        foreach ($this->types as $provider => $types) {
27
            foreach ($types as $type) {
28
                if (static::isTypeOf($content, $type)) {
29
                    $provider::send($content, $type);
30
                }
31
            }
32
        }
33
    }
34
35
    public static function isTypeOf($data, $type) : bool
36
    {
37
        if (is_object($data)) {
38
            $class = new ReflectionClass($data);
0 ignored issues
show
Bug introduced by
The type nebula\application\respo...rovider\ReflectionClass was not found. Did you mean ReflectionClass? If so, make sure to prefix the type with \.
Loading history...
39
            return $class->isSubclassOf($type) || $class->implementsInterface($type);
40
        } else {
41
            return gettype($data) === $type;
42
        }
43
        return false;
0 ignored issues
show
Unused Code introduced by
return false is not 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...
44
    }
45
}
46